macOS Mojave IsDarkmode

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5405
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

macOS Mojave IsDarkmode

Post by mk-soft »

How to get macOS Style... :wink:

Just recompile your own programs and it will run with macOS Mojave Darkmode

Code: Select all

;-TOP

; Workaround PB v5.62+ OSX Darkmode
; Version : v1.0
; Create  : 22.11.2018
; Update  : 

; -----------------------------------------------------------------------------

Procedure IsDarkmode()
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Protected UserDefaults, NSString, String.s
      UserDefaults = CocoaMessage(0, 0, "NSUserDefaults standardUserDefaults")
      NSString = CocoaMessage(0, UserDefaults, "stringForKey:$", @"AppleInterfaceStyle")
      If NSString
        String = PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
        If String = "Dark"
          ProcedureReturn 1
        Else
          ProcedureReturn 0
        EndIf
      Else
        ProcedureReturn 0 
      EndIf
      
    CompilerCase #PB_OS_Windows
      ProcedureReturn 0
    CompilerCase #PB_OS_Linux
      ProcedureReturn 0
      
  CompilerEndSelect
  
EndProcedure

; -----------------------------------------------------------------------------

CompilerIf #PB_Compiler_IsMainFile
  
  LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/Drive.bmp")
  LoadImage(1, #PB_Compiler_Home + "examples/sources/Data/File.bmp")
  
  If OpenWindow(0, 0, 0, 355, 180, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TreeGadget(0, 10, 10, 160, 160)                                         ; TreeGadget Standard
    TreeGadget(1, 180, 10, 160, 160, #PB_Tree_CheckBoxes | #PB_Tree_NoLines); TreeGadget mit Checkboxen + ohne Linien
    
    If IsDarkmode()
      SetGadgetColor(0, #PB_Gadget_FrontColor, $E0E0E0)
      ;SetGadgetColor(1, #PB_Gadget_FrontColor, $E0E0E0)
    EndIf
    
    For ID = 0 To 1
      For a = 0 To 10
        AddGadgetItem (ID, -1, "Normal Item "+Str(a), 0, 0) ; wenn Sie ein Bild hinzufügen möchten, dann 
        AddGadgetItem (ID, -1, "Node "+Str(a), 0, 0)        ; benutzen Sie ImageID(x) als 4. Parameter
        AddGadgetItem(ID, -1, "Sub-Item 1", ImageID(0), 1)  ; diese sind auf dem ersten Sublevel
        AddGadgetItem(ID, -1, "Sub-Item 2", ImageID(0), 1)
        AddGadgetItem(ID, -1, "Sub-Item 3", ImageID(0), 1)
        AddGadgetItem(ID, -1, "Sub-Item 4", ImageID(0), 1)
        AddGadgetItem (ID, -1, "File "+Str(a), ImageID(1), 0)  ; hier wieder Sublevel 0
      Next
    Next
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: macOS Mojave IsDarkmode

Post by wayne-c »

Cool thank you!
As you walk on by, Will you call my name? Or will you walk away?
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: macOS Mojave IsDarkmode

Post by wayne-c »

@Fred the PureBasic editor itself should implement this as well :-)
As you walk on by, Will you call my name? Or will you walk away?
Post Reply