1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Cursor tracking methods for Ghostty-compatible shader effects.
//!
//! This module provides cursor position tracking and style-based dimension
//! calculations for shader-based cursor animations like trails and glows.
use par_term_config::color_u8_to_f32_a;
use par_term_emu_core_rust::cursor::CursorStyle;
use super::CustomShaderRenderer;
impl CustomShaderRenderer {
/// Update cursor position and appearance for shader effects
///
/// This method tracks cursor movement and records the time of change,
/// enabling Ghostty-compatible cursor trail effects and animations.
///
/// # Arguments
/// * `col` - Cursor column position (0-based)
/// * `row` - Cursor row position (0-based)
/// * `opacity` - Cursor opacity (0.0 = invisible, 1.0 = fully visible)
/// * `cursor_color` - Cursor RGBA color
/// * `style` - Cursor style (Block, Beam, Underline)
pub fn update_cursor(
&mut self,
col: usize,
row: usize,
opacity: f32,
cursor_color: [f32; 4],
style: CursorStyle,
) {
let new_pos = (col, row);
let style_changed = style != self.current_cursor_style;
let pos_changed = new_pos != self.current_cursor_pos;
if pos_changed || style_changed {
// Store previous state before updating
self.previous_cursor_pos = self.current_cursor_pos;
self.previous_cursor_opacity = self.current_cursor_opacity;
self.previous_cursor_color = self.current_cursor_color;
self.previous_cursor_style = self.current_cursor_style;
self.current_cursor_pos = new_pos;
self.current_cursor_style = style;
// Record time of change (same timebase as iTime)
self.cursor_change_time = if self.animation_enabled {
self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0)
} else {
0.0
};
if pos_changed {
log::trace!(
"Cursor moved: ({}, {}) -> ({}, {}), change_time={:.3}",
self.previous_cursor_pos.0,
self.previous_cursor_pos.1,
col,
row,
self.cursor_change_time
);
}
}
self.current_cursor_opacity = opacity;
self.current_cursor_color = cursor_color;
}
/// Hide cursor-driven shader effects until a visible cursor update arrives.
pub fn clear_cursor(&mut self) {
if self.current_cursor_opacity == 0.0 && self.current_cursor_color == [0.0, 0.0, 0.0, 0.0] {
return;
}
self.previous_cursor_pos = self.current_cursor_pos;
self.previous_cursor_opacity = self.current_cursor_opacity;
self.previous_cursor_color = self.current_cursor_color;
self.previous_cursor_style = self.current_cursor_style;
self.current_cursor_opacity = 0.0;
self.current_cursor_color = [0.0, 0.0, 0.0, 0.0];
self.cursor_change_time = if self.animation_enabled {
self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0)
} else {
0.0
};
}
/// Update cell dimensions for cursor pixel position calculation
///
/// # Arguments
/// * `cell_width` - Cell width in pixels
/// * `cell_height` - Cell height in pixels
/// * `padding` - Window padding in pixels
pub fn update_cell_dimensions(&mut self, cell_width: f32, cell_height: f32, padding: f32) {
self.cursor_cell_width = cell_width;
self.cursor_cell_height = cell_height;
self.cursor_window_padding = padding;
}
/// Set vertical content offset (e.g., tab bar height)
pub fn set_content_offset_y(&mut self, offset: f32) {
self.cursor_content_offset_y = offset;
}
/// Set horizontal content offset (e.g., tab bar on left)
pub fn set_content_offset_x(&mut self, offset: f32) {
self.cursor_content_offset_x = offset;
}
/// Set display scale factor for DPI-aware cursor sizing
pub fn set_scale_factor(&mut self, scale_factor: f32) {
self.scale_factor = scale_factor;
}
/// Convert cursor cell coordinates to pixel coordinates
///
/// Returns (x, y) in pixels from top-left corner of the window.
pub(super) fn cursor_to_pixels(&self, col: usize, row: usize) -> (f32, f32) {
let x = self.cursor_window_padding
+ self.cursor_content_offset_x
+ (col as f32 * self.cursor_cell_width);
let y = self.cursor_window_padding
+ self.cursor_content_offset_y
+ (row as f32 * self.cursor_cell_height);
(x, y)
}
/// Get cursor width in pixels based on cursor style.
/// Returns physical pixels (cell dimensions are already in physical pixels).
pub(super) fn cursor_width_for_style(&self, style: CursorStyle, scale_factor: f32) -> f32 {
match style {
// Block cursor: full cell width
CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => self.cursor_cell_width,
// Beam/Bar cursor: thin vertical line (2 logical pixels, scaled)
CursorStyle::SteadyBar | CursorStyle::BlinkingBar => 2.0 * scale_factor,
// Underline cursor: full cell width
CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => self.cursor_cell_width,
}
}
/// Get cursor height in pixels based on cursor style.
/// Returns physical pixels (cell dimensions are already in physical pixels).
pub(super) fn cursor_height_for_style(&self, style: CursorStyle, scale_factor: f32) -> f32 {
match style {
// Block cursor: full cell height
CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => self.cursor_cell_height,
// Beam/Bar cursor: full cell height
CursorStyle::SteadyBar | CursorStyle::BlinkingBar => self.cursor_cell_height,
// Underline cursor: thin horizontal line (2 logical pixels, scaled)
CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => 2.0 * scale_factor,
}
}
/// Check if cursor animation might need continuous rendering
///
/// Returns true if a cursor trail animation is likely still in progress
/// (within 1 second of the last cursor movement).
pub fn cursor_needs_animation(&self) -> bool {
if self.animation_enabled {
let current_time =
self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0);
// Allow 1 second for cursor trail animations to complete
(current_time - self.cursor_change_time) < 1.0
} else {
false
}
}
/// Update cursor shader configuration from config values
///
/// # Arguments
/// * `color` - Cursor color for shader effects [R, G, B] (0-255)
/// * `trail_duration` - Duration of cursor trail effect in seconds
/// * `glow_radius` - Radius of cursor glow effect in pixels
/// * `glow_intensity` - Intensity of cursor glow effect (0.0-1.0)
pub fn update_cursor_shader_config(
&mut self,
color: [u8; 3],
trail_duration: f32,
glow_radius: f32,
glow_intensity: f32,
) {
self.cursor_shader_color = color_u8_to_f32_a(color, 1.0);
self.cursor_trail_duration = trail_duration.max(0.0);
self.cursor_glow_radius = glow_radius.max(0.0);
self.cursor_glow_intensity = glow_intensity.clamp(0.0, 1.0);
}
}