Mercurial > minori
annotate src/sys/glib/dark_theme.cc @ 367:8d45d892be88 default tip
*: instead of pugixml, use Qt XML features
this means we have one extra Qt dependency though...
author | Paper <paper@tflc.us> |
---|---|
date | Sun, 17 Nov 2024 22:55:47 -0500 |
parents | 99c961c91809 |
children |
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 | 3 #include <cstring> |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 #include <gio/gio.h> |
250 | 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 | 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 | 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 | 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 | 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 | 77 |
78 if (success) | |
79 return true; | |
250 | 80 } |
81 | |
258 | 82 { |
250 | 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 | 86 return false; |
250 | 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 | 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 | 95 } |
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 | 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 | 101 } // namespace glib |