forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color-scheme.c
286 lines (246 loc) · 7.9 KB
/
color-scheme.c
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
/* color-scheme.c
*
* Copyright (C) 2022-2024 HandBrake Team
*
* 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, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "color-scheme.h"
#define APP_DARK APP_PREFERS_DARK /* 1 */
#define APP_FORCE APP_FORCES_LIGHT /* 2 */
#define DBUS_TIMEOUT 1000 /* Timeout for D-Bus to respond in ms */
#if GLIB_CHECK_VERSION(2, 72, 0)
#define SETTING_CHANGED_SIGNAL "g-signal::SettingChanged"
#else
#define SETTING_CHANGED_SIGNAL "g-signal"
#endif
static AppColorScheme app_scheme = APP_FORCES_LIGHT;
static GDBusProxy *desktop_portal;
static GtkSettings *settings;
static gboolean init_done = FALSE;
static void
set_gtk_theme (gboolean dark)
{
g_object_set(settings, "gtk-application-prefer-dark-theme", dark, NULL);
}
/*
* Returns the currently selected app colour scheme.
*/
AppColorScheme
color_scheme_get_app_scheme (void)
{
return app_scheme;
}
/*
* Returns TRUE if the app is currently using the dark theme.
*/
gboolean
color_scheme_is_dark_theme (void)
{
gboolean is_dark;
g_object_get (settings, "gtk-application-prefer-dark-theme", &is_dark, NULL);
return is_dark;
}
/*
* Switches the color state of the app, no matter which color
* is currently in use. Afterwards, the app color scheme is
* changed to APP_FORCES_LIGHT or APP_FORCES_DARK, so changes
* to the desktop color scheme no longer have any effect.
*/
gboolean
color_scheme_toggle (void)
{
gboolean set_dark = !color_scheme_is_dark_theme();
g_object_set(settings, "gtk-application-prefer-dark-theme",
set_dark, NULL);
/* Change app_scheme to APP_FORCES_[DARK|LIGHT] */
app_scheme = (AppColorScheme) (set_dark | APP_FORCE);
return set_dark;
}
static void
setting_changed (GDBusProxy *proxy, char *sender_name, char *signal_name,
GVariant *parameters, gpointer user_data)
{
const char *namespace_;
const char *key;
g_autoptr (GVariant) var = NULL;
guint32 portal_value;
gboolean set_dark;
if (g_strcmp0(signal_name, "SettingChanged"))
return;
g_variant_get(parameters, "(&s&sv)", &namespace_, &key, &var);
if (!g_strcmp0(namespace_, "org.freedesktop.appearance") &&
!g_strcmp0(key, "color-scheme"))
{
g_variant_get(var, "u", &portal_value);
set_dark = (portal_value == DESKTOP_PREFERS_DARK) ? TRUE : FALSE;
if (!(app_scheme & APP_FORCE))
set_gtk_theme(set_dark);
}
}
static GDBusProxy *
portal_init (void)
{
GDBusProxy *portal;
GError *error = NULL;
portal = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE, NULL,
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings",
NULL, &error);
if (!portal)
g_debug ("Could not access portal: %s", error->message);
return portal;
}
static void
set_theme (void)
{
DesktopColorScheme desktop_scheme;
gboolean set_dark;
desktop_scheme = color_scheme_get_desktop_scheme();
if ((app_scheme & APP_FORCE) || desktop_scheme == DESKTOP_NO_PREFERENCE)
set_dark = (app_scheme & APP_DARK);
else
set_dark = (desktop_scheme == DESKTOP_PREFERS_DARK);
set_gtk_theme(set_dark);
}
static void
init_color_scheme_cb (GObject *object, GAsyncResult *res, gpointer user_data)
{
GDBusProxy *portal;
GError *error = NULL;
portal = g_dbus_proxy_new_for_bus_finish(res, &error);
if (portal)
{
g_signal_connect(portal, SETTING_CHANGED_SIGNAL,
G_CALLBACK(setting_changed), NULL);
}
else
{
g_debug("Could not access portal: %s", error->message);
}
init_done = TRUE;
}
static void
init_color_scheme_async (void)
{
static size_t init = 0;
if (g_once_init_enter(&init))
{
settings = gtk_settings_get_default();
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE, NULL,
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings",
NULL,
(GAsyncReadyCallback)init_color_scheme_cb,
NULL);
g_once_init_leave(&init, 1);
}
set_theme();
}
static gboolean
init_color_scheme (void)
{
static GMutex init_mutex;
g_mutex_lock(&init_mutex);
{
if (!init_done)
{
settings = gtk_settings_get_default();
desktop_portal = portal_init();
if (desktop_portal)
{
g_signal_connect(desktop_portal, SETTING_CHANGED_SIGNAL,
G_CALLBACK(setting_changed), NULL);
}
}
}
init_done = TRUE;
g_mutex_unlock(&init_mutex);
return TRUE;
}
/*
* Returns the current desktop color scheme, given by
* the org.freedesktop.portal.Settings portal. If the portal
* is not available, DESKTOP_NO_PREFERENCE is returned.
*/
DesktopColorScheme
color_scheme_get_desktop_scheme (void)
{
g_autoptr (GVariant) outer = NULL;
g_autoptr (GVariant) inner = NULL;
g_autoptr (GVariant) result = NULL;
g_autoptr (GError) error = NULL;
DesktopColorScheme scheme;
if (!init_done && !init_color_scheme())
return DESKTOP_NO_PREFERENCE;
if (!desktop_portal)
return DESKTOP_NO_PREFERENCE;
GVariant *call = g_variant_new("(ss)", "org.freedesktop.appearance",
"color-scheme");
result = g_dbus_proxy_call_sync(desktop_portal, "Read", call,
G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT,
NULL, &error);
if (!result)
{
g_debug("%s", error->message);
return DESKTOP_NO_PREFERENCE;
}
g_variant_get(result, "(v)", &outer);
g_variant_get(outer, "v", &inner);
scheme = (DesktopColorScheme) g_variant_get_uint32(inner);
if (scheme > DESKTOP_PREFERS_LIGHT)
scheme = DESKTOP_NO_PREFERENCE;
return scheme;
}
/*
* Sets the color scheme to one of the four options.
* Can be called once GTK has been initialised.
* Returns FALSE if the color scheme could not be set.
* If the return value is not needed, use color_scheme_set_async instead.
*/
gboolean
color_scheme_set (AppColorScheme scheme)
{
gboolean set_dark;
DesktopColorScheme desktop_scheme;
if (!init_done && !init_color_scheme())
return FALSE;
app_scheme = scheme;
desktop_scheme = color_scheme_get_desktop_scheme();
if ((app_scheme & APP_FORCE) || desktop_scheme == DESKTOP_NO_PREFERENCE)
set_dark = (app_scheme & APP_DARK);
else
set_dark = (desktop_scheme == DESKTOP_PREFERS_DARK);
set_gtk_theme(set_dark);
return TRUE;
}
/*
* Sets the color scheme to one of the four options.
* Can be called once GTK has been initialised.
* Runs asynchronously, so preferred over color_scheme_set.
*/
void
color_scheme_set_async (AppColorScheme scheme)
{
app_scheme = scheme;
if (init_done)
set_theme();
else
init_color_scheme_async();
}