From 3c4a8a1e7e54b7c569a7a795288e7292366d80fe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Apr 2025 07:11:39 +0530 Subject: [PATCH] Resize OS panels on font size change --- glfw/cocoa_window.m | 7 ++++--- glfw/wl_window.c | 5 +++-- kitty/glfw.c | 41 +++++++++++++++++++++++++++-------------- kitty/state.c | 5 +---- kitty/state.h | 2 +- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 7d26292ac..42cf70834 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -1929,9 +1929,10 @@ bool _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig *value) { #define config window->ns.layer_shell.config window->resizable = false; + if (value) config = *value; const bool is_transparent = ![window->ns.object isOpaque]; - int background_blur = value->related.background_blur; - if (!is_transparent || value->related.background_opacity >= 1.f) { background_blur = 0; } + int background_blur = config.related.background_blur; + if (!is_transparent || config.related.background_opacity >= 1.f) { background_blur = 0; } [window->ns.object setBackgroundColor:nil]; _glfwPlatformSetWindowBlur(window, background_blur); window->ns.titlebar_hidden = true; @@ -1940,7 +1941,7 @@ _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig [window->ns.object setHasShadow:false]; [window->ns.object setTitleVisibility:NSWindowTitleHidden]; NSColorSpace *cs = nil; - switch (value->related.color_space) { + switch (config.related.color_space) { case SRGB_COLORSPACE: cs = [NSColorSpace sRGBColorSpace]; break; case DISPLAY_P3_COLORSPACE: cs = [NSColorSpace displayP3ColorSpace]; break; case DEFAULT_COLORSPACE: cs = nil; break; // using deviceRGBColorSpace causes a hang when transitioning to fullscreen diff --git a/glfw/wl_window.c b/glfw/wl_window.c index 505cb3ceb..a2bbd2ca1 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -1741,9 +1741,10 @@ void _glfwPlatformHideWindow(_GLFWwindow* window) debug("Window %llu unmapped\n", window->id); } -bool _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig *value) { +bool +_glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig *value) { if (!is_layer_shell(window)) return false; - window->wl.layer_shell.config = *value; + if (value) window->wl.layer_shell.config = *value; layer_set_properties(window); commit_window_surface(window); return true; diff --git a/kitty/glfw.c b/kitty/glfw.c index efd9e1473..00cd7c957 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -129,6 +129,16 @@ min_size_for_os_window(OSWindow *window, int *min_width, int *min_height) { static void get_window_dpi(GLFWwindow *w, double *x, double *y); static void get_window_content_scale(GLFWwindow *w, float *xscale, float *yscale, double *xdpi, double *ydpi); +static bool +set_layer_shell_config_for(OSWindow *w, GLFWLayerShellConfig *lsc) { + if (lsc) { + lsc->related.background_opacity = w->background_opacity; + lsc->related.background_blur = OPT(background_blur); + lsc->related.color_space = OPT(macos_colorspace); + } + return glfwSetLayerShellConfig(w->handle, lsc); +} + void update_os_window_viewport(OSWindow *window, bool notify_boss) { int w, h, fw, fh; @@ -171,6 +181,7 @@ update_os_window_viewport(OSWindow *window, bool notify_boss) { if (notify_boss) { call_boss(on_window_resize, "KiiO", window->id, window->viewport_width, window->viewport_height, dpi_changed ? Py_True : Py_False); } + if (dpi_changed && window->is_layer_shell && window->handle) set_layer_shell_config_for(window, NULL); } // callbacks {{{ @@ -1197,14 +1208,18 @@ layer_shell_config_from_python(PyObject *p, GLFWLayerShellConfig *ans) { #undef A } -static bool -set_layer_shell_config_for(OSWindow *w, GLFWLayerShellConfig *lsc) { - lsc->related.background_opacity = w->background_opacity; - lsc->related.background_blur = OPT(background_blur); - lsc->related.color_space = OPT(macos_colorspace); - return glfwSetLayerShellConfig(w->handle, lsc); +static void +os_window_update_size_increments(OSWindow *window) { + if (OPT(resize_in_steps)) { + if (window->handle && window->fonts_data) glfwSetWindowSizeIncrements( + window->handle, window->fonts_data->fcm.cell_width, window->fonts_data->fcm.cell_height); + } else { + if (window->handle) glfwSetWindowSizeIncrements( + window->handle, GLFW_DONT_CARE, GLFW_DONT_CARE); + } } + static PyObject* create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) { int x = INT_MIN, y = INT_MIN, window_state = WINDOW_NORMAL, disallow_override_title = 0; @@ -1440,14 +1455,12 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) { } void -os_window_update_size_increments(OSWindow *window) { - if (OPT(resize_in_steps)) { - if (window->handle && window->fonts_data) glfwSetWindowSizeIncrements( - window->handle, window->fonts_data->fcm.cell_width, window->fonts_data->fcm.cell_height); - } else { - if (window->handle) glfwSetWindowSizeIncrements( - window->handle, GLFW_DONT_CARE, GLFW_DONT_CARE); - } +on_os_window_font_size_change(OSWindow *os_window, double new_sz) { + double xdpi, ydpi; float xscale, yscale; + get_os_window_content_scale(os_window, &xdpi, &ydpi, &xscale, &yscale); + os_window->fonts_data = load_fonts_data(new_sz, xdpi, ydpi); + os_window_update_size_increments(os_window); + if (os_window->is_layer_shell) set_layer_shell_config_for(os_window, NULL); } #ifdef __APPLE__ diff --git a/kitty/state.c b/kitty/state.c index 7935a3a46..a75f5f608 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -1082,9 +1082,7 @@ PYWRAP1(os_window_font_size) { PA("K|dp", &os_window_id, &new_sz, &force); WITH_OS_WINDOW(os_window_id) if (new_sz > 0 && (force || new_sz != os_window->fonts_data->font_sz_in_pts)) { - double xdpi, ydpi; float xscale, yscale; - get_os_window_content_scale(os_window, &xdpi, &ydpi, &xscale, &yscale); - os_window->fonts_data = load_fonts_data(new_sz, xdpi, ydpi); + on_os_window_font_size_change(os_window, new_sz); send_prerendered_sprites_for_window(os_window); resize_screen(os_window, os_window->tab_bar_render_data.screen, false); for (size_t ti = 0; ti < os_window->num_tabs; ti++) { @@ -1094,7 +1092,6 @@ PYWRAP1(os_window_font_size) { resize_screen(os_window, w->render_data.screen, true); } } - os_window_update_size_increments(os_window); // On Wayland with CSD title needs to be re-rendered in a different font size if (os_window->window_title && global_state.is_wayland) set_os_window_title(os_window, NULL); } diff --git a/kitty/state.h b/kitty/state.h index 3272374d7..6093840e1 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -419,7 +419,7 @@ void remove_main_loop_timer(id_type timer_id); void update_main_loop_timer(id_type timer_id, monotonic_t interval, bool enabled); void run_main_loop(tick_callback_fun, void*); void stop_main_loop(void); -void os_window_update_size_increments(OSWindow *window); +void on_os_window_font_size_change(OSWindow *window, double new_sz); void set_os_window_title_from_window(Window *w, OSWindow *os_window); void update_os_window_title(OSWindow *os_window); void fake_scroll(Window *w, int amount, bool upwards);