From 754288557d373fc0ec6df4fbe213131ef806a610 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 Nov 2024 10:38:13 +0530 Subject: [PATCH] Track initial color scheme preference read on Linux --- glfw/cocoa_window.m | 2 +- glfw/glfw3.h | 5 +++-- glfw/input.c | 4 ++-- glfw/internal.h | 2 +- glfw/linux_desktop_settings.c | 3 ++- kitty/boss.py | 9 ++++++--- kitty/glfw-wrapper.h | 5 +++-- kitty/glfw.c | 6 +++--- 8 files changed, 21 insertions(+), 15 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index bd897a812..f0df3985f 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -955,7 +955,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; GLFWColorScheme new_appearance = glfwGetCurrentSystemColorTheme(); if (new_appearance != appearance) { appearance = new_appearance; - _glfwInputColorScheme(appearance); + _glfwInputColorScheme(appearance, false); } } diff --git a/glfw/glfw3.h b/glfw/glfw3.h index 447080e56..f4c30218c 100644 --- a/glfw/glfw3.h +++ b/glfw/glfw3.h @@ -1429,16 +1429,17 @@ typedef void (* GLFWapplicationclosefun)(int); * * This is the function pointer type for system color theme changes. * @code - * void function_name(int theme_type) + * void function_name(GLFWColorScheme theme_type, bool is_initial_value) * @endcode * * @param[in] theme_type 0 for unknown, 1 for dark and 2 for light + * @param[in] is_initial_value true if this is the initial read of the color theme on systems where it is asynchronous such as Linux * * @sa @ref glfwSetSystemColorThemeChangeCallback * * @ingroup window */ -typedef void (* GLFWsystemcolorthemechangefun)(GLFWColorScheme); +typedef void (* GLFWsystemcolorthemechangefun)(GLFWColorScheme, bool); /*! @brief The function pointer type for window content refresh callbacks. diff --git a/glfw/input.c b/glfw/input.c index 9671c1061..6e8c16ed2 100644 --- a/glfw/input.c +++ b/glfw/input.c @@ -448,9 +448,9 @@ void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value) js->hats[hat] = value; } -void _glfwInputColorScheme(GLFWColorScheme value) { +void _glfwInputColorScheme(GLFWColorScheme value, bool is_initial_value) { _glfwPlatformInputColorScheme(value); - if (_glfw.callbacks.system_color_theme_change) _glfw.callbacks.system_color_theme_change(value); + if (_glfw.callbacks.system_color_theme_change) _glfw.callbacks.system_color_theme_change(value, is_initial_value); } ////////////////////////////////////////////////////////////////////////// diff --git a/glfw/internal.h b/glfw/internal.h index f3d10357a..d30bee73c 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -814,7 +814,7 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods) void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos); void _glfwInputCursorEnter(_GLFWwindow* window, bool entered); int _glfwInputDrop(_GLFWwindow* window, const char *mime, const char *text, size_t sz); -void _glfwInputColorScheme(GLFWColorScheme); +void _glfwInputColorScheme(GLFWColorScheme, bool); void _glfwPlatformInputColorScheme(GLFWColorScheme); void _glfwInputJoystick(_GLFWjoystick* js, int event); void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value); diff --git a/glfw/linux_desktop_settings.c b/glfw/linux_desktop_settings.c index a15b74ac8..d960d4abf 100644 --- a/glfw/linux_desktop_settings.c +++ b/glfw/linux_desktop_settings.c @@ -43,6 +43,7 @@ process_fdo_setting(const char *key, DBusMessageIter *value) { dbus_message_iter_get_basic(value, &val); if (val > 2) val = 0; appearance = val; + _glfwInputColorScheme(appearance, true); } } } @@ -161,7 +162,7 @@ on_color_scheme_change(DBusMessage *message) { if (val > 2) val = 0; if (val != appearance) { appearance = val; - _glfwInputColorScheme(appearance); + _glfwInputColorScheme(appearance, false); } } break; diff --git a/kitty/boss.py b/kitty/boss.py index 246426abb..20bf74ade 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -3058,7 +3058,7 @@ class Boss: if w is not None: output = debug_config(get_options(), self.mappings.global_shortcuts) set_clipboard_string(re.sub(r'\x1b.+?m', '', output)) - output += '\n\x1b[35mThis debug output has been copied to the clipboard\x1b[m' + output += '\n\x1b[35mThis debug output has been copied to the clipboard\x1b[m' # ]]] self.display_scrollback(w, output, title=_('Current kitty options'), report_cursor=False) @ac('misc', 'Discard this event completely ignoring it') @@ -3069,8 +3069,11 @@ class Boss: def sanitize_url_for_dispay_to_user(self, url: str) -> str: return sanitize_url_for_dispay_to_user(url) - def on_system_color_scheme_change(self, appearance: Literal['light', 'dark', 'no_preference']) -> None: - log_error('system color theme changed:', appearance) + def on_system_color_scheme_change(self, appearance: Literal['light', 'dark', 'no_preference'], is_initial_value: bool) -> None: + if is_initial_value: + pass + else: + log_error('system color theme changed:', appearance) @ac('win', ''' Toggle to the tab matching the specified expression diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index 5cae77b64..94bc7ef28 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -1167,16 +1167,17 @@ typedef void (* GLFWapplicationclosefun)(int); * * This is the function pointer type for system color theme changes. * @code - * void function_name(int theme_type) + * void function_name(GLFWColorScheme theme_type, bool is_initial_value) * @endcode * * @param[in] theme_type 0 for unknown, 1 for dark and 2 for light + * @param[in] is_initial_value true if this is the initial read of the color theme on systems where it is asynchronous such as Linux * * @sa @ref glfwSetSystemColorThemeChangeCallback * * @ingroup window */ -typedef void (* GLFWsystemcolorthemechangefun)(GLFWColorScheme); +typedef void (* GLFWsystemcolorthemechangefun)(GLFWColorScheme, bool); /*! @brief The function pointer type for window content refresh callbacks. diff --git a/kitty/glfw.c b/kitty/glfw.c index 24391efaa..21ead3f1a 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -47,15 +47,15 @@ get_platform_dependent_config_values(void *glfw_window) { } static void -on_system_color_scheme_change(GLFWColorScheme appearance) { +on_system_color_scheme_change(GLFWColorScheme appearance, bool is_initial_value) { const char *which = NULL; switch (appearance) { case GLFW_COLOR_SCHEME_NO_PREFERENCE: which = "no_preference"; break; case GLFW_COLOR_SCHEME_DARK: which = "dark"; break; case GLFW_COLOR_SCHEME_LIGHT: which = "light"; break; } - debug("system color-scheme changed to: %s\n", which); - call_boss(on_system_color_scheme_change, "s", which); + debug("system color-scheme changed to: %s is_initial_value: %d\n", which, is_initial_value); + call_boss(on_system_color_scheme_change, "sO", which, is_initial_value ? Py_True : Py_False); } static void