Skip to content

Commit

Permalink
Merge pull request #31 from nwg-piotr/ratios
Browse files Browse the repository at this point in the history
dock hotspot delay; autotiling splitwidth/splitheight
  • Loading branch information
nwg-piotr authored Nov 17, 2022
2 parents c5cbed2 + 677d035 commit efd995a
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 78 deletions.
128 changes: 64 additions & 64 deletions nwg_shell_config/autotiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@

from nwg_shell_config.tools import temp_dir, get_data_dir, load_json, check_key

# The `~/.local/share/nwg-shell-config/settings` json file should contain the "autotiling-output-limits" key,
# e.g. like this: "autotiling-output-limits": {"DP-1": 3, "HDMI-A-1": 2, "eDP-1": 2}
settings = load_json(os.path.join(get_data_dir(), "settings"))
# Set None if it does not. It's also the value to turn limits off.
check_key(settings, "autotiling-workspaces", "")
check_key(settings, "autotiling-output-limits", {})
check_key(settings, "autotiling-output-splitwidths", {})
check_key(settings, "autotiling-output-splitheights", {})


def save_string(string, file):
Expand All @@ -63,10 +63,11 @@ def find_output_name(con):
return find_output_name(p)


def switch_splitting(i3, e, debug, workspaces):
def switch_splitting(i3, e, debug):
try:
con = i3.get_tree().find_focused()
if con and not workspaces or (str(con.workspace().num) in workspaces):
if con and not settings["autotiling-workspaces"] or (
str(con.workspace().num) in settings["autotiling-workspaces"]):
if con.floating:
# We're on i3: on sway it would be None
# May be 'auto_on' or 'user_on'
Expand All @@ -75,35 +76,35 @@ def switch_splitting(i3, e, debug, workspaces):
# We are on sway
is_floating = con.type == "floating_con"

# depth_limit contributed by @Syphdias to original autotiling script
# We only use per-output depth limits (the original depth_limit argument has been abandoned).
# Depth_limit contributed by @Syphdias to original autotiling script
output_name = find_output_name(con)
if settings["autotiling-output-limits"]:
output_name = find_output_name(con)
output_depth_limit = settings["autotiling-output-limits"][output_name] if output_name in settings[
"autotiling-output-limits"] else 0

# Assume we reached the depth limit, unless we can find a workspace
depth_limit_reached = True
current_con = con
current_depth = 0
while current_depth < output_depth_limit:
# Check if we found the workspace of the current container
if current_con.type == "workspace":
# Found the workspace within the depth limitation
depth_limit_reached = False
break

# Look at the parent for next iteration
current_con = current_con.parent

# Only count up the depth, if the container has more than one container as child
if len(current_con.nodes) > 1:
current_depth += 1

if depth_limit_reached:
if debug:
print("Debug: Depth limit reached")
return
if output_depth_limit:
# Assume we reached the depth limit, unless we can find a workspace
depth_limit_reached = True
current_con = con
current_depth = 0
while current_depth < output_depth_limit:
# Check if we found the workspace of the current container
if current_con.type == "workspace":
# Found the workspace within the depth limitation
depth_limit_reached = False
break

# Look at the parent for next iteration
current_con = current_con.parent

# Only count up the depth, if the container has more than one container as child
if len(current_con.nodes) > 1:
current_depth += 1

if depth_limit_reached:
if debug:
print("Debug: Depth limit reached", file=sys.stderr)
return

is_full_screen = con.fullscreen_mode == 1
is_stacked = con.parent.layout == "stacked"
Expand All @@ -114,6 +115,7 @@ def switch_splitting(i3, e, debug, workspaces):
and not is_stacked
and not is_tabbed
and not is_full_screen):

new_layout = "splitv" if con.rect.height > con.rect.width else "splith"

if new_layout != con.parent.layout:
Expand All @@ -123,6 +125,25 @@ def switch_splitting(i3, e, debug, workspaces):
elif debug:
print("Error: Switch failed with err {}".format(result[0].error), file=sys.stderr, )

