Skip to content

Commit e2a9a74

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 66efb9f commit e2a9a74

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
@@ -36,7 +36,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
3636
private int height = -1;
3737

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

4242
construct {
@@ -46,8 +46,6 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
4646
}
4747
});
4848

49-
notify["anchor"].connect (() => position = Position.from_anchor (anchor));
50-
5149
unowned var workspace_manager = window.display.get_workspace_manager ();
5250
workspace_manager.workspace_added.connect (update_strut);
5351
workspace_manager.workspace_removed.connect (update_strut);
@@ -65,15 +63,15 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
6563
hide_tracker.show.connect (show);
6664

6765
window.size_changed.connect (update_target);
68-
notify["position"].connect (update_target);
66+
notify["anchor"].connect (update_target);
6967
update_target ();
7068

7169
var window_actor = (Meta.WindowActor) window.get_compositor_private ();
7270

7371
window_actor.notify["width"].connect (update_clip);
7472
window_actor.notify["height"].connect (update_clip);
7573
window_actor.notify["translation-y"].connect (update_clip);
76-
notify["position"].connect (update_clip);
74+
notify["anchor"].connect (update_clip);
7775
}
7876

7977
public Mtk.Rectangle get_custom_window_rect () {
@@ -86,7 +84,7 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
8684
if (height > 0) {
8785
window_rect.height = height;
8886

89-
if (position == BOTTOM) {
87+
if (anchor == BOTTOM) {
9088
var geom = window.display.get_monitor_geometry (window.get_monitor ());
9189
window_rect.y = geom.y + geom.height - height;
9290
}
@@ -177,6 +175,28 @@ public class Gala.PanelWindow : ShellWindow, RootTarget {
177175
}
178176
}
179177

178+
protected override void get_window_position (
179+
Mtk.Rectangle window_rect, Mtk.Rectangle monitor_rect, out int x, out int y
180+
) {
181+
switch (anchor) {
182+
case TOP:
183+
x = monitor_rect.x + (monitor_rect.width - window_rect.width) / 2;
184+
y = monitor_rect.y;
185+
break;
186+
187+
case BOTTOM:
188+
x = monitor_rect.x + (monitor_rect.width - window_rect.width) / 2;
189+
y = monitor_rect.y + monitor_rect.height - window_rect.height;
190+
break;
191+
192+
default:
193+
warning ("Unsupported anchor %s for PanelWindow", anchor.to_string ());
194+
x = 0;
195+
y = 0;
196+
break;
197+
}
198+
}
199+
180200
private void update_target () {
181201
var to_value = anchor == TOP ? -get_custom_window_rect ().height : get_custom_window_rect ().height;
182202
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 void propagate (UpdateType update_type, GestureAction action, double progress) {
2521
switch (update_type) {
2622
case START:

0 commit comments

Comments
 (0)