adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
530 stars 125 forks source link

minigui: Deactivate button #313

Closed andre2007 closed 2 years ago

andre2007 commented 2 years ago

A user should only be allowed to click on a button when the action is valid at that specific point in time. Therefore I want do disable a button. Is there a way? I tried DynamicState, but is has no effect:

/+ dub.sdl:
    name "application"
    dependency "arsd-official:minigui" version="10.3.10"
+/
import arsd.minigui;

void main()
{      
    new MyWindow().loop();
}

class MyWindow: MainWindow
{
    this()
    {
        super("sample", 161*4, 372);
        auto b = new Button("Test", this);

        b.setDynamicState(DynamicState.disabled, true);
        b.addEventListener("triggered", () {
            messageBox("Execute");
        });
    }
}
adamdruppe commented 2 years ago

dynamic state is for visual hints only. I don't believe in disabling controls so I haven't implemented this.

The function is https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablewindow EnableWindow(widget.hwnd)

I guess I could add a function for it. I'd hate it less if there's a string param to the disable function that tells the user why. That's what drives me nuts as a user - seeing something greyed out and having no idea why meaning I can't figure out how to use it.

andre2007 commented 2 years ago

Thanks Adam.

I am working on a midi recorder/player. There is a start button and a stop button side by side. Only one button should be enabled at a time to guide the user.

Another way would be to keep the buttons enabled and show an error message in case of wrong usage. Or to have only one button which changes its meaning (and title) either start or stop.

Having two buttons and enabling/disabling them seems to me the easiest solution for the users and this specific scenario.

adamdruppe commented 2 years ago

How does this look to you?

https://github.com/adamdruppe/arsd/commit/6e4c9dbb49a14fdb76dd8ff55be9b92c1f87ff78

andre2007 commented 2 years ago

Thank you so much Adam.

adamdruppe commented 2 years ago

Just be warned I might change this a little before the official release. I think if you disable a parent it also disables the child and I might want the api to reflect that too. Like maybe disabledByWhichParent that returns the disabled parent or null if it isn't disabled. Then the disabled property might walk up the chain too.

But it should work basically the same way anyway, especially if you're just disabling buttons instead of containers.