2023-10-27 13:02:14 +00:00
|
|
|
from std/osproc import execProcess, ProcessOption, startProcess, waitForExit, close
|
2023-11-01 18:13:17 +00:00
|
|
|
from std/envvars import existsEnv, getEnv, delEnv, putEnv
|
|
|
|
from std/os import symlinkExists, getConfigDir, `/`
|
2023-11-03 12:34:30 +00:00
|
|
|
from std/options import Option, some, get, isNone
|
2023-10-29 07:41:17 +00:00
|
|
|
from std/strutils import split, endsWith
|
2023-10-27 13:02:14 +00:00
|
|
|
from std/strformat import fmt
|
|
|
|
import owlkettle
|
|
|
|
|
|
|
|
const
|
|
|
|
# Set the directory path where icons are stored
|
|
|
|
iconsDir = "/usr/share/tromjaro-theme-switcher/icons"
|
|
|
|
# GTK CSS for overriding the default icon size of buttons
|
2023-10-28 06:30:04 +00:00
|
|
|
gtkCSS = "button { -gtk-icon-size: 85px; }"
|
2023-11-01 18:13:17 +00:00
|
|
|
applicationID = "com.tromjaro.ThemeSwitcher"
|
2023-10-27 13:02:14 +00:00
|
|
|
themeColors = ["Grey", "Pink", "Green", "Orange", "Purple", "Yellow", "Teal"]
|
|
|
|
themeStyles = ["Light", "Nord", "Dark", "Dark-Nord"]
|
|
|
|
|
|
|
|
proc runCommand(command: string, args: openArray[string]): bool =
|
|
|
|
## This will run a command with the given args and return true upon success
|
|
|
|
let process = startProcess(command, args=args, options={poParentStreams})
|
|
|
|
result = process.waitForExit() == 0
|
|
|
|
process.close()
|
|
|
|
|
|
|
|
proc setIconTheme(iconName: string) =
|
|
|
|
# Change icon theme in XFCE
|
|
|
|
discard runCommand("/usr/bin/xfconf-query",
|
|
|
|
args=["--channel=xsettings", "--property=/Net/IconThemeName", "--create", "--type=string", "--set", iconName])
|
|
|
|
|
|
|
|
proc enableDarkPanels(): bool =
|
|
|
|
# Return if dark panels is already enabled
|
|
|
|
if execProcess("/usr/bin/xfconf-query",
|
|
|
|
args=["--channel=xfce4-panel", "--property=/panels/dark-mode"], options={}) == "true\n":
|
|
|
|
return
|
|
|
|
# Try to enable it and return true upon success
|
|
|
|
result = runCommand("/usr/bin/xfconf-query",
|
|
|
|
args=["--channel=xfce4-panel", "--property=/panels/dark-mode", "--create", "--type=bool", "--set=true"])
|
|
|
|
|
|
|
|
proc setTheme(themeName: string) =
|
|
|
|
|
|
|
|
var panelNotification: string
|
|
|
|
|
|
|
|
# Set the icon theme and panel color according to the chosen theme
|
|
|
|
if themeName.endsWith("-Dark") or themeName.endsWith("-Dark-Nord"):
|
|
|
|
setIconTheme("zafiro-dark")
|
|
|
|
else:
|
|
|
|
setIconTheme("zafiro")
|
|
|
|
if enableDarkPanels():
|
|
|
|
panelNotification = " with dark panels"
|
|
|
|
|
|
|
|
# Change the main theme to the chosen one
|
|
|
|
discard runCommand("/usr/bin/xfconf-query",
|
|
|
|
args=["--channel=xsettings", "--property=/Net/ThemeName", "--create", "--type=string", "--set", themeName])
|
|
|
|
|
|
|
|
# Send notification about the theme change
|
2023-11-01 18:13:17 +00:00
|
|
|
sendNotification(applicationID, "Theme Switcher", fmt"{themeName} theme{panelNotification} is enabled",
|
2023-10-27 13:02:14 +00:00
|
|
|
icon=fmt"{iconsDir}/{themeName}.svg")
|
|
|
|
|
|
|
|
|
2023-11-01 18:13:17 +00:00
|
|
|
var
|
2023-11-03 12:34:30 +00:00
|
|
|
oldConfigDir: Option[string]
|
2023-11-01 18:13:17 +00:00
|
|
|
configDirChanged: bool
|
|
|
|
|
2023-10-27 13:02:14 +00:00
|
|
|
# Prevent loading GTK theme from ~/.config/gtk-4.0 directory when it is a symlink
|
|
|
|
if symlinkExists(getConfigDir() / "gtk-4.0"):
|
2023-11-01 18:13:17 +00:00
|
|
|
if existsEnv("XDG_CONFIG_HOME"):
|
2023-11-03 12:34:30 +00:00
|
|
|
oldConfigDir = some(getEnv("XDG_CONFIG_HOME"))
|
2023-10-27 13:02:14 +00:00
|
|
|
putEnv("XDG_CONFIG_HOME", "/dev/null")
|
2023-11-01 18:13:17 +00:00
|
|
|
configDirChanged = true
|
2023-10-27 13:02:14 +00:00
|
|
|
|
|
|
|
# Display the GTK-4 GUI using owlkettle
|
|
|
|
viewable App:
|
2023-10-28 21:04:56 +00:00
|
|
|
highlightButton: array[2, string]
|
2023-10-27 13:02:14 +00:00
|
|
|
hooks:
|
|
|
|
build:
|
2023-11-01 18:13:17 +00:00
|
|
|
# Reset the user's XDG_CONFIG_HOME variable back to what it was before
|
|
|
|
if configDirChanged == true:
|
2023-11-03 12:34:30 +00:00
|
|
|
if oldConfigDir.isNone():
|
2023-11-01 18:13:17 +00:00
|
|
|
delEnv("XDG_CONFIG_HOME")
|
|
|
|
else:
|
2023-11-03 12:34:30 +00:00
|
|
|
putEnv("XDG_CONFIG_HOME", get(oldConfigDir))
|
2023-11-01 18:13:17 +00:00
|
|
|
|
2023-10-27 13:02:14 +00:00
|
|
|
let currentTheme: string = execProcess("/usr/bin/xfconf-query",
|
|
|
|
args=["--channel=xsettings", "--property=/Net/ThemeName"],
|
2023-10-29 07:41:17 +00:00
|
|
|
options={})[0 .. ^2]
|
2023-10-27 13:02:14 +00:00
|
|
|
|
|
|
|
let currentThemeSplit: seq[string] = currentTheme.split('-', 2)
|
|
|
|
|
|
|
|
if currentThemeSplit.len() < 3 or currentThemeSplit[0] != "Colloid":
|
|
|
|
return
|
|
|
|
|
2023-10-28 21:04:56 +00:00
|
|
|
let currentThemeColor = currentThemeSplit[1]
|
|
|
|
let currentThemeStyle = currentThemeSplit[2]
|
2023-10-27 13:02:14 +00:00
|
|
|
|
|
|
|
# Highlight current theme button
|
2023-10-28 21:04:56 +00:00
|
|
|
state.highlightButton = [currentThemeColor, currentThemeStyle]
|
2023-10-27 13:02:14 +00:00
|
|
|
|
|
|
|
method view(app: AppState): Widget =
|
|
|
|
result = gui:
|
|
|
|
Window:
|
|
|
|
title = "TROMjaro Theme Switcher"
|
|
|
|
# Shrink window to the smallest size
|
|
|
|
defaultSize = (0, 0)
|
2023-11-05 09:51:43 +00:00
|
|
|
iconName = "tromjaro-theme-switcher"
|
2023-10-27 13:02:14 +00:00
|
|
|
# Vertical box
|
|
|
|
Box(orient = OrientY, margin = 7, spacing = 5):
|
|
|
|
Label:
|
2023-10-27 14:43:56 +00:00
|
|
|
text="A Theme Switcher for TROMjaro's default theme-set (Colloid) and icon-set (Zafiro)."
|
|
|
|
Label {.vAlign: AlignStart.}:
|
|
|
|
useMarkup = true
|
|
|
|
text="<span size='small'>NOTE: Some apps need to be reopened for the theme to be applied.</span>"
|
2023-10-28 21:04:56 +00:00
|
|
|
for themeStyle in themeStyles:
|
2023-10-27 13:02:14 +00:00
|
|
|
# Horizontal box
|
|
|
|
Box(orient = OrientX, spacing = 5):
|
2023-10-28 21:04:56 +00:00
|
|
|
for themeColor in themeColors:
|
2023-10-27 13:02:14 +00:00
|
|
|
Button {.vAlign: AlignCenter, hAlign: AlignCenter.}:
|
|
|
|
icon = fmt"Colloid-{themeColor}-{themeStyle}"
|
|
|
|
# Teal-Dark-Nord theme will have "(default)" added to its tooltip
|
|
|
|
tooltip = if (themeColor, themeStyle) == ("Teal", "Dark-Nord"):
|
2023-10-28 21:04:56 +00:00
|
|
|
fmt"{themeColor}-{themeStyle} (default)"
|
|
|
|
else:
|
|
|
|
fmt"{themeColor}-{themeStyle}"
|
|
|
|
style = if [themeColor, themeStyle] == app.highlightButton:
|
|
|
|
ButtonSuggested
|
|
|
|
else:
|
|
|
|
ButtonFlat
|
2023-10-27 13:02:14 +00:00
|
|
|
proc clicked() =
|
|
|
|
setTheme(fmt"Colloid-{themeColor}-{themeStyle}")
|
|
|
|
# Highlight this button
|
2023-10-28 21:04:56 +00:00
|
|
|
app.highlightButton = [themeColor, themeStyle]
|
2023-10-27 13:02:14 +00:00
|
|
|
|
2023-11-01 18:13:17 +00:00
|
|
|
brew(applicationID, gui(App()), icons=[iconsDir], stylesheets=[newStylesheet(gtkCSS)])
|