Hi All,
I’m using a custom sheet from Shane’s NSWindow+MyriadHelpers and I’m getting some behavior I’m not expecting. Namely, the buttons in the main window behind the sheet seem to be storing clicks and acting on them when the sheet closes. I was under the impression that all mouse clicks are ignored in the main window while the sheet is present. That’s what I see when I try Shane’s sample project. The weird thing is that this happens even if I disable the controls under the sheet. FYI, I’m using the sheet to display a message as a several seconds long repeat loop runs, and I was counting on using it to prevent users from pressing buttons while the loop runs. Any ideas on what I’m missing? Thanks.
What do you mean by storing clicks and acting on them when the sheet closes? Do you mean you can click on those buttons while the sheet is present, and then when the sheet closes you see the actions from those clicks happen? Do the buttons highlight when you click them? And, how are you showing the sheet, which showOver?
I don’t see that behavior when I try it. I think you might need to post some code so we can see how the flow of your app is controlled.
Ric
Thanks for the reply. I suppose my original post wasn’t very clear. Let me be more specific. Here’s the code.
on myHandler()
tell mySheetWindow to showOver_(mainWindow) -- this calls an informational sheet that is supposed to prevent clicks
repeat with j from 1 to theCount
-- Stuff done here that can take several seconds
end repeat
tell current application's NSApp to endSheet_(mySheetWindow)
return
end myHandler
When run, buttons in the main window do not register clicks by highlighting, but the clicks appear to be added to the event queue because they highlight and do their thing after the sheet closes. This happens even if I disable the buttons. When I test with Shane’s sample project for custom sheets, they work perfectly. I’m wondering if I’ve got some checkbox somewhere I haven’t checked.
I’m not sure what’s going on here either – I get the same result as you with your code. The difference between your code and Shane’s example is that he has a button to close the window instead of putting the endSheet command in the same method as the showOver command. If I do that, then clicking the button in the main window while the sheet is showing doesn’t produce anything after the sheet closes, but I don’t think that you want the user to have to press a button to get the sheet to go away.
The fact that the button “stores” the clicks even when disabled ( setEnabled_(0) ), is the really weird thing for me.
Ric
It may be that the modality doesn’t actually begin until after the current event is fully handled. The workaround would be to put the repeat loop and endSheet_ in a separate handler, and call it with performSelector_withObject_afterDelay_.
Shane,
Forgetting the modality question for the moment, I still don’t understand the order that things get executed — it seems as though, in the following code, the statement, theButton’s setEnabled_(1), is executed before the repeat loop finishes. If I click the button that launches the sheet, and then click aButton four times while the repeat loop is executing, I get “clicked” logged 4 times after the sheet closes. I get the same response if I comment out the sheet launching and closing lines, so it doesn’t have anything to do with the sheet.
script ButtonResponse_TesterAppDelegate
property parent : class "NSObject"
property mySheetWindow : missing value
property mainWindow : missing value
property aButton : missing value -- a button in the main window
on applicationWillFinishLaunching_(aNotification)
aButton's setEnabled_(0)
end applicationWillFinishLaunching_
on myHandler_(sender) -- IBAction for a button in the main window
mySheetWindow's showOver_(mainWindow)
set x to 0
repeat with j from 1 to 5000000 -- This take 3 or 4 seconds to complete
set x to x + 1
end repeat
current application's NSApp's endSheet_(mySheetWindow)
aButton's setEnabled_(1)
end myHandler_
on buttonClick_(sender) -- IBAction for a aButton in mainWindow
log "clicked"
end buttonClick_
end script
Ric
Ric,
The events that get queued aren’t button presses, they’re mouse clicks at some coordinate. So by the time they get handled, the button is enabled, and hence they produce button presses.
This gets back to the problem in the first project in my book – see page 78 of the third edition, under the subhead ‘Half a Solution’.
That was a tricky one. I tried a bunch of different things but the ‘Half a Solution’ from the 2nd edition (I assume that’s the same as the 3rd edition) seems to have done the trick. Thanks!