GTK3 DarkMode

Linux specific forum
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

GTK3 DarkMode

Post by mk-soft »

DarkMode for GTK3 Application ... :wink:

Update v1.02.1

Code: Select all

;-TOP

; GTK DarkMode by mk-soft, v1.02.1, 18.12.2022
; Link: https://www.purebasic.fr/english/viewtopic.php?t=80298

Procedure.s GetThemeName()
  Protected ThemeName.s, *Settings, *Theme
  
  *Settings = gtk_settings_get_default_()
  If *Settings
    g_object_get_(*Settings, "gtk-theme-name", @strVal);
    ThemeName.s = PeekS(strVal, -1, #PB_UTF8)
    g_free_(strVal)
    ProcedureReturn ThemeName
  EndIf
EndProcedure

Procedure SetThemeName(ThemeName.s)
  Protected *Settings, *Theme
  
  *Settings = gtk_settings_get_default_()
  If *Settings
    *Theme = UTF8(ThemeName)
    g_object_set_(*Settings, "gtk-theme-name", *Theme);
    FreeMemory(*Theme)
    ProcedureReturn #True
  EndIf
EndProcedure

; ----

Procedure IsDarkMode()
  Protected boolVal, *Settings, *Theme
  
  *Settings = gtk_settings_get_default_()
  If *Settings
    g_object_get_(*Settings, "gtk-application-prefer-dark-theme", @boolVal);
    ProcedureReturn boolVal
  EndIf
EndProcedure

Procedure SetDarkMode(State)
  Protected boolVal, *Settings
  
  *Settings = gtk_settings_get_default_()
  If *Settings
    g_object_get_(*Settings, "gtk-application-prefer-dark-theme", @boolVal)
    g_object_set_(*Settings, "gtk-application-prefer-dark-theme", State);
    ProcedureReturn boolVal
  EndIf
EndProcedure

; ****

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButton1
  #MainButton2
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration


Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx -10, dy - 45)
  ResizeGadget(#MainButton1, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButton2, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy, Theme.s
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Window" , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButton1, 10, dy - 35, 120, 30, "Set Dark")
    ButtonGadget(#MainButton2, dx - 130, dy - 35, 120, 30, "Clear Dark")
    
    Theme = GetThemeName()
    Theme = RemoveString(Theme, "-dark", #PB_String_NoCase)
    SetThemeName(Theme)
    
    If Not IsDarkMode()
      SetDarkMode(#True)
    EndIf
    
    SetGadgetText(#MainEdit, "ThemeName: " +  GetThemeName())
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
              EndSelect
              
            Case #MainButton1
              Select EventType()
                Case #PB_EventType_LeftClick
                  If SetDarkMode(#True)
                    StatusBarText(#MainStatusBar, 0, " Old DarkMode: True")
                  Else
                    StatusBarText(#MainStatusBar, 0, " Old DarkMode: False")
                  EndIf
                  
              EndSelect
              
            Case #MainButton2
              Select EventType()
                Case #PB_EventType_LeftClick
                  If SetDarkMode(#False)
                    StatusBarText(#MainStatusBar, 0, " Old DarkMode: True")
                  Else
                    StatusBarText(#MainStatusBar, 0, " Old DarkMode: False")
                  EndIf
                  
              EndSelect
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
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