Missing observer for full screen ...
Code: Select all
;-TOP by mk-soft, v1.02, 29.10.2022
; PB intern function to get window PB_ID (Only on macOS)
Import ""
PB_Window_GetID(Object)
EndImport
Enumeration CustomEvent #PB_Event_FirstCustomValue
#MyEvent_ResizeDone
#MyEvent_EnterFullScreen
#MyEvent_ExitFullScreen
EndEnumeration
Procedure InitApplication()
Global notificationCenter = CocoaMessage(0, 0, "NSNotificationCenter defaultCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")
EndProcedure : InitApplication()
; Common observer callbacks
ProcedureC Notification_ResizeDoneCB(obj, sel, notification)
Protected object, window
object = CocoaMessage(0, notification, "object")
window = PB_Window_GetID(object)
PostEvent(#MyEvent_ResizeDone, window, 0)
EndProcedure
ProcedureC Notification_EnterFullScreenCB(obj, sel, notification)
object = CocoaMessage(0, notification, "object")
window = PB_Window_GetID(object)
PostEvent(#MyEvent_EnterFullScreen, window, 0)
EndProcedure
ProcedureC Notification_ExitFullScreenCB(obj, sel, notification)
object = CocoaMessage(0, notification, "object")
window = PB_Window_GetID(object)
PostEvent(#MyEvent_ExitFullScreen, window, 0)
EndProcedure
; Add observer to window object
Procedure AddObserver(Window)
Protected selector
selector = sel_registerName_("windowResizeDone:")
If Not class_getInstanceMethod_(delegateClass, selector)
class_addMethod_(delegateClass, selector, @Notification_ResizeDoneCB(), "v@:@")
EndIf
CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWindowDidEndLiveResizeNotification", "object:", WindowID(Window))
selector = sel_registerName_("windowDidEnterFullScreen:")
If Not class_getInstanceMethod_(delegateClass, selector)
class_addMethod_(delegateClass, selector, @Notification_EnterFullScreenCB(), "v@:@")
EndIf
CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWindowDidEnterFullScreenNotification", "object:", WindowID(Window))
selector = sel_registerName_("windowDidExitFullScreen:")
If Not class_getInstanceMethod_(delegateClass, selector)
class_addMethod_(delegateClass, selector, @Notification_ExitFullScreenCB(), "v@:@")
EndIf
CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWindowDidExitFullScreenNotification", "object:", WindowID(Window))
EndProcedure
; Remove observer from window object
Procedure RemoveObserver(Window)
CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWindowDidEndLiveResizeNotification", "object:", WindowID(Window))
CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWindowDidEnterFullScreenNotification", "object:", WindowID(Window))
CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWindowDidExitFullScreenNotification", "object:", WindowID(Window))
EndProcedure
; ****
Procedure SizeInfo(Text.s)
Protected window, dx, dy
window = EventWindow()
dx = WindowWidth(window)
dy = WindowHeight(window)
AddGadgetItem(0, -1, Text + " / Window = " + window + " / DX = " + dx + " / DY = " + dy)
EndProcedure
; ----
Procedure UpdateWindow()
Protected dx, dy
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
; Resize Gadgets
ResizeGadget(0, 0, 0, dx, dy)
EndProcedure
Procedure MyWindow()
#WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
OpenWindow(1, 200, 200, 300, 200, "Window 2", #WinStyle)
EndProcedure
Procedure Main()
Protected dx, dy
#WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Window 1", #WinStyle)
; MenuBar
CreateMenu(0, WindowID(0))
MenuTitle("File")
; StatusBar
CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
; Gadgets
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
ListViewGadget(0, 0, 0, dx, dy)
; Bind Events
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
MyWindow()
AddObserver(0)
AddObserver(1)
; Main Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Select EventWindow()
Case 0
Break
EndSelect
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
Case #PB_Event_MinimizeWindow
SizeInfo("Minimize")
Case #PB_Event_MaximizeWindow
SizeInfo("Maximize")
Case #PB_Event_RestoreWindow
SizeInfo("Restore")
Case #MyEvent_ResizeDone
SizeInfo("ResizeDone")
Case #MyEvent_EnterFullScreen
SizeInfo("EnterFullScreen")
Case #MyEvent_ExitFullScreen
SizeInfo("ExitFullScreen")
EndSelect
ForEver
RemoveObserver(0)
RemoveObserver(1)
EndIf
EndProcedure : Main() : End