annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
1 #include "sys/glib/dark_theme.h"
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
2
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
3 #include <cstring>
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <gio/gio.h>
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
5 #include <string_view>
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
6 #include <memory>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
7 #include <array>
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
8 #include <iostream>
292
ac1451035c85 dialog/about: add utf8proc
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
9
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 namespace glib {
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
12 /* deleters */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
13 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
14 struct g_object_del {
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
15 void operator()(T* p) const { ::g_object_unref(p); };
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
16 };
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
17
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
18 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
19 struct g_variant_del {
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
20 void operator()(T* p) const { ::g_variant_unref(p); };
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
21 };
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
22
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
23 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
24 struct g_malloc_del {
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
25 void operator()(T* p) const { ::g_free(p); };
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
26 };
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
27
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
28 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
29 using GObjectPtr = std::unique_ptr<T, g_object_del<T>>;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
30
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
31 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
32 using GVariantPtr = std::unique_ptr<T, g_variant_del<T>>;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
33
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
34 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
35 using GMallocPtr = std::unique_ptr<T, g_malloc_del<T>>;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
36
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
37 /* not really "glib" but GNOME-related enough */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
38 bool IsGTKThemeDark(const std::string_view str) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
39 /* if that doesn't exist, use the GTK theme and check for some known
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
40 * suffixes. if one is found, return
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
41 *
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
42 * XXX probably better to use case folding here */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
43 static constexpr std::array<std::string_view, 3> suffixes = {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
44 "-dark", /* Adwaita-dark */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
45 "-Dark", /* Arc-Dark */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
46 "-Darker", /* Arc-Darker */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
47 };
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
48
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
49 for (const auto& suffix : suffixes) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
50 if (str.size() < suffix.size())
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
51 continue;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
52
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
53 if (std::equal(str.data() + str.size() - suffix.length(), str.data() + str.size(), suffix.begin(), suffix.end()))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
54 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
55 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
56
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
57 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
58 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
59
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 bool IsInDarkTheme() {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
61 GObjectPtr<GSettings> settings(::g_settings_new("org.gnome.desktop.interface"));
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 if (!settings)
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 return false;
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
65 {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
66 /* first attempt to get the colorscheme */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
67 GVariantPtr<GVariant> val(::g_settings_get_value(settings.get(), "color-scheme"));
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
68 if (!val)
258
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
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
71 /* this is free'd upon deconstruction of the GVariantPtr */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
72 gsize size;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
73 const gchar* str = ::g_variant_get_string(val.get(), &size);
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
74 if (!str)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
75 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
77 bool success = !std::strncmp(str, "prefer-dark", size);
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
78
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
79 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
80 return true;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
81 }
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
82
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
83 {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
84
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
85 GVariantPtr<GVariant> gtk_theme(::g_settings_get_value(settings.get(), "gtk-theme"));
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
86 if (!gtk_theme)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
87 return false;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
88
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
89 gsize size;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
90 const gchar* str = ::g_variant_get_string(gtk_theme.get(), &size);
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
91 if (!str)
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
92 return false;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
93
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
94 if (IsGTKThemeDark({str, size}))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
95 return true;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
96 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
97
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
98 /* welp, we tried */
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
99 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
102 } // namespace glib