aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <lthms@soap.coffee>2022-08-31 21:05:57 +0200
committerThomas Letan <lthms@soap.coffee>2022-08-31 21:05:57 +0200
commit5a16520a9eab7fa0238a71db0ff64975fe008cdf (patch)
tree08460f5006a1ca431fd90439a2eccea8b521746f
parentAdd the 'Get_workspaces' command (diff)
Implement a command that allows to move to a specific workspace
-rw-r--r--README.md7
-rw-r--r--bin/spatial/state.ml24
-rw-r--r--lib/spatial_ipc/spatial_ipc.ml4
-rw-r--r--lib/spatial_ipc/spatial_ipc.mli1
4 files changed, 36 insertions, 0 deletions
diff --git a/README.md b/README.md
index f09c9a5..25c1a91 100644
--- a/README.md
+++ b/README.md
@@ -34,6 +34,13 @@ bindsym $mod+Shift+t exec $spatialmsg "move prev"
# Move the focused window on the right, shift the loop if necessary.
bindsym $mod+Shift+n exec $spatialmsg "move next"
+# Jump to the previous workspace (that is, N-1 for workspace N, but
+# iff N > 0).
+bindsym $mod+r exec $spatialmsg "workspace prev"
+
+# Jump to the next workspace (that is, N+1 for workspace N).
+bindsym $mod+s exec $spatialmsg "workspace next"
+
# Toggle between a mode where only one window is visible (maximized
# mode), or a fixed numbers (split mode). spatial-sway will remember
# how may windows you want visible when not in full view mode.
diff --git a/bin/spatial/state.ml b/bin/spatial/state.ml
index 557f327..54ad420 100644
--- a/bin/spatial/state.ml
+++ b/bin/spatial/state.ml
@@ -140,6 +140,24 @@ let init default_full_view default_maximum_visible =
| None -> state)
(empty cw.name) workspaces
+(* TODO: Make it configurable *)
+let max_workspace = 6
+
+let send_command_workspace : 'kind Spatial_ipc.target -> state -> unit =
+ fun dir state ->
+ (match (dir, int_of_string_opt state.current_workspace) with
+ | Next, Some x when x < max_workspace -> Some (x + 1)
+ | Prev, Some x when 1 < x -> Some (x - 1)
+ | (Next | Prev), Some _ -> None
+ | (Next | Prev), None -> Some 0
+ | Index t, _ -> Some t)
+ |> function
+ | Some target ->
+ ignore
+ @@ Sway_ipc.send_command
+ (Run_command [ Workspace (string_of_int target) ])
+ | None -> ()
+
let client_command_handle :
type a. state -> a Spatial_ipc.t -> (state * bool * int64 option) * a =
fun state cmd ->
@@ -174,6 +192,12 @@ let client_command_handle :
None )
| Focus (Index x) ->
(focus_index state.current_workspace state x, true, None)
+ | Workspace dir ->
+ (* Don’t update the state, but ask Sway to change the
+ current workspace instead. This will trigger an event
+ that we will eventually received. *)
+ send_command_workspace dir state;
+ (state, false, None)
| Move Prev ->
(move_window_left state.current_workspace state, true, None)
| Move Next ->
diff --git a/lib/spatial_ipc/spatial_ipc.ml b/lib/spatial_ipc/spatial_ipc.ml
index d523680..8024391 100644
--- a/lib/spatial_ipc/spatial_ipc.ml
+++ b/lib/spatial_ipc/spatial_ipc.ml
@@ -53,6 +53,7 @@ let operation_to_string = function Incr -> "increment" | Decr -> "decrement"
type command =
| Focus of focus target
+ | Workspace of focus target
| Move of move target
| Maximize of switch
| Split of operation
@@ -64,6 +65,8 @@ let command_of_string str =
|> List.filter (function "" -> false | _ -> true)
|> function
| [ "focus"; target ] -> (fun x -> Focus x) <$> target_of_string_opt target
+ | [ "workspace"; target ] ->
+ (fun x -> Workspace x) <$> target_of_string_opt target
| [ "move"; target ] ->
Option.bind (target_of_string_opt target) @@ fun target ->
(fun x -> Move x) <$> restrict_move_target target
@@ -79,6 +82,7 @@ let command_of_string_exn str =
let command_to_string = function
| Focus dir -> Format.sprintf "focus %s" (target_to_string dir)
+ | Workspace dir -> Format.sprintf "workspace %s" (target_to_string dir)
| Move dir -> Format.sprintf "move %s" (target_to_string dir)
| Maximize switch -> Format.sprintf "maximize %s" (switch_to_string switch)
| Split op -> Format.sprintf "split %s" (operation_to_string op)
diff --git a/lib/spatial_ipc/spatial_ipc.mli b/lib/spatial_ipc/spatial_ipc.mli
index c2af336..0f11b6d 100644
--- a/lib/spatial_ipc/spatial_ipc.mli
+++ b/lib/spatial_ipc/spatial_ipc.mli
@@ -17,6 +17,7 @@ type operation = Incr | Decr
type command =
| Focus of focus target
+ | Workspace of focus target
| Move of move target
| Maximize of switch
| Split of operation