Skip to content

LafManager

weisj edited this page May 19, 2020 · 3 revisions

Using the LafManager

Everything related to handling themes is located inside the LafManager.

Toggle decorations

You can enable or disable window decorations using

LafManager.setDecorationsEnabled(enabled);

Setting the Theme

A theme can be set in multiple ways.

  • Explicitly
    LafManager.installTheme(new IntelliJTheme());
  • Using an preferred theme style
    LafManager.installTheme(preferredThemeStyle);
    
    This actually uses the current ThemeProvider to create a theme from the preferred theme style. A preferred theme style consists of:
    • ContrastRule: High or default contrast.
    • ColorToneRule: Dark or light.
    • FontSizeRule: Specifies the font scaling.
    • AccentColorRule: Specifies the accent and selection color. The simplest way to use a custom theme provider is by using DefaultThemeProvider:
    LafManager.setThemeProvider(new DefaultThemeProvider(
        new SolarizedLightTheme(),
        new SolarizedDarkTheme(),
        new HighContrastLightTheme(), 
        new HighContrastDarkTheme()
    ));
  • Using the preferred theme style of the system:
    LafManager.installTheme(LafManager.getPreferredThemeStyle());
    If native code is allowed this will report the system preferences if available. Otherwise it will fall back to a ContrastRule.STANDARD, ColorToneRule.LIGHT theme.

See ThemeProvider, DefaultThemeProvider

Settings monitoring

Darklaf enables you to follow the system preferences on supported machines. By default it is disabled and can be enabled through

LafManager.enabledPreferenceChangeReporting(true);

Any changes in the system preferences are then send through ThemePreferenceChangeEvents. You can listen to them by registering a ThemePreferenceListener:

LafManager.addThemePreferenceChangeListener(new CustomThemeListener());
// Remove using LafManger#removeThemePreferenceChangeListener

class CustomThemeListener implements ThemePreferenceListener {

    public void void themePreferenceChanged(final ThemePreferenceChangeEvent e) {
        System.out.println("New preffered theme style is: " + e.getPreferredThemeStyle());
    }
}

The ThemeSettings class encapsulates a lot of the logic when dealing with theme monitoring and customizations.

Simply open the settings dialog:

ThemeSettings.showSettingsDialog(parent);

or integrate the settings panel in your application

comp.add(ThemeSettings.getSettingsPanel());

Theme Monitoring

If you want to save the settings between sessions you will still need to handle it yourself. THe values can be simply checked by obtaining the ThemeSettings instance.

ThemeSettings settings = ThemeSetting.getInstance();

settings.getTheme();
settings.isSystemPreferencesEnabled();
settings.isAccentColorFollowsSystem();
settings.isSelectionColorFollowsSystem();
settings.isThemeFollowsSystem();
// etc...

See ThemeSettings.

Customizing properties

If you want to customize certain properties of the laf you can do so by registering custom initialization tasks.

  • DefaultsAdjustmentTask: The task is run directly after loading the defaults of the theme and works on these properties only. Changing values here will result in them being propagated to all components that use them. Generally you'll want to use these tasks for modifications of colors. All available values for a theme can be found here.

    • When modifying colors you should be aware that themes can be light/dark and normal/high contrast.
  • DefaultsInitTask: The task is run after all defaults have been populated and works directly on the UIDefaults. This is essentially the same as using UIManager#put each time the laf is changed.

Note All custom values need to implement UIResource or else they won't be changed when changing the theme or laf.

LafManager.registerDefaultsAdjustmentTask(new CustomAdjustmentTask());
// Remove using LafManager#removeDefaultsAdjustmentTask

LafManager.registerInitTask(new CustomInitTask());
// Remove using LafManager#removeInitTask


...

class CustomAdjustmentTask implements DefaultsAdjustmentTask {

    public void run(final Theme currentTheme, final Properties properties) {
        if (Theme.isDark(currentTheme)) {
            properties.put("controlBorder", new ColorUIResource(Color.BLUE));
        }
    }
}

class CustomInitTask implements DefaultsInitTask {

    public void run(final Theme currentTheme, final UIDefaults defaults) {
        defaults.put("Button.background", new ColorUIResource(Color.RED));
    }
}
Clone this wiki locally