Rewrite layout switcher in Nim #3

Merged
tio merged 8 commits from nim-rewrite into master 2023-12-26 00:08:43 +00:00
Showing only changes of commit 1f29b13f85 - Show all commits

View File

@ -75,24 +75,6 @@ proc disableTopBarIntegration(): bool =
args=["--channel=xfwm4", "--property=/general/button_layout", "--create", "--type=string", "--set=|HMC"])
return true
proc enableLayout(layoutName: string) =
case layoutName
of "Windows-Like", "MX-Like", "Gnome-Like", "TopX-Like":
if not disableTopBarIntegration():
return
of "Unity-Like", "MacOS-Like":
if not enableTopBarIntegration():
return
else:
return
discard runCommand("/usr/bin/xfce4-panel-profiles", ["load", profilesDir / layoutName & ".tar.bz2"])
if runCommand("/usr/bin/killall", ["mate-hud"]) == 0:
discard runCommand("/usr/bin/setsid", ["-f", "/usr/lib/mate-hud/mate-hud"])
sleep(5000)
sendNotification(appID, "Layout Switcher", layoutName & " layout was enabled",
icon = iconsDir / layoutName.toLowerAscii() & "-layout.png")
var
oldConfigDir: Option[string]
configDirChanged: bool
@ -105,6 +87,7 @@ if symlinkExists(getConfigDir() / "gtk-4.0"):
configDirChanged = true
viewable App:
loading: bool
hooks:
build:
# Reset the user's XDG_CONFIG_HOME variable back to what it was before
@ -114,6 +97,37 @@ viewable App:
else:
putEnv("XDG_CONFIG_HOME", get(oldConfigDir))
proc enableLayout(args: tuple[layoutName: string, app: AppState]) {.thread.} =
let
layoutName = args.layoutName
app = args.app
defer:
app.loading = false
app.redrawFromThread()
case layoutName
of "Windows-Like", "MX-Like", "Gnome-Like", "TopX-Like":
if not disableTopBarIntegration():
return
of "Unity-Like", "MacOS-Like":
if not enableTopBarIntegration():
return
else:
return
# Load the layout profile
discard runCommand("/usr/bin/xfce4-panel-profiles", ["load", profilesDir / layoutName & ".tar.bz2"])
# Restart mate-hud
if runCommand("/usr/bin/killall", ["mate-hud"]) == 0:
discard runCommand("/usr/bin/setsid", ["-f", "/usr/lib/mate-hud/mate-hud"])
# Wait 5 seconds for the effects to take place on screen
sleep(5000)
sendNotification(appID, "Layout Switcher", layoutName & " layout was enabled",
icon = iconsDir / layoutName.toLowerAscii() & "-layout.png")
var thread: Thread[(string, AppState)]
method view(app: AppState): Widget =
result = gui:
Window:
@ -121,6 +135,11 @@ method view(app: AppState): Widget =
# Shrink window to the smallest size
defaultSize = (0, 0)
iconName = "tromjaro-layout-switcher"
if app.loading:
Box(orient = OrientY, margin = 70):
Spinner(spinning = true)
Label(text = "Loading your layout, please wait...") {.expand: false.}
else:
Box(orient = OrientY, margin = 7, spacing = 5):
Box(orient = OrientX):
Label {.hAlign: AlignEnd.}:
@ -142,6 +161,9 @@ method view(app: AppState): Widget =
tooltip = tooltip
style = [ButtonFlat, StyleClass("layout-button")]
proc clicked() =
enableLayout(layoutName)
app.loading = true
createThread(thread, enableLayout, (layoutName, app))
brew(appID, gui(App()), icons=[iconsDir], stylesheets=[newStylesheet(gtkCSS)])
joinThread(thread)