Skip to content

Commit 4e0852b

Browse files
committed
PositionedWindow: Introduce an abstract method for position calculation
Instead of an enum based on which the position is calculated, introduce an abstract method that should be overridden by subclasses for position calculation. This introduces more flexibility in the future while reducing code complexity.
1 parent 50a2adc commit 4e0852b

File tree

4 files changed

+42
-60
lines changed

4 files changed

+42
-60
lines changed

src/ShellClients/ExtendedBehaviorWindow.vala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
public class Gala.ExtendedBehaviorWindow : ShellWindow {
99
public ExtendedBehaviorWindow (Meta.Window window) {
1010
var target = new PropertyTarget (CUSTOM, window.get_compositor_private (), "opacity", typeof (uint), 255u, 0u);
11-
Object (window: window, position: Position.CENTER, hide_target: target);
11+
Object (window: window, hide_target: target);
12+
}
13+
14+
protected override void get_window_position (Mtk.Rectangle window_rect, out int x, out int y) {
15+
var monitor_rect = window.display.get_monitor_geometry (window.get_monitor ());
16+
17+
x = monitor_rect.x + (monitor_rect.width - window_rect.width) / 2;
18+
y = monitor_rect.y + (monitor_rect.height - window_rect.height) / 2;
1219
}
1320
}

src/ShellClients/PanelWindow.vala

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
3939
private int height = -1;
4040

4141
public PanelWindow (WindowManager wm, Meta.Window window, Pantheon.Desktop.Anchor anchor) {
42-
Object (wm: wm, anchor: anchor, window: window, position: Position.from_anchor (anchor));
42+
Object (wm: wm, window: window, anchor: anchor);
4343
}
4444

4545
construct {
@@ -49,7 +49,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
4949
}
5050
});
5151

52-
notify["anchor"].connect (() => position = Position.from_anchor (anchor));
52+
notify["anchor"].connect (position_window);
5353

5454
unowned var workspace_manager = window.display.get_workspace_manager ();
5555
workspace_manager.workspace_added.connect (update_strut);
@@ -75,7 +75,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
7575
workspace_hide_tracker.window_state_changed_progress_updated.connect (workspace_gesture_controller.goto);
7676

7777
window.size_changed.connect (update_target);
78-
notify["position"].connect (update_target);
78+
notify["anchor"].connect (update_target);
7979
update_target ();
8080

8181
add_gesture_controller (user_gesture_controller);
@@ -86,7 +86,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
8686
window_actor.notify["width"].connect (update_clip);
8787
window_actor.notify["height"].connect (update_clip);
8888
window_actor.notify["translation-y"].connect (update_clip);
89-
notify["position"].connect (update_clip);
89+
notify["anchor"].connect (update_clip);
9090
}
9191

