PreferencesFX icon indicating copy to clipboard operation
PreferencesFX copied to clipboard

How to read preferences from command line mode?

Open trixon opened this issue 4 years ago • 3 comments

I'm switching an app to WorkbenchFX and in that process I'm also looking into PreferencesFX. The app in question can after being run and configurated in gui mode be used from the command line.

Is it correct that reading preferences produced by PreferencesFX requires the fx toolkit to be initialized?

In order to read them I came up with

    public static void main(String[] args) {
        new JFXPanel();
        Platform.runLater(() -> {
            Preferences p = new Preferences();
            System.out.println(p.fontSize);
            System.out.println(p.brightness);
            System.out.println(p.isNightMode());
            Platform.exit();
        });
    }

The preferences class here is https://github.com/dlsc-software-consulting-gmbh/WorkbenchFX/blob/master/workbenchfx-demo/src/main/java/com/dlsc/workbenchfx/modules/preferences/Preferences.java

Is this the best way for me to do this?

trixon avatar Aug 15 '19 06:08 trixon

Hi @trixon This is actually a use case we didn't think of, but it makes sense!

Actually I think with the current implementation of PreferencesFX, yes, your solution is probably the easiest way to read them. However, I could imagine it would make sense to offer a way to load the preferences without initializing the toolkit, however I can't imagine at the moment a way that wouldn't require us to do dramatic changes, since we heavily rely on properties, and as far as I'm aware, those require an initialized toolkit...

Does your solution work well for you, or did you stumble into any difficulties?

martinfrancois avatar Aug 17 '19 08:08 martinfrancois

Hi @martinfrancois

Does your solution work well for you, or did you stumble into any difficulties?

I have not done a real implementation yet, I was looking for a way forward, but I will begin now.

since we heavily rely on properties, and as far as I'm aware, those require an initialized toolkit...

I don't think that simple properties rely on the toolkit, the following works anyway.

public class PropertyTest {

    private SimpleBooleanProperty mVisibleProperty = new SimpleBooleanProperty();

    public static void main(String[] args) {
        new PropertyTest();
    }

    public PropertyTest() {
        System.out.println(getVisible());
        setVisible(true);
        System.out.println(getVisible());
        System.out.println(visibleProperty());
    }

    public boolean getVisible() {
        return mVisibleProperty.get();
    }

    public void setVisible(boolean visible) {
        mVisibleProperty.setValue(visible);
    }

    public SimpleBooleanProperty visibleProperty() {
        return mVisibleProperty;
    }
}

trixon avatar Aug 18 '19 06:08 trixon

Interesting... Actually, then I could imagine a way we could do this is to postpone the initialization of the GUI elements (essentially in PreferencesFx.init() everything after preferencesFxModel.loadSettingValues();) to only run when either getView() or show() is called for the first time. That way, you don't need to initialize the toolkit and can access all of the property's values on the command line, as long as you don't call getView() or show() (which you wouldn't of course on a command line).

Feel free to try it out and submit a PR, if it works for you!

martinfrancois avatar Aug 19 '19 17:08 martinfrancois