-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathAppearance.vala
More file actions
421 lines (347 loc) · 16 KB
/
Appearance.vala
File metadata and controls
421 lines (347 loc) · 16 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Copyright 2018–2021 elementary, Inc. (https://elementary.io)
*
* 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 3 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 PantheonShell.Appearance : Gtk.Grid {
private const string INTERFACE_SCHEMA = "org.gnome.desktop.interface";
private const string STYLESHEET_KEY = "gtk-theme";
private const string STYLESHEET_PREFIX = "io.elementary.stylesheet.";
private enum AccentColor {
NO_PREFERENCE,
RED,
ORANGE,
YELLOW,
GREEN,
MINT,
BLUE,
PURPLE,
PINK,
BROWN,
GRAY;
public string to_string () {
switch (this) {
case RED:
return "strawberry";
case ORANGE:
return "orange";
case YELLOW:
return "banana";
case GREEN:
return "lime";
case MINT:
return "mint";
case BLUE:
return "blueberry";
case PURPLE:
return "grape";
case PINK:
return "bubblegum";
case BROWN:
return "cocoa";
case GRAY:
return "slate";
}
return "auto";
}
}
construct {
column_spacing = 12;
halign = Gtk.Align.CENTER;
row_spacing = 6;
margin_start = margin_end = 12;
margin_bottom = 24;
var dark_label = new Gtk.Label (_("Style:")) {
halign = Gtk.Align.END
};
var prefer_default_image = new Gtk.Image.from_resource ("/io/elementary/switchboard/plug/pantheon-shell/appearance-default.svg");
var prefer_default_card = new Gtk.Grid () {
margin = 6,
margin_start = 12
};
prefer_default_card.add (prefer_default_image);
unowned Gtk.StyleContext prefer_default_card_context = prefer_default_card.get_style_context ();
prefer_default_card_context.add_class (Granite.STYLE_CLASS_CARD);
prefer_default_card_context.add_class (Granite.STYLE_CLASS_ROUNDED);
var prefer_default_grid = new Gtk.Grid () {
row_spacing = 6
};
prefer_default_grid.attach (prefer_default_card, 0, 0);
prefer_default_grid.attach (new Gtk.Label (_("Default")), 0, 1);
var prefer_default_radio = new Gtk.RadioButton (null) {
halign = Gtk.Align.START
};
prefer_default_radio.get_style_context ().add_class ("image-button");
prefer_default_radio.add (prefer_default_grid);
var prefer_dark_image = new Gtk.Image.from_resource ("/io/elementary/switchboard/plug/pantheon-shell/appearance-dark.svg");
var prefer_dark_card = new Gtk.Grid () {
margin = 6,
margin_start = 12
};
prefer_dark_card.add (prefer_dark_image);
unowned Gtk.StyleContext prefer_dark_card_context = prefer_dark_card.get_style_context ();
prefer_dark_card_context.add_class (Granite.STYLE_CLASS_CARD);
prefer_dark_card_context.add_class (Granite.STYLE_CLASS_ROUNDED);
var prefer_dark_grid = new Gtk.Grid () {
row_spacing = 6
};
prefer_dark_grid.attach (prefer_dark_card, 0, 0);
prefer_dark_grid.attach (new Gtk.Label (_("Dark")), 0, 1);
var prefer_dark_radio = new Gtk.RadioButton.from_widget (prefer_default_radio) {
halign = Gtk.Align.START,
hexpand = true
};
prefer_dark_radio.get_style_context ().add_class ("image-button");
prefer_dark_radio.add (prefer_dark_grid);
var dark_info = new Gtk.Label (_("Preferred visual style for system components. Apps may also choose to follow this preference.")) {
max_width_chars = 60,
margin_bottom = 18,
wrap = true,
xalign = 0
};
dark_info.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL);
var schedule_label = new Gtk.Label (_("Schedule:")) {
halign = Gtk.Align.END,
xalign = 1
};
var schedule_mode_button = new Granite.Widgets.ModeButton ();
schedule_mode_button.append_text (_("Disabled"));
schedule_mode_button.append_text (_("Sunset to Sunrise"));
schedule_mode_button.append_text (_("Manual"));
var from_label = new Gtk.Label (_("From:"));
var from_time = new Granite.Widgets.TimePicker () {
hexpand = true
};
var to_label = new Gtk.Label (_("To:"));
var to_time = new Granite.Widgets.TimePicker () {
hexpand = true
};
var schedule_grid = new Gtk.Grid () {
column_spacing = 12,
margin_bottom = 24
};
schedule_grid.add (from_label);
schedule_grid.add (from_time);
schedule_grid.add (to_label);
schedule_grid.add (to_time);
Pantheon.AccountsService? pantheon_act = null;
string? user_path = null;
try {
FDO.Accounts? accounts_service = GLib.Bus.get_proxy_sync (
GLib.BusType.SYSTEM,
"org.freedesktop.Accounts",
"/org/freedesktop/Accounts"
);
user_path = accounts_service.find_user_by_name (GLib.Environment.get_user_name ());
} catch (Error e) {
critical (e.message);
}
if (user_path != null) {
try {
pantheon_act = GLib.Bus.get_proxy_sync (
GLib.BusType.SYSTEM,
"org.freedesktop.Accounts",
user_path,
GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES
);
} catch (Error e) {
warning ("Unable to get AccountsService proxy, color scheme preference may be incorrect");
}
}
if (((GLib.DBusProxy) pantheon_act).get_cached_property ("PrefersColorScheme") != null) {
attach (dark_label, 0, 0);
attach (prefer_default_radio, 1, 0);
attach (prefer_dark_radio, 2, 0);
attach (dark_info, 1, 1, 2);
attach (schedule_label, 0, 2, 1, 1);
attach (schedule_mode_button, 1, 2, 2, 1);
attach (schedule_grid, 1, 3, 2, 1);
switch (pantheon_act.prefers_color_scheme) {
case Granite.Settings.ColorScheme.DARK:
prefer_dark_radio.active = true;
break;
default:
prefer_default_radio.active = true;
break;
}
prefer_default_radio.toggled.connect (() => {
pantheon_act.prefers_color_scheme = Granite.Settings.ColorScheme.NO_PREFERENCE;
});
prefer_dark_radio.toggled.connect (() => {
pantheon_act.prefers_color_scheme = Granite.Settings.ColorScheme.DARK;
});
/* Connect to button_release_event so that this is only triggered
* through user interaction, not if scheduling changes the selection
*/
prefer_default_radio.button_release_event.connect (() => {
schedule_mode_button.selected = 0;
return Gdk.EVENT_PROPAGATE;
});
prefer_dark_radio.button_release_event.connect (() => {
schedule_mode_button.selected = 0;
return Gdk.EVENT_PROPAGATE;
});
((GLib.DBusProxy) pantheon_act).g_properties_changed.connect ((changed, invalid) => {
var color_scheme = changed.lookup_value ("PrefersColorScheme", new VariantType ("i"));
if (color_scheme != null) {
switch ((Granite.Settings.ColorScheme) color_scheme.get_int32 ()) {
case Granite.Settings.ColorScheme.DARK:
prefer_dark_radio.active = true;
break;
default:
prefer_default_radio.active = true;
break;
}
}
});
var settings = new GLib.Settings ("io.elementary.settings-daemon.prefers-color-scheme");
from_time.time = double_date_time (settings.get_double ("prefer-dark-schedule-from"));
from_time.time_changed.connect (() => {
settings.set_double ("prefer-dark-schedule-from", date_time_double (from_time.time));
});
to_time.time = double_date_time (settings.get_double ("prefer-dark-schedule-to"));
to_time.time_changed.connect (() => {
settings.set_double ("prefer-dark-schedule-to", date_time_double (to_time.time));
});
var schedule = settings.get_string ("prefer-dark-schedule");
from_label.sensitive = schedule == "manual";
from_time.sensitive = schedule == "manual";
to_label.sensitive = schedule == "manual";
to_time.sensitive = schedule == "manual";
if (schedule == "sunset-to-sunrise") {
schedule_mode_button.selected = 1;
} else if (schedule == "manual") {
schedule_mode_button.selected = 2;
} else {
schedule_mode_button.selected = 0;
}
schedule_mode_button.mode_changed.connect (() => {
if (schedule_mode_button.selected == 1) {
schedule = "sunset-to-sunrise";
} else if (schedule_mode_button.selected == 2) {
schedule = "manual";
} else {
schedule = "disabled";
}
settings.set_string ("prefer-dark-schedule", schedule);
from_label.sensitive = schedule == "manual";
from_time.sensitive = schedule == "manual";
to_label.sensitive = schedule == "manual";
to_time.sensitive = schedule == "manual";
});
}
var interface_settings = new GLib.Settings (INTERFACE_SCHEMA);
var current_stylesheet = interface_settings.get_string (STYLESHEET_KEY);
debug ("Current stylesheet: %s", current_stylesheet);
if (current_stylesheet.has_prefix (STYLESHEET_PREFIX)) {
/// TRANSLATORS: as in "Accent color"
var accent_label = new Gtk.Label (_("Accent:"));
accent_label.halign = Gtk.Align.END;
var blueberry_button = new PrefersAccentColorButton (pantheon_act, AccentColor.BLUE);
blueberry_button.tooltip_text = _("Blueberry");
var mint_button = new PrefersAccentColorButton (pantheon_act, AccentColor.MINT, blueberry_button);
mint_button.tooltip_text = _("Mint");
var lime_button = new PrefersAccentColorButton (pantheon_act, AccentColor.GREEN, blueberry_button);
lime_button.tooltip_text = _("Lime");
var banana_button = new PrefersAccentColorButton (pantheon_act, AccentColor.YELLOW, blueberry_button);
banana_button.tooltip_text = _("Banana");
var orange_button = new PrefersAccentColorButton (pantheon_act, AccentColor.ORANGE, blueberry_button);
orange_button.tooltip_text = _("Orange");
var strawberry_button = new PrefersAccentColorButton (pantheon_act, AccentColor.RED, blueberry_button);
strawberry_button.tooltip_text = _("Strawberry");
var bubblegum_button = new PrefersAccentColorButton (pantheon_act, AccentColor.PINK, blueberry_button);
bubblegum_button.tooltip_text = _("Bubblegum");
var grape_button = new PrefersAccentColorButton (pantheon_act, AccentColor.PURPLE, blueberry_button);
grape_button.tooltip_text = _("Grape");
var cocoa_button = new PrefersAccentColorButton (pantheon_act, AccentColor.BROWN, blueberry_button);
cocoa_button.tooltip_text = _("Cocoa");
var slate_button = new PrefersAccentColorButton (pantheon_act, AccentColor.GRAY, blueberry_button);
slate_button.tooltip_text = _("Slate");
var auto_button = new PrefersAccentColorButton (pantheon_act, AccentColor.NO_PREFERENCE, blueberry_button);
auto_button.tooltip_text = _("Automatic based on wallpaper");
var accent_grid = new Gtk.Grid ();
accent_grid.column_spacing = 6;
accent_grid.add (blueberry_button);
accent_grid.add (mint_button);
accent_grid.add (lime_button);
accent_grid.add (banana_button);
accent_grid.add (orange_button);
accent_grid.add (strawberry_button);
accent_grid.add (bubblegum_button);
accent_grid.add (grape_button);
accent_grid.add (cocoa_button);
accent_grid.add (slate_button);
accent_grid.add (auto_button);
var accent_info = new Gtk.Label (_("Used across the system by default. Apps can always use their own accent color.")) {
margin_bottom = 18,
xalign = 0,
wrap = true
};
accent_info.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL);
attach (accent_label, 0, 4);
attach (accent_grid, 1, 4, 2);
attach (accent_info, 1, 5, 2);
}
}
private class PrefersAccentColorButton : Gtk.RadioButton {
public AccentColor color { get; construct; }
public Pantheon.AccountsService? pantheon_act { get; construct; default = null; }
private static GLib.Settings interface_settings;
public PrefersAccentColorButton (Pantheon.AccountsService? pantheon_act, AccentColor color, Gtk.RadioButton? group_member = null) {
Object (
pantheon_act: pantheon_act,
color: color,
group: group_member
);
}
static construct {
interface_settings = new GLib.Settings (INTERFACE_SCHEMA);
var current_stylesheet = interface_settings.get_string (STYLESHEET_KEY);
}
construct {
unowned Gtk.StyleContext context = get_style_context ();
context.add_class (Granite.STYLE_CLASS_COLOR_BUTTON);
context.add_class (color.to_string ());
realize.connect (() => {
active = color == pantheon_act.prefers_accent_color;
toggled.connect (() => {
if (color != AccentColor.NO_PREFERENCE) {
interface_settings.set_string (
STYLESHEET_KEY,
STYLESHEET_PREFIX + color.to_string ()
);
}
if (((GLib.DBusProxy) pantheon_act).get_cached_property ("PrefersAccentColor") != null) {
pantheon_act.prefers_accent_color = color;
}
});
});
}
}
private static DateTime double_date_time (double dbl) {
var hours = (int) dbl;
var minutes = (int) Math.round ((dbl - hours) * 60);
var date_time = new DateTime.local (1, 1, 1, hours, minutes, 0.0);
return date_time;
}
private static double date_time_double (DateTime date_time) {
double time_double = 0;
time_double += date_time.get_hour ();
time_double += (double) date_time.get_minute () / 60;
return time_double;
}
}