From 7b178ab2d766ab9462c83d23f2cd14d923f0376b Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 2 Jan 2024 18:54:21 +0100 Subject: [PATCH 1/6] Implement window focusing --- src/AppWindow.vala | 1 + src/DesktopIntegration.vala | 1 + src/Launcher.vala | 21 +++++++++++++++++++++ src/MainWindow.vala | 15 ++++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/AppWindow.vala b/src/AppWindow.vala index d17570b8..571f2dae 100644 --- a/src/AppWindow.vala +++ b/src/AppWindow.vala @@ -6,6 +6,7 @@ public class Dock.AppWindow : GLib.Object { public uint64 uid { get; construct set; } + public string title { get; set; } public AppWindow (uint64 uid) { Object (uid: uid); diff --git a/src/DesktopIntegration.vala b/src/DesktopIntegration.vala index 2180163d..3f8df2d6 100644 --- a/src/DesktopIntegration.vala +++ b/src/DesktopIntegration.vala @@ -23,4 +23,5 @@ public interface Dock.DesktopIntegration : GLib.Object { public abstract RunningApplication[] get_running_applications () throws GLib.DBusError, GLib.IOError; public abstract Window[] get_windows () throws GLib.DBusError, GLib.IOError; + public abstract void focus_window (uint64 uid) throws GLib.DBusError, GLib.IOError; } diff --git a/src/Launcher.vala b/src/Launcher.vala index b6c6fe79..b287f12f 100644 --- a/src/Launcher.vala +++ b/src/Launcher.vala @@ -21,7 +21,9 @@ public class Dock.Launcher : Gtk.Button { private int drag_offset_y = 0; private string animate_css_class_name = ""; private uint animate_timeout_id = 0; + private SimpleActionGroup window_focus_action_group; + private Menu window_section; private Gtk.PopoverMenu popover; public Launcher (GLib.DesktopAppInfo app_info) { @@ -42,6 +44,10 @@ public class Dock.Launcher : Gtk.Button { construct { windows = new GLib.List (); get_style_context ().add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); + window_focus_action_group = new SimpleActionGroup (); + insert_action_group ("focus", window_focus_action_group); + + window_section = new Menu (); var action_section = new Menu (); foreach (var action in app_info.list_actions ()) { @@ -58,6 +64,7 @@ public class Dock.Launcher : Gtk.Button { ); var model = new Menu (); + model.append_section (null, window_section); if (action_section.get_n_items () > 0) { model.append_section (null, action_section); } @@ -168,6 +175,20 @@ public class Dock.Launcher : Gtk.Button { } else { windows = (owned) new_windows; } + + window_section.remove_all (); + if (windows.length () < 2) { + return; + } + + foreach (var window in windows) { + var menu_item = new MenuItem ( + window.title, + MainWindow.ACTION_PREFIX + MainWindow.LAUNCHER_FOCUS_TEMPLATE.printf (app_info.get_id (), window.uid) + ); + menu_item.set_icon (app_info.get_icon ()); + window_section.append_item (menu_item); + } } public AppWindow? find_window (uint64 window_uid) { diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 1c9fcd17..781b8f1c 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -5,7 +5,9 @@ public class Dock.MainWindow : Gtk.ApplicationWindow { // First %s is the app id second %s the action name - public const string LAUNCHER_ACTION_TEMPLATE = "%s.%s"; + public const string LAUNCHER_FOCUS_TEMPLATE = "%s.focus.%" + uint64.FORMAT; + // First %s is the app id second %s the action name + public const string LAUNCHER_ACTION_TEMPLATE = "%s.action.%s"; // %s is the app id public const string LAUNCHER_PINNED_ACTION_TEMPLATE = "%s-pinned"; public const string ACTION_GROUP_PREFIX = "win"; @@ -123,7 +125,18 @@ public class Dock.MainWindow : Gtk.ApplicationWindow { AppWindow? app_window = launcher.find_window (window.uid); if (app_window == null) { app_window = new AppWindow (window.uid); + + var action = new SimpleAction (LAUNCHER_FOCUS_TEMPLATE.printf (app_id, window.uid), null); + add_action (action); + action.activate.connect (() => { + try { + desktop_integration.focus_window (window.uid); + } catch (Error e) { + warning ("Failed to focus window: %t", window.uid); + } + }); } + app_window.title = (string) window.properties["title"]; unowned var window_list = launcher_window_list.get (launcher); if (window_list == null) { From 909dbd38fc7c0eba6d256c24902a816c3b463c90 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 2 Jan 2024 19:01:58 +0100 Subject: [PATCH 2/6] Cleanup --- src/Launcher.vala | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Launcher.vala b/src/Launcher.vala index b287f12f..570dc6a9 100644 --- a/src/Launcher.vala +++ b/src/Launcher.vala @@ -21,7 +21,6 @@ public class Dock.Launcher : Gtk.Button { private int drag_offset_y = 0; private string animate_css_class_name = ""; private uint animate_timeout_id = 0; - private SimpleActionGroup window_focus_action_group; private Menu window_section; private Gtk.PopoverMenu popover; From dc0dfae5d830136ddc7b8fdc92f3d0ac15877ffc Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 2 Jan 2024 19:02:26 +0100 Subject: [PATCH 3/6] More cleanup --- src/Launcher.vala | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Launcher.vala b/src/Launcher.vala index 570dc6a9..817769de 100644 --- a/src/Launcher.vala +++ b/src/Launcher.vala @@ -43,8 +43,6 @@ public class Dock.Launcher : Gtk.Button { construct { windows = new GLib.List (); get_style_context ().add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); - window_focus_action_group = new SimpleActionGroup (); - insert_action_group ("focus", window_focus_action_group); window_section = new Menu (); From d83a89a62154b0b523449e245dc2a514c9ef3adf Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 2 Jan 2024 20:04:29 +0100 Subject: [PATCH 4/6] Disable focus action if window already has focus --- src/MainWindow.vala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 781b8f1c..fd8583e7 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -137,6 +137,9 @@ public class Dock.MainWindow : Gtk.ApplicationWindow { }); } app_window.title = (string) window.properties["title"]; + + var focus_action = (SimpleAction) lookup_action (LAUNCHER_FOCUS_TEMPLATE.printf (app_id, window.uid)); + focus_action.set_enabled (!((bool) window.properties["has-focus"])); unowned var window_list = launcher_window_list.get (launcher); if (window_list == null) { From ecdefc1fa786e5f6aee10e4316c0808de32e64f6 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 2 Jan 2024 23:49:16 +0100 Subject: [PATCH 5/6] Fix lint --- src/MainWindow.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index fd8583e7..161c0ec7 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -137,7 +137,7 @@ public class Dock.MainWindow : Gtk.ApplicationWindow { }); } app_window.title = (string) window.properties["title"]; - + var focus_action = (SimpleAction) lookup_action (LAUNCHER_FOCUS_TEMPLATE.printf (app_id, window.uid)); focus_action.set_enabled (!((bool) window.properties["has-focus"])); From c2262c6fa55a6f068cdb6c47118590a713b91dbd Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 2 Jan 2024 23:50:38 +0100 Subject: [PATCH 6/6] Add checks --- src/MainWindow.vala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 161c0ec7..aeacc687 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -139,7 +139,9 @@ public class Dock.MainWindow : Gtk.ApplicationWindow { app_window.title = (string) window.properties["title"]; var focus_action = (SimpleAction) lookup_action (LAUNCHER_FOCUS_TEMPLATE.printf (app_id, window.uid)); - focus_action.set_enabled (!((bool) window.properties["has-focus"])); + if (focus_action != null && "has-focus" in window.properties) { + focus_action.set_enabled (!((bool) window.properties["has-focus"])); + } unowned var window_list = launcher_window_list.get (launcher); if (window_list == null) {