# splitwidth & splitheight contributed by @JoseConseco to original autotiling script
if e.change in ["new", "move"] and con.percent:
if con.parent.layout == "splitv": # top / bottom
if output_name in settings["autotiling-output-splitheights"] and \
settings["autotiling-output-splitheights"][output_name] != 1.0:
i3.command("resize set height {} ppt".format(
int(con.percent * settings["autotiling-output-splitheights"][output_name] * 100)))
if debug:
print("Debug: Scaling height x {}".format(
settings["autotiling-output-splitheights"][output_name]), file=sys.stderr)
else: # left / right
if output_name in settings["autotiling-output-splitwidths"] and \
settings["autotiling-output-splitwidths"][output_name] != 1.0:
i3.command("resize set width {} ppt".format(
int(con.percent * settings["autotiling-output-splitwidths"][output_name] * 100)))
if debug:
print("Debug: Scaling width x {}".format(
settings["autotiling-output-splitwidths"][output_name]), file=sys.stderr)

elif debug:
print("Debug: No focused container found or autotiling on the workspace turned off", file=sys.stderr)

Expand All @@ -143,27 +164,6 @@ def main():
"--debug",
action="store_true",
help="print debug messages to stderr")
parser.add_argument("-v",
"--version",
action="version",
version="%(prog)s {}, Python {}".format(__version__, sys.version),
help="display version information", )
parser.add_argument("-w",
"--workspaces",
help="restricts autotiling to certain workspaces; example: autotiling --workspaces 8 9",
nargs="*",
type=str,
default=[], )
"""
Changing event subscription has already been the objective of several pull request. To avoid doing this again
and again, let's allow to specify them in the `--events` argument.
"""
parser.add_argument("-e",
"--events",
help="list of events to trigger switching split orientation; default: WINDOW MODE",
nargs="*",
type=str,
default=["WINDOW", "MODE"])

args = parser.parse_args()

Expand All @@ -180,27 +180,27 @@ def main():
for sig in catchable_sigs:
signal.signal(sig, signal_handler)

if args.debug and args.workspaces:
print("Debug: autotiling is only active on workspaces:", ','.join(args.workspaces))

if args.debug and settings["autotiling-output-limits"]:
print("Debug: autotiling per-output limits: {}".format(settings["autotiling-output-limits"]))
if args.debug:
if settings["autotiling-workspaces"]:
print("Debug: autotiling is only active on workspaces:", settings["autotiling-workspaces"], file=sys.stderr)
if settings["autotiling-output-limits"]:
print("Debug: per-output limits: {}".format(settings["autotiling-output-limits"]), file=sys.stderr)
if settings["autotiling-output-splitwidths"]:
print("Debug: per-output split widths: {}".format(settings["autotiling-output-splitwidths"]), file=sys.stderr)
if settings["autotiling-output-splitheights"]:
print("Debug: per-output split heights: {}".format(settings["autotiling-output-splitheights"]), file=sys.stderr)

# For use w/ nwg-panel
ws_file = os.path.join(temp_dir(), "autotiling")
if args.workspaces:
save_string(','.join(args.workspaces), ws_file)
if settings["autotiling-workspaces"]:
save_string(settings["autotiling-workspaces"], ws_file)
else:
if os.path.isfile(ws_file):
os.remove(ws_file)

if not args.events:
print("No events specified", file=sys.stderr)
sys.exit(1)

