comparison src/sys/glib/dark_theme.cc @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents c130f47f6f48
children ac1451035c85
comparison
equal deleted inserted replaced
257:699a20c57dc8 258:862d0d8619f6
1 #include <cstring>
1 #include <gio/gio.h> 2 #include <gio/gio.h>
2 #include <cstring>
3 #include <string_view> 3 #include <string_view>
4 4
5 namespace glib { 5 namespace glib {
6 6
7 bool IsInDarkTheme() { 7 bool IsInDarkTheme() {
8 bool success = false;
9
10 GSettings* settings = ::g_settings_new("org.gnome.desktop.interface"); 8 GSettings* settings = ::g_settings_new("org.gnome.desktop.interface");
11 if (!settings) 9 if (!settings)
12 return false; 10 return false;
13 11
14 GVariant* val = ::g_settings_get_value(settings, "color-scheme"); 12 {
15 if (!val) 13 GVariant* val = ::g_settings_get_value(settings, "color-scheme");
16 return false; 14 if (!val) {
15 ::g_object_unref(settings);
16 return false;
17 }
17 18
18 const gchar* str; 19 const gchar* str = nullptr;
19 ::g_variant_get(val, "&s", &str); /* should not be freed */ 20 ::g_variant_get(val, "&s", &str); /* should not be freed */
20 if (!str) /* how */ 21 if (!str) { /* how */
21 return false; 22 ::g_variant_unref(val);
23 ::g_object_unref(settings);
24 return false;
25 }
22 26
23 success |= !std::strcmp(str, "prefer-dark"); 27 bool success = !std::strcmp(str, "prefer-dark");
24 28
25 ::g_variant_unref(val); 29 ::g_variant_unref(val);
26 30
27 if (success) { 31 if (success)
28 ::g_object_unref(settings); 32 return true;
29 return success;
30 } 33 }
31 34
32 GVariant* gtk_theme = ::g_settings_get_value(settings, "gtk-theme"); 35 {
33 if (!gtk_theme) 36 GVariant* gtk_theme = ::g_settings_get_value(settings, "gtk-theme");
34 return false; 37 if (!gtk_theme) {
38 ::g_object_unref(settings);
39 return false;
40 }
35 41
36 const gchar* gtk_theme_str; 42 const gchar* gtk_theme_str = nullptr;
37 ::g_variant_get(gtk_theme, "&s", gtk_theme_str); 43 ::g_variant_get(gtk_theme, "&s", gtk_theme_str);
38 if (!gtk_theme_str) 44 if (!gtk_theme_str) {
39 return false; 45 ::g_variant_unref(gtk_theme);
46 ::g_object_unref(settings);
47 return false;
48 }
40 49
41 static constexpr std::string_view suffix = "-dark"; 50 static constexpr std::string_view suffix = "-dark";
42 51
43 size_t gtk_theme_len = strlen(gtk_theme_str); 52 size_t gtk_theme_len = strlen(gtk_theme_str);
44 53
45 success |= !std::strncmp(gtk_theme_str + gtk_theme_len - suffix.length(), suffix.data(), suffix.length()); 54 if (gtk_theme_len < suffix.length()) {
55 ::g_variant_unref(gtk_theme);
56 ::g_object_unref(settings);
57 return false;
58 }
46 59
47 ::g_object_unref(settings); 60 bool success = !std::strncmp(gtk_theme_str + gtk_theme_len - suffix.length(), suffix.data(), suffix.length());
48 return success; 61
62 ::g_variant_unref(gtk_theme);
63 ::g_object_unref(settings);
64
65 if (success)
66 return true;
67 }
68
69 return false;
49 } 70 }
50 71
51 } 72 } // namespace glib