mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.23k stars 243 forks source link

Theme docs #529

Open mieubrisse opened 3 years ago

mieubrisse commented 3 years ago

The Theme API is quite neat and self-directing, but it doesn't have usage docs. It took a bit of digging to find DefaultTheme, and then I was confused why it was package-private it until I found LanternaThemes.getDefault(). After that, I'm getting a strange issue in my InteractableRenderer where the following code:

    @Override
    public void drawComponent(TextGUIGraphics graphics, ChecklistItemInteractable component) {
        var theme = component.getTheme();

        System.out.println("Focused: " + component.isFocused());
        ...
        System.out.println("Background: " + style.getBackground());
        System.out.println("Foreground: " + style.getForeground());

        // TODO Handle multiline strings
        graphics.applyThemeStyle(style)
                .putString(0, 0, itemString);
    }

Prints out:

Focused: true
Background: BLUE
Foreground: WHITE

But the item sits as white-on-black:

Screen Shot 2020-12-27 at 4 30 33 PM

It would be very helpful to have docs on the proper way to use this, in case I'm doing something dumb.

mieubrisse commented 3 years ago

Extra things I've tried:

  1. Removing the setTheme on my Window object

  2. graphics.putString(0, 0, itemString, style.getSGRs());

  3. graphics.putCSIStyledString(0, 0, itemString);

  4. Swapping the order of applyTheme and putString

  5. Add .setTerminalEmulatorColorConfiguration(TerminalEmulatorColorConfiguration.getDefault()) to my terminal factory

  6. Set terminal emulator color config to TerminalEmulatorColorConfiguration.newInstance(TerminalEmulatorPalette.MAC_OS_X_TERMINAL_APP) (thinking maybe colors were turned off)

  7. terminal.setBackgroundColor(TextColor.ANSI.BLUE_BRIGHT);

  8. Making a new theme and directly setting my item to it:

     var clownTheme = SimpleTheme.makeTheme(
            true,
            TextColor.ANSI.WHITE,
            TextColor.ANSI.BLUE,
            TextColor.ANSI.WHITE,
            TextColor.ANSI.MAGENTA,
            TextColor.ANSI.WHITE,
            TextColor.ANSI.CYAN,
            TextColor.ANSI.GREEN);
     .....
     item1.setTheme(clownTheme);
  9. Drop putString and make absolutely sure that I'm writing a colored character:

    graphics.setCharacter(
            0,
            0,
            TextCharacter.fromCharacter('x', TextColor.ANSI.BLACK, TextColor.ANSI.BLUE)[0]
    );

No luck yet

mabe02 commented 3 years ago

Check out the Checkbox.java and the renderer at the end. You first grab the Theme definition from the component (which is coming from the global theme or whatever has been overridden):

ThemeDefinition themeDefinition = component.getThemeDefinition();

Then you'll need to activate a theme style on the graphics to setup the colors/styles for the subsequent drawing operations. Here's how the checkbox chooses a different visual style depending on if the checkbox currently has input focus or not:

            if(component.isFocused()) {
                graphics.applyThemeStyle(themeDefinition.getActive());
            }
            else {
                graphics.applyThemeStyle(themeDefinition.getNormal());
            }

There are 5 standard styles and any number of custom styles that can be defined, but you'll have to check the theme definition property file to see exactly what they are setup as. After the graphics object has activated the style, the next operations will be using those colors:

            graphics.fill(' ');
            graphics.putString(4, 0, component.label);

Hope that helps.

mabe02 commented 3 years ago

...actually, there seems to be a theme issue in the latest build. That might be what you're seeing. I'm investigating.

mabe02 commented 3 years ago

It was a typo, I've fixed it and will release a new version.

mabe02 commented 3 years ago

3.1.1 released