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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use super::{BackgroundInstance, Cell, CellRenderer, RowCacheEntry, TextInstance, pipeline};
/// Terminal grid dimensions, cell sizes, padding, and content offsets.
pub(crate) struct GridLayout {
pub(crate) cols: usize,
pub(crate) rows: usize,
pub(crate) cell_width: f32,
pub(crate) cell_height: f32,
pub(crate) window_padding: f32,
/// Vertical offset for terminal content (e.g., tab bar at top).
/// Content is rendered starting at y = window_padding + content_offset_y.
pub(crate) content_offset_y: f32,
/// Horizontal offset for terminal content (e.g., tab bar on left).
/// Content is rendered starting at x = window_padding + content_offset_x.
pub(crate) content_offset_x: f32,
/// Bottom inset for terminal content (e.g., tab bar at bottom).
/// Reduces available height without shifting content vertically.
pub(crate) content_inset_bottom: f32,
/// Right inset for terminal content (e.g., AI Inspector panel).
/// Reduces available width without shifting content horizontally.
pub(crate) content_inset_right: f32,
/// Additional bottom inset from egui panels (status bar, tmux bar).
/// This is added to content_inset_bottom for scrollbar bounds only,
/// since egui panels already claim space before wgpu rendering.
pub(crate) egui_bottom_inset: f32,
/// Additional right inset from egui panels (AI Inspector).
/// This is added to content_inset_right for scrollbar bounds only,
/// since egui panels already claim space before wgpu rendering.
pub(crate) egui_right_inset: f32,
}
impl CellRenderer {
pub fn cell_width(&self) -> f32 {
self.grid.cell_width
}
pub fn cell_height(&self) -> f32 {
self.grid.cell_height
}
pub fn window_padding(&self) -> f32 {
self.grid.window_padding
}
pub fn content_offset_y(&self) -> f32 {
self.grid.content_offset_y
}
/// Set the vertical content offset (e.g., tab bar height at top).
/// Returns Some((cols, rows)) if grid size changed, None otherwise.
pub fn set_content_offset_y(&mut self, offset: f32) -> Option<(usize, usize)> {
if (self.grid.content_offset_y - offset).abs() > f32::EPSILON {
self.grid.content_offset_y = offset;
let size = (self.config.width, self.config.height);
return Some(self.resize(size.0, size.1));
}
None
}
pub fn content_offset_x(&self) -> f32 {
self.grid.content_offset_x
}
/// Set the horizontal content offset (e.g., tab bar on left).
/// Returns Some((cols, rows)) if grid size changed, None otherwise.
pub fn set_content_offset_x(&mut self, offset: f32) -> Option<(usize, usize)> {
if (self.grid.content_offset_x - offset).abs() > f32::EPSILON {
self.grid.content_offset_x = offset;
let size = (self.config.width, self.config.height);
return Some(self.resize(size.0, size.1));
}
None
}
pub fn content_inset_bottom(&self) -> f32 {
self.grid.content_inset_bottom
}
/// Set the bottom content inset (e.g., tab bar at bottom).
/// Returns Some((cols, rows)) if grid size changed, None otherwise.
pub fn set_content_inset_bottom(&mut self, inset: f32) -> Option<(usize, usize)> {
if (self.grid.content_inset_bottom - inset).abs() > f32::EPSILON {
self.grid.content_inset_bottom = inset;
let size = (self.config.width, self.config.height);
return Some(self.resize(size.0, size.1));
}
None
}
pub fn content_inset_right(&self) -> f32 {
self.grid.content_inset_right
}
/// Set the right content inset (e.g., AI Inspector panel).
/// Returns Some((cols, rows)) if grid size changed, None otherwise.
pub fn set_content_inset_right(&mut self, inset: f32) -> Option<(usize, usize)> {
if (self.grid.content_inset_right - inset).abs() > f32::EPSILON {
log::info!(
"[SCROLLBAR] set_content_inset_right: {:.1} -> {:.1} (physical px)",
self.grid.content_inset_right,
inset
);
self.grid.content_inset_right = inset;
let size = (self.config.width, self.config.height);
return Some(self.resize(size.0, size.1));
}
None
}
pub fn grid_size(&self) -> (usize, usize) {
(self.grid.cols, self.grid.rows)
}
pub fn resize(&mut self, width: u32, height: u32) -> (usize, usize) {
if width == 0 || height == 0 {
return (self.grid.cols, self.grid.rows);
}
let (width, height) = super::surface::clamp_surface_extent(
width,
height,
self.device.limits().max_texture_dimension_2d,
);
// `Surface::configure` is not idempotent: it drains the GPU queue,
// tears down the swapchain, and re-sets every CAMetalLayer property
// (colorspace included since wgpu 30). Inset-driven calls arrive with
// an unchanged extent, so configure only when it actually changed;
// forced recovery reconfigures go through `reconfigure_surface`.
if self.config.width != width || self.config.height != height {
log::info!(
"Configuring surface {}x{} (was {}x{})",
width,
height,
self.config.width,
self.config.height
);
self.config.width = width;
self.config.height = height;
self.configure_surface();
} else {
log::debug!(
"Surface extent unchanged ({}x{}), skipping configure",
width,
height
);
}
// Match the pane render path formula (see `chrome_overhead` below),
// which is always active. Width: no scrollbar deduction here — the pane
// render path conditionally subtracts scrollbar_width via RendererSizing
// when the scrollbar is visible.
// Height: 1× padding (top margin is content_offset_y, not padding).
let available_width = (width as f32
- self.grid.window_padding * 2.0
- self.grid.content_offset_x
- self.grid.content_inset_right)
.max(0.0);
let available_height = (height as f32
- self.grid.window_padding
- self.grid.content_offset_y
- self.grid.content_inset_bottom
- self.grid.egui_bottom_inset)
.max(0.0);
let new_cols = (available_width / self.grid.cell_width).max(1.0) as usize;
let new_rows = (available_height / self.grid.cell_height).max(1.0) as usize;
if new_cols != self.grid.cols || new_rows != self.grid.rows {
self.grid.cols = new_cols;
self.grid.rows = new_rows;
self.cells = vec![Cell::default(); self.grid.cols * self.grid.rows];
self.dirty_rows = vec![true; self.grid.rows];
self.row_cache = vec![None::<RowCacheEntry>; self.grid.rows];
self.recreate_instance_buffers();
}
self.update_bg_image_uniforms(None);
(self.grid.cols, self.grid.rows)
}
/// Returns total non-terminal pixel overhead as (horizontal_px, vertical_px).
///
/// Matches the pane render path formula, which is always active:
/// Horizontal: window_padding*2 + content_offset_x + content_inset_right
/// Vertical: window_padding + content_offset_y + content_inset_bottom + egui_bottom_inset
///
/// Note: scrollbar is NOT included — it is conditionally subtracted in the
/// pane render path (RendererSizing.scrollbar_width) only when visible.
/// Height uses 1× padding (bottom only; top margin is content_offset_y).
pub fn chrome_overhead(&self) -> (f32, f32) {
let chrome_x = self.grid.window_padding * 2.0
+ self.grid.content_offset_x
+ self.grid.content_inset_right;
let chrome_y = self.grid.window_padding
+ self.grid.content_offset_y
+ self.grid.content_inset_bottom
+ self.grid.egui_bottom_inset;
(chrome_x, chrome_y)
}
/// Instance capacity the single-grid (offscreen) layout needs:
/// cells + cursor overlays + one separator per row + one gutter bar per row.
pub(crate) fn single_grid_instance_capacity(&self) -> (usize, usize) {
(
super::render::SingleGridLayout::new(self.grid.cols, self.grid.rows).bg_instances(),
self.grid.cols * self.grid.rows * super::TEXT_INSTANCES_PER_CELL,
)
}
pub(crate) fn recreate_instance_buffers(&mut self) {
let (bg, text) = self.single_grid_instance_capacity();
self.allocate_instance_buffers(bg, text);
}
/// Ensure both instance buffers (GPU and CPU side) can hold the given capacities.
///
/// Existing allocations are reused when they already suffice: grid resizes used to
/// wholesale-reallocate the GPU buffers and the big CPU mirror arrays on every
/// grid change (a Malloc Large / resize-spike contributor). Arrays hold at their
/// high-water capacity; draw ranges come from the actual_* counts, never the caps.
pub(crate) fn allocate_instance_buffers(&mut self, max_bg: usize, max_text: usize) {
if max_bg > self.buffers.max_bg_instances || max_text > self.buffers.max_text_instances {
let (bg_buf, text_buf) =
pipeline::create_instance_buffers(&self.device, max_bg, max_text);
self.buffers.bg_instance_buffer = bg_buf;
self.buffers.text_instance_buffer = text_buf;
}
self.buffers.max_bg_instances = max_bg;
self.buffers.max_text_instances = max_text;
// Reset actual counts - will be updated when instance buffers are built
self.buffers.actual_bg_instances = 0;
self.buffers.actual_text_instances = 0;
self.buffers.pane_bg_cursor = 0;
self.buffers.pane_text_cursor = 0;
// A new allocation may well be the fix for a previous over-run, so let it
// be reported again if it still does not fit.
self.buffers.overflow_reported = false;
// Reuse when capacity suffices; vec![x; n] here was a wholesale realloc on
// every resize. resize() corrects len without reallocating in that case.
if self.bg_instances.capacity() >= max_bg {
self.bg_instances.resize(max_bg, BackgroundInstance::BLANK);
} else {
self.bg_instances = vec![
BackgroundInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
};
max_bg
];
}
if self.text_instances.capacity() >= max_text {
self.text_instances.resize(
max_text,
TextInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
tex_offset: [0.0, 0.0],
tex_size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
is_colored: 0,
},
);
} else {
self.text_instances = vec![
TextInstance {
position: [0.0, 0.0],
size: [0.0, 0.0],
tex_offset: [0.0, 0.0],
tex_size: [0.0, 0.0],
color: [0.0, 0.0, 0.0, 0.0],
is_colored: 0,
};
max_text
];
}
// Resize scratch buffers to match new grid; keep existing allocations if large enough
self.scratch_row_bg.reserve(
self.grid
.cols
.saturating_sub(self.scratch_row_bg.capacity()),
);
self.scratch_row_text
.reserve((self.grid.cols * 2).saturating_sub(self.scratch_row_text.capacity()));
}
/// Update scale factor and recalculate all font metrics and cell dimensions.
/// This is called when the window is dragged between displays with different DPIs.
pub fn update_scale_factor(&mut self, scale_factor: f64) {
let new_scale = scale_factor as f32;
// Skip if scale factor hasn't changed
if (self.scale_factor - new_scale).abs() < f32::EPSILON {
return;
}
log::info!(
"Recalculating font metrics for scale factor change: {} -> {}",
self.scale_factor,
new_scale
);
self.scale_factor = new_scale;
// Recalculate font_size_pixels based on new scale factor
let platform_dpi = if cfg!(target_os = "macos") {
crate::cell_renderer::MACOS_PLATFORM_DPI
} else {
crate::cell_renderer::DEFAULT_PLATFORM_DPI
};
let base_font_pixels =
self.font.base_font_size * platform_dpi / crate::cell_renderer::FONT_REFERENCE_DPI;
self.font.font_size_pixels = (base_font_pixels * new_scale).max(1.0);
// Re-extract font metrics at new scale
let (font_ascent, font_descent, font_leading, char_advance) = {
let primary_font = self.font_manager.get_font(0).expect(
"Primary font at index 0 must exist in FontManager when updating scale factor",
);
let metrics = primary_font.metrics(&[]);
let scale = self.font.font_size_pixels / metrics.units_per_em as f32;
let glyph_id = primary_font.charmap().map('m');
let advance = primary_font.glyph_metrics(&[]).advance_width(glyph_id) * scale;
(
metrics.ascent * scale,
metrics.descent * scale,
metrics.leading * scale,
advance,
)
};
self.font.font_ascent = font_ascent;
self.font.font_descent = font_descent;
self.font.font_leading = font_leading;
self.font.char_advance = char_advance;
// Recalculate cell dimensions (rounded to integer pixels for uniform glyph brightness)
let natural_line_height = font_ascent + font_descent + font_leading;
self.grid.cell_height = (natural_line_height * self.font.line_spacing)
.max(1.0)
.round();
self.grid.cell_width = (char_advance * self.font.char_spacing).max(1.0).round();
log::info!(
"New cell dimensions: {}x{} (font_size_pixels: {})",
self.grid.cell_width,
self.grid.cell_height,
self.font.font_size_pixels
);
// Clear glyph cache - glyphs need to be re-rasterized at new DPI
self.clear_glyph_cache();
// Mark all rows as dirty to force re-rendering
self.dirty_rows.fill(true);
}
pub fn update_window_padding(&mut self, padding: f32) -> Option<(usize, usize)> {
if (self.grid.window_padding - padding).abs() > f32::EPSILON {
self.grid.window_padding = padding;
let size = (self.config.width, self.config.height);
return Some(self.resize(size.0, size.1));
}
None
}
}