par-term 0.30.9

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Split-pane rendering helpers.
//!
//! Contains:
//! - `PaneRenderData`: per-pane snapshot used during `submit_gpu_frame`
//! - `gather_pane_render_data`: collects per-pane cells/graphics/metadata from the pane manager
//! - `render_split_panes_with_data`: drives the GPU split-pane render pass

use super::types::RendererSizing;
use crate::cell_renderer::PaneViewport;
use crate::config::{Config, PaneTitlePosition, color_u8_to_f32};
use crate::renderer::{
    DividerRenderInfo, PaneDividerSettings, PaneRenderInfo, PaneTitleInfo, Renderer,
};
use crate::scrollback_metadata::ScrollbackMark;
use crate::selection::SelectionMode;
use anyhow::Result;

/// Pane render data for split pane rendering
pub(super) struct PaneRenderData {
    /// Viewport bounds and state for this pane
    pub(super) viewport: PaneViewport,
    /// Cells to render (should match viewport grid size)
    pub(super) cells: Vec<crate::cell_renderer::Cell>,
    /// Grid dimensions (cols, rows)
    pub(super) grid_size: (usize, usize),
    /// Cursor position within this pane (col, row), or None if no cursor visible
    pub(super) cursor_pos: Option<(usize, usize)>,
    /// Cursor opacity (0.0 = hidden, 1.0 = fully visible)
    pub(super) cursor_opacity: f32,
    /// Scrollback marks for this pane
    pub(super) marks: Vec<ScrollbackMark>,
    /// Scrollback length for this pane (needed for separator mark mapping)
    pub(super) scrollback_len: usize,
    /// Current scroll offset for this pane (needed for separator mark mapping)
    pub(super) scroll_offset: usize,
    /// Per-pane background image override (None = use global background)
    pub(super) background: Option<crate::pane::PaneBackground>,
    /// Inline graphics (Sixel/iTerm2/Kitty) to render for this pane
    pub(super) graphics: Vec<par_term_emu_core_rust::graphics::TerminalGraphic>,
}

/// Result of `gather_pane_render_data`.
pub(super) type PaneRenderDataResult = Option<(
    Vec<PaneRenderData>,
    Vec<crate::pane::DividerRect>,
    Vec<PaneTitleInfo>,
    Option<PaneViewport>,
    usize, // focused pane scrollback_len (for tab.cache update)
)>;

