Skip to content

Commit

Permalink
set gnome dark theme preference + css theme message moved from textbox
Browse files Browse the repository at this point in the history
  • Loading branch information
ALEX11BR committed Jul 16, 2024
1 parent 3690279 commit d4d28fa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
32 changes: 17 additions & 15 deletions src/applythemes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (C) 2021 Popa Ioan Alexandru
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Expand Down Expand Up @@ -56,7 +56,7 @@ def applyThemes(self, props, gtk2Theme, gtk4Theme, kvantumTheme, kvantumThemeFil
iconKeyFile.set_string("Icon Theme", "Comment", "Default icon theme")
iconKeyFile.set_string("Icon Theme", "Inherits", props.gtk_cursor_theme_name)
iconKeyFile.save_to_file(os.path.join(GLib.get_home_dir(), ".icons", "default", "index.theme"))

if kvantumTheme:
kvantumThemeKeyFile = GLib.KeyFile()
if kvantumThemeFilePath[:2] != "//":
Expand All @@ -80,7 +80,7 @@ def applyThemes(self, props, gtk2Theme, gtk4Theme, kvantumTheme, kvantumThemeFil
kvantumKeyFile = GLib.KeyFile()
kvantumKeyFile.set_string("General", "theme", kvantumTheme+"#")
kvantumKeyFile.save_to_file(os.path.join(GLib.get_user_config_dir(), "Kvantum", "kvantum.kvconfig"))

with open(os.path.join(GLib.get_home_dir(), ".gtkrc-2.0"), "w") as gtk2File:
gtk2File.write(f'gtk-theme-name="{gtk2Theme}"\n')
gtk2File.write(f'gtk-icon-theme-name="{props.gtk_icon_theme_name}"\n')
Expand Down Expand Up @@ -114,7 +114,7 @@ def applyThemes(self, props, **kwargs):
self.settings.set_string("icon-theme", props.gtk_icon_theme_name)
self.settings.set_string("gtk-key-theme", props.gtk_key_theme_name or "")
self.settings.set_string("font-name", props.gtk_font_name)

class MateApplyThemes(GSettingsApplyThemes):
"""
Live reloadable theme & options setting code specific to Mate.
Expand All @@ -137,7 +137,7 @@ def applyThemes(self, props, **kwargs):
else:
self.settingsFont.set_string("antialiasing", "grayscale" if props.gtk_xft_antialias else "none")
self.settingsFont.set_string("hinting", props.gtk_xft_hintstyle[4:]) # drop the first 4 letters ('hint' in all cases)

self.settings.set_boolean("gtk-overlay-scrolling", props.gtk_overlay_scrolling)
self.settings.set_boolean("buttons-have-icons", props.gtk_button_images)
self.settings.set_boolean("menus-have-icons", props.gtk_menu_images)
Expand All @@ -159,10 +159,12 @@ class GnomeApplyThemes(CinnGnomeApplyThemes):
"""
def __init__(self):
self.settings = Gio.Settings.new("org.gnome.desktop.interface")

def applyThemes(self, props, **kwargs):
super().applyThemes(props, **kwargs)

self.settings.set_string("color-scheme", "prefer-dark" if props.gtk_application_prefer_dark_theme else "default")

if props.gtk_xft_rgba != "none":
self.settings.set_string("font-rgba-order", props.gtk_xft_rgba or "rgb")
self.settings.set_string("font-antialiasing", "rgba" if props.gtk_xft_antialias else "none")
Expand Down Expand Up @@ -233,18 +235,18 @@ def __init__(self):

def applyThemes(self, props, **kwargs):
super().applyThemes(props, **kwargs)

lxsessionKeyFile = GLib.KeyFile()
lxsessionKeyFile.load_from_file(self.lxsessionConfigPath, GLib.KeyFileFlags.NONE)

for option in self.options:
value = self.options[option]

if type(value) is str:
lxsessionKeyFile.set_string("GTK", "s"+option, value)
else:
lxsessionKeyFile.set_integer("GTK", "i"+option, int(value))

lxsessionKeyFile.save_to_file(self.lxsessionConfigPath)

class XfconfApplyThemes(XApplyThemes):
Expand All @@ -263,7 +265,7 @@ def applyThemes(self, props, **kwargs):

for option in self.options:
value = self.options[option]

if option == "Xft/DPI":
value = int(value/1024)
if type(value) is int:
Expand Down Expand Up @@ -293,14 +295,14 @@ def applyThemes(self, props, **kwargs):
with open(self.confFile, "w") as file:
for option in self.options:
value = self.options[option]

if type(value) is str:
value = f'"{value}"'
elif type(value) is bool:
value = int(value)

file.write(f'{option} {value}\n')

subprocess.run(["pkill", "-HUP", "^xsettingsd$"])

def isRunning(app):
Expand Down
22 changes: 11 additions & 11 deletions src/window.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (C) 2021 Popa Ioan Alexandru
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Expand All @@ -27,7 +27,7 @@
@Gtk.Template(resource_path='/com/github/alex11br/themechanger/window.ui')
class ThemechangerWindow(Gtk.ApplicationWindow):
__gtype_name__ = 'ThemechangerWindow'

gtkProps = Gtk.Settings.get_default().props

defaultDisplay = Gdk.Display.get_default()
Expand Down Expand Up @@ -107,14 +107,14 @@ def __init__(self, app):
self.defaultScreen, self.cssProvider, Gtk.STYLE_PROVIDER_PRIORITY_USER+1
# A higher priority than the USER one, to allow a theme change without requiring heavy meddling with the cssProvider
)

# Now we'll set up the widgets
try:
with open(os.path.join(GLib.get_user_config_dir(), "gtk-3.0", "gtk.css"), "r") as cssFile:
self.cssTextBuffer.set_text(cssFile.read())
# If the CSS file doesn't exist (or we are unable to access it for reading), we'll set up a nice text placeholder
except:
self.cssTextBuffer.set_text("/* Feel free to edit this and see instantaneous results */")
self.cssTextBuffer.set_text("")

self.themeApplier = getThemeApplier()
if type(self.themeApplier) == BaseApplyThemes:
Expand Down Expand Up @@ -231,7 +231,7 @@ def __init__(self, app):
self.hintingCombobox.set_active_id(self.gtkProps.gtk_xft_hintstyle if (self.gtkProps.gtk_xft_hinting != 0) else "hintnone")

self.subpixelCombobox.set_active_id(self.gtkProps.gtk_xft_rgba or "none")


def updateGtkThemeCssProvider(self):
gtkThemeCssProvider = Gtk.CssProvider.get_named(self.gtkProps.gtk_theme_name, "dark" if self.gtkProps.gtk_application_prefer_dark_theme else None)
Expand All @@ -245,7 +245,7 @@ def setDefaultCursor(self):
# This makes the cursor update to the recently selected theme
defaultCursor = Gdk.Cursor.new_for_display(self.defaultDisplay, Gdk.CursorType.LEFT_PTR)
self.defaultScreen.get_root_window().set_cursor(defaultCursor)

def onGtkThemeChanged(self, themename):
self.onSettingChanged()

Expand Down Expand Up @@ -306,7 +306,7 @@ def overlayScrollbarsSwitchStateSet(self, switch, state):
def anotherGtk2ThemeSwitchStateSet(self, switch, state):
self.gtk2ThemeName = self.gtk2SearchableThemeList.selectedTheme if state else self.gtkProps.gtk_theme_name
self.gtk2SearchableThemeList.set_visible(state)

@Gtk.Template.Callback()
def anotherGtk4ThemeSwitchStateSet(self, switch, state):
self.gtk4ThemeName = self.gtk4SearchableThemeList.selectedTheme if state else self.gtkProps.gtk_theme_name
Expand Down Expand Up @@ -435,12 +435,12 @@ def onSettingChanged(self, *args):
Whenever an option is changed, this function is called,
either being defined as one of the callback functions from the 'window.ui' file,
either as the callback function of SearchableThemeList's.
Its goal is to enable the 'Apply' button(s),
that get disabled whenever the settings are applied.
"""
self.gtkThemesApplyButton.set_sensitive(True)
self.iconThemesApplyButton.set_sensitive(True)
self.cursorThemesApplyButton.set_sensitive(True)
self.editCssApplyButton.set_sensitive(True)
self.otherOptionsApplyButton.set_sensitive(True)
self.otherOptionsApplyButton.set_sensitive(True)
32 changes: 29 additions & 3 deletions src/window.ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.1 -->
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkImage" id="addImage1">
Expand Down Expand Up @@ -895,6 +895,32 @@
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">The CSS won't be aplied to GTK2 apps.</property>
<property name="label" translatable="yes">Feel free to edit the GTK CSS below and see instantaneous results in this application.</property>
<property name="justify">center</property>
<property name="wrap">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
Expand All @@ -912,7 +938,7 @@
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
<property name="position">1</property>
</packing>
</child>
<child>
Expand Down Expand Up @@ -971,7 +997,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
</object>
Expand Down

0 comments on commit d4d28fa

Please sign in to comment.