|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0 |
| 3 | + * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) |
| 4 | + */ |
| 5 | + |
| 6 | +public class Dock.WindowDragManager : Object { |
| 7 | + [DBus (name = "io.elementary.desktop.wm.WindowDragProvider")] |
| 8 | + private interface WindowDragProvider : Object { |
| 9 | + public signal void enter (uint64 window_id); |
| 10 | + public signal void motion (int x, int y); |
| 11 | + public signal void leave (); |
| 12 | + public signal void dropped (); |
| 13 | + } |
| 14 | + |
| 15 | + public Gtk.Window dock_window { get; construct; } |
| 16 | + |
| 17 | + private Window? current_window = null; |
| 18 | + private WorkspaceIconGroup? current_icon_group = null; |
| 19 | + |
| 20 | + private WindowDragProvider provider; |
| 21 | + |
| 22 | + public WindowDragManager (Gtk.Window dock_window) { |
| 23 | + Object (dock_window: dock_window); |
| 24 | + } |
| 25 | + |
| 26 | + construct { |
| 27 | + connect_to_dbus.begin (); |
| 28 | + } |
| 29 | + |
| 30 | + private async void connect_to_dbus () { |
| 31 | + try { |
| 32 | + provider = yield Bus.get_proxy (SESSION, "io.elementary.gala", "/io/elementary/gala"); |
| 33 | + provider.enter.connect (on_enter); |
| 34 | + provider.motion.connect (on_motion); |
| 35 | + provider.leave.connect (on_leave); |
| 36 | + provider.dropped.connect (on_dropped); |
| 37 | + } catch (Error e) { |
| 38 | + warning ("Failed to connect to WindowDragProvider DBus interface: %s", e.message); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private void on_enter (uint64 window_id) { |
| 43 | + current_window = WindowSystem.get_default ().find_window (window_id); |
| 44 | + } |
| 45 | + |
| 46 | + private void on_motion (int x, int y) { |
| 47 | + if (current_window == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + var icon_group = find_icon_group (x, y); |
| 52 | + |
| 53 | + if (icon_group == current_icon_group) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (current_icon_group != null) { |
| 58 | + current_icon_group.window_left (); |
| 59 | + } |
| 60 | + |
| 61 | + current_icon_group = icon_group; |
| 62 | + |
| 63 | + if (current_icon_group != null) { |
| 64 | + current_icon_group.window_entered (current_window); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private WorkspaceIconGroup? find_icon_group (int x, int y) { |
| 69 | + double root_x, root_y; |
| 70 | + dock_window.get_surface_transform (out root_x, out root_y); |
| 71 | + |
| 72 | + var widget = dock_window.pick (x - root_x, y - root_y, DEFAULT); |
| 73 | + |
| 74 | + while (!(widget is WorkspaceIconGroup) && widget != null) { |
| 75 | + widget = widget.get_parent (); |
| 76 | + } |
| 77 | + |
| 78 | + if (widget is WorkspaceIconGroup) { |
| 79 | + return (WorkspaceIconGroup) widget; |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private void on_leave () { |
| 86 | + if (current_icon_group != null) { |
| 87 | + current_icon_group.window_left (); |
| 88 | + current_icon_group = null; |
| 89 | + } |
| 90 | + |
| 91 | + current_window = null; |
| 92 | + } |
| 93 | + |
| 94 | + private void on_dropped () { |
| 95 | + if (current_icon_group != null && current_window != null && |
| 96 | + current_icon_group.workspace.index != current_window.workspace_index |
| 97 | + ) { |
| 98 | + WindowSystem.get_default ().move_window_to_workspace.begin ( |
| 99 | + current_window.uid, current_icon_group.workspace.index |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments