I want to update my canvas-based gadgets when the user changes their macOS Appearance mode, but in the following, the colours are the same as before when the change happens. What am I doing wrong?
Code: Select all
EnableExplicit
Global text_color, control_background_color
Declare UpdateColors()
Define app = CocoaMessage(0,0,"NSApplication sharedApplication") ; By deseven: https://www.purebasic.fr/english/viewtopic.php?p=494505#p494505
Define appDelegate = CocoaMessage(0,app,"delegate")
Define delegateClass = object_getClass_(appDelegate)
Define selector = sel_registerName_("darkModeChanged:")
Define distributedNotificationCenter = CocoaMessage(0,0,"NSDistributedNotificationCenter defaultCenter")
Procedure darkModeChanged(notification)
UpdateColors()
EndProcedure
class_addMethod_(delegateClass,selector,@darkModeChanged(),"v@:@")
CocoaMessage(0,distributedNotificationCenter,
"addObserver:",appDelegate,
"selector:",selector,
"name:$",@"AppleInterfaceThemeChangedNotification",
"object:",#nil)
Procedure.i GetCocoaColor(ColorName.s) ; By wilbert: https://www.purebasic.fr/english/viewtopic.php?p=419571#p419571
Protected.i Result, Rect.NSRect, Image, NSColor = CocoaMessage(#Null, #Null, "NSColor " + ColorName)
If NSColor
Rect\size\width = 1
Rect\size\height = 1
Image = CreateImage(#PB_Any, 1, 1)
StartDrawing(ImageOutput(Image))
CocoaMessage(#Null, NSColor, "drawSwatchInRect:@", @Rect)
Result = Point(0, 0)
StopDrawing()
FreeImage(Image)
ProcedureReturn Result
Else
ProcedureReturn -1
EndIf
EndProcedure
; ---
Procedure UpdateColors()
text_color = GetCocoaColor("textColor")
control_background_color = GetCocoaColor("controlBackgroundColor")
If StartDrawing(CanvasOutput(0))
Box(0, 0, OutputWidth(), OutputHeight(), control_background_color)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(10, 10, "Hello World", text_color)
StopDrawing()
EndIf
EndProcedure
OpenWindow(0, #PB_Ignore, #PB_Ignore, 300, 150, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
UpdateColors()
Repeat : Until WaitWindowEvent(100) = #PB_Event_CloseWindow