You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
use XDG-Portal to detect night mode
Get dark mode property directly from XDG portal instead of extracting it from the GTK theme. Also we pass the value to GTK, which would normaly be done by libadwaita. This also has the advantage, that we can switch the theme at runtime. but for now the change will only affect newly created Activities.
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <libportal/portal.h>
|
||||||
|
#include <libportal/settings.h>
|
||||||
|
|
||||||
#include "portal-openuri.h"
|
#include "portal-openuri.h"
|
||||||
|
|
||||||
@@ -16,23 +18,41 @@ JNIEXPORT jstring JNICALL Java_android_content_Context_native_1get_1apk_1path(JN
|
|||||||
return _JSTRING(apk_path);
|
return _JSTRING(apk_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void settings_changed_cb(XdpSettings *xdp_settings, gchar *namestpace, gchar *key, GVariant* value, jobject configuration)
|
||||||
|
{
|
||||||
|
JNIEnv *env;
|
||||||
|
if (!strcmp(namestpace, "org.freedesktop.appearance") && !strcmp(key, "color-scheme")) {
|
||||||
|
int color_sheme = g_variant_get_uint32(value);
|
||||||
|
g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", color_sheme == 1, NULL);
|
||||||
|
env = get_jni_env();
|
||||||
|
if (!configuration) {
|
||||||
|
jobject resources = _GET_STATIC_OBJ_FIELD(handle_cache.context.class, "r", "Landroid/content/res/Resources;");
|
||||||
|
configuration = _GET_OBJ_FIELD(resources, "mConfiguration", "Landroid/content/res/Configuration;");
|
||||||
|
}
|
||||||
|
if (color_sheme == 1) // Prefer dark appearance
|
||||||
|
_SET_INT_FIELD(configuration, "uiMode", /*UI_MODE_NIGHT_YES*/ 0x20);
|
||||||
|
else if (color_sheme == 2) // Prefer light appearance
|
||||||
|
_SET_INT_FIELD(configuration, "uiMode", /*UI_MODE_NIGHT_NO*/ 0x10);
|
||||||
|
else // No preference
|
||||||
|
_SET_INT_FIELD(configuration, "uiMode", /*UI_MODE_NIGHT_UNDEFINED*/ 0x00);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static XdpSettings *xdp_settings = NULL;
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_android_content_Context_native_1updateConfig(JNIEnv *env, jclass this, jobject config)
|
JNIEXPORT void JNICALL Java_android_content_Context_native_1updateConfig(JNIEnv *env, jclass this, jobject config)
|
||||||
{
|
{
|
||||||
GtkSettings *settings = gtk_settings_get_default();
|
if (!xdp_settings) {
|
||||||
char *theme_name;
|
XdpPortal *portal = xdp_portal_new();
|
||||||
gboolean prefer_dark = false;
|
xdp_settings = xdp_portal_get_settings(portal);
|
||||||
gboolean theme_name_from_env = false;
|
g_object_unref(portal);
|
||||||
|
g_signal_connect(xdp_settings, "changed", G_CALLBACK(settings_changed_cb), NULL);
|
||||||
theme_name = getenv("GTK_THEME");
|
}
|
||||||
theme_name_from_env = theme_name != NULL;
|
GVariant *color_sheme = xdp_settings_read_value(xdp_settings, "org.freedesktop.appearance", "color-scheme", NULL, NULL);
|
||||||
if (!theme_name_from_env)
|
if (color_sheme) {
|
||||||
g_object_get(settings, "gtk-theme-name", &theme_name, "gtk-application-prefer-dark-theme", &prefer_dark, NULL);
|
settings_changed_cb(xdp_settings, "org.freedesktop.appearance", "color-scheme", color_sheme, config);
|
||||||
bool night_mode = prefer_dark || strcasestr(theme_name, "dark") || strcasestr(theme_name, "black");
|
g_variant_unref(color_sheme);
|
||||||
if (night_mode) {
|
|
||||||
_SET_INT_FIELD(config, "uiMode", /*UI_MODE_NIGHT_YES*/ 0x20);
|
|
||||||
}
|
}
|
||||||
if (!theme_name_from_env)
|
|
||||||
g_free(theme_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_android_content_Context_nativeOpenFile(JNIEnv *env, jclass class, jint fd)
|
JNIEXPORT void JNICALL Java_android_content_Context_nativeOpenFile(JNIEnv *env, jclass class, jint fd)
|
||||||
|
|||||||
Reference in New Issue
Block a user