From 161c6337a4458d93db3b73710dda010cc4083b57 Mon Sep 17 00:00:00 2001 From: kenji Date: Sun, 28 Dec 2025 12:13:41 -0600 Subject: [PATCH] feat(hypr): add force switching of workspace useful for going from special workspace to normal workspace --- apps/hyprland/home.nix | 2 ++ apps/hyprland/scripts.nix | 41 ++++++++++++++++++++++++++++++++++ test/force-switch-workspace.sh | 26 +++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 apps/hyprland/scripts.nix create mode 100755 test/force-switch-workspace.sh diff --git a/apps/hyprland/home.nix b/apps/hyprland/home.nix index cc97bf5..978a866 100644 --- a/apps/hyprland/home.nix +++ b/apps/hyprland/home.nix @@ -7,6 +7,8 @@ ./hypr/animation.nix ./hypr/misc.nix ./hypr/input.nix + + ./scripts.nix ]; wayland.windowManager.hyprland.enable = true; wayland.windowManager.hyprland.systemd.enable = false; # for UWSM support... diff --git a/apps/hyprland/scripts.nix b/apps/hyprland/scripts.nix new file mode 100644 index 0000000..95ba9bb --- /dev/null +++ b/apps/hyprland/scripts.nix @@ -0,0 +1,41 @@ +{pkgs, ...}: let + hakase-workspace-switch-force = pkgs.writeShellScriptBin "hakase-workspace-switch-force" '' + # Define binary paths to ensure they work regardless of environment PATH + HYPRCTL="${pkgs.hyprland}/bin/hyprctl" + JQ="${pkgs.jq}/bin/jq" + + # Get the special workspace name + special_workspace=$($HYPRCTL monitors -j | $JQ -r '.[] | select(.focused) | .specialWorkspace.name') + + # Strip the "special:" prefix (Nix escape: ''${var#*:}) + workspace_name=''${special_workspace#*:} + + # Get the target workspace number from argument + chosen_workspace_num=$1 + + if [[ -z ''${chosen_workspace_num} ]]; then + echo "Usage: $(basename "$0") [number]" + exit 1 + fi + + # Check if we are actually in a special workspace + if [[ "''${special_workspace}" == *"special"* ]]; then + echo "[LOG] workspace is ''${special_workspace}" + + # Toggle the special workspace OFF + $HYPRCTL dispatch togglespecialworkspace "''${workspace_name}" + + # Switch to the requested normal workspace + $HYPRCTL dispatch workspace "''${chosen_workspace_num}" + else + echo "[ERR] workspace is not special" + exit 1 + fi + + exit 0 + ''; +in { + home.packages = [ + hakase-workspace-switch-force + ]; +} diff --git a/test/force-switch-workspace.sh b/test/force-switch-workspace.sh new file mode 100755 index 0000000..f812509 --- /dev/null +++ b/test/force-switch-workspace.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# flow +# check workspace +# if the workspace is a special workspace, toggle it off +# switch to workspace based on the chosen num + +special_workspace=$(hyprctl monitors -j | jq -r '.[] | select(.focused) | .specialWorkspace.name') +workspace_name=${special_workspace#*:} +chosen_workspace_num=$1 + +if [[ -z ${chosen_workspace_num} ]]; then + echo "Usage: $0 [number]" + exit 1 +fi + +if [[ "${special_workspace}" == *"special"* ]]; then + echo "[LOG] workspace is ${special_workspace}" + hyprctl dispatch togglespecialworkspace "${workspace_name}" + hyprctl dispatch workspace "${chosen_workspace_num}" +else + echo "[ERR] workspace is not special" + exit 1 +fi + +exit 0