gardesk/tarmac / 08cbef1

Browse files

show focused workspace window in tray

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
08cbef1b4050cf9180fc8f0a8fa47540c85d219d
Parents
e4de1c8
Tree
bc0b0b1

2 changed files

StatusFile+-
M tarmac/src/main.rs 62 0
M tarmac/src/ui/tray.rs 17 6
tarmac/src/main.rsmodified
@@ -886,6 +886,25 @@ fn cleanup_socket() {
886886
     let _ = std::fs::remove_file(&path);
887887
 }
888888
 
889
+fn workspace_last_active_title(
890
+    workspace: &tarmac::core::workspace::Workspace,
891
+    registry: &tarmac::core::window::WindowRegistry,
892
+) -> Option<String> {
893
+    workspace
894
+        .focused
895
+        .or_else(|| workspace.focus_history.last().copied())
896
+        .and_then(|window_id| registry.get(window_id))
897
+        .and_then(|window| {
898
+            let title = window.title.trim();
899
+            if !title.is_empty() {
900
+                Some(title.to_string())
901
+            } else {
902
+                let app_name = window.app_name.trim();
903
+                (!app_name.is_empty()).then(|| app_name.to_string())
904
+            }
905
+        })
906
+}
907
+
889908
 fn update_tray(tray: &tarmac::ui::tray::TrayWidget) {
890909
     WM_STATE.with(|s| {
891910
         if let Some(state) = s.borrow().as_ref() {
@@ -902,6 +921,7 @@ fn update_tray(tray: &tarmac::ui::tray::TrayWidget) {
902921
                         id: ws.id.to_string(),
903922
                         active: ws.visible,
904923
                         windows: ws.all_window_ids().len(),
924
+                        last_active_title: workspace_last_active_title(ws, &state.registry),
905925
                         switch_target,
906926
                     })
907927
                 })
@@ -1728,6 +1748,7 @@ fn run_app() {
17281748
 mod tests {
17291749
     use super::*;
17301750
     use tarmac::config::document::ConfigSource;
1751
+    use tarmac::core::window::{WindowRegistry, WindowState};
17311752
 
17321753
     fn sample_keybind(key: Key) -> LuaKeybind {
17331754
         LuaKeybind {
@@ -1797,6 +1818,24 @@ mod tests {
17971818
         }
17981819
     }
17991820
 
1821
+    fn sample_window(id: u32, app: &str, title: &str) -> WindowState {
1822
+        WindowState {
1823
+            id,
1824
+            app_pid: 100,
1825
+            app_name: app.to_string(),
1826
+            app_bundle_id: format!("com.test.{app}"),
1827
+            title: title.to_string(),
1828
+            role: "AXWindow".to_string(),
1829
+            subrole: "AXStandardWindow".to_string(),
1830
+            x: 0.0,
1831
+            y: 0.0,
1832
+            width: 800.0,
1833
+            height: 600.0,
1834
+            floating: false,
1835
+            minimized: false,
1836
+        }
1837
+    }
1838
+
18001839
     #[test]
18011840
     fn add_keybind_uses_unique_shortcut() {
18021841
         let mut keybinds = vec![sample_keybind(Key::Period)];
@@ -1969,4 +2008,27 @@ mod tests {
19692008
         assert_eq!(specials[0].name, "term");
19702009
     }
19712010
 
2011
+    #[test]
2012
+    fn workspace_last_active_title_prefers_window_title_then_app_name() {
2013
+        let mut registry = WindowRegistry::new();
2014
+        registry.add(sample_window(1, "Firefox", "New Tab"));
2015
+        registry.add(sample_window(2, "Firefox", ""));
2016
+
2017
+        let mut workspace = tarmac::core::workspace::Workspace::new(WorkspaceId::Numbered(1));
2018
+        workspace.focused = Some(1);
2019
+        assert_eq!(
2020
+            workspace_last_active_title(&workspace, &registry).as_deref(),
2021
+            Some("New Tab")
2022
+        );
2023
+
2024
+        workspace.focused = Some(2);
2025
+        assert_eq!(
2026
+            workspace_last_active_title(&workspace, &registry).as_deref(),
2027
+            Some("Firefox")
2028
+        );
2029
+
2030
+        workspace.focused = Some(99);
2031
+        workspace.focus_history.clear();
2032
+        assert_eq!(workspace_last_active_title(&workspace, &registry), None);
2033
+    }
19722034
 }
tarmac/src/ui/tray.rsmodified
@@ -29,6 +29,7 @@ pub struct TrayWorkspace {
2929
     pub id: String,
3030
     pub active: bool,
3131
     pub windows: usize,
32
+    pub last_active_title: Option<String>,
3233
     pub switch_target: Option<WorkspaceTarget>,
3334
 }
3435
 
@@ -181,12 +182,22 @@ fn build_menu(
181182
             continue;
182183
         }
183184
         let label = if workspace.windows > 0 {
184
-            format!(
185
-                "{}  ({} window{})",
186
-                workspace.id,
187
-                workspace.windows,
188
-                if workspace.windows == 1 { "" } else { "s" }
189
-            )
185
+            if let Some(title) = workspace.last_active_title.as_deref() {
186
+                format!(
187
+                    "{}  ({} window{}, {})",
188
+                    workspace.id,
189
+                    workspace.windows,
190
+                    if workspace.windows == 1 { "" } else { "s" },
191
+                    title
192
+                )
193
+            } else {
194
+                format!(
195
+                    "{}  ({} window{})",
196
+                    workspace.id,
197
+                    workspace.windows,
198
+                    if workspace.windows == 1 { "" } else { "s" }
199
+                )
200
+            }
190201
         } else {
191202
             workspace.id.clone()
192203
         };