RFR: 8301302: Platform preferences API

John Hendrikx jhendrikx at openjdk.org
Sun Jan 29 14:34:23 UTC 2023


On Sun, 29 Jan 2023 01:33:48 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:

> Platform preferences are the preferred UI settings of the operating system. For example, on Windows this includes the color values identified by the `Windows.UI.ViewManagement.UIColorType` enumeration; on macOS this includes the system color values of the `NSColor` class.
> 
> Exposing these dynamic values to JavaFX applications allows developers to create themes that can integrate seamlessly with the color scheme of the operating system.
> 
> Platform preferences are exposed as an `ObservableMap` of platform-specific key-value pairs, which means that the preferences available on Windows are different from macOS or Linux. JavaFX provides a small, curated list of preferences that are available on most platforms, and are therefore exposed with a platform-independent API:
> 
> 
> public interface Preferences extends ObservableMap<String, Object> {
>     // Platform-independent API
>     ReadOnlyObjectProperty<Appearance> appearanceProperty();
>     ReadOnlyObjectProperty<Color> backgroundColorProperty();
>     ReadOnlyObjectProperty<Color> foregroundColorProperty();
>     ReadOnlyObjectProperty<Color> accentColorProperty();
> 
>     // Convenience methods to retrieve platform-specific values from the map
>     String getString(String key);
>     String getString(String key, String fallbackValue);
>     Boolean getBoolean(String key);
>     boolean getBoolean(String key, boolean fallbackValue);
>     Color getColor(String key);
>     Color getColor(String key, Color fallbackValue);
> }
> 
> 
> The platform appearance is defined by the new `javafx.stage.Appearance` enumeration:
> 
> 
> public enum Appearance {
>     LIGHT,
>     DARK
> }
> 
> 
> An instance of the `Preferences` interface can be retrieved by calling `Platform.getPreferences()`.

Just commenting on the convenience API part.  I think in modern Java you'd use `Optional` to avoid creating multiple methods for each type (`Optional` supports far more options as "fallback") and to avoid having to return `null` as a special value.

     /**
      * Gets an optional value associated with the given key. If the key is not present or not of the correct type
      * this optional will be empty.
      */
     Optional<String> getString(String key);

In this way you could get a certain key that may be platform specific like this (names made up):

     int mousePointerWidth = prefs.getInteger("windows-cursor-size")
           .or(() -> prefs.getInteger("linux-mouse-pointer-width"))
           .or(() -> prefs.getInteger("mac-pointer-radius").map(x -> x * 2))  // multiply radius by 2 to get width
           .orElse(16);

-------------

PR: https://git.openjdk.org/jfx/pull/1014


More information about the openjfx-dev mailing list