par-term 0.49.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
//! Context menu for tab options (rename, color, icon, duplicate, close).
//!
//! Contains the [`TabBarUI`] method for rendering the right-click context menu
//! that appears when a tab is right-clicked in either horizontal or vertical layout.

use crate::tab::TabId;
use crate::ui_constants::{
    TAB_COLOR_HEX_EDIT_WIDTH, TAB_COLOR_SWATCH_ROUNDING, TAB_COLOR_SWATCH_SIZE,
    TAB_CONTEXT_MENU_ITEM_HEIGHT, TAB_CONTEXT_MENU_MIN_WIDTH, TAB_ICON_PICKER_GLYPH_SIZE,
    TAB_ICON_PICKER_MAX_HEIGHT, TAB_ICON_PICKER_MIN_WIDTH, TAB_RENAME_EDIT_WIDTH,
};

use super::TabBarAction;
use super::TabBarUI;

impl TabBarUI {
    /// Render the context menu for tab options.
    pub(super) fn render_context_menu(
        &mut self,
        ctx: &egui::Context,
        tab_id: TabId,
    ) -> TabBarAction {
        let mut action = TabBarAction::None;
        let mut close_menu = false;

        // Handle Escape: cancel rename/icon picker if active, otherwise close menu
        if ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
            if self.renaming_tab {
                self.renaming_tab = false;
            } else if self.picking_icon {
                self.picking_icon = false;
            } else {
                close_menu = true;
            }
        }

