Saltar al contenido
CrossFly es 100% open sourceSíguenos en GitHub
CrossFlyCrossFly
Empezar
Todos los ejemplos
RecetasTema65 líneas · 1 archivos

Theme tokens

Flip the switch. The token line follows the theme; the hardcoded one does not, and turns unreadable — no rebuild involved.

Qué muestra esto

Why colours come from tokens: the switch restyles the live tree, and only the token follows.

Ejecutarlo en tu máquina

Cada ejemplo es una Window normal, así que el lanzador solo necesita su nombre.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R02Theme"
Editarlo

Java se compila antes de ejecutarse, así que el navegador no puede reconstruir una edición por su cuenta: el código que ves aquí se convirtió en JavaScript con TeaVM antes de publicarse. Copia el archivo a un proyecto y funciona sin cambios en las cuatro plataformas; el comando de arriba es toda la preparación.

Ejecutándose aquíCrossFly 0.1.32
Iniciando la aplicación…

Compilado a JavaScript por TeaVM: es el mismo código, no un vídeo de él. · Todos los ejemplos

Qué usa

Tipos del framework que toca este ejemplo. Los enlazados tienen página de referencia.

CardComponentStylesSwitchVerticalLayoutWidgets

El código

Completo, sin recortes. Es el archivo, no un fragmento.

Crudo
dev/cf/recipe/R02Theme.java
package dev.cf.recipe;

import dev.cf.Tones;

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

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

/**
 * Why colours come from tokens: the switch restyles the live tree, and only the token follows.
 */
public class R02Theme extends Recipe
{
   /** A colour written as a literal. It cannot follow the theme, and that is the point. */
   private static final int CRAVADO = 0xFF1A1A1A;

   private static final int IC_TOKEN = 0xE40A, IC_FIXED = 0xE897;

   public String title()
   {
      return "Theme tokens";
   }

   public String note()
   {
      return "Flip the switch. The token line follows the theme; the hardcoded one does not, and"
         + " turns unreadable — no rebuild involved.";
   }

   public void build(Component body) throws Exception
   {
      // The tokens, on a card in the tone they themselves resolve: turning dark mode on moves the
      // card, the border and the three lines at once, and nothing here was told.
      Card tokens = Tones.card(Tones.accent());
      tokens.setLayout(new VerticalLayout(Position.START, Position.START));
      tokens.add(LAY_AUTO, Tones.head(IC_TOKEN, "From a token", Tones.accent()));
      tokens.add(LAY_AUTO, outgap(label("Styles.textColor", 1.2f, true, Styles.textColor,
         Position.START), 0, 0, 0, .3f));
      tokens.add(LAY_AUTO, label("Styles.captionColor", 1f, false, Styles.captionColor,
         Position.START));
      tokens.add(LAY_AUTO, outgap(label("Tones.accent() — a role of the scheme", 1f, true,
         Tones.accent(), Position.START), 0, .3f, 0, 0));
      body.add(LAY_AUTO, outgap(tokens, 0, 0, 0, .8f));

      // The literal: readable in light, almost invisible in dark. It is the demonstration, not the
      // example — and the error tone around it says which half of the screen is the wrong one.
      Card cravado = Tones.card(Tones.danger());
      cravado.setLayout(new VerticalLayout(Position.START, Position.START));
      cravado.add(LAY_AUTO, Tones.head(IC_FIXED, "Written down", Tones.danger()));
      cravado.add(LAY_AUTO, outgap(label("0xFF1A1A1A", 1.2f, true, CRAVADO, Position.START),
         0, 0, 0, 0));
      body.add(LAY_AUTO, outgap(cravado, 0, 0, 0, 1f));

      Switch escuro = new Switch("Dark mode");
      escuro.testTag("recipe.dark");
      escuro.checked(Styles.isDarkMode());
      // setDarkMode restyles every live component: nothing here rebuilds, and nothing needs to.
      escuro.onAction(c -> Styles.setDarkMode(escuro.isChecked()));
      body.add(LAY_AUTO, escuro);
   }
}
Para una IA

Versiones de esta página legibles por máquina, para que un modelo lea el estante en vez de adivinar la API.

Este ejemplo, en JSONEste ejemplo, en Markdown
AnteriorLayoutsSiguienteSwitching skins