Skip to content

Commit d850444

Browse files
Introduce a WindowPositioner (#2087)
Co-authored-by: Leo <[email protected]>
1 parent 1e15604 commit d850444

File tree

4 files changed

+80
-74
lines changed

4 files changed

+80
-74
lines changed

src/ShellClients/CenteredWindow.vala

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/ShellClients/ShellClientsManager.vala

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class Gala.ShellClientsManager : Object {
2525
private NotificationsClient notifications_client;
2626
private ManagedClient[] protocol_clients = {};
2727

28-
private GLib.HashTable<Meta.Window, PanelWindow> windows = new GLib.HashTable<Meta.Window, PanelWindow> (null, null);
29-
private GLib.HashTable<Meta.Window, CenteredWindow> centered_windows = new GLib.HashTable<Meta.Window, CenteredWindow> (null, null);
28+
private GLib.HashTable<Meta.Window, PanelWindow> panel_windows = new GLib.HashTable<Meta.Window, PanelWindow> (null, null);
29+
private GLib.HashTable<Meta.Window, WindowPositioner> positioned_windows = new GLib.HashTable<Meta.Window, WindowPositioner> (null, null);
3030

3131
private ShellClientsManager (WindowManager wm) {
3232
Object (wm: wm);
@@ -144,18 +144,18 @@ public class Gala.ShellClientsManager : Object {
144144
}
145145

146146
public void set_anchor (Meta.Window window, Meta.Side side) {
147-
if (window in windows) {
148-
windows[window].update_anchor (side);
147+
if (window in panel_windows) {
148+
panel_windows[window].update_anchor (side);
149149
return;
150150
}
151151

152152
make_dock (window);
153153
// TODO: Return if requested by window that's not a trusted client?
154154

155-
windows[window] = new PanelWindow (wm, window, side);
155+
panel_windows[window] = new PanelWindow (wm, window, side);
156156

157157
// connect_after so we make sure the PanelWindow can destroy its barriers and struts
158-
window.unmanaging.connect_after (() => windows.remove (window));
158+
window.unmanaging.connect_after ((_window) => panel_windows.remove (_window));
159159
}
160160

161161
/**
@@ -166,37 +166,38 @@ public class Gala.ShellClientsManager : Object {
166166
* TODO: Maybe use for strut only?
167167
*/
168168
public void set_size (Meta.Window window, int width, int height) {
169-
if (!(window in windows)) {
169+
if (!(window in panel_windows)) {
170170
warning ("Set anchor for window before size.");
171171
return;
172172
}
173173

174-
windows[window].set_size (width, height);
174+
panel_windows[window].set_size (width, height);
175175
}
176176

177177
public void set_hide_mode (Meta.Window window, Pantheon.Desktop.HideMode hide_mode) {
178-
if (!(window in windows)) {
178+
if (!(window in panel_windows)) {
179179
warning ("Set anchor for window before hide mode.");
180180
return;
181181
}
182182

183-
windows[window].set_hide_mode (hide_mode);
183+
panel_windows[window].set_hide_mode (hide_mode);
184184
}
185185

186-
public void make_centered (Meta.Window window) {
187-
if (window in centered_windows) {
188-
return;
189-
}
186+
public void make_centered (Meta.Window window) requires (!is_itself_positioned (window)) {
187+
positioned_windows[window] = new WindowPositioner (wm, window, CENTER);
190188

191-
centered_windows[window] = new CenteredWindow (wm, window);
189+
// connect_after so we make sure that any queued move is unqueued
190+
window.unmanaging.connect_after ((_window) => positioned_windows.remove (_window));
191+
}
192192

193-
window.unmanaging.connect_after (() => centered_windows.remove (window));
193+
private bool is_itself_positioned (Meta.Window window) {
194+
return (window in positioned_windows) || (window in panel_windows);
194195
}
195196

196197
public bool is_positioned_window (Meta.Window window) {
197-
bool positioned = (window in centered_windows) || (window in windows);
198+
bool positioned = is_itself_positioned (window);
198199
window.foreach_ancestor ((ancestor) => {
199-
if (ancestor in centered_windows || ancestor in windows) {
200+
if (is_itself_positioned (ancestor)) {
200201
positioned = true;
201202
}
202203

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2024 elementary, Inc. (https://elementary.io)
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*
5+
* Authored by: Leonhard Kargl <[email protected]>
6+
*/
7+
8+
public class Gala.WindowPositioner : Object {
9+
public enum Position {
10+
CENTER
11+
}
12+
13+
public Meta.Window window { get; construct; }
14+
public WindowManager wm { get; construct; }
15+
public Position position { get; private set; }
16+
public Variant? position_data { get; private set; }
17+
18+
public WindowPositioner (WindowManager wm, Meta.Window window, Position position, Variant? position_data = null) {
19+
Object (wm: wm, window: window, position: position, position_data: position_data);
20+
}
21+
22+
construct {
23+
window.stick ();
24+
25+
window.size_changed.connect (position_window);
26+
window.position_changed.connect (position_window);
27+
window.shown.connect (position_window);
28+
29+
unowned var monitor_manager = wm.get_display ().get_context ().get_backend ().get_monitor_manager ();
30+
monitor_manager.monitors_changed.connect (position_window);
31+
monitor_manager.monitors_changed_internal.connect (position_window);
32+
}
33+
34+
/**
35+
* This may only be called after the window was shown.
36+
*/
37+
public void update_position (Position new_position, Variant? new_position_data = null) {
38+
position = new_position;
39+
position_data = new_position_data;
40+
41+
position_window ();
42+
}
43+
44+
private void position_window () {
45+
int x = 0, y = 0;
46+
47+
switch (position) {
48+
case CENTER:
49+
unowned var display = wm.get_display ();
50+
var monitor_geom = display.get_monitor_geometry (display.get_primary_monitor ());
51+
var window_rect = window.get_frame_rect ();
52+
53+
x = monitor_geom.x + (monitor_geom.width - window_rect.width) / 2;
54+
y = monitor_geom.y + (monitor_geom.height - window_rect.height) / 2;
55+
break;
56+
}
57+
58+
window.move_frame (false, x, y);
59+
}
60+
}

src/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ gala_bin_sources = files(
4141
'HotCorners/Barrier.vala',
4242
'HotCorners/HotCorner.vala',
4343
'HotCorners/HotCornerManager.vala',
44-
'ShellClients/CenteredWindow.vala',
4544
'ShellClients/HideTracker.vala',
4645
'ShellClients/ManagedClient.vala',
4746
'ShellClients/NotificationsClient.vala',
4847
'ShellClients/PanelClone.vala',
4948
'ShellClients/PanelWindow.vala',
5049
'ShellClients/ShellClientsManager.vala',
50+
'ShellClients/WindowPositioner.vala',
5151
'Widgets/DwellClickTimer.vala',
5252
'Widgets/IconGroup.vala',
5353
'Widgets/IconGroupContainer.vala',

0 commit comments

Comments
 (0)