annotate src/sys/glib/dark_theme.cc @ 364:99c961c91809

core: refactor out byte stream into its own file easy dubs
author Paper <paper@paper.us.eu.org>
date Tue, 16 Jul 2024 21:15:59 -0400
parents c844f8bb87ce
children
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>
292
ac1451035c85 dialog/about: add utf8proc
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
8
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 namespace glib {
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
11 /* deleters */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
12 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
13 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
14 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
15 };
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 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
18 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
19 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
20 };
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 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
23 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
24 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
25 };
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 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
28 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
29
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
30 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
31 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
32
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
33 template<typename T>
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
34 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
35
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
36 /* not really "glib" but GNOME-related enough */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
37 bool IsGTKThemeDark(const std::string_view str) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
38 /* 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
39 * suffixes. if one is found, return
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
40 *
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
41 * XXX probably better to use case folding here */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
42 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
43 "-dark", /* Adwaita-dark */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
44 "-Dark", /* Arc-Dark */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
45 "-Darker", /* Arc-Darker */
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
46 };
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 for (const auto& suffix : suffixes) {
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
49 if (str.size() < suffix.size())
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
50 continue;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
51
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
52 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
53 return true;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
54 }
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 return false;
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
57 }
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
58
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 bool IsInDarkTheme() {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
60 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
61 if (!settings)
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 return false;
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
64 {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
65 /* 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
66 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
67 if (!val)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
68 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
70 /* 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
71 gsize size;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
72 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
73 if (!str)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
74 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
76 bool success = !std::strncmp(str, "prefer-dark", size);
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
77
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
78 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
79 return true;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
80 }
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
81
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
82 {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
83
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
84 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
85 if (!gtk_theme)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
86 return false;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
87
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
88 gsize size;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
89 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
90 if (!str)
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
91 return false;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
92
351
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
93 if (IsGTKThemeDark({str, size}))
c844f8bb87ce gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents: 350
diff changeset
94 return true;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
95 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
96
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
97 /* welp, we tried */
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
98 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 }
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
101 } // namespace glib