jSystemThemeDetector

repository·master·Indexed 42 days ago

https://github.com/dansoftowner/jsystemthemedetector

A Java library for detecting whether the host operating system is using a dark or light theme. It supports Windows 10, MacOS Mojave+, and Linux, allowing Java-based GUI applications like Swing and JavaFX to synchronize with system preferences via OsThemeDetector.

Tokens
935
Snippets
4
Records
5
Agent score
4%

What's inside jSystemThemeDetector

  1. Install jSystemThemeDetector via Gradle

    master

    To use jSystemThemeDetector in a Gradle project, add the JitPack repository to your repositories block and include the dependency in your dependencies block. Note that the version used in the example is 3.6; ensure you check JitPack for the latest available version.

    repositories {
    	...
    	maven { url 'https://jitpack.io' }
    }
    
    dependencies {
        implementation 'com.github.Dansoftowner:jSystemThemeDetector:3.6'
    }
  2. Handle theme changes in JavaFX or Swing

    master

    When updating a GUI in response to a theme change, you must ensure the UI updates occur on the correct application thread to avoid stability issues.

    • JavaFX: Wrap your logic in Platform.runLater() to execute it on the JavaFX Application Thread.
    • AWT/Swing: Wrap your logic in SwingUtilities.invokeLater() to execute it on the Event Dispatch Thread.
    // JavaFX Example
    final OsThemeDetector detector = OsThemeDetector.getDetector();
    detector.registerListener(isDark -> {
        Platform.runLater(() -> {
            if (isDark) {
                // The OS switched to a dark theme
            } else {
                // The OS switched to a light theme
            }
        });
    });
    
    // Swing Example
    final OsThemeDetector detector = OsThemeDetector.getDetector();
    detector.registerListener(isDark -> {
        SwingUtilities.invokeLater(() -> {
            if (isDark) {
                // The OS switched to a dark theme
            } else {
                // The OS switched to a light theme
            }
        });
    });
  3. Detect the current OS theme

    master

    Use OsThemeDetector.getDetector() to obtain an instance of the detector. You can then call .isDark() to check if the operating system is currently using a dark theme.

    final OsThemeDetector detector = OsThemeDetector.getDetector();
    final boolean isDarkThemeUsed = detector.isDark();
    if (isDarkThemeUsed) {
        //The OS uses a dark theme
    } else {
        //The OS uses a light theme
    }
  4. Listen for OS theme changes

    master

    You can register a listener using registerListener to react whenever the user switches between light and dark modes in their operating system settings. The listener receives a boolean indicating whether the new theme is dark.

    final OsThemeDetector detector = OsThemeDetector.getDetector();
    detector.registerListener(isDark -> {
        if (isDark) {
            //The OS switched to a dark theme
        } else {
            //The OS switched to a light theme
        }
    });