#!/bin/bash
# -*- coding: utf-8 -*-
# GPL v.3
# antiX workspace count switcher
# by antiX community 2023,2025
# ver. 0.3

TEXTDOMAINDIR=/usr/share/locale 
TEXTDOMAIN=antiX-workspace-count

GtkdlgRahmen=false
#symbol_antiX=antixlogo  # GTK named symbol needed here
symbol_antiX="preferences-desktop-theme"
initial=$(wmctrl -d | wc -l)
dx=$(wmctrl -d | grep "*" | cut -d" " -f5 | cut -dx -f1)
dy=$(wmctrl -d | grep "*" | cut -d" " -f5 | cut -dx -f2)

export ANTIX_DESKTOP_COUNT_DIALOG='
<window title="'$"aWCS antiX workspace count switcher"'" decorated="$GtkdlgRahmen" icon-name="'$symbol_antiX'" skip_taskbar_hint="true" resizable="false">
  <vbox>
    <text use-markup="true">
      <label>"<b>'$"antiX workspace count switcher"'</b>"</label>
    </text>
    <hbox>
      <text>
        <label>'$"Set the desired number of desktops:"'</label>
      </text>
      <spinbutton range-min="1" range-max="32" range-step="1" range-value="'$initial'" tooltip-text="'$"Use mouse wheel or click on arrows. You can also enter a number by keyboard followed by enter key. The change takes place immediately."'">
	    <variable>ANZAHL</variable>
	    <action>"wmctrl -n $ANZAHL"</action>
      </spinbutton>
    </hbox>
    <hbox space-expand="true" space-fill="true">
      <text><label>""</label></text>
      <button tooltip-text="'$"Click »Done« when desired number of workspaces was set."'">
      <label>"'$"Done"'"</label>
      </button>
      <text><label>""</label></text>
    </hbox>
  </vbox>
</window>
'

gtkdialog --program=ANTIX_DESKTOP_COUNT_DIALOG -G "+$(bc <<<"scale=0; $dx*0.9/10")+$(bc <<<"scale=0; $dy*8.5/10")" >/dev/null

new=$(wmctrl -d | wc -l)
if grep '^wmctrl -n' "$HOME/.desktop-session/startup" >/dev/null 2>&1; then
    sed -i "s/^wmctrl -n ..*$/wmctrl -n $new \&/" "$HOME/.desktop-session/startup"
else
    echo -e "\n## Restore user defined desktop count\nwmctrl -n $new &\n" >> "$HOME/.desktop-session/startup"
fi

exit 0
