view src/sys/glib/dark_theme.cc @ 337:a7d4e5107531

dep/animone: REFACTOR ALL THE THINGS 1: animone now has its own syntax divergent from anisthesia, making different platforms actually have their own sections 2: process names in animone are now called `comm' (this will probably break things). this is what its called in bsd/linux so I'm just going to use it everywhere 3: the X11 code now checks for the existence of a UTF-8 window title and passes it if available 4: ANYTHING THATS NOT LINUX IS 100% UNTESTED AND CAN AND WILL BREAK! I still actually need to test the bsd code. to be honest I'm probably going to move all of the bsds into separate files because they're all essentially different operating systems at this point
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 12:51:15 -0400
parents ac1451035c85
children daa03aa2262d
line wrap: on
line source

#include <cstring>
#include <gio/gio.h>
#include <string_view>

/* this file uses the regular gio C interface because I don't
 * see any real benefit to using the C++ bindings. */

namespace glib {

bool IsInDarkTheme() {
	GSettings* settings = ::g_settings_new("org.gnome.desktop.interface");
	if (!settings)
		return false;

	{
		GVariant* val = ::g_settings_get_value(settings, "color-scheme");
		if (!val) {
			::g_object_unref(settings);
			return false;
		}

		const gchar* str = nullptr;
		::g_variant_get(val, "&s", &str); /* should not be freed */
		if (!str) {                       /* how */
			::g_variant_unref(val);
			::g_object_unref(settings);
			return false;
		}

		bool success = !std::strcmp(str, "prefer-dark");

		::g_variant_unref(val);

		if (success)
			return true;
	}

	{
		GVariant* gtk_theme = ::g_settings_get_value(settings, "gtk-theme");
		if (!gtk_theme) {
			::g_object_unref(settings);
			return false;
		}

		const gchar* gtk_theme_str = nullptr;
		::g_variant_get(gtk_theme, "&s", gtk_theme_str);
		if (!gtk_theme_str) {
			::g_variant_unref(gtk_theme);
			::g_object_unref(settings);
			return false;
		}

		static constexpr std::string_view suffix = "-dark";

		size_t gtk_theme_len = strlen(gtk_theme_str);

		if (gtk_theme_len < suffix.length()) {
			::g_variant_unref(gtk_theme);
			::g_object_unref(settings);
			return false;
		}

		bool success = !std::strncmp(gtk_theme_str + gtk_theme_len - suffix.length(), suffix.data(), suffix.length());

		::g_variant_unref(gtk_theme);
		::g_object_unref(settings);

		if (success)
			return true;
	}

	return false;
}

} // namespace glib