diff --git a/docs/changelog.rst b/docs/changelog.rst index 38a27bf9e..b5e234c13 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -135,6 +135,11 @@ Detailed list of changes - Fix a crash when the VS15 variation selector is received with the cursor at the start of a line, after a wide emoji that was soft wrapped from the previous line +- :doc:`Custom shaders `: Fix groups started or stopped by + OS window focus changes ending up in the wrong state when focus changes + more than once before the window is next drawn, for example, when the window + is hidden and shown again (:iss:`10871`) + - Unicode input kitten: Fix searching by name not finding some characters such as the pound sign (:iss:`10862`) - choose-files kitten: Sanitize file names, archive member names, ebook metadata and previewer output everywhere they are displayed so they cannot inject escape codes into the terminal diff --git a/docs/custom-shaders.rst b/docs/custom-shaders.rst index 4ee121a84..94a18ef17 100644 --- a/docs/custom-shaders.rst +++ b/docs/custom-shaders.rst @@ -709,3 +709,5 @@ started by another program as with ``git log``, is running. Note that if both a start event and a focus-out event fire in the same frame (for example, a mouse click that simultaneously moves focus away), the focus-out takes priority and the animation is not (re-)started. +When focus changes more than once before the OS window is next drawn, for +example, while it is hidden, only the last change counts. diff --git a/kitty/glfw.c b/kitty/glfw.c index 9933e87a1..c2661da51 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -968,10 +968,7 @@ window_focus_callback(GLFWwindow *w, int focused) { debug_input("\x1b[35mon_focus_change\x1b[m: window id: 0x%llu focused: %d\n", osw->id, focused); bool focus_changed = osw->is_focused != focused; osw->is_focused = focused ? true : false; - if (focus_changed) { - if (focused) osw->shader_anim_event_registry |= (1u << SHADER_ANIM_EVENT_OS_WINDOW_FOCUS_IN) | (1u << SHADER_ANIM_EVENT_WINDOW_FOCUS_IN); - else osw->shader_anim_event_registry |= (1u << SHADER_ANIM_EVENT_OS_WINDOW_FOCUS_OUT) | (1u << SHADER_ANIM_EVENT_WINDOW_FOCUS_OUT); - } + if (focus_changed) record_os_window_focus_change_for_shaders(osw, focused); monotonic_t now = monotonic(); id_type wid = osw->id; if (focused) { @@ -2594,7 +2591,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) { glfwSetDropEventCallback(glfw_window, drop_dest_callback); monotonic_t now = monotonic(); w->is_focused = true; - w->shader_anim_event_registry |= (1u << SHADER_ANIM_EVENT_OS_WINDOW_FOCUS_IN) | (1u << SHADER_ANIM_EVENT_WINDOW_FOCUS_IN); + record_os_window_focus_change_for_shaders(w, true); w->cursor_blink_zero_time = now; w->last_mouse_activity_at = now; w->mouse_activate_deadline = -1; diff --git a/kitty/shaders.c b/kitty/shaders.c index df91085c4..72c50ac26 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -800,7 +800,7 @@ init_custom_programs(void) { // animations triggered by os-window-focus-in start on the first render // after shader compilation (the focus-in event fired before shaders were // compiled and was consumed without being processed). - if (osw->is_focused) { osw->shader_anim_event_registry |= (1u << SHADER_ANIM_EVENT_OS_WINDOW_FOCUS_IN) | (1u << SHADER_ANIM_EVENT_WINDOW_FOCUS_IN); } + if (osw->is_focused) record_os_window_focus_change_for_shaders(osw, true); } debug_rendering( "Custom shaders: %zu program(s), %zu group(s), textures_mask=0x%x, initially_active=%d\n", @@ -3981,7 +3981,27 @@ pysimulate_custom_shader_animation(PyObject *self UNUSED, PyObject *args) { Py_CLEAR(ans); break; } - update_custom_shader_animations(parse_anim_event_sequence(names, &custom_shaders.end), now, osw); + // focus:in and focus:out change OS window focus, as the GLFW focus + // callback does, before the tick's events are consumed, the way + // prepare_to_render_os_window() does + for (Py_ssize_t n = 0; n < PySequence_Fast_GET_SIZE(names); n++) { + PyObject *item = PySequence_Fast_GET_ITEM(names, n); + if (!PyUnicode_Check(item)) continue; + const char *name = PyUnicode_AsUTF8(item); + if (!name) { + Py_CLEAR(ans); + break; + } + const bool focus_in = strcmp(name, "focus:in") == 0; + if (focus_in || strcmp(name, "focus:out") == 0) { + const bool focused = focus_in; + if (osw->is_focused != focused) record_os_window_focus_change_for_shaders(osw, focused); + osw->is_focused = focused; + } + } + if (!ans) break; + update_custom_shader_animations(parse_anim_event_sequence(names, &custom_shaders.end) | osw->shader_anim_event_registry, now, osw); + osw->shader_anim_event_registry = 0; RAII_PyObject(states, PyTuple_New(custom_shaders.end.num_groups)); if (!states) { Py_CLEAR(ans); diff --git a/kitty/state.h b/kitty/state.h index ad32c3fbe..e038bdf59 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -881,6 +881,19 @@ bool screen_needs_rendering_in_layers(OSWindow *os_window, Window *w, Screen *sc void setup_os_window_for_rendering(OSWindow *, Tab *, Window *, bool, monotonic_t); monotonic_t update_custom_shader_animations(ShaderAnimEventMask event_mask, monotonic_t now, OSWindow *os_window); void init_shader_animation_state(OSWindow *os_window); + +// Records an OS window focus change for the custom shader animation state +// machine. The pending events are only consumed when the OS window is next +// rendered, which can be much later, for instance, when the window is hidden +// or occluded. So a focus change replaces any pending opposite focus change +// rather than accumulating with it, as both together would be indistinguishable +// from simultaneous events, where focus-out wins. +static inline void +record_os_window_focus_change_for_shaders(OSWindow *osw, bool focused) { + const ShaderAnimEventMask in = (1u << SHADER_ANIM_EVENT_OS_WINDOW_FOCUS_IN) | (1u << SHADER_ANIM_EVENT_WINDOW_FOCUS_IN); + const ShaderAnimEventMask out = (1u << SHADER_ANIM_EVENT_OS_WINDOW_FOCUS_OUT) | (1u << SHADER_ANIM_EVENT_WINDOW_FOCUS_OUT); + osw->shader_anim_event_registry = (osw->shader_anim_event_registry & ~(focused ? out : in)) | (focused ? in : out); +} bool custom_shader_needs_render(const ShaderAnimState *before, const ShaderAnimState *after, ShaderAnimEventMask event_mask, monotonic_t now); void map_pointer_position(const OSWindow *os_window, double *x, double *y); void set_os_window_mouse_position(OSWindow *window, double x, double y); diff --git a/kitty_tests/slang.py b/kitty_tests/slang.py index 34c96e92d..cdf1b124d 100644 --- a/kitty_tests/slang.py +++ b/kitty_tests/slang.py @@ -168,6 +168,38 @@ class TestSlang(BaseTest): ] self.ae(simulate_custom_shader_animation(p, ticks), expected) + def test_custom_shader_focus_changes_between_frames(self): + # https://github.com/kovidgoyal/kitty/issues/10871 focus changes that + # happen before the OS window is next rendered, for instance, while it + # is hidden, must leave groups in the state for the final focus, rather + # than look like simultaneous focus-out and focus-in events + from kitty.fast_data_types import simulate_custom_shader_animation + + lines = [] + for spec in ( + 'animation_start os-window-focus-in\nanimation_stop os-window-focus-out', + 'animation_start os-window-focus-out\nanimation_stop os-window-focus-in', + 'animation_start window-focus-in|user-activity\nanimation_stop window-focus-out', + ): + lines.append('startgroup') + lines.extend(spec.splitlines()) + lines.extend(('animation_step 0', 'shaders sample', 'endgroup')) + p = parse_pipeline_definition(lines, 'test') + focused, unfocused = (True, False, True), (False, True, False) + ticks: list[tuple[str, ...]] = [ + ('focus:in',), + ('focus:out', 'focus:in'), # hidden and shown again + ('focus:in', 'focus:out'), # briefly focused while hidden + ('focus:in', 'focus:out', 'focus:in'), + ('focus:out', 'focus:in', 'focus:out'), + ('focus:in',), + # A start event in the same frame as focus out is a hard stop + ('focus:out', 'user-activity'), + ('user-activity',), + ] + expected = [focused, focused, unfocused, focused, unfocused, focused, unfocused, (False, True, True)] + self.ae(simulate_custom_shader_animation(p, ticks), expected) + def test_custom_shader_animation_progress(self): from kitty.fast_data_types import simulate_custom_shader_animation