Giter Site home page Giter Site logo

tools1000 / jsystemthemedetector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dansoftowner/jsystemthemedetector

0.0 0.0 0.0 504 KB

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

License: Apache License 2.0

Java 100.00%

jsystemthemedetector's Introduction

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

If this library is used by your project, let me know in the Discussions and I will mention that in this section.

  • 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.

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.

jsystemthemedetector's People

Contributors

dansoftowner avatar fenrur avatar airsquared avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.