-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathPopoverManager.vala
More file actions
274 lines (224 loc) · 9.66 KB
/
PopoverManager.vala
File metadata and controls
274 lines (224 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (c) 2011-2015 Ikey Doherty <ikey@solus-project.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
[DBus (name = "org.pantheon.gala")]
public interface WMDBus : GLib.Object {
public abstract async void offset_notifications (int32 offset) throws DBusError, Error;
}
public class Wingpanel.Services.PopoverManager : Object {
private unowned Wingpanel.PanelWindow? owner;
private bool grabbed = false; // whether the wingpanel grabbed focus
private bool mousing = false;
private WMDBus? wm = null;
private Gee.HashMap<string, Wingpanel.Widgets.IndicatorEntry> registered_indicators;
private Wingpanel.Widgets.IndicatorPopover popover;
private Wingpanel.Widgets.IndicatorEntry? _current_indicator = null;
public Wingpanel.Widgets.IndicatorEntry? current_indicator {
get {
return _current_indicator;
}
set {
if (value == null && _current_indicator == null) {
return;
}
if (_current_indicator == null && value != null) { // First open
_current_indicator = value;
} else if (value == null && _current_indicator != null) { // Close requested
_current_indicator.base_indicator.closed ();
_current_indicator = null;
} else if (_current_indicator.base_indicator.code_name == value.base_indicator.code_name) { // Close due to toggle
_current_indicator.base_indicator.closed ();
_current_indicator = null;
} else { // Switch
update_has_tooltip (_current_indicator.display_widget);
_current_indicator.base_indicator.closed ();
_current_indicator = value;
}
if (_current_indicator != null) {
popover.set_content (_current_indicator.indicator_widget);
popover.relative_to = _current_indicator;
update_has_tooltip (_current_indicator.display_widget, false);
owner.set_expanded (true);
make_modal (popover, true);
owner.present ();
popover.popup ();
popover.show_all ();
offset_notifications (popover);
_current_indicator.base_indicator.opened ();
} else {
update_has_tooltip (((Wingpanel.Widgets.IndicatorEntry)popover.get_relative_to ()).display_widget);
popover.popdown ();
offset_notifications ();
}
}
}
public PopoverManager (Wingpanel.PanelWindow? owner) {
registered_indicators = new Gee.HashMap<string, Wingpanel.Widgets.IndicatorEntry> ();
this.owner = owner;
try {
this.wm = Bus.get_proxy_sync (BusType.SESSION, "org.pantheon.gala", "/org/pantheon/gala");
} catch (Error e) {
warning ("Could not connect to dbus org.pantheon.gala");
}
popover = new Wingpanel.Widgets.IndicatorPopover ();
popover.leave_notify_event.connect ((e) => {
Gtk.Allocation allocation;
popover.get_allocation (out allocation);
if (e.mode != Gdk.CrossingMode.NORMAL && e.subwindow == null) {
current_indicator = null;
}
return Gdk.EVENT_PROPAGATE;
});
popover.closed.connect (() => {
make_modal (popover, false);
});
popover.unmap.connect (() => {
if (!grabbed) {
owner.set_expanded (false);
}
});
owner.focus_out_event.connect ((e) => {
if (mousing) {
return Gdk.EVENT_PROPAGATE;
}
if (current_indicator != null && e.window == null) {
current_indicator = null;
}
return Gdk.EVENT_PROPAGATE;
});
owner.button_press_event.connect ((w, e) => {
// "owner" is the invisible window that fills the screen when an indicator is open, if the event didn't hit
// that window directly, then it was probably in the popover, so propagate it.
if (e.window != owner.get_window ()) {
return Gdk.EVENT_PROPAGATE;
}
Gtk.Allocation allocation;
popover.get_allocation (out allocation);
Gtk.Allocation indicator_allocation;
current_indicator.get_allocation (out indicator_allocation);
Gtk.Allocation container_allocation;
current_indicator.get_parent ().get_allocation (out container_allocation);
var wingpanel_width = owner.get_allocated_width ();
allocation.x += indicator_allocation.x +
container_allocation.x -
((allocation.width - indicator_allocation.width) / 2);
if (allocation.x < 0) {
allocation.x = 0;
}
if (allocation.x + allocation.width > wingpanel_width) {
allocation.x = wingpanel_width - allocation.width;
}
if ((e.x < allocation.x || e.x > allocation.x + allocation.width) || (e.y < allocation.y || e.y > allocation.y + allocation.height)) {
current_indicator = null;
}
return Gdk.EVENT_STOP;
});
owner.add_events (Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.ENTER_NOTIFY_MASK | Gdk.EventMask.BUTTON_PRESS_MASK);
}
public void set_popover_visible (string code_name, bool visible) {
if (registered_indicators.has_key (code_name)) {
var new_indicator = registered_indicators.get (code_name);
if (visible && (current_indicator == null || current_indicator.base_indicator.code_name != new_indicator.base_indicator.code_name)) {
current_indicator = new_indicator;
} else if (current_indicator.base_indicator.code_name == new_indicator.base_indicator.code_name && !visible) {
current_indicator = null;
}
}
}
public void toggle_popover_visible (string code_name) {
if (registered_indicators.has_key (code_name)) {
current_indicator = registered_indicators.get (code_name);
}
}
public bool get_visible (Wingpanel.Widgets.IndicatorEntry entry) {
return current_indicator != null && current_indicator.base_indicator.code_name == entry.base_indicator.code_name;
}
private void update_has_tooltip (Gtk.Widget display_widget, bool enable = true) {
if (display_widget != null) {
display_widget.has_tooltip = enable;
}
}
private void make_modal (Gtk.Popover? pop, bool modal = true) {
if (pop == null || pop.get_window () == null || mousing) {
return;
}
if (modal && !grabbed) {
grabbed = true;
Gtk.grab_add (owner);
owner.set_focus (null);
pop.grab_focus ();
} else if (!modal && grabbed) {
grabbed = false;
Gtk.grab_remove (owner);
owner.grab_focus ();
}
}
public void close () {
if (current_indicator != null) {
current_indicator = null;
}
}
public void unregister_indicator (Wingpanel.Widgets.IndicatorEntry? widg) {
if (registered_indicators.has_key (widg.base_indicator.code_name)) {
registered_indicators.unset (widg.base_indicator.code_name);
}
}
public void register_indicator (Wingpanel.Widgets.IndicatorEntry? widg) {
if (registered_indicators.has_key (widg.base_indicator.code_name)) {
return;
}
registered_indicators.set (widg.base_indicator.code_name, widg);
widg.enter_notify_event.connect ((w, e) => {
if (mousing) {
return Gdk.EVENT_PROPAGATE;
}
if (grabbed) {
if (!get_visible (widg) && e.mode != Gdk.CrossingMode.TOUCH_BEGIN) {
mousing = true;
current_indicator = widg;
mousing = false;
}
return Gdk.EVENT_STOP;
}
return Gdk.EVENT_PROPAGATE;
});
widg.notify["visible"].connect (() => {
if (mousing || grabbed) {
return;
}
if (get_visible (widg)) {
current_indicator = null;
}
});
}
private void offset_notifications (Wingpanel.Widgets.IndicatorPopover? popover = null) {
int offset = 0;
if (popover != null
&& current_indicator.base_indicator.code_name != Indicator.APP_LAUNCHER
&& current_indicator.base_indicator.code_name != Indicator.DATETIME) {
popover.get_preferred_height (null, out offset);
}
wm.offset_notifications.begin (offset, (obj, res) => {
try {
wm.offset_notifications.end (res);
} catch (Error e) {
warning ("Could not offset notifications: %s", e.message);
}
});
}
}