2023-10-27 13:02:14 +00:00
|
|
|
from std/osproc import execProcess, ProcessOption, startProcess, waitForExit, close
|
2024-08-20 13:42:00 +00:00
|
|
|
from std/os import symlinkExists, getConfigDir, walkDirs, `/`
|
2023-11-01 18:13:17 +00:00
|
|
|
from std/envvars import existsEnv, getEnv, delEnv, putEnv
|
2024-08-20 13:42:00 +00:00
|
|
|
from std/strutils import split, endsWith, contains
|
2023-11-03 12:34:30 +00:00
|
|
|
from std/options import Option, some, get, isNone
|
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"
|
2024-08-20 13:42:00 +00:00
|
|
|
styleIconsDir = iconsDir / "Styles"
|
|
|
|
accentIconsDir = iconsDir / "Accent Colors"
|
2023-10-27 13:02:14 +00:00
|
|
|
# GTK CSS for overriding the default icon size of buttons
|
2024-09-03 07:42:21 +00:00
|
|
|
gtkCSS = """
|
|
|
|
.style-button {
|
2024-10-19 15:14:29 +00:00
|
|
|
min-width: 5.5em;
|
|
|
|
min-height: 5em;
|
|
|
|
padding: 0.3em;
|
2024-10-19 11:52:53 +00:00
|
|
|
border-radius: 14%;
|
2024-09-03 07:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.accent-button {
|
2024-10-18 17:51:41 +00:00
|
|
|
-gtk-icon-size: 1.65em;
|
|
|
|
padding: 0.4em;
|
2024-10-19 11:52:53 +00:00
|
|
|
border-radius: 50%;
|
2024-09-03 20:04:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-03 23:00:12 +00:00
|
|
|
#.accent-button.suggested-action {
|
|
|
|
# outline-color: black;
|
2024-10-18 17:51:41 +00:00
|
|
|
# outline-width: 0.05em;
|
|
|
|
# outline-offset: -0.45em;
|
2024-09-03 07:42:21 +00:00
|
|
|
}"""
|
2024-08-20 13:42:00 +00:00
|
|
|
appID = "com.tromjaro.ThemeSwitcher"
|
|
|
|
appLogo = "tromjaro-theme-switcher"
|
|
|
|
themeShades = ["Light", "", "Dark"]
|
|
|
|
themeStyles = ["Default", "Nord", "Dracula", "Catppuccin", "Everforest", "Gruvbox"]
|
|
|
|
accentColors = ["Blue", "Red", "Purple", "Pink", "Teal", "Green", "Yellow", "Orange", "Grey"]
|
|
|
|
|
2024-10-19 11:52:53 +00:00
|
|
|
var iconsDirs: seq[string]
|
2024-08-20 13:42:00 +00:00
|
|
|
|
|
|
|
for dir in walkDirs(accentIconsDir / "*"):
|
|
|
|
iconsDirs.add(dir)
|
|
|
|
|
|
|
|
type Theme = tuple[shade: string, style: string, color: string]
|
|
|
|
|
|
|
|
# Function prototypes
|
|
|
|
proc getCurrentTheme(): Theme
|
|
|
|
proc makeThemeButton(shadeName: string, styleName: string): Widget
|
|
|
|
proc makeAccentButton(accentColor: string): Widget
|
|
|
|
proc runCommand(command: string, args: openArray[string]): bool
|
|
|
|
proc setIconTheme(iconName: string)
|
|
|
|
proc enableDarkPanels(): bool
|
|
|
|
proc setTheme(themeName: string)
|
|
|
|
|
|
|
|
# Highlight current theme button
|
|
|
|
var currentTheme = getCurrentTheme()
|
|
|
|
|
|
|
|
var
|
|
|
|
oldConfigDir: Option[string]
|
|
|
|
configDirChanged: bool
|
|
|
|
|
|
|
|
# Prevent loading GTK theme from ~/.config/gtk-4.0 directory when it is a symlink
|
|
|
|
if symlinkExists(getConfigDir() / "gtk-4.0"):
|
|
|
|
if existsEnv("XDG_CONFIG_HOME"):
|
|
|
|
oldConfigDir = some(getEnv("XDG_CONFIG_HOME"))
|
|
|
|
putEnv("XDG_CONFIG_HOME", "/dev/null")
|
|
|
|
configDirChanged = true
|
|
|
|
|
|
|
|
# Display the GTK-4 GUI using owlkettle
|
|
|
|
viewable App:
|
|
|
|
hooks:
|
|
|
|
build:
|
|
|
|
# Reset the user's XDG_CONFIG_HOME variable back to what it was before
|
|
|
|
if configDirChanged == true:
|
|
|
|
if oldConfigDir.isNone():
|
|
|
|
delEnv("XDG_CONFIG_HOME")
|
|
|
|
else:
|
|
|
|
putEnv("XDG_CONFIG_HOME", get(oldConfigDir))
|
|
|
|
|
|
|
|
method view(app: AppState): Widget =
|
|
|
|
result = gui:
|
|
|
|
Window:
|
|
|
|
title = "TROMjaro Theme Switcher"
|
|
|
|
# Shrink window to the smallest size
|
2024-10-19 15:14:29 +00:00
|
|
|
defaultSize = (1, 1)
|
2024-08-20 13:42:00 +00:00
|
|
|
iconName = appLogo
|
|
|
|
# Vertical box
|
|
|
|
Box(orient = OrientY, margin = 13, spacing = 10):
|
|
|
|
Label:
|
|
|
|
useMarkup=true
|
|
|
|
text="<span size='large'>STYLE</span>"
|
|
|
|
# Vertical box
|
|
|
|
Box(orient = OrientY, spacing = 5):
|
|
|
|
for shadeName in themeShades:
|
|
|
|
# Horizontal box
|
|
|
|
Box(orient = OrientX, spacing = 5):
|
|
|
|
for styleName in themeStyles:
|
|
|
|
insert(makeThemeButton(shadeName, styleName)) {.vAlign: AlignCenter, hAlign: AlignCenter.}
|
2024-10-19 11:52:53 +00:00
|
|
|
Separator(margin = Margin(top: 5)) {.vAlign: AlignCenter.}
|
2024-08-20 13:42:00 +00:00
|
|
|
Label:
|
|
|
|
useMarkup=true
|
|
|
|
text="<span size='large'>ACCENT COLOR</span>"
|
|
|
|
# Horizontal box
|
|
|
|
Box(orient = OrientX, spacing = 3):
|
|
|
|
for accentColor in accentColors:
|
|
|
|
insert(makeAccentButton(accentColor)) {.vAlign: AlignCenter, hAlign: AlignCenter.}
|
2024-10-19 11:52:53 +00:00
|
|
|
Separator(margin = Margin(top: 12)) {.vAlign: AlignCenter.}
|
2024-08-20 13:42:00 +00:00
|
|
|
# Vertical box
|
|
|
|
Box(orient = OrientY):
|
|
|
|
Label:
|
|
|
|
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>"
|
|
|
|
|
|
|
|
brew(appID, gui(App()), icons=iconsDirs, stylesheets=[newStylesheet(gtkCSS)])
|
|
|
|
|
|
|
|
|
|
|
|
# Function declarations
|
|
|
|
|
|
|
|
proc getCurrentTheme(): Theme =
|
|
|
|
let currentThemeString = execProcess("/usr/bin/xfconf-query",
|
|
|
|
args=["--channel=xsettings", "--property=/Net/ThemeName"],
|
|
|
|
options={})[0 .. ^2]
|
|
|
|
|
|
|
|
let words = currentThemeString.split('-')
|
|
|
|
if words[0] != "Colloid":
|
|
|
|
return
|
|
|
|
var currentTheme: Theme = ("", "Default", "Blue")
|
|
|
|
|
|
|
|
case len(words):
|
|
|
|
of 1: discard
|
|
|
|
|
|
|
|
of 2:
|
|
|
|
let word1 = words[1]
|
|
|
|
if word1 in accentColors:
|
|
|
|
currentTheme.color = word1
|
|
|
|
elif word1 in themeShades:
|
|
|
|
currentTheme.shade = word1
|
|
|
|
elif word1 in themeStyles:
|
|
|
|
currentTheme.style = word1
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
of 3:
|
|
|
|
let
|
|
|
|
word1 = words[1]
|
|
|
|
word2 = words[2]
|
|
|
|
if (word1 in accentColors) and (word2 in themeShades):
|
|
|
|
currentTheme.color = word1
|
|
|
|
currentTheme.shade = word2
|
|
|
|
elif (word1 in themeShades) and (word2 in themeStyles):
|
|
|
|
currentTheme.shade = word1
|
|
|
|
currentTheme.style = word2
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
of 4:
|
|
|
|
let
|
|
|
|
word1 = words[1]
|
|
|
|
word2 = words[2]
|
|
|
|
word3 = words[3]
|
|
|
|
if (word1 in accentColors) and (word2 in themeShades) and (word3 in themeStyles):
|
|
|
|
currentTheme.color = word1
|
|
|
|
currentTheme.shade = word2
|
|
|
|
currentTheme.style = word3
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
result = currentTheme
|
|
|
|
|
|
|
|
proc makeThemeButton(shadeName: string, styleName: string): Widget =
|
|
|
|
let
|
|
|
|
shade = if shadeName == "": "" else: fmt"-{shadeName}"
|
|
|
|
style = if styleName == "Default": "" else: fmt"-{styleName}"
|
|
|
|
color = if currentTheme.color in ["Blue", ""]: "" else: fmt"-{currentTheme.color}"
|
|
|
|
theme = fmt"Colloid{color}{shade}{style}"
|
|
|
|
|
|
|
|
result = gui:
|
|
|
|
Button:
|
2024-10-19 11:52:53 +00:00
|
|
|
Picture:
|
|
|
|
pixbuf = loadPixbuf(fmt"{styleIconsDir}/Colloid{shade}{style}.svg")
|
2024-08-20 13:42:00 +00:00
|
|
|
tooltip = if shadeName == "": styleName else: fmt"{shadeName}-{styleName}"
|
|
|
|
style = if (shadeName, styleName) == (currentTheme.shade, currentTheme.style):
|
|
|
|
[StyleClass("style-button"), ButtonSuggested]
|
|
|
|
else:
|
|
|
|
[StyleClass("style-button"), ButtonFlat]
|
|
|
|
|
|
|
|
proc clicked() =
|
|
|
|
setTheme(theme)
|
|
|
|
# Highlight this button
|
|
|
|
currentTheme.shade = shadeName
|
|
|
|
currentTheme.style = styleName
|
|
|
|
if currentTheme.color == "":
|
|
|
|
currentTheme.color = "Blue"
|
|
|
|
|
|
|
|
proc makeAccentButton(accentColor: string): Widget =
|
|
|
|
let
|
|
|
|
shade = if currentTheme.shade == "": "" else: fmt"-{currentTheme.shade}"
|
|
|
|
style = if currentTheme.style in ["Default", ""]: "" else: fmt"-{currentTheme.style}"
|
|
|
|
color = if accentColor == "Blue": "" else: fmt"-{accentColor}"
|
|
|
|
theme = fmt"Colloid{color}{shade}{style}"
|
|
|
|
|
|
|
|
result = gui:
|
|
|
|
Button:
|
|
|
|
icon = fmt"{accentColor}{style}{shade}"
|
|
|
|
tooltip = accentColor
|
|
|
|
style = if accentColor == currentTheme.color:
|
|
|
|
[StyleClass("accent-button"), ButtonSuggested]
|
|
|
|
else:
|
|
|
|
[StyleClass("accent-button"), ButtonFlat]
|
|
|
|
|
|
|
|
proc clicked() =
|
|
|
|
setTheme(theme)
|
|
|
|
# Highlight this button
|
|
|
|
currentTheme.color = accentColor
|
|
|
|
if currentTheme.style == "":
|
|
|
|
currentTheme.style = "Default"
|
2023-10-27 13:02:14 +00:00
|
|
|
|
|
|
|
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
|
2024-08-20 13:42:00 +00:00
|
|
|
if themeName.endsWith("-Dark") or themeName.contains("-Dark-"):
|
2023-10-27 13:02:14 +00:00
|
|
|
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
|
2024-08-20 13:42:00 +00:00
|
|
|
sendNotification(appID, "Theme Switcher", fmt"{themeName} theme{panelNotification} is enabled",
|
|
|
|
icon=appLogo)
|