-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathPanel.vala
329 lines (276 loc) · 12.8 KB
/
Panel.vala
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (c) 2011-2015 Wingpanel Developers (http://launchpad.net/wingpanel)
*
* 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.
*/
public class Wingpanel.Widgets.Panel : Gtk.EventBox {
public Services.PopoverManager popover_manager { get; construct; }
private IndicatorMenuBar right_menubar;
private MenuBar left_menubar;
private MenuBar center_menubar;
private Gtk.StyleContext style_context;
private Gtk.CssProvider? style_provider = null;
private static Gtk.CssProvider resource_provider;
public Panel (Services.PopoverManager popover_manager) {
Object (popover_manager : popover_manager);
this.set_size_request (-1, 30);
this.hexpand = true;
this.vexpand = true;
this.valign = Gtk.Align.START;
this.get_style_context ().add_class (StyleClass.PANEL);
left_menubar = new MenuBar ();
left_menubar.halign = Gtk.Align.START;
left_menubar.get_style_context ().add_provider (resource_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
center_menubar = new MenuBar ();
center_menubar.get_style_context ().add_provider (resource_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
right_menubar = new IndicatorMenuBar ();
right_menubar.halign = Gtk.Align.END;
right_menubar.get_style_context ().add_provider (resource_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.pack_start (left_menubar);
box.set_center_widget (center_menubar);
box.pack_end (right_menubar);
add (box);
unowned IndicatorManager indicator_manager = IndicatorManager.get_default ();
indicator_manager.indicator_added.connect (add_indicator);
indicator_manager.indicator_removed.connect (remove_indicator);
indicator_manager.get_indicators ().@foreach ((indicator) => {
add_indicator (indicator);
return true;
});
style_context = this.get_style_context ();
style_context.add_provider (resource_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
Services.BackgroundManager.get_default ().background_state_changed.connect (update_background);
}
static construct {
resource_provider = new Gtk.CssProvider ();
resource_provider.load_from_resource ("io/elementary/wingpanel/panel.css");
}
public override bool button_press_event (Gdk.EventButton event) {
if (event.button != Gdk.BUTTON_PRIMARY) {
return Gdk.EVENT_PROPAGATE;
}
var window = get_window ();
if (window == null) {
return Gdk.EVENT_PROPAGATE;
}
// Grabbing with touchscreen on X does not work unfortunately
if (event.device.get_source () == Gdk.InputSource.TOUCHSCREEN) {
return Gdk.EVENT_PROPAGATE;
}
uint32 time = event.time;
window.get_display ().get_default_seat ().ungrab ();
Gdk.ModifierType state;
event.get_state (out state);
popover_manager.close ();
var scale_factor = this.get_scale_factor ();
var x = (int)event.x_root * scale_factor;
var y = (int)event.y_root * scale_factor;
var background_manager = Services.BackgroundManager.get_default ();
return background_manager.begin_grab_focused_window (x, y, (int)event.button, time, state);
}
public void cycle (bool forward) {
var current = popover_manager.current_indicator;
if (current == null) {
return;
}
IndicatorEntry? sibling;
if (forward) {
sibling = get_next_sibling (current);
} else {
sibling = get_previous_sibling (current);
}
if (sibling != null) {
popover_manager.current_indicator = sibling;
}
}
private IndicatorEntry? get_next_sibling (IndicatorEntry current) {
IndicatorEntry? sibling = null;
switch (current.base_indicator.code_name) {
case Indicator.APP_LAUNCHER:
var children = left_menubar.get_children ();
int index = children.index (current);
if (index == -1) {
break;
} else if (index < children.length () - 1) { // Has more than one indicator in the left menubar
sibling = children.nth_data (index + 1) as IndicatorEntry;
} else { // No more indicators on the left
var center_children = center_menubar.get_children ();
if (center_children.length () > 0) {
sibling = center_children.first ().data as IndicatorEntry;
}
}
break;
case Indicator.DATETIME:
var children = center_menubar.get_children ();
int index = children.index (current);
if (index == -1) {
break;
} else if (index < children.length () - 1) { // Has more than one indicator in the center menubar
sibling = children.nth_data (index + 1) as IndicatorEntry;
} else { // No more indicators on the center
var right_children = right_menubar.get_children ();
if (right_children.length () > 0) {
sibling = right_children.first ().data as IndicatorEntry;
}
}
break;
default:
var children = right_menubar.get_children ();
int index = children.index (current);
if (index == -1) {
break;
} else if (index < children.length () - 1) { // Has more than one indicator in the right menubar
sibling = children.nth_data (index + 1) as IndicatorEntry;
} else { // No more indicators on the right
var left_children = left_menubar.get_children ();
if (left_children.length () > 0) {
sibling = left_children.first ().data as IndicatorEntry;
}
}
break;
}
return sibling;
}
private IndicatorEntry? get_previous_sibling (IndicatorEntry current) {
IndicatorEntry? sibling = null;
switch (current.base_indicator.code_name) {
case Indicator.APP_LAUNCHER:
var children = left_menubar.get_children ();
int index = children.index (current);
if (index == -1) {
break;
} else if (index != 0) { // Is not the first indicator in the left menubar
sibling = children.nth_data (index - 1) as IndicatorEntry;
} else { // No more indicators on the left
var right_children = right_menubar.get_children ();
if (right_children.length () > 0) {
sibling = right_children.last ().data as IndicatorEntry;
}
}
break;
case Indicator.DATETIME:
var children = center_menubar.get_children ();
int index = children.index (current);
if (index == -1) {
break;
} else if (index != 0) { // Is not the first indicator in the center menubar
sibling = children.nth_data (index - 1) as IndicatorEntry;
} else { // No more indicators on the center
var left_children = left_menubar.get_children ();
if (left_children.length () > 0) {
sibling = left_children.last ().data as IndicatorEntry;
}
}
break;
default:
var children = right_menubar.get_children ();
int index = children.index (current);
if (index == -1) {
break;
} else if (index != 0) { // Is not the first indicator in the right menubar
sibling = children.nth_data (index - 1) as IndicatorEntry;
} else { // No more indicators on the right
var center_children = center_menubar.get_children ();
if (center_children.length () > 0) {
sibling = center_children.last ().data as IndicatorEntry;
}
}
break;
}
return sibling;
}
private void add_indicator (Indicator indicator) {
var indicator_entry = new IndicatorEntry (indicator, popover_manager);
switch (indicator.code_name) {
case Indicator.APP_LAUNCHER:
indicator_entry.set_transition_type (Gtk.RevealerTransitionType.SLIDE_RIGHT);
left_menubar.add (indicator_entry);
break;
case Indicator.DATETIME:
indicator_entry.set_transition_type (Gtk.RevealerTransitionType.SLIDE_DOWN);
center_menubar.add (indicator_entry);
break;
default:
indicator_entry.set_transition_type (Gtk.RevealerTransitionType.SLIDE_LEFT);
right_menubar.insert_sorted (indicator_entry);
break;
}
indicator_entry.show_all ();
}
private void remove_indicator (Indicator indicator) {
remove_indicator_from_container (left_menubar, indicator);
remove_indicator_from_container (center_menubar, indicator);
remove_indicator_from_container (right_menubar, indicator);
}
private void remove_indicator_from_container (Gtk.Container container, Indicator indicator) {
foreach (unowned Gtk.Widget child in container.get_children ()) {
unowned IndicatorEntry? entry = (child as IndicatorEntry);
if (entry != null && entry.base_indicator == indicator) {
container.remove (child);
return;
}
}
}
private void update_background (Services.BackgroundState state, uint animation_duration) {
if (style_provider == null) {
style_provider = new Gtk.CssProvider ();
style_context.add_provider (style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}
string css = """
.panel {
transition: all %ums ease-in-out;
}
""".printf (animation_duration);
try {
style_provider.load_from_data (css, css.length);
} catch (Error e) {
warning ("Parsing own style configuration failed: %s", e.message);
}
switch (state) {
case Services.BackgroundState.DARK :
style_context.add_class ("color-light");
style_context.remove_class ("color-dark");
style_context.remove_class ("maximized");
style_context.remove_class ("translucent");
break;
case Services.BackgroundState.LIGHT:
style_context.add_class ("color-dark");
style_context.remove_class ("color-light");
style_context.remove_class ("maximized");
style_context.remove_class ("translucent");
break;
case Services.BackgroundState.MAXIMIZED:
style_context.add_class ("maximized");
style_context.remove_class ("color-light");
style_context.remove_class ("color-dark");
style_context.remove_class ("translucent");
break;
case Services.BackgroundState.TRANSLUCENT_DARK:
style_context.add_class ("translucent");
style_context.add_class ("color-light");
style_context.remove_class ("color-dark");
style_context.remove_class ("maximized");
break;
case Services.BackgroundState.TRANSLUCENT_LIGHT:
style_context.add_class ("translucent");
style_context.add_class ("color-dark");
style_context.remove_class ("color-light");
style_context.remove_class ("maximized");
break;
}
}
}