annotate src/sys/glib/dark_theme.cc @ 259:0362f3c4534c

widgets/graph: improve drawing code
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 18:11:15 -0400
parents 862d0d8619f6
children ac1451035c85
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
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 namespace glib {
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 bool IsInDarkTheme() {
236
4d461ef7d424 HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents: 232
diff changeset
8 GSettings* settings = ::g_settings_new("org.gnome.desktop.interface");
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 if (!settings)
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 return false;
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
12 {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
13 GVariant* val = ::g_settings_get_value(settings, "color-scheme");
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
14 if (!val) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
15 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
16 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
17 }
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
19 const gchar* str = nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
20 ::g_variant_get(val, "&s", &str); /* should not be freed */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
21 if (!str) { /* how */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
22 ::g_variant_unref(val);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
23 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
24 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
25 }
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
27 bool success = !std::strcmp(str, "prefer-dark");
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
28
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
29 ::g_variant_unref(val);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
30
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
31 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
32 return true;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
33 }
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
34
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
35 {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
36 GVariant* gtk_theme = ::g_settings_get_value(settings, "gtk-theme");
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
37 if (!gtk_theme) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
38 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
39 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
40 }
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
42 const gchar* gtk_theme_str = nullptr;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
43 ::g_variant_get(gtk_theme, "&s", gtk_theme_str);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
44 if (!gtk_theme_str) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
45 ::g_variant_unref(gtk_theme);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
46 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
47 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
48 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
49
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
50 static constexpr std::string_view suffix = "-dark";
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
51
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
52 size_t gtk_theme_len = strlen(gtk_theme_str);
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
53
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
54 if (gtk_theme_len < suffix.length()) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
55 ::g_variant_unref(gtk_theme);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
56 ::g_object_unref(settings);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
57 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
58 }
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
59
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
60 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
61
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
62 ::g_variant_unref(gtk_theme);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
63 ::g_object_unref(settings);
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
64
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
65 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
66 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
67 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
68
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
69 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 }
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
72 } // namespace glib