annotate src/sys/glib/dark_theme.cc @ 350:daa03aa2262d

sys/glib: general cleanup, use C++ principles, add more suffixes
author Paper <paper@paper.us.eu.org>
date Sun, 14 Jul 2024 19:12:40 -0400
parents ac1451035c85
children c844f8bb87ce
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
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 bool IsInDarkTheme() {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
37 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
38 if (!settings)
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 return false;
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
41 {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
42 /* 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
43 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
44 if (!val)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
45 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
47 /* 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
48 gsize size;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
49 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
50 if (!str)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
51 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
53 bool success = !std::strncmp(str, size, "prefer-dark");
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
54
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
55 if (success)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
56 return true;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
57 }
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
58
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
59 {
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
60 /* if that doesn't exist, use the GTK theme and check for some known
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
61 * suffixes. if one is found, return
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
62 *
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
63 * XXX probably better to use case folding here */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
64 static constexpr std::array<std::string_view, 3> suffixes = {
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
65 "-dark", /* Adwaita-dark */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
66 "-Dark", /* Arc-Dark */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
67 "-Darker", /* Arc-Darker */
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
68 };
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
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 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
71 if (!gtk_theme)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
72 return false;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
73
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
74 gsize size;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
75 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
76 if (!str)
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
77 return false;
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
78
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
79 for (const auto& suffix : suffixes) {
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
80 if (size < suffix.size())
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
81 continue;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
82
350
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
83 if (std::equal(str + size - suffix.length(), str + size, suffix.begin(), suffix.end()))
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
84 return true;
daa03aa2262d sys/glib: general cleanup, use C++ principles, add more suffixes
Paper <paper@paper.us.eu.org>
parents: 292
diff changeset
85 }
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
86 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
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 /* welp, we tried */
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
89 return false;
232
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 }
ff0061e75f0f theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
92 } // namespace glib