From 4d6446d47d391ede296fddec079304ddf27ac7fb Mon Sep 17 00:00:00 2001 From: rokosun Date: Sat, 28 Oct 2023 23:04:56 +0200 Subject: [PATCH 1/2] Simplify code, remove unnecessary 2D array --- themeSwitcher.nim | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/themeSwitcher.nim b/themeSwitcher.nim index 160de63..caf2da0 100644 --- a/themeSwitcher.nim +++ b/themeSwitcher.nim @@ -1,7 +1,6 @@ from std/osproc import execProcess, ProcessOption, startProcess, waitForExit, close from std/os import symlinkExists, getConfigDir, putEnv, `/` from std/strutils import split, endsWith, strip -from std/algorithm import fill from std/strformat import fmt import owlkettle @@ -58,19 +57,11 @@ proc setTheme(themeName: string) = if symlinkExists(getConfigDir() / "gtk-4.0"): putEnv("XDG_CONFIG_HOME", "/dev/null") -var - lastClickedButton: array[2, int] - # Display the GTK-4 GUI using owlkettle viewable App: - # 2D array storing the style for each button - buttonStyles: array[themeStyles.len(),array[themeColors.len(), StyleClass]] + highlightButton: array[2, string] hooks: build: - # Make all buttons flat by default - for index in 0..state.buttonStyles.high(): - fill(state.buttonStyles[index], ButtonFlat) - let currentTheme: string = execProcess("/usr/bin/xfconf-query", args=["--channel=xsettings", "--property=/Net/ThemeName"], options={}).strip(leading=false, trailing=true, chars={'\n'}) @@ -80,15 +71,11 @@ viewable App: if currentThemeSplit.len() < 3 or currentThemeSplit[0] != "Colloid": return - let currentThemeColorIndex = find(themeColors, currentThemeSplit[1]) - let currentThemeStyleIndex = find(themeStyles, currentThemeSplit[2]) - - if -1 in [currentThemeColorIndex, currentThemeStyleIndex]: - return + let currentThemeColor = currentThemeSplit[1] + let currentThemeStyle = currentThemeSplit[2] # Highlight current theme button - state.buttonStyles[currentThemeStyleIndex][currentThemeColorIndex] = ButtonSuggested - lastClickedButton = [currentThemeStyleIndex, currentThemeColorIndex] + state.highlightButton = [currentThemeColor, currentThemeStyle] method view(app: AppState): Widget = result = gui: @@ -103,26 +90,24 @@ method view(app: AppState): Widget = Label {.vAlign: AlignStart.}: useMarkup = true text="NOTE: Some apps need to be reopened for the theme to be applied." - for y, themeStyle in themeStyles: + for themeStyle in themeStyles: # Horizontal box Box(orient = OrientX, spacing = 5): - for x, themeColor in themeColors: + for themeColor in themeColors: 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"): - fmt"{themeColor}-{themeStyle} (default)" - else: - fmt"{themeColor}-{themeStyle}" - style = app.buttonStyles[y][x] + fmt"{themeColor}-{themeStyle} (default)" + else: + fmt"{themeColor}-{themeStyle}" + style = if [themeColor, themeStyle] == app.highlightButton: + ButtonSuggested + else: + ButtonFlat proc clicked() = - var thisButton = [y, x] setTheme(fmt"Colloid-{themeColor}-{themeStyle}") # Highlight this button - app.buttonStyles[y][x] = ButtonSuggested - if lastClickedButton != thisButton: - # Remove highlight from lastClickedButton - app.buttonStyles[lastClickedButton[0]][lastClickedButton[1]] = ButtonFlat - lastClickedButton = thisButton + app.highlightButton = [themeColor, themeStyle] brew("com.tromjaro.ThemeSwitcher", gui(App()), icons=[iconsDir], stylesheets=[newStylesheet(gtkCSS)]) From d3c1054ec04616911e2d9ec4e85f32802a8f0fb0 Mon Sep 17 00:00:00 2001 From: rokosun Date: Sun, 29 Oct 2023 08:41:17 +0100 Subject: [PATCH 2/2] No need to use strip here, just remove the last character --- themeSwitcher.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themeSwitcher.nim b/themeSwitcher.nim index caf2da0..7bf4110 100644 --- a/themeSwitcher.nim +++ b/themeSwitcher.nim @@ -1,6 +1,6 @@ from std/osproc import execProcess, ProcessOption, startProcess, waitForExit, close from std/os import symlinkExists, getConfigDir, putEnv, `/` -from std/strutils import split, endsWith, strip +from std/strutils import split, endsWith from std/strformat import fmt import owlkettle @@ -64,7 +64,7 @@ viewable App: build: let currentTheme: string = execProcess("/usr/bin/xfconf-query", args=["--channel=xsettings", "--property=/Net/ThemeName"], - options={}).strip(leading=false, trailing=true, chars={'\n'}) + options={})[0 .. ^2] let currentThemeSplit: seq[string] = currentTheme.split('-', 2)