Pular para o conteúdo
CrossFly é 100% open sourceSiga no GitHub
CrossFlyCrossFly
Começar
Todos os exemplos
ReceitasEntrada84 linhas · 1 arquivos

Pickers

show(answer -> …) on all of them, and answer is null when the user backed out. The ones needing hardware live in R21, and the system file picker in R31 — those can be refused.

O que isto mostra

The pickers, and the callback shape they all share.

Every picker is a Window that answers: show(answer -> …), where answer is null when the user cancelled. One shape for date, time, colour, file, photo, barcode and location — so learning one is learning all of them.

Handle the null. Cancelling is not an error and not an edge case; it is the most common outcome of opening a picker by mistake, and a callback that assumes an answer throws on a path every user takes.

For a date inside a form, do not open a picker by hand at all — a TextField in EditMode.DATE opens it for you and writes the answer back. See R10Form.

Rodar na sua máquina

Todo exemplo é uma Window comum, então o launcher só precisa do nome dela.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R20Pickers"
Editar o exemplo

Java é compilado antes de rodar, então o navegador não consegue reconstruir uma edição sozinho — o código que você vê aqui virou JavaScript pelo TeaVM antes de ser publicado. Copie o arquivo para um projeto e ele roda sem mudanças nas quatro plataformas; o comando acima é toda a preparação.

Rodando aquiCrossFly 0.1.32
Iniciando o aplicativo…

Compilado para JavaScript pelo TeaVM — é o mesmo código, não um vídeo dele. · Todos os exemplos

O que ele usa

Tipos do framework que este exemplo toca. Os que têm link têm página de referência.

CardColorPickerComponentDatePickerDateRangePickerGapGridLayoutLabelScreenStylesTimePickerWidgets

O código

Completo, sem cortes. É o arquivo, não um trecho dele.

Bruto
dev/cf/recipe/R20Pickers.java
package dev.cf.recipe;

import dev.cf.Tones;

import dev.crossfly.ui.*;
import dev.crossfly.ui.color.*;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.picker.*;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * The pickers, and the callback shape they all share.
 *
 * <p>Every picker is a {@link dev.crossfly.ui.Window} that answers:
 * {@code show(answer -> …)}, where {@code answer} is {@code null} when the user cancelled. One
 * shape for date, time, colour, file, photo, barcode and location — so learning one is learning
 * all of them.</p>
 *
 * <p><b>Handle the null.</b> Cancelling is not an error and not an edge case; it is the most common
 * outcome of opening a picker by mistake, and a callback that assumes an answer throws on a path
 * every user takes.</p>
 *
 * <p>For a date <i>inside a form</i>, do not open a picker by hand at all — a {@code TextField} in
 * {@code EditMode.DATE} opens it for you and writes the answer back. See {@link R10Form}.</p>
 */
public class R20Pickers extends Recipe
{
   private Label answer;

   public String title()
   {
      return "Pickers";
   }

   public String note()
   {
      return "show(answer -> …) on all of them, and answer is null when the user backed out. The"
         + " ones needing hardware live in R21, and the system file picker in R31 — those can be"
         + " refused.";
   }

   public void build(Component body) throws Exception
   {
      answer = label("Nothing picked yet.", 1f, false, Styles.captionColor, Position.START);
      answer.testTag("pickers.answer");
      // Pinned below the scrolling body, so the answer stays in view (see Recipe.pin).
      pin(answer);

      open(body, 0xE878, "Date", () -> new DatePicker().show(a -> said("Date", a)));
      open(body, 0xE8B5, "Time", () -> new TimePicker().show(a -> said("Time", a)));
      open(body, 0xE916, "Date range", () -> new DateRangePicker().show(a -> said("Range", a)));
      open(body, 0xE40A, "Colour", () -> new ColorPicker(Styles.primaryColor.color())
         .show(a -> said("Colour", a)));
   }

   /** A row per picker: its glyph on a tonal tile, its name, and the whole card opens it. */
   private void open(Component body, int icone, String name, Runnable go) throws Exception
   {
      Card c = new Card();
      c.setLayout(new GridLayout(Position.START, Position.CENTER));
      c.getStyle()
         .outgap(Gap.ofFont(0, 0, 0, .5f))
         .pressable();
      c.testTag("pickers." + name.toLowerCase()
         .replace(' ', '.'));
      c.onAction(e -> go.run());
      c.add(0f, 1f, 0f, Tones.tile(icone, Tones.accent()),
         outgap(label(name, 1.2f, true, Styles.textColor, Position.START), .6f, 0, 0, 0),
         matglyph(0xE5CC, tertiary(), 1f));   // chevron_right
      body.add(LAY_AUTO, c);
   }

   /** One place that handles the answer, including the null every picker can return. */
   private void said(String what, Object a)
   {
      answer.setText(a == null ? what + ": cancelled" : what + ": " + a);
      answer.invalidateMeasure(); // the pinned band must grow with it (see Recipe.pin)
      Screen.repaint();
   }
}
Para uma IA

Versões desta página legíveis por máquina, para um modelo ler a prateleira em vez de adivinhar a API.

Este exemplo, em JSONEste exemplo, em Markdown
AnteriorCapturing a signaturePróximoCamera, barcode and GPS