/// Gather per-pane render data from the active tab's pane manager.
///
/// This is a free function (not a `&mut self` method) so it can be called while
/// `self.renderer` is mutably borrowed.  The caller must already hold `&mut Tab`
/// from `tab_manager.active_tab_mut()`.
///
/// Returns `None` when no pane manager is present or the tab is absent.
/// # Arguments
/// * `scrollbar_inset` - Physical pixels to subtract from each pane's content width
///   for the scrollbar.  In split-pane mode this is applied to ALL panes so the
///   column count never changes on focus switch (preventing layout reflow).
///   The scrollbar is shown per-pane based on each pane's scrollback state.
pub(super) fn gather_pane_render_data(
    tab: &mut crate::tab::Tab,
    config: &Config,
    sizing: &RendererSizing,
    effective_pane_padding: f32,
    cursor_opacity: f32,
    pane_count: usize,
    scrollbar_inset: f32,
) -> PaneRenderDataResult {
    let effective_padding = if pane_count > 1 && config.window.hide_window_padding_on_split {
        0.0
    } else {
        sizing.padding
    };

    let content_width = sizing.size.width as f32
        - effective_padding * 2.0
        - sizing.content_offset_x
        - sizing.content_inset_right;
    let content_height = sizing.size.height as f32
        - sizing.content_offset_y
        - sizing.content_inset_bottom
        - effective_padding
        - sizing.status_bar_height;

    let tab_scroll_offset = tab.active_scroll_state().offset;

    let pm = tab.pane_manager.as_mut()?;

    // Update pane bounds
    let bounds = crate::pane::PaneBounds::new(
        effective_padding + sizing.content_offset_x,
        sizing.content_offset_y,
        content_width,
        content_height,
    );
    pm.set_bounds(bounds);

    // Terminal resize is done per-pane in the loop below so each pane
    // subtracts `scrollbar_inset` from its column calculation.
    // This avoids two competing resize calls that would cause SIGWINCH storms.
    // Note: title_height_offset is not needed here because `viewport_height`
    // (computed per-pane below) already subtracts the title bar height.

    let focused_pane_id = pm.focused_pane_id();
    let all_pane_ids: Vec<_> = pm.all_panes().iter().map(|p| p.id).collect();
    let dividers = pm.get_dividers();

    let pane_bg_opacity = config.pane_background_opacity;
    let inactive_opacity = if config.dim_inactive_panes {
        config.inactive_pane_opacity
    } else {
        1.0
    };

    // Title settings (all in physical pixels)
    let show_titles = config.show_pane_titles;
    let title_height = config.pane_title_height * sizing.scale_factor;
    let title_position = config.pane_title_position;
    let title_text_color = color_u8_to_f32(config.pane_title_color);
    let title_bg_color = color_u8_to_f32(config.pane_title_bg_color);
    let need_marks = config.scrollbar_command_marks || config.command_separator_enabled;

    let mut pane_data: Vec<PaneRenderData> = Vec::new();
    let mut pane_titles: Vec<PaneTitleInfo> = Vec::new();
    let mut focused_pane_scrollback_len: usize = 0;
    let mut focused_viewport: Option<PaneViewport> = None;

    for pane_id in &all_pane_ids {
        let Some(pane) = pm.get_pane_mut(*pane_id) else {
            continue;
        };
        let is_focused = Some(*pane_id) == focused_pane_id;
        let bounds = pane.bounds;

        // Viewport y and height accounting for title bar position
        let (viewport_y, viewport_height) = if show_titles {
            match title_position {
                PaneTitlePosition::Top => (
                    bounds.y + title_height,
                    (bounds.height - title_height).max(0.0),
                ),
                PaneTitlePosition::Bottom => (bounds.y, (bounds.height - title_height).max(0.0)),
            }
        } else {
            (bounds.y, bounds.height)
        };

        let physical_pane_padding = effective_pane_padding * sizing.scale_factor;

        // Compute grid size and resize the PTY BEFORE gathering cells so that
        // get_cells_with_scrollback returns cells at the correct dimensions.
        // All panes subtract the scrollbar inset so column counts remain stable
        // across focus changes (preventing layout reflow on pane click).
        let sb_inset = scrollbar_inset;
        let content_w =
            (bounds.width - physical_pane_padding * 2.0 - sb_inset).max(sizing.cell_width);
        let content_h = (viewport_height - physical_pane_padding * 2.0).max(sizing.cell_height);
        let cols = ((content_w / sizing.cell_width).floor() as usize).max(1);
        let rows = ((content_h / sizing.cell_height).floor() as usize).max(1);

        // Center the cell grid within the content area by distributing
        // remainder pixels evenly on both sides (like Alacritty/Kitty).
        // Floor to integer pixels so all cell boundaries land on exact pixel
        // positions — sub-pixel centering offsets cause hairline gaps between
        // adjacent differently-colored cells due to GPU FP rasterization.
        let actual_content_w = cols as f32 * sizing.cell_width;
        let actual_content_h = rows as f32 * sizing.cell_height;
        let center_offset_x = ((content_w - actual_content_w) / 2.0).floor();
        let center_offset_y = ((content_h - actual_content_h) / 2.0).floor();

        pane.resize_terminal_with_cell_dims(
            cols,
            rows,
            sizing.cell_width as u32,
            sizing.cell_height as u32,
        );

        let mut viewport = PaneViewport::with_padding(
            bounds.x,
            viewport_y,
            bounds.width,
            viewport_height,
            is_focused,
            if is_focused {
                pane_bg_opacity
            } else {
                pane_bg_opacity * inactive_opacity
            },
            physical_pane_padding,
        );
        viewport.content_offset_x = center_offset_x;
        viewport.content_offset_y = center_offset_y;

        if is_focused {
            focused_viewport = Some(viewport);
        }

        // Build pane title info
        if show_titles {
            let title_y = match title_position {
                PaneTitlePosition::Top => bounds.y,
                PaneTitlePosition::Bottom => bounds.y + bounds.height - title_height,
            };
            pane_titles.push(PaneTitleInfo {
                x: bounds.x,
                y: title_y,
                width: bounds.width,
                height: title_height,
                title: pane.get_title(),
                focused: is_focused,
                text_color: title_text_color,
                bg_color: title_bg_color,
            });
        }

        // Gather cells — fall back to cached cells on lock contention to prevent
        // empty-frame flashes (animated shaders trigger 60fps redraws, so lock
        // contention with the PTY reader is common during heavy output).
        //
        // For the focused pane, gather_render_data already called
        // get_cells_with_scrollback() in extract_tab_cells and stored the result
        // in pane.cache.pane_cells with the current generation.  Reuse those cells
        // to avoid a second blocking terminal.lock() call, which is the primary
        // cause of FPS drops when the PTY reader is busy (e.g. tmux with many panes).
        //
        // However, the cached cells may have been generated at a different grid
        // size (e.g., before a scrollbar appeared due to focus change).  If the
        // cell count doesn't match cols*rows, invalidate the cache so fresh cells
        // are generated at the correct dimensions.
        let expected_cell_count = cols * rows;
        let scroll_offset = if is_focused { tab_scroll_offset } else { 0 };
        let cache_dims_match = is_focused
            && pane.cache.pane_cells_generation > 0
            && pane
                .cache
                .pane_cells
                .as_ref()
                .is_some_and(|c| c.len() == expected_cell_count);
        let cells = if cache_dims_match {
            // Fresh cells from extract_tab_cells at matching dimensions.
            // Reset generation so this path only fires once per frame.
            pane.cache.pane_cells_generation = 0;
            pane.cache.pane_cells.take().unwrap()
        } else if let Ok(term) = pane.terminal.try_write() {
            let selection = pane
                .mouse
                .selection
                .map(|sel| sel.viewport_adjusted(scroll_offset).normalized());
            let rectangular = pane
                .mouse
                .selection
                .map(|sel| sel.mode == SelectionMode::Rectangular)
                .unwrap_or(false);
            // Use try_get_cells_with_scrollback to avoid blocking on the internal
            // terminal mutex when the PTY reader is processing output.  Falls through
            // to the pane_cells cache on contention.
            if let Some(fresh) =
                term.try_get_cells_with_scrollback(scroll_offset, selection, rectangular)
            {
                pane.cache.pane_cells = Some(fresh.clone());
                fresh
            } else if let Some(ref cached) = pane.cache.pane_cells {
                cached.clone()
            } else {
                Vec::new()
            }
        } else if let Some(ref cached) = pane.cache.pane_cells {
            // try_lock miss — use last successfully gathered cells to avoid
            // rendering an empty pane for this frame.
            cached.clone()
        } else {
            Vec::new()
        };

        // Gather marks and scrollback length — use cached scrollback_len on lock miss
        let (marks, pane_scrollback_len) = if need_marks {
            if let Ok(mut term) = pane.terminal.try_write() {
                let sb_len = term.scrollback_len();
                term.update_scrollback_metadata(sb_len, 0);
                pane.cache.pane_scrollback_len = sb_len;
                (term.scrollback_marks(), sb_len)
            } else {
                (Vec::new(), pane.cache.pane_scrollback_len)
            }
        } else {
            // Still need scrollback_len for graphics position math
            let sb_len = if let Ok(term) = pane.terminal.try_write() {
                pane.cache.pane_scrollback_len = term.scrollback_len();
                pane.cache.pane_scrollback_len
            } else {
                pane.cache.pane_scrollback_len
            };
            (Vec::new(), sb_len)
        };
        let pane_scroll_offset = if is_focused { tab_scroll_offset } else { 0 };

        // Cache focused pane scrollback_len for scroll operations
        if is_focused && pane_scrollback_len > 0 {
            focused_pane_scrollback_len = pane_scrollback_len;
        }

        // Per-pane backgrounds only apply when multiple panes exist
        let pane_background = if all_pane_ids.len() > 1 && pane.background().has_image() {
            Some(pane.background().clone())
        } else {
            None
        };

        // Cursor position — only show when viewport is not scrolled away from the live screen.
        // When scroll_offset > 0 the cursor is off-screen (in scrollback), so hide it.
        let cursor_pos = if scroll_offset == 0 {
            if let Ok(term) = pane.terminal.try_write() {
                if term.is_cursor_visible() {
                    Some(term.cursor_position())
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        };

        // Collect inline graphics (Sixel/iTerm2/Kitty)
        let pane_graphics = if let Ok(term) = pane.terminal.try_write() {
            // Remove graphics whose rows have been overwritten by cell writes
            // (e.g. tmux control-mode redraw doesn't send ED 2).
            term.invalidate_overwritten_graphics();

            let mut g = term.get_graphics_with_animations();
            let sb = term.get_scrollback_graphics();
            crate::debug_log!(
                "GRAPHICS",
                "pane {:?}: active_graphics={}, scrollback_graphics={}, scrollback_len={}, scroll_offset={}, visible_rows={}, viewport=({},{},{}x{})",
                pane_id,
                g.len(),
                sb.len(),
                pane_scrollback_len,
                pane_scroll_offset,
                rows,
                viewport.x,
                viewport.y,
                viewport.width,
                viewport.height
            );
            for (i, gfx) in g.iter().chain(sb.iter()).enumerate() {
                crate::debug_log!(
                    "GRAPHICS",
                    "  graphic[{}]: id={}, pos=({},{}), scroll_offset_rows={}, scrollback_row={:?}, size={}x{}",
                    i,
                    gfx.id,
                    gfx.position.0,
                    gfx.position.1,
                    gfx.scroll_offset_rows,
                    gfx.scrollback_row,
                    gfx.width,
                    gfx.height
                );
            }
            g.extend(sb);
            g
        } else {
            crate::debug_log!(
                "GRAPHICS",
                "pane {:?}: try_lock() failed, no graphics",
                pane_id
            );
            Vec::new()
        };

        pane_data.push(PaneRenderData {
            viewport,
            cells,
            grid_size: (cols, rows),
            cursor_pos,
            cursor_opacity: if is_focused { cursor_opacity } else { 0.0 },
            marks,
            scrollback_len: pane_scrollback_len,
            scroll_offset: pane_scroll_offset,
            background: pane_background,
            graphics: pane_graphics,
        });
    }

    Some((
        pane_data,
        dividers,
        pane_titles,
        focused_viewport,
        focused_pane_scrollback_len,
    ))
}

/// Parameters for [`WindowState::render_split_panes_with_data`].
pub(super) struct SplitPaneRenderParams<'a> {
    pub pane_data: Vec<PaneRenderData>,
    pub dividers: Vec<crate::pane::DividerRect>,
    pub pane_titles: Vec<PaneTitleInfo>,
    pub focused_viewport: Option<PaneViewport>,
    pub config: &'a Config,
    pub egui_data: Option<(egui::FullOutput, &'a egui::Context)>,
    pub hovered_divider_index: Option<usize>,
    pub show_scrollbar: bool,
}

impl crate::app::window_state::WindowState {
    /// Render split panes when the active tab has multiple panes
    pub(super) fn render_split_panes_with_data(
        renderer: &mut Renderer,
        p: SplitPaneRenderParams<'_>,
    ) -> Result<bool> {
        let SplitPaneRenderParams {
            pane_data,
            dividers,
            pane_titles,
            focused_viewport,
            config,
            egui_data,
            hovered_divider_index,
            show_scrollbar,
        } = p;
        // Two-phase construction: separate owned cell data from pane metadata
        // so PaneRenderInfo can borrow cell slices safely.  This replaces the
        // previous unsafe Box::into_raw / Box::from_raw pattern that leaked
        // memory if render_split_panes panicked.
        //
        // Phase 1: Extract cells into a Vec that outlives the render infos.
        // The remaining pane fields are collected into partial render infos.
        let mut owned_cells: Vec<Vec<crate::cell_renderer::Cell>> =
            Vec::with_capacity(pane_data.len());
        let mut partial_infos: Vec<PaneRenderInfo> = Vec::with_capacity(pane_data.len());

        for pane in pane_data {
            let focused = pane.viewport.focused;
            owned_cells.push(pane.cells);
            partial_infos.push(PaneRenderInfo {
                viewport: pane.viewport,
                // Placeholder — will be patched in Phase 2 once owned_cells
                // is finished growing and its elements have stable addresses.
                cells: &[],
                grid_size: pane.grid_size,
                cursor_pos: pane.cursor_pos,
                cursor_opacity: pane.cursor_opacity,
                // Focused pane: respect autohide via show_scrollbar flag.
                // Unfocused panes: always show scrollbar when they have scrollback
                // content, so the scrollbar doesn't disappear on focus loss.
                show_scrollbar: if focused {
                    show_scrollbar && pane.scrollback_len > 0
                } else {
                    pane.scrollback_len > 0
                },
                marks: pane.marks,
                scrollback_len: pane.scrollback_len,
                scroll_offset: pane.scroll_offset,
                background: pane.background,
                graphics: pane.graphics,
            });
        }

        // Phase 2: Patch cell references now that owned_cells won't reallocate.
        // owned_cells lives until scope exit (even on panic), so the borrows
        // are valid for the lifetime of partial_infos.
        for (info, cells) in partial_infos.iter_mut().zip(owned_cells.iter()) {
            info.cells = cells.as_slice();
        }
        let pane_render_infos = partial_infos;

        // Build divider render info
        let divider_render_infos: Vec<DividerRenderInfo> = dividers
            .iter()
            .enumerate()
            .map(|(i, d)| DividerRenderInfo::from_rect(d, hovered_divider_index == Some(i)))
            .collect();

        // Build divider settings from config
        let divider_settings = PaneDividerSettings {
            divider_color: color_u8_to_f32(config.pane_divider_color),
            hover_color: color_u8_to_f32(config.pane_divider_hover_color),
            show_focus_indicator: config.pane_focus_indicator,
            focus_color: color_u8_to_f32(config.pane_focus_color),
            focus_width: config.pane_focus_width * renderer.scale_factor(),
            divider_style: config.pane_divider_style,
        };

        // Call the split pane renderer.
        // owned_cells is dropped automatically at scope exit, even on panic.
        renderer.render_split_panes(crate::renderer::SplitPanesRenderParams {
            panes: &pane_render_infos,
            dividers: &divider_render_infos,
            pane_titles: &pane_titles,
            focused_viewport: focused_viewport.as_ref(),
            divider_settings: &divider_settings,
            egui_data,
            force_egui_opaque: false,
        })
    }
}