Custom shaders: Fix focus driven groups ending in the wrong state after hide/unhide

Shader animation events are accumulated per OS window and only consumed
when the window is next rendered. A focus-out followed by a focus-in with
no rendered frame in between, as happens when the window is hidden and
shown again, or a brief focus steal within one repaint interval, left both
bits pending. The state machine then treated them as simultaneous, so the
focus-out hard stop prevented os-window-focus-in groups from starting and
groups started by focus-out were turned on while focused.

Now a focus change replaces any pending opposite focus change, so only the
final focus state is reported. Genuinely simultaneous events, such as a
click that moves focus away, still give focus-out priority.

Closes #10871
This commit is contained in:
Kovid Goyal 2026-11-15 08:39:25 +05:30
parent 64a02b4061
commit 87d06354dd
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
6 changed files with 76 additions and 7 deletions

View file

@ -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 <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

View file

@ -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.

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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