Skip to content

Commit 1cf6b6c

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 108cb1e commit 1cf6b6c

File tree

4 files changed

+44
-60
lines changed

4 files changed

+44
-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 (
15+
Mtk.Rectangle window_rect, Mtk.Rectangle monitor_rect, out int x, out int y
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 & 6 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,8 +49,6 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
4949
}
5050
});
5151

52-
notify["anchor"].connect (() => position = Position.from_anchor (anchor));
53-
5452
unowned var workspace_manager = window.display.get_workspace_manager ();
5553
workspace_manager.workspace_added.connect (update_strut);
5654
workspace_manager.workspace_removed.connect (update_strut);
@@ -77,15 +75,15 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
7775
workspace_hide_tracker.window_state_changed_progress_updated.connect (workspace_gesture_controller.goto);
7876

7977
window.size_changed.connect (update_target);
80-
notify["position"].connect (update_target);
78+
notify["anchor"].connect (update_target);
8179
update_target ();
8280

8381
var window_actor = (Meta.WindowActor) window.get_compositor_private ();
8482

8583
window_actor.notify["width"].connect (update_clip);
8684
window_actor.notify["height"].connect (update_clip);
8785
window_actor.notify["translation-y"].connect (update_clip);
88-
notify["position"].connect (update_clip);
86+
notify["anchor"].connect (update_clip);
8987
}
9088

9189
public Mtk.Rectangle get_custom_window_rect () {
@@ -98,7 +96,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
9896
if (height > 0) {
9997
window_rect.height = height;
10098

101-
if (position == BOTTOM) {
99+
if (anchor == BOTTOM) {
102100
var geom = window.display.get_monitor_geometry (window.get_monitor ());
103101
window_rect.y = geom.y + geom.height - height;
104102
}
@@ -247,6 +245,28 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
247245
}
248246
}
249247

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

src/ShellClients/PositionedWindow.vala

Lines changed: 10 additions & 49 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,24 @@ 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

5325
private void position_window () {
54-
int x = 0, y = 0;
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;
27+
var monitor_rect = window.display.get_monitor_geometry (window.display.get_primary_monitor ());
7028

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-
}
29+
int x = 0, y = 0;
30+
get_window_position (window_rect, monitor_rect, out x, out y);
7731

7832
SignalHandler.block (window, position_changed_id);
7933
window.move_frame (false, x, y);
8034
SignalHandler.unblock (window, position_changed_id);
8135
}
36+
37+
protected virtual void get_window_position (
38+
Mtk.Rectangle window_rect, Mtk.Rectangle monitor_rect, out int x, out int y
39+
) {
40+
x = 0;
41+
y = 0;
42+
}
8243
}

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)