Skip to content

Commit 5007257

Browse files
Remodel GestureController lifetimes (#2355)
Co-authored-by: Leo <[email protected]>
1 parent a57ee53 commit 5007257

File tree

10 files changed

+54
-23
lines changed

10 files changed

+54
-23
lines changed

src/Gestures/GestureController.vala

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public class Gala.GestureController : Object {
3535
public GestureAction action { get; construct; }
3636
public WindowManager wm { get; construct; }
3737

38-
private GestureTarget? _target;
39-
public GestureTarget target {
38+
private unowned RootTarget? _target;
39+
public RootTarget target {
4040
get { return _target; }
41-
set {
41+
private set {
4242
_target = value;
43-
target.propagate (UPDATE, action, progress);
43+
_target.propagate (UPDATE, action, progress);
4444
}
4545
}
4646

@@ -90,8 +90,21 @@ public class Gala.GestureController : Object {
9090

9191
private SpringTimeline? timeline;
9292

93-
public GestureController (GestureAction action, GestureTarget target, WindowManager wm) {
94-
Object (action: action, target: target, wm: wm);
93+
public GestureController (GestureAction action, WindowManager wm) {
94+
Object (action: action, wm: wm);
95+
}
96+
97+
/**
98+
* Do not call this directly, use {@link RooTarget.add_controller} instead.
99+
*/
100+
public void attached (RootTarget target) {
101+
ref ();
102+
this.target = target;
103+
}
104+
105+
public void detached () {
106+
_target = null;
107+
unref ();
95108
}
96109

97110
public void enable_touchpad () {

src/Gestures/RootTarget.vala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2025 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 interface Gala.RootTarget : Object, GestureTarget {
9+
public void add_gesture_controller (GestureController controller) requires (controller.target == null) {
10+
controller.attached (this);
11+
weak_ref (controller.detached);
12+
}
13+
}

src/Gestures/ScrollBackend.vala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class Gala.ScrollBackend : Object, GestureBackend {
2626
private const double FINISH_DELTA_HORIZONTAL = 40;
2727
private const double FINISH_DELTA_VERTICAL = 30;
2828

29-
public Clutter.Actor actor { get; construct; }
3029
public Clutter.Orientation orientation { get; construct; }
3130
public GestureSettings settings { get; construct; }
3231

@@ -46,7 +45,7 @@ public class Gala.ScrollBackend : Object, GestureBackend {
4645
}
4746

4847
public ScrollBackend (Clutter.Actor actor, Clutter.Orientation orientation, GestureSettings settings) {
49-
Object (actor: actor, orientation: orientation, settings: settings);
48+
Object (orientation: orientation, settings: settings);
5049

5150
actor.captured_event.connect (on_scroll_event);
5251
actor.leave_event.connect (on_leave_event);

src/ShellClients/PanelWindow.vala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Authored by: Leonhard Kargl <[email protected]>
66
*/
77

8-
public class Gala.PanelWindow : ShellWindow {
8+
public class Gala.PanelWindow : ShellWindow, RootTarget {
99
private const int ANIMATION_DURATION = 250;
1010

1111
private static HashTable<Meta.Window, Meta.Strut?> window_struts = new HashTable<Meta.Window, Meta.Strut?> (null, null);
@@ -50,8 +50,6 @@ public class Gala.PanelWindow : ShellWindow {
5050
if (window_struts.remove (window)) {
5151
update_struts ();
5252
}
53-
54-
gesture_controller = null; // make it release its reference on us
5553
});
5654

5755
notify["anchor"].connect (() => position = Position.from_anchor (anchor));
@@ -63,7 +61,8 @@ public class Gala.PanelWindow : ShellWindow {
6361
window.size_changed.connect (update_strut);
6462
window.position_changed.connect (update_strut);
6563

66-
gesture_controller = new GestureController (DOCK, this, wm);
64+
gesture_controller = new GestureController (DOCK, wm);
65+
add_gesture_controller (gesture_controller);
6766

6867
window.display.in_fullscreen_changed.connect (() => {
6968
if (wm.get_display ().get_monitor_in_fullscreen (window.get_monitor ())) {

src/Widgets/MultitaskingView/MultitaskingView.vala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* preparing the wm, opening the components and holds containers for
2121
* the icon groups, the WorkspaceClones and the MonitorClones.
2222
*/
23-
public class Gala.MultitaskingView : ActorTarget, ActivatableComponent {
23+
public class Gala.MultitaskingView : ActorTarget, RootTarget, ActivatableComponent {
2424
public const int ANIMATION_DURATION = 250;
2525

2626
private GestureController workspaces_gesture_controller;
@@ -57,18 +57,20 @@ public class Gala.MultitaskingView : ActorTarget, ActivatableComponent {
5757
opened = false;
5858
display = wm.get_display ();
5959

60-
multitasking_gesture_controller = new GestureController (MULTITASKING_VIEW, this, wm);
60+
multitasking_gesture_controller = new GestureController (MULTITASKING_VIEW, wm);
6161
multitasking_gesture_controller.enable_touchpad ();
62+
add_gesture_controller (multitasking_gesture_controller);
6263

6364
add_target (ShellClientsManager.get_instance ()); // For hiding the panels
6465

6566
workspaces = new WorkspaceRow (display);
6667

67-
workspaces_gesture_controller = new GestureController (SWITCH_WORKSPACE, this, wm) {
68+
workspaces_gesture_controller = new GestureController (SWITCH_WORKSPACE, wm) {
6869
overshoot_upper_clamp = 0.1
6970
};
7071
workspaces_gesture_controller.enable_touchpad ();
7172
workspaces_gesture_controller.enable_scroll (this, HORIZONTAL);
73+
add_gesture_controller (workspaces_gesture_controller);
7274

7375
icon_groups = new IconGroupContainer (display.get_monitor_scale (display.get_primary_monitor ()));
7476

src/Widgets/MultitaskingView/WindowClone.vala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* A container for a clone of the texture of a MetaWindow, a WindowIcon, a Tooltip with the title,
99
* a close button and a shadow. Used together with the WindowCloneContainer.
1010
*/
11-
public class Gala.WindowClone : ActorTarget {
11+
public class Gala.WindowClone : ActorTarget, RootTarget {
1212
private const int WINDOW_ICON_SIZE = 64;
1313
private const int ACTIVE_SHAPE_SIZE = 12;
1414
private const int FADE_ANIMATION_DURATION = 200;
@@ -93,8 +93,9 @@ public class Gala.WindowClone : ActorTarget {
9393
construct {
9494
reactive = true;
9595

96-
gesture_controller = new GestureController (CLOSE_WINDOW, this, wm);
96+
gesture_controller = new GestureController (CLOSE_WINDOW, wm);
9797
gesture_controller.enable_scroll (this, VERTICAL);
98+
add_gesture_controller (gesture_controller);
9899

99100
window.unmanaged.connect (unmanaged);
100101
window.notify["fullscreen"].connect (check_shadow_requirements);

src/Widgets/WindowOverview.vala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* SPDX-License-Identifier: GPL-3.0-or-later
66
*/
77

8-
public class Gala.WindowOverview : ActorTarget, ActivatableComponent {
8+
public class Gala.WindowOverview : ActorTarget, RootTarget, ActivatableComponent {
99
private const int BORDER = 10;
1010
private const int TOP_GAP = 30;
1111
private const int BOTTOM_GAP = 100;
@@ -26,9 +26,10 @@ public class Gala.WindowOverview : ActorTarget, ActivatableComponent {
2626
construct {
2727
visible = false;
2828
reactive = true;
29-
gesture_controller = new GestureController (MULTITASKING_VIEW, this, wm) {
29+
gesture_controller = new GestureController (MULTITASKING_VIEW, wm) {
3030
enabled = false
3131
};
32+
add_gesture_controller (gesture_controller);
3233
}
3334

3435

src/Widgets/WindowSwitcher/WindowSwitcher.vala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* SPDX-License-Identifier: GPL-3.0-or-later
88
*/
99

10-
public class Gala.WindowSwitcher : CanvasActor, GestureTarget {
10+
public class Gala.WindowSwitcher : CanvasActor, GestureTarget, RootTarget {
1111
public const int ICON_SIZE = 64;
1212
public const int WRAPPER_PADDING = 12;
1313

@@ -60,13 +60,14 @@ public class Gala.WindowSwitcher : CanvasActor, GestureTarget {
6060
construct {
6161
style_manager = Drawing.StyleManager.get_instance ();
6262

63-
gesture_controller = new GestureController (SWITCH_WINDOWS, this, wm) {
63+
gesture_controller = new GestureController (SWITCH_WINDOWS, wm) {
6464
overshoot_upper_clamp = int.MAX,
6565
overshoot_lower_clamp = int.MIN,
6666
snap = false
6767
};
6868
gesture_controller.enable_touchpad ();
6969
gesture_controller.notify["recognizing"].connect (recognizing_changed);
70+
add_gesture_controller (gesture_controller);
7071

7172
container = new Clutter.Actor () {
7273
reactive = true,

src/Zoom.vala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* SPDX-License-Identifier: GPL-3.0-or-later
66
*/
77

8-
public class Gala.Zoom : Object, GestureTarget {
8+
public class Gala.Zoom : Object, GestureTarget, RootTarget {
99
private const float MIN_ZOOM = 1.0f;
1010
private const float MAX_ZOOM = 10.0f;
1111
private const float SHORTCUT_DELTA = 0.5f;
@@ -34,10 +34,11 @@ public class Gala.Zoom : Object, GestureTarget {
3434
display.add_keybinding ("zoom-in", schema, Meta.KeyBindingFlags.NONE, (Meta.KeyHandlerFunc) zoom_in);
3535
display.add_keybinding ("zoom-out", schema, Meta.KeyBindingFlags.NONE, (Meta.KeyHandlerFunc) zoom_out);
3636

37-
gesture_controller = new GestureController (ZOOM, this, wm) {
37+
gesture_controller = new GestureController (ZOOM, wm) {
3838
snap = false
3939
};
4040
gesture_controller.enable_touchpad ();
41+
add_gesture_controller (gesture_controller);
4142

4243
behavior_settings = new GLib.Settings ("io.elementary.desktop.wm.behavior");
4344

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ gala_bin_sources = files(
4242
'Gestures/GestureSettings.vala',
4343
'Gestures/GestureTarget.vala',
4444
'Gestures/PropertyTarget.vala',
45+
'Gestures/RootTarget.vala',
4546
'Gestures/ScrollBackend.vala',
4647
'Gestures/SpringTimeline.vala',
4748
'Gestures/ToucheggBackend.vala',

0 commit comments

Comments
 (0)