comparison src/sys/glib/dark_theme.cc @ 351:c844f8bb87ce

gui/theme: add xsettings backend this also adds newly-necessary endianness methods in core/endian.h which just so happen to be constexpr as well
author Paper <paper@paper.us.eu.org>
date Sun, 14 Jul 2024 23:23:56 -0400
parents daa03aa2262d
children 99c961c91809
comparison
equal deleted inserted replaced
350:daa03aa2262d 351:c844f8bb87ce
3 #include <cstring> 3 #include <cstring>
4 #include <gio/gio.h> 4 #include <gio/gio.h>
5 #include <string_view> 5 #include <string_view>
6 #include <memory> 6 #include <memory>
7 #include <array> 7 #include <array>
8 #include <iostream>
8 9
9 namespace glib { 10 namespace glib {
10 11
11 /* deleters */ 12 /* deleters */
12 template<typename T> 13 template<typename T>
31 using GVariantPtr = std::unique_ptr<T, g_variant_del<T>>; 32 using GVariantPtr = std::unique_ptr<T, g_variant_del<T>>;
32 33
33 template<typename T> 34 template<typename T>
34 using GMallocPtr = std::unique_ptr<T, g_malloc_del<T>>; 35 using GMallocPtr = std::unique_ptr<T, g_malloc_del<T>>;
35 36
37 /* not really "glib" but GNOME-related enough */
38 bool IsGTKThemeDark(const std::string_view str) {
39 /* if that doesn't exist, use the GTK theme and check for some known
40 * suffixes. if one is found, return
41 *
42 * XXX probably better to use case folding here */
43 static constexpr std::array<std::string_view, 3> suffixes = {
44 "-dark", /* Adwaita-dark */
45 "-Dark", /* Arc-Dark */
46 "-Darker", /* Arc-Darker */
47 };
48
49 for (const auto& suffix : suffixes) {
50 if (str.size() < suffix.size())
51 continue;
52
53 if (std::equal(str.data() + str.size() - suffix.length(), str.data() + str.size(), suffix.begin(), suffix.end()))
54 return true;
55 }
56
57 return false;
58 }
59
36 bool IsInDarkTheme() { 60 bool IsInDarkTheme() {
37 GObjectPtr<GSettings> settings(::g_settings_new("org.gnome.desktop.interface")); 61 GObjectPtr<GSettings> settings(::g_settings_new("org.gnome.desktop.interface"));
38 if (!settings) 62 if (!settings)
39 return false; 63 return false;
40 64
48 gsize size; 72 gsize size;
49 const gchar* str = ::g_variant_get_string(val.get(), &size); 73 const gchar* str = ::g_variant_get_string(val.get(), &size);
50 if (!str) 74 if (!str)
51 return false; 75 return false;
52 76
53 bool success = !std::strncmp(str, size, "prefer-dark"); 77 bool success = !std::strncmp(str, "prefer-dark", size);
54 78
55 if (success) 79 if (success)
56 return true; 80 return true;
57 } 81 }
58 82
59 { 83 {
60 /* if that doesn't exist, use the GTK theme and check for some known
61 * suffixes. if one is found, return
62 *
63 * XXX probably better to use case folding here */
64 static constexpr std::array<std::string_view, 3> suffixes = {
65 "-dark", /* Adwaita-dark */
66 "-Dark", /* Arc-Dark */
67 "-Darker", /* Arc-Darker */
68 };
69 84
70 GVariantPtr<GVariant> gtk_theme(::g_settings_get_value(settings.get(), "gtk-theme")); 85 GVariantPtr<GVariant> gtk_theme(::g_settings_get_value(settings.get(), "gtk-theme"));
71 if (!gtk_theme) 86 if (!gtk_theme)
72 return false; 87 return false;
73 88
74 gsize size; 89 gsize size;
75 const gchar* str = ::g_variant_get_string(gtk_theme.get(), &size); 90 const gchar* str = ::g_variant_get_string(gtk_theme.get(), &size);
76 if (!str) 91 if (!str)
77 return false; 92 return false;
78 93
79 for (const auto& suffix : suffixes) { 94 if (IsGTKThemeDark({str, size}))
80 if (size < suffix.size()) 95 return true;
81 continue;
82
83 if (std::equal(str + size - suffix.length(), str + size, suffix.begin(), suffix.end()))
84 return true;
85 }
86 } 96 }
87 97
88 /* welp, we tried */ 98 /* welp, we tried */
89 return false; 99 return false;
90 } 100 }