Skip to content

Java library for detecting that the (desktop) operating system uses dark UI theme or not

License

Notifications You must be signed in to change notification settings

CrossPaste/jSystemThemeDetector

 
 

Repository files navigation

jSystemThemeDetector

GitHub last commit GitHub issues GitHub

In most modern operating systems there is a dark mode option. This library is created for detecting this using java.

It can be useful for example if you want to synchronize your GUI App's look and feel with the operating system.

This library is inspired by the dark-theme detection in Intellij Idea.

Compatibility

It works on Windows 10, MacOS Mojave (or later) and even on some Linux distributions.

Requirements

Java 11 or higher

Basic examples

Simple detection

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
}

Listening to changes

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
    }
});

Using it with JavaFX/Swing

If you use the listener for changing the UI in a JavaFX application, make sure you use Platform.runLater in it:

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
        }
    });
});

It's important because if you are doing JavaFX specific stuff in the listener, you should execute it on the UI thread (JavaFX Application Thread). Otherwise, you might face some serious issues.

In case of AWT/Swing, use SwingUtilities.invokeLater for the same reason:

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
        }
    });
});

Using it with Gradle, Maven... etc

It's available on JitPack!

Gradle example:

repositories {
	...
	maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.Dansoftowner:jSystemThemeDetector:3.6'
}

GUI Demo application

There is a Demo application available here. It's a basic JavaFX application that changes the UI when the OS switched to a dark/light theme.

Linux/Ubuntu example

Running the demo app on Ubuntu

Windows 10 example

Running the demo app on Windows 10

Projects using jSystemThemeDetector

  • Boomega - A modern book explorer & catalog application
  • Document Archiver - Archive all your documents in a consistent way, which enables you to retrieve them later fast and easy.
  • pearletj - Cross-platform multi-chain thick wallet for Cryto-currencies

If this library is used by your project, feel free to create a PR and mention that in this section!

Used libraries

  • SLF4J - Simple Logging Facade for Java
  • Jetbrains Annotations - Annotations for JVM-based languages
  • JNA - Java Native Access
  • JFA - Java Foundation Access
  • OSHI - Operating system & hardware information
  • Version Compare - Lightweight Android & Java library to compare version strings.

About

Java library for detecting that the (desktop) operating system uses dark UI theme or not

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%