Skip to main content

par_term_render/custom_shader_renderer/
cursor.rs

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