9292
public Mtk.Rectangle get_custom_window_rect () {
@@ -99,7 +99,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
9999
if (height > 0) {
100100
window_rect.height = height;
101101

102-
if (position == BOTTOM) {
102+
if (anchor == BOTTOM) {
103103
var geom = window.display.get_monitor_geometry (window.get_monitor ());
104104
window_rect.y = geom.y + geom.height - height;
105105
}
@@ -248,6 +248,27 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
248248
}
249249
}
250250

251+
protected override void get_window_position (Mtk.Rectangle window_rect, out int x, out int y) {
252+
var monitor_rect = window.display.get_monitor_geometry (window.display.get_primary_monitor ());
253+
switch (anchor) {
254+
case TOP:
255+
x = monitor_rect.x + (monitor_rect.width - window_rect.width) / 2;
256+
y = monitor_rect.y;
257+
break;
258+
259+
case BOTTOM:
260+
x = monitor_rect.x + (monitor_rect.width - window_rect.width) / 2;
261+
y = monitor_rect.y + monitor_rect.height - window_rect.height;
262+
break;
263+
264+
default:
265+
warning ("Unsupported anchor %s for PanelWindow", anchor.to_string ());
266+
x = 0;
267+
y = 0;
268+
break;
269+
}
270+
}
271+
251272
private void update_target () {
252273
var to_value = anchor == TOP ? -get_custom_window_rect ().height : get_custom_window_rect ().height;
253274
hide_target = new PropertyTarget (CUSTOM, actor, "translation-y", typeof (float), 0f, (float) to_value);

src/ShellClients/PositionedWindow.vala

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,10 @@
66
*/
77

88
public class Gala.PositionedWindow : Object {
9-
public enum Position {
10-
TOP,
11-
BOTTOM,
12-
CENTER;
13-
14-
public static Position from_anchor (Pantheon.Desktop.Anchor anchor) {
15-
if (anchor > 1) {
16-
warning ("Position %s not supported yet", anchor.to_string ());
17-
return CENTER;
18-
}
19-
20-
return (Position) anchor;
21-
}
22-
}
23-
249
public Meta.Window window { get; construct; }
25-
/**
26-
* This may only be set after the window was shown.
27-
* The initial position should only be given in the constructor.
28-
*/
29-
public Position position { get; construct set; }
30-
public Variant? position_data { get; construct set; }
3110

3211
private ulong position_changed_id;
3312

34-
public PositionedWindow (Meta.Window window, Position position, Variant? position_data = null) {
35-
Object (window: window, position: position, position_data: position_data);
36-
}
37-
3813
construct {
3914
window.stick ();
4015

@@ -45,38 +20,21 @@ public class Gala.PositionedWindow : Object {
4520
unowned var monitor_manager = window.display.get_context ().get_backend ().get_monitor_manager ();
4621
monitor_manager.monitors_changed.connect (position_window);
4722
monitor_manager.monitors_changed_internal.connect (position_window);
48-
49-
notify["position"].connect (position_window);
50-
notify["position-data"].connect (position_window);
5123
}
5224

53-
private void position_window () {
54-
int x = 0, y = 0;
25+
protected void position_window () {
5526
var window_rect = window.get_frame_rect ();
56-
unowned var display = window.display;
57-
58-
switch (position) {
59-
case CENTER:
60-
var monitor_geom = display.get_monitor_geometry (display.get_primary_monitor ());
61-
x = monitor_geom.x + (monitor_geom.width - window_rect.width) / 2;
62-
y = monitor_geom.y + (monitor_geom.height - window_rect.height) / 2;
63-
break;
64-
65-
case TOP:
66-
var monitor_geom = display.get_monitor_geometry (display.get_primary_monitor ());
67-
x = monitor_geom.x + (monitor_geom.width - window_rect.width) / 2;
68-
y = monitor_geom.y;
69-
break;
7027

71-
case BOTTOM:
72-
var monitor_geom = display.get_monitor_geometry (display.get_primary_monitor ());
73-
x = monitor_geom.x + (monitor_geom.width - window_rect.width) / 2;
74-
y = monitor_geom.y + monitor_geom.height - window_rect.height;
75-
break;
76-
}
28+
int x = 0, y = 0;
29+
get_window_position (window_rect, out x, out y);
7730

7831
SignalHandler.block (window, position_changed_id);
7932
window.move_frame (false, x, y);
8033
SignalHandler.unblock (window, position_changed_id);
8134
}
35+
36+
protected virtual void get_window_position (Mtk.Rectangle window_rect, out int x, out int y) {
37+
x = 0;
38+
y = 0;
39+
}
8240
}

src/ShellClients/ShellWindow.vala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public class Gala.ShellWindow : PositionedWindow, GestureTarget {
1717
private double multitasking_view_progress = 0;
1818
private int animations_ongoing = 0;
1919

20-
public ShellWindow (Meta.Window window, Position position, Variant? position_data = null) {
21-
base (window, position, position_data);
22-
}
23-
2420
public virtual void propagate (UpdateType update_type, GestureAction action, double progress) {
2521
switch (update_type) {
2622
case START:

0 commit comments

Comments
 (0)