handler = partial(switch_splitting, debug=args.debug, workspaces=args.workspaces)
handler = partial(switch_splitting, debug=args.debug)
i3 = Connection()
for e in args.events:
for e in ["WINDOW", "MODE"]:
try:
i3.on(Event[e], handler)
print("{} subscribed".format(Event[e]))
Expand Down
7 changes: 7 additions & 0 deletions nwg_shell_config/langs/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"autotiling-tooltip": "Automates changing the horizontal/vertical window split orientation.",
"autotiling-depth-limit": "Depth limit",
"autotiling-depth-limit-tooltip": "Depth limit determines how deep will autotiling work. The '2' value allows to mimic the master/stack layout on horizontal displays. On vertical displays you may like the '3' value. Use '0' for no limit. Beyond the limit, tabbed and stacking layouts will work back again. The '1' value is not recommended, as it will spoil autotiling.",
"autotiling-split-height": "Split height",
"autotiling-split-width": "Split width",
"autotiling-split-tooltip": "Split width/height specifies the horizontal/vertical value multiplier to resize descendant containers to after splitting.",
"backgrounds": "Backgrounds",
"before-sleep": "Before sleep",
"before-sleep-tooltip": "Command to execute before systemd puts the computer to sleep.",
Expand Down Expand Up @@ -89,6 +92,8 @@
"gtklock": "Gtklock",
"help-window": "Help window",
"horizontal-alignment": "Horizontal alignment",
"hotspot-delay": "Hotspot delay",
"hotspot-delay-tooltip": "Hotspot delay in milliseconds: the smaller, the faster the mouse\npointer needs to enter the hotspot for the dock to appear.\nSet 0 to disable this feature (default 20).",
"icon-size": "Icon size",
"idle-lock-screen": "Idle & Lock screen",
"idle-settings": "Idle settings",
Expand Down Expand Up @@ -137,6 +142,7 @@
"middle-emulation": "Middle emulation",
"middle-emulation-tooltip": "Enables or disables middle click emulation",
"modules": "Modules",
"more-info": "More info",
"name": "name",
"natural-scroll": "Natural scroll",
"natural-scroll-tooltip": "Enables or disables natural (inverted) scrolling.",
Expand Down Expand Up @@ -167,6 +173,7 @@
"panel-css-name-tooltip": "Panel css style sheet file name",
"panel-settings": "Panel settings",
"pending-updates": "Pending updates",
"per-output": "Per output",
"permanent": "Permanent",
"permanent-tooltip": "Keeps the dock resident, but w/o the hotspot.",
"playerctl-module": "Playerctl module",
Expand Down
11 changes: 9 additions & 2 deletions nwg_shell_config/langs/pl_PL.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"autotiling-tooltip": "Automatyzuje zmianę poziom/pion orientacji podziału okna.",
"autotiling-depth-limit": "Limit głębokości",
"autotiling-depth-limit-tooltip": "Limit głębokości określa jak głęboko ma działać autotiling. Wartość '2' naśladuje układ master/stack na wyświetlaczach poziomych. Na pionowych może Ci się spodobać wartość '3'. Użyj '0' by wyłączyć limit. Poza limitem układ tabbed i stacking będą ponownie działać. Nie polecana jest wartość '1', która zepsuje autotiling.",
"autotiling-split-height": "Wysokość podziału",
"autotiling-split-width": "Szerokość podziału",
"autotiling-split-tooltip": "Szerokość/wysokość podziału określa mnożnik wymiaru,\ndo którego kontener potomny zostanie przeskalowany po podziale.",
"backgrounds": "Tapety",
"before-sleep": "Przed uśpieniem",
"before-sleep-tooltip": "Komenda do wykonania zanim `systemd` uśpi komputer.",
Expand Down Expand Up @@ -89,6 +92,8 @@
"gtklock": "Gtklock",
"help-window": "Okno pomocy",
"horizontal-alignment": "Wyrównywanie w poziomie",
"hotspot-delay": "Opóźnienie hotspota",
"hotspot-delay-tooltip": "Opóźnienie hotspota w milisekundach: im mniejsze, tym szybciej\nmysz musi być wprowadzona nad gorący punkt, by dok się pojawił.\nUstaw 0 by wyłączyć tę funkcję (domyślnie 20).",
"icon-size": "Wielkość ikony",
"idle-lock-screen": "Bezczynność i blokada",
"idle-settings": "Bezczynność",
Expand Down Expand Up @@ -137,6 +142,7 @@
"middle-emulation": "Emulacja środkowego",
"middle-emulation-tooltip": "Włącza / wyłącza emulację kliknięcia środkowym przyciskiem.",
"modules": "Moduły",
"more-info": "Więcej informacji",
"name": "nazwa",
"natural-scroll": "Naturalne przewijanie",
"natural-scroll-tooltip": "Włącza / wyłącza naturalne (odwrócone) przewijanie.",
Expand Down Expand Up @@ -165,8 +171,9 @@
"panel-css": "Panel i css",
"panel-css-name": "Arkusz stylów panelu",
"panel-css-name-tooltip": "Nazwa pliku css z arkuszem stylów panelu",
"panel-settings": "Ustawienia panelu",
"panel-settings": "Ust. panelu",
"pending-updates": "Oczekujące aktualizacje",
"per-output": "Na wyjście",
"permanent": "Stały",
"permanent-tooltip": "Trzymaj dok w pamięci, ale bez hotspota.",
"playerctl-module": "Moduł playerctl",
Expand Down Expand Up @@ -200,7 +207,7 @@
"show-category-buttons": "Pokazuj przyciski kategorii",
"show-category-buttons-tooltip": "Pokazuj na górze przyciski kategorii freedesktop.",
"show-labels": "Pokazuj etykiety",
"show-on-startup": "Pokazuj po uruchomieniu",
"show-on-startup": "Pokazuj na start",
"show-on-startup-tooltip": "Odznacz by nie uruchamiać tego okna podczas startu systemu.",
"sleep-command": "Komenda uśpienia",
"sleep-timeout-tooltip": "Opóźnienie uśpienia w sekundach.\nMusi być dłuższe niż opóźnienie blokady.",
Expand Down
5 changes: 5 additions & 0 deletions nwg_shell_config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ def save_includes():
if preset["dock-icon-size"]:
cmd_dock += " -i {}".format(preset["dock-icon-size"])

