diff options
author | Thomas Letan <lthms@soap.coffee> | 2022-09-10 12:20:26 +0200 |
---|---|---|
committer | Thomas Letan <lthms@soap.coffee> | 2022-09-10 12:20:26 +0200 |
commit | 4c1bca65bd65bf8eb06651547032fddaf63166c6 (patch) | |
tree | 924c08c8d4c95a4c0a037ed0b964ee6548b4bce9 | |
parent | Fade unfocused windows (diff) |
Allow to take window’s name into account to assign an icon
Thank you, no thank you, Electron.
-rw-r--r-- | bin/spatial/state.ml | 34 | ||||
-rw-r--r-- | bin/spatial/windows_registry.ml | 12 | ||||
-rw-r--r-- | bin/spatialmsg/main.ml | 22 | ||||
-rw-r--r-- | lib/spatial_ipc/spatial_ipc.ml | 20 | ||||
-rw-r--r-- | lib/spatial_ipc/spatial_ipc.mli | 9 |
5 files changed, 57 insertions, 40 deletions
diff --git a/bin/spatial/state.ml b/bin/spatial/state.ml index 54ad420..0bbc849 100644 --- a/bin/spatial/state.ml +++ b/bin/spatial/state.ml @@ -16,17 +16,6 @@ let empty current_workspace = let set_current_workspace current_workspace state = { state with current_workspace } -let insert_window default_full_view default_maximum_visible workspace window - app_id state = - { - state with - workspaces = - Workspaces_registry.register_window default_full_view - default_maximum_visible workspace window state.workspaces; - windows = - Windows_registry.register window { app_id; workspace } state.windows; - } - let focus_index workspace state index = { state with @@ -105,10 +94,19 @@ let arrange_current_workspace ?force_focus state = let register_window default_full_view default_maximum_visible workspace state (tree : Node.t) = - match (tree.node_type, tree.app_id) with - | Con, Some app_id -> - insert_window default_full_view default_maximum_visible workspace tree.id - app_id state + match tree.node_type with + | Con -> + let id = tree.id in + let app_id = Option.value ~default:"" tree.app_id in + let name = Option.value ~default:"" tree.name in + { + state with + workspaces = + Workspaces_registry.register_window default_full_view + default_maximum_visible workspace id state.workspaces; + windows = + Windows_registry.register id { app_id; workspace; name } state.windows; + } | _ -> state let unregister_window state window = @@ -228,8 +226,7 @@ let client_command_handle : in let windows = Workspaces_registry.summary state.workspaces - |> List.map (fun (k, w) -> - (k, (Windows_registry.find w state.windows).app_id)) + |> List.map (fun (k, w) -> (k, Windows_registry.find w state.windows)) in ((state, false, None), { current; windows }) | Get_windows -> ( @@ -246,8 +243,7 @@ let client_command_handle : focus = Some f; windows = List.map - (fun id -> - (Windows_registry.find id state.windows).app_id) + (fun id -> Windows_registry.find id state.windows) (l @ ribbon.hidden); } | None -> { focus = None; windows = [] }) )) diff --git a/bin/spatial/windows_registry.ml b/bin/spatial/windows_registry.ml index 9c36083..13ae3e1 100644 --- a/bin/spatial/windows_registry.ml +++ b/bin/spatial/windows_registry.ml @@ -1,17 +1,17 @@ +open Spatial_ipc module Map = Map.Make (Int64) -type info = { workspace : string; app_id : string } -type t = info Map.t +type t = window_info Map.t let empty : t = Map.empty -let register : int64 -> info -> t -> t = Map.add +let register : int64 -> window_info -> t -> t = Map.add let unregister = Map.remove let find = Map.find let find_opt = Map.find_opt -let pp_window fmt (id, { app_id; workspace }) = - Format.fprintf fmt "{ id = %Ld; app_id = %s; workspace = %s }" id app_id - workspace +let pp_window fmt (id, { app_id; name; workspace }) = + Format.fprintf fmt "{ id = %Ld; app_id = %s; name = %s; workspace = %s }" id + name app_id workspace let pp fmt windows = let open Format in diff --git a/bin/spatialmsg/main.ml b/bin/spatialmsg/main.ml index 2000365..83c2da9 100644 --- a/bin/spatialmsg/main.ml +++ b/bin/spatialmsg/main.ml @@ -3,16 +3,20 @@ open Spatial_ipc let default_icon = "" let empty_workspace_icon = "◯" -let icon_of = function +(* TODO: This should be part of the config of spatial + Something like 'for window [app_id="firefox"] icon ""'. *) +let icon_of info = + match info.app_id with | "firefox" -> Some "" | "kitty" -> Some "" - | "Slack" -> Some "" + | "Electron" -> + if String.starts_with ~prefix:"Slack" info.name then Some "" else None | "emacs" -> Some "" | _ -> None let workspace_icon workspace windows = List.assq_opt workspace windows |> function - | Some app_id -> Option.value ~default:default_icon (icon_of app_id) + | Some info -> Option.value ~default:default_icon (icon_of info) | None -> empty_workspace_icon let () = @@ -36,20 +40,20 @@ let () = match (cmd, reply.focus) with | Some idx, Some focus when idx < List.length reply.windows -> - let tooltip = List.nth reply.windows idx in + let window = List.nth reply.windows idx in let name = Option.value - ~default:(default_icon ^ " " ^ tooltip) - (icon_of tooltip) + ~default:(default_icon ^ " " ^ window.app_id) + (icon_of window) in let cls = if idx = focus then "focus" else "unfocus" in - Format.(printf "%s\n%s\n%s" name tooltip cls) + Format.(printf "%s\n%s\n%s" name window.app_id cls) | Some _, _ -> () | None, _ -> List.iteri - (fun idx name -> + (fun idx info -> let marker = if reply.focus = Some idx then "*" else "" in - Format.printf "| %s%s%s | " marker name marker) + Format.printf "| %s%s%s | " marker info.app_id marker) reply.windows) | "get_workspaces" -> ( let cmd = Clap.optional_int ~placeholder:"INDEX" () in diff --git a/lib/spatial_ipc/spatial_ipc.ml b/lib/spatial_ipc/spatial_ipc.ml index 8024391..fa33170 100644 --- a/lib/spatial_ipc/spatial_ipc.ml +++ b/lib/spatial_ipc/spatial_ipc.ml @@ -96,23 +96,35 @@ let run_command_reply_encoding = (fun success -> { success }) (obj1 (req "success" bool)) -type get_windows_reply = { focus : int option; windows : string list } +type window_info = { workspace : string; app_id : string; name : string } +type get_windows_reply = { focus : int option; windows : window_info list } + +let window_info_encoding : window_info Data_encoding.t = + let open Data_encoding in + conv + (fun { workspace; app_id; name } -> (workspace, app_id, name)) + (fun (workspace, app_id, name) -> { workspace; app_id; name }) + (obj3 (req "focus" string) (req "app_id" string) (req "name" string)) let get_windows_reply_encoding : get_windows_reply Data_encoding.t = let open Data_encoding in conv (fun { focus; windows } -> (focus, windows)) (fun (focus, windows) -> { focus; windows }) - (obj2 (opt "focus" int31) (req "windows" @@ list string)) + (obj2 (opt "focus" int31) (req "windows" @@ list window_info_encoding)) -type get_workspaces_reply = { current : int; windows : (int * string) list } +type get_workspaces_reply = { + current : int; + windows : (int * window_info) list; +} let get_workspaces_reply_encoding : get_workspaces_reply Data_encoding.t = let open Data_encoding in conv (fun { current; windows } -> (current, windows)) (fun (current, windows) -> { current; windows }) - (obj2 (req "current" int31) (req "windows" @@ list (tup2 int31 string))) + (obj2 (req "current" int31) + (req "windows" @@ list (tup2 int31 window_info_encoding))) type 'a t = | Run_command : command -> run_command_reply t diff --git a/lib/spatial_ipc/spatial_ipc.mli b/lib/spatial_ipc/spatial_ipc.mli index 0f11b6d..6313179 100644 --- a/lib/spatial_ipc/spatial_ipc.mli +++ b/lib/spatial_ipc/spatial_ipc.mli @@ -28,8 +28,13 @@ val command_of_string_exn : string -> command (** @raise [Invalid_argument] *) type run_command_reply = { success : bool } -type get_windows_reply = { focus : int option; windows : string list } -type get_workspaces_reply = { current : int; windows : (int * string) list } +type window_info = { workspace : string; app_id : string; name : string } +type get_windows_reply = { focus : int option; windows : window_info list } + +type get_workspaces_reply = { + current : int; + windows : (int * window_info) list; +} type 'a t = | Run_command : command -> run_command_reply t |