cocopon / tweakpane

:control_knobs: Compact GUI for fine-tuning parameters and monitoring value changes
https://tweakpane.github.io/docs/
MIT License
3.69k stars 92 forks source link

Setting ListBladeApi default value does not call change for the first time #619

Open aniongithub opened 3 months ago

aniongithub commented 3 months ago

Given a simple list, it seems more complicated than needed to set a default value and have Tweakpane invoke the on(change) event for first-time load.

const models_list = settingsPage.addBlade({
        view: "list",
        label: "MyList",
        options: [
            { text: "Foo", value: "assets/foo.txt" },
            { text: "Bar", value: "assets/bar.txt" },
            { text: "Foobar", value: "assets/foobar.txt" },
            { text: "BarBar", value: "assets/barbar.txt" }      
        ],
        value: "assets/foo.txt", // Have to set something other than what we want to start with
    }) as ListBladeApi<string>;
    models_list
        .on('change', async (ev) => {
            await asset.load(ev.value);
        }); 
    models_list.value = "assets/foo.txt"; // This does nothing if we hadn't set it to a non-default value

Ideally, just setting the value in opts should invoke the handler for the first time post-creation. But this doesn't happen. Also, setting the value explicitly to the same value as specified in opts doesn't invoke the event, because nothing has "changed" (but it was never really set in application state!).

The workaround is to set a different value than what is default and then explicitly set it to default after creation, which seems convoluted.

cocopon commented 1 week ago

I understand your perspective, but the change event is literally intended for detecting value changes. A behavior that doesn't align with its name could lead to confusion.