cmd_dock += " -hd {}".format(preset["dock-hotspot-delay"])

if preset["dock-exclusive"]:
cmd_dock += " -x"

Expand Down Expand Up @@ -760,6 +762,8 @@ def load_settings():
"autotiling-on": True,
"autotiling-limit": False,
"autotiling-output-limits": {},
"autotiling-output-splitwidths": {},
"autotiling-output-splitheights": {},
"appindicator": True,
"night-lat": -1,
"night-long": -1,
Expand Down Expand Up @@ -896,6 +900,7 @@ def load_preset(file_name):
"dock-alignment": "center",
"dock-margin": 0,
"dock-icon-size": 48,
"dock-hotspot-delay": 20,
"dock-css": "",
"dock-on": False,
"swaync-positionX": "right",
Expand Down
1 change: 1 addition & 0 deletions nwg_shell_config/shell/custom
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dock-alignment": "center",
"dock-margin": 0,
"dock-icon-size": 48,
"dock-hotspot-delay": 20,
"dock-css": "",
"dock-on": false,
"swaync-positionX": "right",
Expand Down
1 change: 1 addition & 0 deletions nwg_shell_config/shell/preset-0
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dock-alignment": "center",
"dock-margin": 0,
"dock-icon-size": 48,
"dock-hotspot-delay": 20,
"dock-css": "preset-0.css",
"dock-on": false,
"swaync-positionX": "right",
Expand Down
1 change: 1 addition & 0 deletions nwg_shell_config/shell/preset-1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dock-alignment": "center",
"dock-margin": 0,
"dock-icon-size": 48,
"dock-hotspot-delay": 20,
"dock-css": "preset-1.css",
"dock-on": true,
"swaync-positionX": "center",
Expand Down
1 change: 1 addition & 0 deletions nwg_shell_config/shell/preset-2
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dock-alignment": "center",
"dock-margin": 0,
"dock-icon-size": 48,
"dock-hotspot-delay": 20,
"dock-css": "preset-2.css",
"dock-on": false,
"swaync-positionX": "right",
Expand Down
1 change: 1 addition & 0 deletions nwg_shell_config/shell/preset-3
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dock-alignment": "start",
"dock-margin": 0,
"dock-icon-size": 42,
"dock-hotspot-delay": 20,
"dock-css": "preset-3.css",
"dock-on": true,
"swaync-positionX": "right",
Expand Down
Loading

0 comments on commit efd995a

Please sign in to comment.