From b1bd581065dbe89542fbb9fda6b5b1ef35193bf2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Feb 2019 19:42:24 +0530 Subject: [PATCH] Make live resize code a little cleaner --- kitty/child-monitor.c | 16 +++++++++++----- kitty/glfw.c | 20 ++++++++++++-------- kitty/state.h | 11 +++++++++-- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 94823fefe..32efbbcdd 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -807,16 +807,22 @@ process_pending_resizes(double now) { global_state.has_pending_resizes = false; for (size_t i = 0; i < global_state.num_os_windows; i++) { OSWindow *w = global_state.os_windows + i; - if (w->has_pending_resizes) { - if (w->has_live_resize_information) { - if (!w->live_resize_in_progress) update_os_window_viewport(w, true); + if (w->live_resize.in_progress) { + bool update_viewport = false; + if (w->live_resize.from_os_notification) { + if (w->live_resize.os_says_resize_complete || (now - w->live_resize.last_resize_event_at) > 1) update_viewport = true; } else { - if (now - w->last_resize_event_at >= RESIZE_DEBOUNCE_TIME) update_os_window_viewport(w, true); + if (now - w->live_resize.last_resize_event_at >= RESIZE_DEBOUNCE_TIME) update_viewport = true; else { global_state.has_pending_resizes = true; - set_maximum_wait(RESIZE_DEBOUNCE_TIME - now + w->last_resize_event_at); + set_maximum_wait(RESIZE_DEBOUNCE_TIME - now + w->live_resize.last_resize_event_at); } } + if (update_viewport) { + static const LiveResizeInfo empty = {0}; + update_os_window_viewport(w, true); + w->live_resize = empty; + } } } } diff --git a/kitty/glfw.c b/kitty/glfw.c index 09b488524..6e7a1cdff 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -44,7 +44,6 @@ update_os_window_viewport(OSWindow *window, bool notify_boss) { bool dpi_changed = (xr != 0.0 && xr != window->viewport_x_ratio) || (yr != 0.0 && yr != window->viewport_y_ratio) || (xdpi != window->logical_dpi_x) || (ydpi != window->logical_dpi_y); window->viewport_size_dirty = true; - window->has_pending_resizes = false; window->viewport_width = MAX(window->viewport_width, 100); window->viewport_height = MAX(window->viewport_height, 100); window->window_width = MAX(w, 100); @@ -131,9 +130,13 @@ window_iconify_callback(GLFWwindow *window, int iconified UNUSED) { static void live_resize_callback(GLFWwindow *w, bool started) { if (!set_callback_window(w)) return; - global_state.callback_os_window->has_live_resize_information = true; - global_state.callback_os_window->live_resize_in_progress = started; - if (!started) request_tick_callback(); + global_state.callback_os_window->live_resize.from_os_notification = true; + global_state.callback_os_window->live_resize.in_progress = true; + global_state.has_pending_resizes = true; + if (!started) { + global_state.callback_os_window->live_resize.os_says_resize_complete = true; + request_tick_callback(); + } global_state.callback_os_window = NULL; } @@ -142,8 +145,9 @@ framebuffer_size_callback(GLFWwindow *w, int width, int height) { if (!set_callback_window(w)) return; if (width >= min_width && height >= min_height) { OSWindow *window = global_state.callback_os_window; - window->has_pending_resizes = true; global_state.has_pending_resizes = true; - window->last_resize_event_at = monotonic(); + global_state.has_pending_resizes = true; + window->live_resize.in_progress = true; + window->live_resize.last_resize_event_at = monotonic(); #ifdef __APPLE__ // On cocoa request_tick_callback() does not work while the window // is being resized, because that happens in a sub-loop. With semi-transparent @@ -166,8 +170,8 @@ dpi_change_callback(GLFWwindow *w, float x_scale UNUSED, float y_scale UNUSED) { // Ensure update_os_window_viewport() is called in the near future, it will // take care of DPI changes. OSWindow *window = global_state.callback_os_window; - window->has_pending_resizes = true; global_state.has_pending_resizes = true; - window->last_resize_event_at = monotonic(); + window->live_resize.in_progress = true; global_state.has_pending_resizes = true; + window->live_resize.last_resize_event_at = monotonic(); global_state.callback_os_window = NULL; request_tick_callback(); } diff --git a/kitty/state.h b/kitty/state.h index 1ff9f9410..7a6b7395e 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -106,6 +106,14 @@ typedef struct { enum RENDER_STATE { RENDER_FRAME_NOT_REQUESTED, RENDER_FRAME_REQUESTED, RENDER_FRAME_READY }; +typedef struct { + double last_resize_event_at; + bool in_progress; + bool from_os_notification; + bool os_says_resize_complete; +} LiveResizeInfo; + + typedef struct { void *handle; id_type id; @@ -125,8 +133,7 @@ typedef struct { PyObject *window_title; bool is_key_pressed[MAX_KEY_COUNT]; bool viewport_size_dirty; - double last_resize_event_at; - bool has_live_resize_information, live_resize_in_progress; + LiveResizeInfo live_resize; bool has_pending_resizes, is_semi_transparent, shown_once, is_damaged; uint32_t offscreen_texture_id; unsigned int clear_count;