Mercurial > minori
annotate src/sys/glib/dark_theme.cc @ 270:0718f538c5f9
dep/animone: filter open files by access mode
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 12 Apr 2024 19:13:50 -0400 |
parents | 862d0d8619f6 |
children | ac1451035c85 |
rev | line source |
---|---|
258 | 1 #include <cstring> |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 #include <gio/gio.h> |
250 | 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 | 12 { |
13 GVariant* val = ::g_settings_get_value(settings, "color-scheme"); | |
14 if (!val) { | |
15 ::g_object_unref(settings); | |
16 return false; | |
17 } | |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 |
258 | 19 const gchar* str = nullptr; |
20 ::g_variant_get(val, "&s", &str); /* should not be freed */ | |
21 if (!str) { /* how */ | |
22 ::g_variant_unref(val); | |
23 ::g_object_unref(settings); | |
24 return false; | |
25 } | |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
258 | 27 bool success = !std::strcmp(str, "prefer-dark"); |
250 | 28 |
258 | 29 ::g_variant_unref(val); |
30 | |
31 if (success) | |
32 return true; | |
250 | 33 } |
34 | |
258 | 35 { |
36 GVariant* gtk_theme = ::g_settings_get_value(settings, "gtk-theme"); | |
37 if (!gtk_theme) { | |
38 ::g_object_unref(settings); | |
39 return false; | |
40 } | |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 |
258 | 42 const gchar* gtk_theme_str = nullptr; |
43 ::g_variant_get(gtk_theme, "&s", gtk_theme_str); | |
44 if (!gtk_theme_str) { | |
45 ::g_variant_unref(gtk_theme); | |
46 ::g_object_unref(settings); | |
47 return false; | |
48 } | |
49 | |
50 static constexpr std::string_view suffix = "-dark"; | |
250 | 51 |
258 | 52 size_t gtk_theme_len = strlen(gtk_theme_str); |
250 | 53 |
258 | 54 if (gtk_theme_len < suffix.length()) { |
55 ::g_variant_unref(gtk_theme); | |
56 ::g_object_unref(settings); | |
57 return false; | |
58 } | |
250 | 59 |
258 | 60 bool success = !std::strncmp(gtk_theme_str + gtk_theme_len - suffix.length(), suffix.data(), suffix.length()); |
61 | |
62 ::g_variant_unref(gtk_theme); | |
63 ::g_object_unref(settings); | |
250 | 64 |
258 | 65 if (success) |
66 return true; | |
67 } | |
68 | |
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 | 72 } // namespace glib |