        let area_response = egui::Area::new(egui::Id::new("tab_context_menu"))
            .fixed_pos(self.context_menu_pos)
            .constrain(true)
            .order(egui::Order::Foreground)
            .show(ctx, |ui| {
                egui::Frame::popup(ui.style())
                    .inner_margin(egui::Margin::symmetric(1, 4))
                    .show(ui, |ui| {
                        ui.set_min_width(TAB_CONTEXT_MENU_MIN_WIDTH);
                        ui.style_mut().spacing.item_spacing = egui::vec2(0.0, 0.0);

                        // Menu item helper
                        let menu_item = |ui: &mut egui::Ui, label: &str| -> bool {
                            let response = ui.add_sized(
                                [ui.available_width(), TAB_CONTEXT_MENU_ITEM_HEIGHT],
                                egui::Button::new(label)
                                    .frame(false)
                                    .fill(egui::Color32::TRANSPARENT),
                            );
                            response.clicked()
                        };

                        // Rename Tab
                        if self.renaming_tab {
                            ui.horizontal(|ui| {
                                ui.add_space(8.0);
                                let response = ui.add(
                                    egui::TextEdit::singleline(&mut self.rename_buffer)
                                        .desired_width(TAB_RENAME_EDIT_WIDTH)
                                        .hint_text("Tab name"),
                                );
                                // Auto-focus when first shown
                                if !response.has_focus() {
                                    response.request_focus();
                                }
                                // Submit on Enter
                                if ui.input(|i| i.key_pressed(egui::Key::Enter)) {
                                    let name = self.rename_buffer.trim().to_string();
                                    action = TabBarAction::RenameTab(tab_id, name);
                                    self.renaming_tab = false;
                                    close_menu = true;
                                }
                            });
                            ui.horizontal(|ui| {
                                ui.add_space(8.0);
                                ui.label(
                                    egui::RichText::new("Leave blank to use auto title")
                                        .weak()
                                        .small(),
                                );
                            });
                            ui.add_space(2.0);
                        } else if menu_item(ui, "Rename Tab") {
                            self.renaming_tab = true;
                            self.rename_activated_frame = ui.ctx().cumulative_frame_nr();
                            self.rename_buffer = self.context_menu_title.clone();
                        }

                        ui.add_space(4.0);
                        ui.separator();
                        ui.add_space(4.0);

                        // Tab Icon section
                        if self.picking_icon {
                            ui.horizontal(|ui| {
                                ui.add_space(8.0);
                                ui.label("Icon:");
                                let response = ui.add(
                                    egui::TextEdit::singleline(&mut self.icon_buffer)
                                        .desired_width(TAB_COLOR_HEX_EDIT_WIDTH)
                                        .hint_text("Icon"),
                                );
                                if !response.has_focus() {
                                    response.request_focus();
                                }
                                // Nerd Font picker button
                                let picker_label = if self.icon_buffer.is_empty() {
                                    "\u{ea7b}"
                                } else {
                                    &self.icon_buffer
                                };
                                let picker_btn =
                                    ui.button(picker_label).on_hover_text("Icon picker");
                                egui::Popup::from_toggle_button_response(&picker_btn)
                                    .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
                                    .show(|ui| {
                                        ui.set_min_width(TAB_ICON_PICKER_MIN_WIDTH);
                                        egui::ScrollArea::vertical()
                                            .max_height(TAB_ICON_PICKER_MAX_HEIGHT)
                                            .show(ui, |ui| {
                                                egui::Frame::NONE
                                                    .inner_margin(egui::Margin {
                                                        right: 10,
                                                        ..Default::default()
                                                    })
                                                    .show(ui, |ui| {
                                                        for (category, icons) in
                                                            crate::settings_ui::nerd_font::NERD_FONT_PRESETS
                                                        {
                                                            ui.label(
                                                                egui::RichText::new(*category)
                                                                    .small()
                                                                    .strong(),
                                                            );
                                                            ui.horizontal_wrapped(|ui| {
                                                                for (icon, label) in *icons {
                                                                    let btn = ui.add_sized(
                                                                        [
                                                                            TAB_ICON_PICKER_GLYPH_SIZE
                                                                                + 12.0,
                                                                            TAB_ICON_PICKER_GLYPH_SIZE
                                                                                + 12.0,
                                                                        ],
                                                                        egui::Button::new(
                                                                            egui::RichText::new(*icon)
                                                                                .size(
                                                                                TAB_ICON_PICKER_GLYPH_SIZE,
                                                                            ),
                                                                        )
                                                                        .frame(false),
                                                                    );
                                                                    if btn.on_hover_text(*label).clicked() {
                                                                        self.icon_buffer = icon.to_string();
                                                                        egui::Popup::close_all(ui.ctx());
                                                                    }
                                                                }
                                                            });
                                                            ui.add_space(2.0);
                                                        }
                                                        ui.add_space(4.0);
                                                        if ui.button("Clear icon").clicked() {
                                                            self.icon_buffer.clear();
                                                            egui::Popup::close_all(ui.ctx());
                                                        }
                                                    });
                                            });
                                    });
                            });
                            // Submit on Enter
                            if ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
                                let icon = self.icon_buffer.trim().to_string();
                                action = TabBarAction::SetTabIcon(
                                    tab_id,
                                    if icon.is_empty() { None } else { Some(icon) },
                                );
                                self.picking_icon = false;
                                close_menu = true;
                            }
                        } else if menu_item(ui, "Set Icon") {
                            self.picking_icon = true;
                            self.icon_activated_frame = ui.ctx().cumulative_frame_nr();
                        }

                        // Clear Icon (only show when tab has a custom icon)
                        if self.context_menu_icon.is_some()
                            && !self.picking_icon
                            && menu_item(ui, "Clear Icon")
                        {
                            action = TabBarAction::SetTabIcon(tab_id, None);
                            close_menu = true;
                        }

                        // Duplicate Tab
                        if menu_item(ui, "Duplicate Tab") {
                            action = TabBarAction::Duplicate(tab_id);
                            close_menu = true;
                        }

                        // Profile (UX.md PR5/PR6): change it through Open
                        // Profiles, pin it against automatic switching.
                        if menu_item(ui, "Change Profile…") {
                            action = TabBarAction::ChangeProfile(tab_id);
                            close_menu = true;
                        }
                        if menu_item(ui, "Pin / Unpin Profile") {
                            action = TabBarAction::TogglePinProfile(tab_id);
                            close_menu = true;
                        }

                        // Close Tab
                        if menu_item(ui, "Close Tab") {
                            action = TabBarAction::Close(tab_id);
                            close_menu = true;
                        }

                        ui.add_space(4.0);
                        ui.separator();
                        ui.add_space(4.0);

                        // ----- Move Tab entries -----
                        let can_move = !self.move_gateway_active;
                        let has_other_windows = !self.move_candidates.is_empty();

                        // "Move Tab to New Window" — disabled for gateway-active windows
                        // and for solo-tab source windows (visually a no-op).
                        let new_window_enabled =
                            can_move && self.move_source_tab_count >= 2;
                        ui.add_enabled_ui(new_window_enabled, |ui| {
                            if menu_item(ui, "Move Tab to New Window") {
                                action = TabBarAction::MoveTabToNewWindow(tab_id);
                                close_menu = true;
                            }
                        });

                        // "Move Tab to Window →" submenu — hidden entirely if there
                        // are no other windows or the move is disabled by gateway.
                        if can_move && has_other_windows {
                            let candidates = self.move_candidates.clone();
                            ui.menu_button("Move Tab to Window ▸", |ui| {
                                for (win_id, label) in candidates {
                                    if ui.button(&label).clicked() {
                                        action = TabBarAction::MoveTabToExistingWindow(
                                            tab_id, win_id,
                                        );
                                        close_menu = true;
                                    }
                                }
                            });
                        }

                        ui.add_space(4.0);
                        ui.separator();
                        ui.add_space(4.0);
                        // ----- end Move Tab entries -----

                        // ----- Promote / Demote -----
                        ui.add_space(4.0);
                        ui.separator();
                        ui.add_space(4.0);

                        // "Promote Pane to Tab" — only when tab has multiple panes
                        let has_multiple_panes = self.tab_has_multiple_panes;
                        ui.add_enabled_ui(has_multiple_panes, |ui| {
                            if menu_item(ui, "Promote Pane to Tab") {
                                action = TabBarAction::PromotePaneToTab(tab_id);
                                close_menu = true;
                            }
                        });

                        // "Demote Tab to Pane" — only when there are other tabs to receive it
                        // — and never for a par-mux tab `join-pane` cannot move.
                        let has_other_tabs = self.move_source_tab_count >= 2;
                        let refusal = self.demote_refusal;
                        let response = ui.add_enabled_ui(
                            has_other_tabs && refusal.is_none(),
                            |ui| {
                                if menu_item(ui, "Demote Tab to Pane") {
                                    action = TabBarAction::DemoteTabToPane(tab_id);
                                    close_menu = true;
                                }
                            },
                        );
                        if let Some(reason) = refusal {
                            response.response.on_disabled_hover_text(reason);
                        }

                        // Tab Color section
                        ui.horizontal(|ui| {
                            ui.add_space(8.0);
                            ui.label("Tab Color:");
                        });

                        ui.add_space(4.0);

                        // Color presets row
                        ui.horizontal(|ui| {
                            ui.add_space(8.0);

                            let presets: &[([u8; 3], &str)] = &[
                                ([165, 38, 38], "Red"),
                                ([165, 98, 38], "Orange"),
                                ([165, 135, 38], "Yellow"),
                                ([38, 135, 38], "Green"),
                                ([38, 135, 135], "Cyan"),
                                ([38, 75, 165], "Blue"),
                                ([135, 38, 135], "Purple"),
                            ];

                            for (color, name) in presets {
                                let btn = ui.add(
                                    egui::Button::new("")
                                        .fill(egui::Color32::from_rgb(color[0], color[1], color[2]))
                                        .min_size(egui::vec2(
                                            TAB_COLOR_SWATCH_SIZE,
                                            TAB_COLOR_SWATCH_SIZE,
                                        ))
                                        .corner_radius(TAB_COLOR_SWATCH_ROUNDING),
                                );
                                if btn.clicked() {
                                    action = TabBarAction::SetColor(tab_id, *color);
                                    close_menu = true;
                                }
                                if btn.hovered() {
                                    btn.on_hover_text(*name);
                                }
                            }

                            ui.add_space(4.0);

                            // Custom color picker
                            if ui.color_edit_button_srgb(&mut self.editing_color).changed() {
                                action = TabBarAction::SetColor(tab_id, self.editing_color);
                            }
                        });

                        ui.add_space(4.0);

                        // Clear color option
                        if menu_item(ui, "Clear Color") {
                            action = TabBarAction::ClearColor(tab_id);
                            close_menu = true;
                        }
                    });
            });

        // Close menu if clicked outside (but not on the same frame it was opened,
        // rename mode was activated, or icon picker mode was activated)
        let current_frame = ctx.cumulative_frame_nr();
        if current_frame > self.context_menu_opened_frame
            && current_frame > self.rename_activated_frame
            && current_frame > self.icon_activated_frame
            && ctx.input(|i| i.pointer.any_click())
            && !area_response.response.hovered()
            // Only close if no action was taken (let button clicks register)
            && !close_menu
            && action == TabBarAction::None
            // Don't close while icon picker is active — the popup opens on a
            // separate egui layer so clicks inside it appear "outside" this area
            && !self.picking_icon
        {
            // UX.md B31 / OV4: clicking away from an inline rename cancels it,
            // the same as pane rename; only Enter submits.
            close_menu = true;
        }

        // Close menu if action taken or cancelled
        if close_menu {
            self.context_menu_tab = None;
            self.renaming_tab = false;
            self.picking_icon = false;
        }

        action
    }
}

