Skip to main content

par_term_render/cell_renderer/
layout.rs

1use super::{BackgroundInstance, Cell, CellRenderer, RowCacheEntry, TextInstance, pipeline};
2
3/// Terminal grid dimensions, cell sizes, padding, and content offsets.
4pub(crate) struct GridLayout {
5    pub(crate) cols: usize,
6    pub(crate) rows: usize,
7    pub(crate) cell_width: f32,
8    pub(crate) cell_height: f32,
9    pub(crate) window_padding: f32,
10    /// Vertical offset for terminal content (e.g., tab bar at top).
11    /// Content is rendered starting at y = window_padding + content_offset_y.
12    pub(crate) content_offset_y: f32,
13    /// Horizontal offset for terminal content (e.g., tab bar on left).
14    /// Content is rendered starting at x = window_padding + content_offset_x.
15    pub(crate) content_offset_x: f32,
16    /// Bottom inset for terminal content (e.g., tab bar at bottom).
17    /// Reduces available height without shifting content vertically.
18    pub(crate) content_inset_bottom: f32,
19    /// Right inset for terminal content (e.g., AI Inspector panel).
20    /// Reduces available width without shifting content horizontally.
21    pub(crate) content_inset_right: f32,
22    /// Additional bottom inset from egui panels (status bar, tmux bar).
23    /// This is added to content_inset_bottom for scrollbar bounds only,
24    /// since egui panels already claim space before wgpu rendering.
25    pub(crate) egui_bottom_inset: f32,
26    /// Additional right inset from egui panels (AI Inspector).
27    /// This is added to content_inset_right for scrollbar bounds only,
28    /// since egui panels already claim space before wgpu rendering.
29    pub(crate) egui_right_inset: f32,
30}
31
32impl CellRenderer {
33    pub fn cell_width(&self) -> f32 {
34        self.grid.cell_width
35    }
36    pub fn cell_height(&self) -> f32 {
37        self.grid.cell_height
38    }
39    pub fn window_padding(&self) -> f32 {
40        self.grid.window_padding
41    }
42    pub fn content_offset_y(&self) -> f32 {
43        self.grid.content_offset_y
44    }
45    /// Set the vertical content offset (e.g., tab bar height at top).
46    /// Returns Some((cols, rows)) if grid size changed, None otherwise.
47    pub fn set_content_offset_y(&mut self, offset: f32) -> Option<(usize, usize)> {
48        if (self.grid.content_offset_y - offset).abs() > f32::EPSILON {
49            self.grid.content_offset_y = offset;
50            let size = (self.config.width, self.config.height);
51            return Some(self.resize(size.0, size.1));
52        }
53        None
54    }
55    pub fn content_offset_x(&self) -> f32 {
56        self.grid.content_offset_x
57    }
58    /// Set the horizontal content offset (e.g., tab bar on left).
59    /// Returns Some((cols, rows)) if grid size changed, None otherwise.
60    pub fn set_content_offset_x(&mut self, offset: f32) -> Option<(usize, usize)> {
61        if (self.grid.content_offset_x - offset).abs() > f32::EPSILON {
62            self.grid.content_offset_x = offset;
63            let size = (self.config.width, self.config.height);
64            return Some(self.resize(size.0, size.1));
65        }
66        None
67    }
68    pub fn content_inset_bottom(&self) -> f32 {
69        self.grid.content_inset_bottom
70    }
71    /// Set the bottom content inset (e.g., tab bar at bottom).
72    /// Returns Some((cols, rows)) if grid size changed, None otherwise.
73    pub fn set_content_inset_bottom(&mut self, inset: f32) -> Option<(usize, usize)> {
74        if (self.grid.content_inset_bottom - inset).abs() > f32::EPSILON {
75            self.grid.content_inset_bottom = inset;
76            let size = (self.config.width, self.config.height);
77            return Some(self.resize(size.0, size.1));
78        }
79        None
80    }
81    pub fn content_inset_right(&self) -> f32 {
82        self.grid.content_inset_right
83    }
84    /// Set the right content inset (e.g., AI Inspector panel).
85    /// Returns Some((cols, rows)) if grid size changed, None otherwise.
86    pub fn set_content_inset_right(&mut self, inset: f32) -> Option<(usize, usize)> {
87        if (self.grid.content_inset_right - inset).abs() > f32::EPSILON {
88            log::info!(
89                "[SCROLLBAR] set_content_inset_right: {:.1} -> {:.1} (physical px)",
90                self.grid.content_inset_right,
91                inset
92            );
93            self.grid.content_inset_right = inset;
94            let size = (self.config.width, self.config.height);
95            return Some(self.resize(size.0, size.1));
96        }
97        None
98    }
99    pub fn grid_size(&self) -> (usize, usize) {
100        (self.grid.cols, self.grid.rows)
101    }
102
103    pub fn resize(&mut self, width: u32, height: u32) -> (usize, usize) {
104        if width == 0 || height == 0 {
105            return (self.grid.cols, self.grid.rows);
106        }
107        self.config.width = width;
108        self.config.height = height;
109        self.surface.configure(&self.device, &self.config);
110
111        // Match the pane render path formula (pane_render.rs:72-80) which is
112        // always active.  Width: no scrollbar deduction here — the pane render
113        // path conditionally subtracts scrollbar_width via RendererSizing when
114        // the scrollbar is visible.
115        // Height: 1× padding (top margin is content_offset_y, not padding).
116        let available_width = (width as f32
117            - self.grid.window_padding * 2.0
118            - self.grid.content_offset_x
119            - self.grid.content_inset_right)
120            .max(0.0);
121        let available_height = (height as f32
122            - self.grid.window_padding
123            - self.grid.content_offset_y
124            - self.grid.content_inset_bottom
125            - self.grid.egui_bottom_inset)
126            .max(0.0);
127        let new_cols = (available_width / self.grid.cell_width).max(1.0) as usize;
128        let new_rows = (available_height / self.grid.cell_height).max(1.0) as usize;
129
130        if new_cols != self.grid.cols || new_rows != self.grid.rows {
131            self.grid.cols = new_cols;
132            self.grid.rows = new_rows;
133            self.cells = vec![Cell::default(); self.grid.cols * self.grid.rows];
134            self.dirty_rows = vec![true; self.grid.rows];
135            self.row_cache = (0..self.grid.rows).map(|_| None::<RowCacheEntry>).collect();
136            self.recreate_instance_buffers();
137        }
138
139        self.update_bg_image_uniforms(None);
140        (self.grid.cols, self.grid.rows)
141    }
142
143    /// Returns total non-terminal pixel overhead as (horizontal_px, vertical_px).
144    ///
145    /// Matches the pane render path formula (pane_render.rs:72-80) which is always active:
146    ///   Horizontal: window_padding*2 + content_offset_x + content_inset_right
147    ///   Vertical:   window_padding + content_offset_y + content_inset_bottom + egui_bottom_inset
148    ///
149    /// Note: scrollbar is NOT included — it is conditionally subtracted in the
150    /// pane render path (RendererSizing.scrollbar_width) only when visible.
151    /// Height uses 1× padding (bottom only; top margin is content_offset_y).
152    pub fn chrome_overhead(&self) -> (f32, f32) {
153        let chrome_x = self.grid.window_padding * 2.0
154            + self.grid.content_offset_x
155            + self.grid.content_inset_right;
156        let chrome_y = self.grid.window_padding
157            + self.grid.content_offset_y
158            + self.grid.content_inset_bottom
159            + self.grid.egui_bottom_inset;
160        (chrome_x, chrome_y)
161    }
162
163    pub(crate) fn recreate_instance_buffers(&mut self) {
164        self.buffers.max_bg_instances =
165            self.grid.cols * self.grid.rows + 10 + self.grid.rows + self.grid.rows; // Extra slots for cursor overlays + separator lines + gutter indicators
166        self.buffers.max_text_instances = self.grid.cols * self.grid.rows * 2;
167        let (bg_buf, text_buf) = pipeline::create_instance_buffers(
168            &self.device,
169            self.buffers.max_bg_instances,
170            self.buffers.max_text_instances,
171        );
172        self.buffers.bg_instance_buffer = bg_buf;
173        self.buffers.text_instance_buffer = text_buf;
174        // Reset actual counts - will be updated when instance buffers are built
175        self.buffers.actual_bg_instances = 0;
176        self.buffers.actual_text_instances = 0;
177
178        self.bg_instances = vec![
179            BackgroundInstance {
180                position: [0.0, 0.0],
181                size: [0.0, 0.0],
182                color: [0.0, 0.0, 0.0, 0.0],
183            };
184            self.buffers.max_bg_instances
185        ];
186        self.text_instances = vec![
187            TextInstance {
188                position: [0.0, 0.0],
189                size: [0.0, 0.0],
190                tex_offset: [0.0, 0.0],
191                tex_size: [0.0, 0.0],
192                color: [0.0, 0.0, 0.0, 0.0],
193                is_colored: 0,
194            };
195            self.buffers.max_text_instances
196        ];
197
198        // Resize scratch buffers to match new grid; keep existing allocations if large enough
199        self.scratch_row_bg.reserve(
200            self.grid
201                .cols
202                .saturating_sub(self.scratch_row_bg.capacity()),
203        );
204        self.scratch_row_text
205            .reserve((self.grid.cols * 2).saturating_sub(self.scratch_row_text.capacity()));
206    }
207
208    /// Update scale factor and recalculate all font metrics and cell dimensions.
209    /// This is called when the window is dragged between displays with different DPIs.
210    pub fn update_scale_factor(&mut self, scale_factor: f64) {
211        let new_scale = scale_factor as f32;
212
213        // Skip if scale factor hasn't changed
214        if (self.scale_factor - new_scale).abs() < f32::EPSILON {
215            return;
216        }
217
218        log::info!(
219            "Recalculating font metrics for scale factor change: {} -> {}",
220            self.scale_factor,
221            new_scale
222        );
223
224        self.scale_factor = new_scale;
225
226        // Recalculate font_size_pixels based on new scale factor
227        let platform_dpi = if cfg!(target_os = "macos") {
228            crate::cell_renderer::MACOS_PLATFORM_DPI
229        } else {
230            crate::cell_renderer::DEFAULT_PLATFORM_DPI
231        };
232        let base_font_pixels =
233            self.font.base_font_size * platform_dpi / crate::cell_renderer::FONT_REFERENCE_DPI;
234        self.font.font_size_pixels = (base_font_pixels * new_scale).max(1.0);
235
236        // Re-extract font metrics at new scale
237        let (font_ascent, font_descent, font_leading, char_advance) = {
238            let primary_font = self.font_manager.get_font(0).expect(
239                "Primary font at index 0 must exist in FontManager when updating scale factor",
240            );
241            let metrics = primary_font.metrics(&[]);
242            let scale = self.font.font_size_pixels / metrics.units_per_em as f32;
243            let glyph_id = primary_font.charmap().map('m');
244            let advance = primary_font.glyph_metrics(&[]).advance_width(glyph_id) * scale;
245            (
246                metrics.ascent * scale,
247                metrics.descent * scale,
248                metrics.leading * scale,
249                advance,
250            )
251        };
252
253        self.font.font_ascent = font_ascent;
254        self.font.font_descent = font_descent;
255        self.font.font_leading = font_leading;
256        self.font.char_advance = char_advance;
257
258        // Recalculate cell dimensions (rounded to integer pixels for uniform glyph brightness)
259        let natural_line_height = font_ascent + font_descent + font_leading;
260        self.grid.cell_height = (natural_line_height * self.font.line_spacing)
261            .max(1.0)
262            .round();
263        self.grid.cell_width = (char_advance * self.font.char_spacing).max(1.0).round();
264
265        log::info!(
266            "New cell dimensions: {}x{} (font_size_pixels: {})",
267            self.grid.cell_width,
268            self.grid.cell_height,
269            self.font.font_size_pixels
270        );
271
272        // Clear glyph cache - glyphs need to be re-rasterized at new DPI
273        self.clear_glyph_cache();
274
275        // Mark all rows as dirty to force re-rendering
276        self.dirty_rows.fill(true);
277    }
278
279    pub fn update_window_padding(&mut self, padding: f32) -> Option<(usize, usize)> {
280        if (self.grid.window_padding - padding).abs() > f32::EPSILON {
281            self.grid.window_padding = padding;
282            let size = (self.config.width, self.config.height);
283            return Some(self.resize(size.0, size.1));
284        }
285        None
286    }
287}