From 8f19a9ce9789e3dd16327ab08a27b2d7a54915da Mon Sep 17 00:00:00 2001 From: Rick Choi Date: Sun, 27 Oct 2024 13:55:53 +0900 Subject: [PATCH] fade out cursor trail where cursor is hidden --- kitty/cursor_trail.c | 15 +++++++++++++++ kitty/shaders.c | 5 ++++- kitty/state.h | 1 + kitty/trail_fragment.glsl | 3 ++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/kitty/cursor_trail.c b/kitty/cursor_trail.c index 21cb3a1c2..8f4474c3d 100644 --- a/kitty/cursor_trail.c +++ b/kitty/cursor_trail.c @@ -123,6 +123,21 @@ update_cursor_trail_state(CursorTrail *ct, Window *w, monotonic_t now, OSWindow } } + + const bool cursor_trail_always_visible = false; + if (cursor_trail_always_visible) { + log_error("A: cursor trail always visible"); + ct->opacity = 1.0f; + } else if (WD.screen->modes.mDECTCEM) { + log_error("B: cursor is visible"); + ct->opacity += monotonic_t_to_s_double(now - ct->updated_at) / OPT(cursor_trail_decay_slow); + ct->opacity = fminf(ct->opacity, 1.0f); + } else { + log_error("C: cursor is invisible"); + ct->opacity -= monotonic_t_to_s_double(now - ct->updated_at) / OPT(cursor_trail_decay_slow); + ct->opacity = fmaxf(ct->opacity, 0.0f); + } + ct->updated_at = now; ct->needs_render = false; diff --git a/kitty/shaders.c b/kitty/shaders.c index f953fe71b..e58f781fd 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -1149,6 +1149,8 @@ init_trail_program(void) { void draw_cursor_trail(CursorTrail *trail, Window *active_window) { bind_program(TRAIL_PROGRAM); + glEnable(GL_BLEND); + BLEND_ONTO_OPAQUE; glUniform4fv(trail_program_layout.uniforms.x_coords, 1, trail->corner_x); glUniform4fv(trail_program_layout.uniforms.y_coords, 1, trail->corner_y); @@ -1157,9 +1159,10 @@ draw_cursor_trail(CursorTrail *trail, Window *active_window) { glUniform2fv(trail_program_layout.uniforms.cursor_edge_y, 1, trail->cursor_edge_y); color_vec3(trail_program_layout.uniforms.trail_color, active_window ? active_window->render_data.screen->last_rendered.cursor_bg : OPT(foreground)); + glUniform1fv(trail_program_layout.uniforms.trail_opacity, 1, &trail->opacity); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - + glDisable(GL_BLEND); unbind_program(); } diff --git a/kitty/state.h b/kitty/state.h index 4c5a6d6c1..56d9cd9b9 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -219,6 +219,7 @@ typedef struct { typedef struct { bool needs_render; monotonic_t updated_at; + float opacity; float corner_x[4]; float corner_y[4]; float cursor_edge_x[2]; diff --git a/kitty/trail_fragment.glsl b/kitty/trail_fragment.glsl index b9dadb866..664dd6666 100644 --- a/kitty/trail_fragment.glsl +++ b/kitty/trail_fragment.glsl @@ -1,6 +1,7 @@ uniform vec2 cursor_edge_x; uniform vec2 cursor_edge_y; uniform vec3 trail_color; +uniform float trail_opacity; in vec2 frag_pos; out vec4 final_color; @@ -10,6 +11,6 @@ void main() { cursor_edge_y[1] <= frag_pos.y && frag_pos.y <= cursor_edge_y[0]) { discard; } else { - final_color = vec4(trail_color, 1.0); + final_color = vec4(trail_color, trail_opacity); } }