#[cfg(test)]
mod tests {
    use super::{TabBarAction, TabBarUI};

    /// Run one frame of the context menu with `events` as input.
    fn frame(ctx: &egui::Context, bar: &mut TabBarUI, events: Vec<egui::Event>) -> TabBarAction {
        let input = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(1200.0, 800.0),
            )),
            events,
            ..Default::default()
        };
        let mut action = TabBarAction::None;
        let mut output = ctx.run_ui(input, |ui| action = bar.render_context_menu(ui.ctx(), 7));
        // An undrained texture delta trips epaint's debug assertion on drop.
        output.textures_delta.clear();
        action
    }

    fn button(pos: egui::Pos2, pressed: bool) -> egui::Event {
        egui::Event::PointerButton {
            pos,
            button: egui::PointerButton::Primary,
            pressed,
            modifiers: Default::default(),
        }
    }

    /// UX.md TW6 + B31: a double-click rename opens in rename mode, and a
    /// click away cancels it instead of saving the buffer.
    #[test]
    fn click_away_cancels_an_inline_rename() {
        let ctx = egui::Context::default();
        let mut bar = TabBarUI::new();
        frame(&ctx, &mut bar, vec![]);
        bar.begin_rename(7, "Tab 1", egui::pos2(100.0, 40.0), 0);
        assert!(bar.is_renaming());
        bar.rename_buffer = "typed but not submitted".to_string();

        let away = egui::pos2(1000.0, 700.0);
        frame(&ctx, &mut bar, vec![egui::Event::PointerMoved(away)]);
        let pressed = frame(&ctx, &mut bar, vec![button(away, true)]);
        let released = frame(&ctx, &mut bar, vec![button(away, false)]);

        for action in [pressed, released] {
            assert_eq!(action, TabBarAction::None, "click-away must not rename");
        }
        assert!(!bar.is_renaming());
    }
}