user-interfacehaskellwxwidgetswxhaskell

wxHaskell Button State


I'm writing an application using wxHaskell and I want to be able to detect the state of a button (whether or not it is pressed at any given time). I'm having a bit of trouble figuring out how to do this, however. First I thought that there might be a "button is pressed" attribute that I could use, but there didn't seem to be. Then I had the idea of maintaining an IORef which I update on button-up and button-down events. However, that would require that the Button object actually have button-up and button-down events, which is does not appear to. It is an instance of Commanding, but I assume that the command event is fired on button-up only, which isn't enough for that idea. Does anyone have any other suggestions?


Solution

  • Workaround

    You can implement this yourself by detecting the low-level actions that trigger those events (eg. mouse button down, space bar down).

    In WX you can use the following function and constructor:

    mouse :: Reactive w => Event w (EventMouse -> IO ())
    data EventMouse = ... | MouseLeftDown !Point !Modifiers
    

    And, as you suggest, you could keep the state yourself in an IORef. My suspicion is that left button here means main button (right for left-handed users).

    UI design principles

    The second question, which you haven't asked by I'll answer, is whether this is good UI design.

    The behaviour of a button (assuming interaction using a mouse) is that click events are reported when the user releases the mouse button in the button area after pressing the mouse button down in the same area. If the user moves away and releases, or presses 'Escape', there is no click.

    Taking any action on a button being pressed (not clicked) would feel unnatural for users.

    In practice, the only acceptable way to use this would be, imho, to take an action whose effects can only be witnessed after releasing and which is immediately undone if the click is cancelled (ie. mouse button released outside button area).

    EDIT: Please, also, take into account that users with accessibility requirements may have OS settings enabled that affect how and when button clicks are reported (but not down/up mouse events).