3

In the following Rebol 2 code, why does button a become visible 5 seconds after it's clicked, while remaining hidden 5 seconds after button b is clicked?

f: does [hide a wait 5]

view layout [
    a: button "a" [f]
    b: button "b" [f]
]
WiseGenius
  • 226
  • 2
  • 9
  • You shouldn't use `wait` in GUI action.WAIT runs the event loop and you are already in it, which is problematic. – rebolek Aug 28 '15 at 08:02

2 Answers2

2

It looks like a bug that

view layout [
    a: button "hide me" [ hide face ]
    b: button "hide a" [ hide a ]
]

doesn't work to hide the "a" button unless the hide is called from another button. Your wait 5 must be triggering an update of the layout so that the button disappears.

Instead of wait 5, using do-events (wait []) keeps the button hidden.

view layout [
    a: button "hide me" [ hide face do-events ]
    b: button "hide a" [ hide a ]
]
Graham Chiu
  • 4,856
  • 1
  • 23
  • 41
  • On the contrary, rather than triggering an update so the button disappears, I think `wait 5` is postponing an update so that the button remains hidden. Without the `wait 5`, the button would be hidden and then shown again too quickly to see that it was ever hidden. I think I may have worked out why it gets shown again now, so I posted an answer. Thanks. – WiseGenius Aug 28 '15 at 11:09
1

When each button is clicked, it is redrawn to look “pressed”, and stays “pressed” until its action has completed. Then, after it's action has completed, the button is redrawn as “unpressed”.

During button a's action, it is hidden, but when its action is completed, it is shown again when it's “unpressed” state is drawn. According to this function summary of hide, hide only “temporarily removes the face from view”, and “The face will become visible again the next time the face is shown either directly or indirectly through one of its parent faces.”

During button b's action, button a is hidden, but when button b's action is completed, it is button b that is redrawn as “unpressed”. At this point, button a is untouched and remained hidden.

Considering Graham Chiu's code:

view layout [
    a: button "hide me" [ hide face do-events ]
    b: button "hide a" [ hide a ]
]

In this case, the reason why button a remains hidden after being clicked, is that its action doesn't reach completion until the window is closed. If wait 5 represents code which needs to be executed when the button is clicked, it needs to be put before do-events. Otherwise it is not executed until the window is closed.

view layout [
    a: button "hide me and print" [
        hide face
        print "I need to say this when clicked."
        do-events
        print "I can wait until the window is closed."
    ]
    b: button "hide a" [ hide a ]
]

Some other ways to make a button hide itself can be found on this page under the subheading: “Hiding self”. For example:

view l: layout [b: button [b/show?: false unview/all view l]]
WiseGenius
  • 226
  • 2
  • 9
  • If you click on the button face, and while holding the mouse button down, move the mouse away, the action is not started. So, the action is initiated by a left mouse button up event. The hide takes place, the face is hidden, the action completes, and then view must be updating the face ignoring the show? flag so that the face is shown again. – Graham Chiu Aug 28 '15 at 21:12