annotate src/sys/glib/dark_theme.cc @ 308:da2c5a8ff306

time: don't use time_t!
author Paper <paper@paper.us.eu.org>
date Tue, 11 Jun 2024 04:24:19 -0400
parents ac1451035c85
children daa03aa2262d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
1 #include <cstring>
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include <gio/gio.h>
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
3 #include <string_view>
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4
292
ac1451035c85 dialog/about: add utf8proc
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
5 /* this file uses the regular gio C interface because I don't
ac1451035c85 dialog/about: add utf8proc
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
6 * see any real benefit to using the C++ bindings. */
ac1451035c85 dialog/about: add utf8proc
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
7
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 namespace glib {
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 bool IsInDarkTheme() {
236
4d461ef7d424 HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents: 232
diff changeset
11 GSettings* settings = ::g_settings_new("org.gnome.desktop.interface");
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 if (!settings)
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 return false;
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
15 {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
16 GVariant* val = ::g_settings_get_value(settings, "color-scheme");
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
17 if (!val) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
18 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
19 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
20 }
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
22 const gchar* str = nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
23 ::g_variant_get(val, "&s", &str); /* should not be freed */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
24 if (!str) { /* how */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
25 ::g_variant_unref(val);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
26 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
27 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
28 }
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
30 bool success = !std::strcmp(str, "prefer-dark");
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
31
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
32 ::g_variant_unref(val);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
33
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
34 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
35 return true;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
36 }
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
37
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
38 {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
39 GVariant* gtk_theme = ::g_settings_get_value(settings, "gtk-theme");
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
40 if (!gtk_theme) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
41 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
42 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
43 }
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
45 const gchar* gtk_theme_str = nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
46 ::g_variant_get(gtk_theme, "&s", gtk_theme_str);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
47 if (!gtk_theme_str) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
48 ::g_variant_unref(gtk_theme);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
49 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
50 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
51 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
52
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
53 static constexpr std::string_view suffix = "-dark";
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
54
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
55 size_t gtk_theme_len = strlen(gtk_theme_str);
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
56
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
57 if (gtk_theme_len < suffix.length()) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
58 ::g_variant_unref(gtk_theme);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
59 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
60 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
61 }
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
62
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
63 bool success = !std::strncmp(gtk_theme_str + gtk_theme_len - suffix.length(), suffix.data(), suffix.length());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
64
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
65 ::g_variant_unref(gtk_theme);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
66 ::g_object_unref(settings);
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
67
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
68 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
69 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
70 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
71
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
72 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 }
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
75 } // namespace glib