Re: EventClose on Dock
Posted: Wed Sep 04, 2019 4:34 pm
Sorry, I was talking in the context of logout. It does work nicely from context menu close, but not from logout.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
EnableExplicit
Prototype Proto_AppNotification(Object, Selector, Sender)
DeclareC AppShouldTerminate(Object, Selector, Sender)
DeclareC AppWillTerminate(Object, Selector, Sender)
Global AppDelegate, AppShouldTerminate_.Proto_AppNotification, AppWillTerminate_.Proto_AppNotification
AppDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
AppShouldTerminate_ = class_replaceMethod_(CocoaMessage(0, AppDelegate, "class"),
sel_registerName_("applicationShouldTerminate:"),
@AppShouldTerminate(), "v@:@")
AppWillTerminate_ = class_replaceMethod_(CocoaMessage(0, AppDelegate, "class"),
sel_registerName_("applicationWillTerminate:"),
@AppWillTerminate(), "v@:@")
; class_addMethod_(CocoaMessage(0, AppDelegate, "class"),
; sel_registerName_("applicationWillTerminate:"),
; @AppWillTerminate(), "v@:@")
ProcedureC AppShouldTerminate(Object, Selector, Sender)
Debug "AppShouldTerminate"
If AppShouldTerminate_
AppShouldTerminate_(Object, Selector, Sender)
EndIf
If MessageRequester("Confirmation", "Do you want to terminate the application?",
#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
End
Else
ProcedureReturn #NO
EndIf
EndProcedure
ProcedureC AppWillTerminate(Object, Selector, Sender)
Debug "AppWillTerminate"
End
EndProcedure
OpenWindow(0, 270, 100, 200, 170, "Test", #PB_Window_MinimizeGadget)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Select all
ProcedureC AppShouldTerminate(Object, Selector, Sender)
Debug "AppShouldTerminate"
ProcedureReturn #YES
EndProcedure
ProcedureC AppWillTerminate(Object, Selector, Sender)
Debug "AppWillTerminate"
End
EndProcedure
w = OpenWindow(0, 0, 0, 300, 200, "Test")
CocoaMessage(@app, 0, "NSApplication sharedApplication")
myAppDelegateClass = objc_allocateClassPair_(objc_getClass_("NSObject"), "myAppDelegate", 0)
class_addProtocol_(myAppDelegateClass, objc_getProtocol_("NSApplicationDelegate"))
class_addMethod_(myAppDelegateClass, sel_registerName_("applicationShouldTerminate:"), @appShouldTerminate(), "L@:@")
class_addMethod_(myAppDelegateClass, sel_registerName_("applicationWillTerminate:"), @appWillTerminate(), "v@:@")
objc_registerClassPair_(myAppDelegateClass)
myAppDelegate = class_createInstance_(myAppDelegateClass, 0)
CocoaMessage(0, app, "setDelegate:", myAppDelegate)
Repeat
e = WaitWindowEvent(10)
Until e = #PB_Event_CloseWindow
When you place End within the AppShouldTerminate procedure, it already ends the application so AppWillTerminate will never be called.Rinzwind wrote:Why appWillTerminate is not called in below case?
Yes, this is wrong. Creating you own app delegate is only safe when you create everything yourself (including windows, event loop etc.).Rinzwind wrote:And is there anything wrong with the way to do things like this instead or does this break stuff?: