par-term 0.48.0

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
//! Vertical tab rendering and shared tab painting utilities.
//!
//! Contains:
//! - [`TabRenderParams`] — parameters struct shared by both layout renderers
//! - [`TabBarUI::compute_tab_bg_color`] — shared background-color computation
//! - [`TabBarUI::render_vertical_tab`] — per-tab drawing for the vertical (left-side) layout
//!
//! Horizontal per-tab drawing ([`TabBarUI::render_tab_with_width`]) lives in the
//! sibling `tab_painter` module.

use crate::config::Config;
use crate::tab::TabId;
use crate::ui_constants::{
    TAB_ACTIVE_INDICATOR_WIDTH, TAB_CLOSE_BTN_MARGIN, TAB_CLOSE_BTN_SIZE_H, TAB_CONTENT_PAD_X,
    TAB_CONTENT_PAD_Y, TAB_DRAW_SHRINK_X, TAB_DRAW_SHRINK_Y, TAB_ROUNDING,
};

use super::TabBarAction;
use super::TabBarUI;
use super::title_utils::{estimate_max_chars, sanitize_egui_title_text, truncate_plain};

/// Parameters shared by both `render_vertical_tab` and `render_tab_with_width`.
///
/// The `tab_size` field carries the tab height (vertical bar) or tab width (horizontal bar).
pub(super) struct TabRenderParams<'a> {
    pub id: TabId,
    pub index: usize,
    pub title: &'a str,
    pub profile_icon: Option<&'a str>,
    pub custom_icon: Option<&'a str>,
    pub is_active: bool,
    pub has_activity: bool,
    pub is_bell_active: bool,
    /// The tab's par-mux view: attached badge (V2) and agent badges (V3),
    /// decided per tab.
    pub mux_view: &'a crate::tab::pane_badges::TabMuxView,
    /// Pane-mode badge (zoom / broadcast, UX.md V5/V6) and its hover text.
    pub pane_badge: Option<(&'static str, &'static str)>,
    /// "Profile: X (auto: rule)" hover line (UX.md PR5); `None` for a
    /// plain tab.
    pub profile_tooltip: Option<String>,
    pub custom_color: Option<[u8; 3]>,
    pub config: &'a Config,
    /// Tab height (vertical layout) or tab width (horizontal layout).
    pub tab_size: f32,
    pub tab_count: usize,
}

impl TabBarUI {
    /// Compute the tab background color based on active/hover/drag state and custom color.
    ///
    /// Shared by both `render_tab_with_width` (horizontal) and `render_vertical_tab` (vertical)
    /// to eliminate duplicated ~35-line bg-color blocks.
    pub(super) fn compute_tab_bg_color(
        &self,
        id: TabId,
        is_active: bool,
        custom_color: Option<[u8; 3]>,
        config: &Config,
    ) -> (egui::Color32, u8) {
        let is_hovered = self.hovered_tab == Some(id);
        let is_being_dragged = self.dragging_tab == Some(id) && self.drag_in_progress;
        let should_dim =
            is_being_dragged || (config.tab_colors.dim_inactive_tabs && !is_active && !is_hovered);
        let opacity: u8 = if is_being_dragged {
            100
        } else if should_dim {
            (config.tab_colors.inactive_tab_opacity * 255.0) as u8
        } else {
            255
        };

        // Whether this inactive tab should render as outline-only (no fill)
        let outline_only = config.tab_colors.tab_inactive_outline_only && !is_active;

        let bg_color = if outline_only {
            egui::Color32::TRANSPARENT
        } else if let Some(custom) = custom_color {
            if is_active {
                egui::Color32::from_rgba_unmultiplied(custom[0], custom[1], custom[2], 255)
            } else if is_hovered {
                let lighten = |c: u8| c.saturating_add(20);
                egui::Color32::from_rgba_unmultiplied(
                    lighten(custom[0]),
                    lighten(custom[1]),
                    lighten(custom[2]),
                    255,
                )
            } else {
                let darken = |c: u8| c.saturating_sub(30);
                egui::Color32::from_rgba_unmultiplied(
                    darken(custom[0]),
                    darken(custom[1]),
                    darken(custom[2]),
                    opacity,
                )
            }
        } else if is_active {
            let c = config.tab_colors.tab_active_background;
            egui::Color32::from_rgba_unmultiplied(c[0], c[1], c[2], 255)
        } else if is_hovered {
            let c = config.tab_colors.tab_hover_background;
            egui::Color32::from_rgba_unmultiplied(c[0], c[1], c[2], 255)
        } else {
            let c = config.tab_colors.tab_inactive_background;
            egui::Color32::from_rgba_unmultiplied(c[0], c[1], c[2], opacity)
        };

        (bg_color, opacity)
    }

    /// Render a single tab as a full-width row in the vertical tab bar.
    pub(super) fn render_vertical_tab(
        &mut self,
        ui: &mut egui::Ui,
        p: TabRenderParams<'_>,
    ) -> (TabBarAction, egui::Rect) {
        let TabRenderParams {
            id,
            index: _index,
            title,
            profile_icon,
            custom_icon,
            is_active,
            has_activity,
            is_bell_active,
            mux_view,
            pane_badge,
            profile_tooltip,
            custom_color,
            config,
            tab_size: tab_height,
            tab_count,
        } = p;
        let mut action = TabBarAction::None;

        let is_hovered = self.hovered_tab == Some(id);

        let (bg_color, opacity) = self.compute_tab_bg_color(id, is_active, custom_color, config);

        // Whether this inactive tab should render as outline-only (no fill)
        let outline_only = config.tab_colors.tab_inactive_outline_only && !is_active;

        let full_width = ui.available_width();
        let (tab_rect, _) =
            ui.allocate_exact_size(egui::vec2(full_width, tab_height), egui::Sense::hover());

        let tab_draw_rect = tab_rect.shrink2(egui::vec2(TAB_DRAW_SHRINK_X, TAB_DRAW_SHRINK_Y));
        let tab_rounding = TAB_ROUNDING;
        if ui.is_rect_visible(tab_rect) {
            ui.painter()
                .rect_filled(tab_draw_rect, tab_rounding, bg_color);

            // Draw outline border for outline-only inactive tabs (brighten on hover)
            if outline_only {
                let base = if let Some(custom) = custom_color {
                    custom
                } else {
                    config.tab_colors.tab_border_color
                };
                let c = if is_hovered {
                    let brighten = |v: u8| v.saturating_add(60);
                    [brighten(base[0]), brighten(base[1]), brighten(base[2])]
                } else {
                    base
                };
                let border_width = config.tab_colors.tab_border_width.max(1.0);
                ui.painter().rect_stroke(
                    tab_draw_rect,
                    tab_rounding,
                    egui::Stroke::new(border_width, egui::Color32::from_rgb(c[0], c[1], c[2])),
                    egui::StrokeKind::Inside,
                );
            }

            // Active indicator: left edge bar instead of bottom underline
            if is_active {
                let c = if let Some(custom) = custom_color {
                    let lighten = |v: u8| v.saturating_add(50);
                    [lighten(custom[0]), lighten(custom[1]), lighten(custom[2])]
                } else {
                    config.tab_colors.tab_active_indicator
                };
                let indicator_rect = egui::Rect::from_min_size(
                    tab_draw_rect.left_top(),
                    egui::vec2(TAB_ACTIVE_INDICATOR_WIDTH, tab_draw_rect.height()),
                );
                ui.painter().rect_filled(
                    indicator_rect,
                    egui::CornerRadius {
                        nw: tab_rounding as u8,
                        sw: tab_rounding as u8,
                        ne: 0,
                        se: 0,
                    },
                    egui::Color32::from_rgb(c[0], c[1], c[2]),
                );
            }

            // Content
            let content_rect = tab_rect.shrink2(egui::vec2(TAB_CONTENT_PAD_X, TAB_CONTENT_PAD_Y));
            let mut content_ui = ui.new_child(
                egui::UiBuilder::new()
                    .max_rect(content_rect)
                    .layout(egui::Layout::left_to_right(egui::Align::Center)),
            );

            content_ui.horizontal(|ui| {
                if is_bell_active {
                    let c = config.tab_colors.tab_bell_indicator;
                    ui.colored_label(egui::Color32::from_rgb(c[0], c[1], c[2]), "🔔");
                    ui.add_space(2.0);
                } else if has_activity && !is_active {
                    let c = config.tab_colors.tab_activity_indicator;
                    ui.colored_label(egui::Color32::from_rgb(c[0], c[1], c[2]), "•");
                    ui.add_space(2.0);
                }

                // Profile icon
                let icon_width = if let Some(icon) = profile_icon {
                    let icon = sanitize_egui_title_text(icon);
                    ui.label(icon.as_ref());
                    ui.add_space(2.0);
                    18.0
                } else {
                    0.0
                };

                let text_color = if is_active {
                    let c = config.tab_colors.tab_active_text;
                    egui::Color32::from_rgba_unmultiplied(c[0], c[1], c[2], 255)
                } else {
                    let c = config.tab_colors.tab_inactive_text;
                    egui::Color32::from_rgba_unmultiplied(c[0], c[1], c[2], opacity)
                };

                // par-mux badges (UX.md V2/V3), decided per tab
                let mux_width = mux_badges(ui, mux_view, text_color)
                    + pane_badge_label(ui, pane_badge, text_color);

                // Truncate title to fit available width
                let close_width = if config.tabs.tab_show_close_button {
                    TAB_CLOSE_BTN_SIZE_H + TAB_CLOSE_BTN_MARGIN
                } else {
                    0.0
                };
                let available =
                    (full_width - TAB_CONTENT_PAD_X * 2.0 - icon_width - mux_width - close_width)
                        .max(TAB_CONTENT_PAD_X * 2.0 + TAB_CLOSE_BTN_MARGIN);
                let base_font_id = ui.style().text_styles[&egui::TextStyle::Button].clone();
                let max_chars = estimate_max_chars(ui, &base_font_id, available);
                let safe_title = sanitize_egui_title_text(title);
                let display_title = truncate_plain(safe_title.as_ref(), max_chars);
                let truncated = display_title != safe_title.as_ref();
                let label = ui.label(egui::RichText::new(display_title).color(text_color));
                // UX.md V13: a truncated title shows in full on hover.
                if truncated {
                    label.on_hover_text(safe_title.as_ref());
                }
            });

            // Close button at right edge
            if config.tabs.tab_show_close_button {
                let close_size = TAB_CLOSE_BTN_SIZE_H;
                let close_rect = egui::Rect::from_min_size(
                    egui::pos2(
                        tab_rect.right() - close_size - TAB_CLOSE_BTN_MARGIN,
                        tab_rect.center().y - close_size / 2.0,
                    ),
                    egui::vec2(close_size, close_size),
                );
                let pointer_pos = ui.ctx().input(|i| i.pointer.hover_pos());
                let close_hovered = pointer_pos.is_some_and(|pos| close_rect.contains(pos));

                if close_hovered {
                    self.close_hovered = Some(id);
                } else if self.close_hovered == Some(id) {
                    self.close_hovered = None;
                }

                let close_color = if self.close_hovered == Some(id) {
                    let c = config.tab_colors.tab_close_button_hover;
                    egui::Color32::from_rgb(c[0], c[1], c[2])
                } else {
                    let c = config.tab_colors.tab_close_button;
                    egui::Color32::from_rgba_unmultiplied(c[0], c[1], c[2], opacity)
                };

                ui.painter().text(
                    close_rect.center(),
                    egui::Align2::CENTER_CENTER,
                    "×",
                    egui::FontId::proportional(12.0),
                    close_color,
                );
            }
        }

        // Handle click and drag
        let tab_response = with_profile_tooltip(
            ui.interact(
                tab_rect,
                egui::Id::new(("tab_click", id)),
                egui::Sense::click_and_drag(),
            ),
            profile_tooltip,
        );

        let pointer_in_tab = tab_response.hovered();
        let clicked = tab_response.clicked_by(egui::PointerButton::Primary);

        if tab_count > 1
            && !self.drag_in_progress
            && self.close_hovered != Some(id)
            && tab_response.drag_started_by(egui::PointerButton::Primary)
        {
            self.drag_in_progress = true;
            self.dragging_tab = Some(id);
            self.dragging_title = title.to_string();
            self.dragging_color = custom_color;
            self.dragging_tab_width = full_width;
        }

        let is_dragging_this = self.dragging_tab == Some(id) && self.drag_in_progress;

        if clicked
            && !is_dragging_this
            && action == TabBarAction::None
            && self.close_hovered != Some(id)
        {
            action = TabBarAction::SwitchTo(id);
        }

        if clicked && self.close_hovered == Some(id) {
            action = TabBarAction::Close(id);
        }

        // UX.md TW7: middle-click closes the tab, the browser convention.
        if tab_response.clicked_by(egui::PointerButton::Middle) {
            action = TabBarAction::Close(id);
        }

        // UX.md TW6: double-clicking a tab opens its inline rename.
        if tab_response.double_clicked_by(egui::PointerButton::Primary) {
            self.editing_color = custom_color.unwrap_or([100, 100, 100]);
            self.context_menu_icon = custom_icon.map(|s| s.to_string());
            self.icon_buffer = custom_icon.unwrap_or("").to_string();
            let pos = ui
                .ctx()
                .input(|i| i.pointer.interact_pos())
                .unwrap_or(tab_rect.left_bottom());
            self.begin_rename(id, title, pos, ui.ctx().cumulative_frame_nr());
        }

        if tab_response.secondary_clicked() {
            self.editing_color = custom_color.unwrap_or([100, 100, 100]);
            self.context_menu_tab = Some(id);
            self.context_menu_title = title.to_string();
            self.context_menu_icon = custom_icon.map(|s| s.to_string());
            self.icon_buffer = custom_icon.unwrap_or("").to_string();
            self.picking_icon = false;
            if let Some(pos) = ui.ctx().input(|i| i.pointer.interact_pos()) {
                self.context_menu_pos = pos;
            }
            self.context_menu_opened_frame = ui.ctx().cumulative_frame_nr();
        }

        if pointer_in_tab {
            self.hovered_tab = Some(id);
        } else if self.hovered_tab == Some(id) {
            self.hovered_tab = None;
        }

        (action, tab_rect)
    }
}

/// Hover a tab's whole rect for its profile line (UX.md PR5), shared by
/// both layouts so the tooltip cannot drift between them.
pub(super) fn with_profile_tooltip(
    response: egui::Response,
    tooltip: Option<String>,
) -> egui::Response {
    match tooltip {
        Some(text) => response.on_hover_text(text),
        None => response,
    }
}

/// The profile line for `tab`'s tooltip, names resolved live.
pub(super) fn tab_profile_tooltip(
    tab: &crate::tab::Tab,
    profiles: &crate::profile::ProfileManager,
) -> Option<String> {
    crate::tab::profile_tooltip(&tab.profile, |id| profiles.get(&id).map(|p| p.name.clone()))
}

/// Draw the pane-mode badge (UX.md V5/V6) and return the width it takes.
/// Draw a tab's par-mux badges — attached (V2) and its most urgent agent
/// (V3) — and return the width they took, for title truncation.
pub(super) fn mux_badges(
    ui: &mut egui::Ui,
    view: &crate::tab::pane_badges::TabMuxView,
    text_color: egui::Color32,
) -> f32 {
    let mut width = 0.0;
    if view.attached {
        ui.label(egui::RichText::new(crate::tab::pane_badges::ATTACHED_BADGE).color(text_color))
            .on_hover_text(view.attached_tooltip());
        ui.add_space(2.0);
        width += 18.0;
    }
    if let Some(agent) = view.agent_badge() {
        ui.label(egui::RichText::new(agent.glyph()).color(agent.color(text_color)))
            .on_hover_text(agent.describe());
        ui.add_space(2.0);
        width += 18.0;
    }
    width
}

pub(super) fn pane_badge_label(
    ui: &mut egui::Ui,
    badge: Option<(&'static str, &'static str)>,
    color: egui::Color32,
) -> f32 {
    let Some((glyph, tooltip)) = badge else {
        return 0.0;
    };
    ui.label(egui::RichText::new(glyph).color(color))
        .on_hover_text(tooltip);
    ui.add_space(2.0);
    18.0
}