Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement window focusing #196

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AppWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/DesktopIntegration.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 18 additions & 0 deletions src/Launcher.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Dock.Launcher : Gtk.Button {
private string animate_css_class_name = "";
private uint animate_timeout_id = 0;

private Menu window_section;
private Gtk.PopoverMenu popover;

public Launcher (GLib.DesktopAppInfo app_info) {
Expand All @@ -43,6 +44,8 @@ public class Dock.Launcher : Gtk.Button {
windows = new GLib.List<AppWindow> ();
get_style_context ().add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

window_section = new Menu ();

var action_section = new Menu ();
foreach (var action in app_info.list_actions ()) {
action_section.append (
Expand All @@ -58,6 +61,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);
}
Expand Down Expand Up @@ -168,6 +172,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) {
Expand Down
20 changes: 19 additions & 1 deletion src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -123,6 +125,22 @@ 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"];

var focus_action = (SimpleAction) lookup_action (LAUNCHER_FOCUS_TEMPLATE.printf (app_id, window.uid));
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);
Expand Down