From 8d275ddb3105a955b26f9b2a61d07256d25ec1c7 Mon Sep 17 00:00:00 2001 From: deepend Date: Sat, 17 Jan 2026 22:52:32 -0700 Subject: [PATCH] - Added dark mode mode constants, config storage as an integer, and a helper to resolve Auto/Dark/Light using system preferences where available. - Replaced the dark mode checkbox with an Auto/Dark/Light color mode selector and ensured palette edits use the resolved mode. - Applied the resolved color mode consistently across palette saving and GTK styling in the user list and channel tree/theme application paths. --- src/common/cfgfiles.c | 2 +- src/common/zoitechat.h | 4 +++ src/fe-gtk/chanview.c | 2 +- src/fe-gtk/fe-gtk.c | 55 +++++++++++++++++++++++++++++++++++++++++- src/fe-gtk/fe-gtk.h | 3 +++ src/fe-gtk/maingui.c | 2 +- src/fe-gtk/palette.c | 9 ++++--- 7 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/common/cfgfiles.c b/src/common/cfgfiles.c index f12b38e3..9787f95e 100644 --- a/src/common/cfgfiles.c +++ b/src/common/cfgfiles.c @@ -437,7 +437,7 @@ const struct prefs vars[] = {"gui_tab_dialogs", P_OFFINT (hex_gui_tab_dialogs), TYPE_BOOL}, {"gui_tab_dots", P_OFFINT (hex_gui_tab_dots), TYPE_BOOL}, {"gui_tab_icons", P_OFFINT (hex_gui_tab_icons), TYPE_BOOL}, - {"gui_dark_mode", P_OFFINT (hex_gui_dark_mode), TYPE_BOOL}, + {"gui_dark_mode", P_OFFINT (hex_gui_dark_mode), TYPE_INT}, {"gui_tab_layout", P_OFFINT (hex_gui_tab_layout), TYPE_INT}, {"gui_tab_middleclose", P_OFFINT (hex_gui_tab_middleclose), TYPE_BOOL}, {"gui_tab_newtofront", P_OFFINT (hex_gui_tab_newtofront), TYPE_INT}, diff --git a/src/common/zoitechat.h b/src/common/zoitechat.h index 893beea5..f8f0b58f 100644 --- a/src/common/zoitechat.h +++ b/src/common/zoitechat.h @@ -83,6 +83,10 @@ gboolean zoitechat_import_theme (const char *path, GError **error); #define USERNAMELEN 10 #define HIDDEN_CHAR 8 /* invisible character for xtext */ +#define ZOITECHAT_DARK_MODE_AUTO 0 +#define ZOITECHAT_DARK_MODE_DARK 1 +#define ZOITECHAT_DARK_MODE_LIGHT 2 + struct nbexec { int myfd; diff --git a/src/fe-gtk/chanview.c b/src/fe-gtk/chanview.c index ea5113b7..4dc6547a 100644 --- a/src/fe-gtk/chanview.c +++ b/src/fe-gtk/chanview.c @@ -122,7 +122,7 @@ chanview_apply_theme (chanview *cv) return; w = GTK_WIDGET (tv->tree); - if (prefs.hex_gui_dark_mode) + if (fe_dark_mode_is_enabled ()) { gtk_widget_modify_base (w, GTK_STATE_NORMAL, &colors[COL_BG]); gtk_widget_modify_text (w, GTK_STATE_NORMAL, &colors[COL_FG]); diff --git a/src/fe-gtk/fe-gtk.c b/src/fe-gtk/fe-gtk.c index 374720bb..5e4b2306 100644 --- a/src/fe-gtk/fe-gtk.c +++ b/src/fe-gtk/fe-gtk.c @@ -270,6 +270,59 @@ static const char adwaita_workaround_rc[] = "}" "widget \"*.zoitechat-inputbox\" style \"zoitechat-input-workaround\""; +static gboolean +fe_system_prefers_dark (void) +{ + GtkSettings *settings = gtk_settings_get_default (); + gboolean prefer_dark = FALSE; + char *theme_name = NULL; + + if (!settings) + return FALSE; + + if (g_object_class_find_property (G_OBJECT_GET_CLASS (settings), + "gtk-application-prefer-dark-theme")) + { + g_object_get (settings, "gtk-application-prefer-dark-theme", &prefer_dark, NULL); + } + + if (!prefer_dark) + { + g_object_get (settings, "gtk-theme-name", &theme_name, NULL); + if (theme_name) + { + char *lower = g_ascii_strdown (theme_name, -1); + if (g_str_has_suffix (lower, "-dark") || g_strrstr (lower, "dark")) + prefer_dark = TRUE; + g_free (lower); + g_free (theme_name); + } + } + + return prefer_dark; +} + +gboolean +fe_dark_mode_is_enabled_for (unsigned int mode) +{ + switch (mode) + { + case ZOITECHAT_DARK_MODE_DARK: + return TRUE; + case ZOITECHAT_DARK_MODE_LIGHT: + return FALSE; + case ZOITECHAT_DARK_MODE_AUTO: + default: + return fe_system_prefers_dark (); + } +} + +gboolean +fe_dark_mode_is_enabled (void) +{ + return fe_dark_mode_is_enabled_for (prefs.hex_gui_dark_mode); +} + GtkStyle * create_input_style (GtkStyle *style) { @@ -317,7 +370,7 @@ void fe_init (void) { palette_load (); - palette_apply_dark_mode (prefs.hex_gui_dark_mode); + palette_apply_dark_mode (fe_dark_mode_is_enabled ()); key_init (); pixmaps_init (); diff --git a/src/fe-gtk/fe-gtk.h b/src/fe-gtk/fe-gtk.h index f8617517..a0bf7c26 100644 --- a/src/fe-gtk/fe-gtk.h +++ b/src/fe-gtk/fe-gtk.h @@ -182,6 +182,9 @@ typedef struct session_gui extern cairo_surface_t *channelwin_pix; extern cairo_surface_t *dialogwin_pix; +gboolean fe_dark_mode_is_enabled (void); +gboolean fe_dark_mode_is_enabled_for (unsigned int mode); + #define SPELL_ENTRY_GET_TEXT(e) ((char *)(gtk_entry_get_text (GTK_ENTRY(e)))) #define SPELL_ENTRY_SET_TEXT(e,txt) gtk_entry_set_text(GTK_ENTRY(e),txt) #define SPELL_ENTRY_SET_EDITABLE(e,v) gtk_editable_set_editable(GTK_EDITABLE(e),v) diff --git a/src/fe-gtk/maingui.c b/src/fe-gtk/maingui.c index 31f92f26..d410a940 100644 --- a/src/fe-gtk/maingui.c +++ b/src/fe-gtk/maingui.c @@ -2650,7 +2650,7 @@ mg_create_userlist (session_gui *gui, GtkWidget *box) * - When "Dark mode" is enabled, we also force the user list to use the * palette colors so it doesn't stay blindingly white on GTK2 themes. */ - if (prefs.hex_gui_ulist_style || prefs.hex_gui_dark_mode) + if (prefs.hex_gui_ulist_style || fe_dark_mode_is_enabled ()) { gtk_widget_modify_base (ulist, GTK_STATE_NORMAL, &colors[COL_BG]); gtk_widget_modify_text (ulist, GTK_STATE_NORMAL, &colors[COL_FG]); diff --git a/src/fe-gtk/palette.c b/src/fe-gtk/palette.c index e35f607f..f2af5bd1 100644 --- a/src/fe-gtk/palette.c +++ b/src/fe-gtk/palette.c @@ -311,20 +311,21 @@ palette_save (void) char prefname[256]; const GdkColor *lightpal = colors; const GdkColor *darkpal = NULL; + gboolean dark_mode_active = fe_dark_mode_is_enabled (); /* If we're currently in dark mode, keep colors.conf's legacy keys as the user's light palette. */ - if (prefs.hex_gui_dark_mode && user_colors_valid) + if (dark_mode_active && user_colors_valid) lightpal = user_colors; /* If we're currently in light mode, ensure the snapshot stays in sync. */ - if (!prefs.hex_gui_dark_mode) + if (!dark_mode_active) { memcpy (user_colors, colors, sizeof (user_colors)); user_colors_valid = TRUE; } /* If dark mode is enabled but we haven't snapshotted a custom dark palette yet, capture it now. */ - if (prefs.hex_gui_dark_mode && !dark_user_colors_valid) + if (dark_mode_active && !dark_user_colors_valid) { memcpy (dark_user_colors, colors, sizeof (dark_user_colors)); dark_user_colors_valid = TRUE; @@ -332,7 +333,7 @@ palette_save (void) if (dark_user_colors_valid) darkpal = dark_user_colors; - else if (prefs.hex_gui_dark_mode) + else if (dark_mode_active) darkpal = colors; /* current dark palette (likely defaults) */ fh = zoitechat_open_file ("colors.conf", O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE);