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
//! Close confirmation dialog for tabs with running jobs.
//!
//! Shows a confirmation dialog when the user attempts to close a terminal tab
//! that has a running command (detected via shell integration). Allows the user
//! to either force close the tab or cancel the close operation.
use crate::tab::TabId;
/// Action returned by the close confirmation dialog
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CloseConfirmAction {
/// User confirmed - close the tab/pane with the given IDs
Close {
tab_id: TabId,
pane_id: Option<crate::pane::PaneId>,
},
/// User cancelled - keep the tab open
Cancel,
/// No action yet (dialog still showing)
None,
}
/// State for the close confirmation dialog
pub struct CloseConfirmationUI {
/// Whether the dialog is visible
visible: bool,
/// The tab ID pending close
pending_tab_id: Option<TabId>,
/// The pane ID pending close (None means close entire tab)
pending_pane_id: Option<crate::pane::PaneId>,
/// The name of the running command
command_name: String,
/// The tab title for display
tab_title: String,
/// The pane's job could not be checked (a par-mux daemon that did not
/// answer in time): the dialog asks without naming a command.
unverified: bool,
/// Shared ConfirmDialog state (UX.md OV3)
confirm: crate::app::overlay::confirm::ConfirmState,
/// "Don't ask again" was ticked on the last Close answer
dont_ask_again_chosen: bool,
}
impl Default for CloseConfirmationUI {
fn default() -> Self {
Self::new()
}
}
impl CloseConfirmationUI {
/// Create a new close confirmation UI
pub fn new() -> Self {
Self {
visible: false,
pending_tab_id: None,
pending_pane_id: None,
command_name: String::new(),
tab_title: String::new(),
unverified: false,
confirm: Default::default(),
dont_ask_again_chosen: false,
}
}
/// Check if the dialog is currently visible
pub fn is_visible(&self) -> bool {
self.visible
}
/// Show the confirmation dialog for a tab with a running command
pub fn show_for_tab(&mut self, tab_id: TabId, tab_title: &str, command_name: &str) {
self.visible = true;
self.pending_tab_id = Some(tab_id);
self.pending_pane_id = None;
self.command_name = command_name.to_string();
self.tab_title = tab_title.to_string();
self.unverified = false;
}
/// Show the confirmation for a tab whose panes' running commands could
/// not be checked: the close is held rather than allowed to end a job
/// unseen.
pub fn show_for_tab_unverified(&mut self, tab_id: TabId, tab_title: &str) {
self.show_for_tab(tab_id, tab_title, "");
self.unverified = true;
}
/// Show the confirmation dialog for a pane with a running command
pub fn show_for_pane(
&mut self,
tab_id: TabId,
pane_id: crate::pane::PaneId,
tab_title: &str,
command_name: &str,
) {
self.visible = true;
self.pending_tab_id = Some(tab_id);
self.pending_pane_id = Some(pane_id);
self.command_name = command_name.to_string();
self.tab_title = tab_title.to_string();
self.unverified = false;
}
/// Show the confirmation for a pane whose running command could not be
/// checked: the close is held rather than allowed to end a job unseen.
pub fn show_for_pane_unverified(
&mut self,
tab_id: TabId,
pane_id: crate::pane::PaneId,
tab_title: &str,
) {
self.show_for_pane(tab_id, pane_id, tab_title, "");
self.unverified = true;
}
/// Hide the dialog and clear state
pub(crate) fn hide(&mut self) {
self.visible = false;
self.pending_tab_id = None;
self.pending_pane_id = None;
self.command_name.clear();
self.tab_title.clear();
self.unverified = false;
self.confirm.reset();
}
/// Render the dialog and return any action.
///
/// Drawn by the shared `ConfirmDialog` (UX.md OV3): the title names the
/// object being closed ("Close Pane?" — the old title said "Close Tab?"
/// for panes too), Enter and Escape cancel (MD5), and Close needs its
/// own chord or its button. "Don't ask again" turns off
/// `confirm_close_running_jobs`.
pub fn show(&mut self, ctx: &egui::Context) -> CloseConfirmAction {
use crate::app::overlay::confirm::{self, ConfirmChoice, ConfirmSpec};
if !self.visible {
return CloseConfirmAction::None;
}
let (title, target) = if self.pending_pane_id.is_some() {
("Close Pane?", "pane")
} else {
("Close Tab?", "tab")
};
let body = if self.unverified {
[
format!(
"The par-mux daemon did not report whether the {target} \"{}\" is running a command.",
self.tab_title
),
"Closing will terminate any process running in it.".to_string(),
]
} else {
[
format!("The {target} \"{}\" has a running command:", self.tab_title),
"Closing will terminate this process.".to_string(),
]
};
let spec = ConfirmSpec {
title,
body: &body,
detail: (!self.unverified).then_some(self.command_name.as_str()),
safe_label: "Cancel",
alternate_label: None,
destructive_label: "Close Anyway",
offers_dont_ask_again: true,
};
let Some(answer) = confirm::show(ctx, &spec, &mut self.confirm) else {
return CloseConfirmAction::None;
};
self.dont_ask_again_chosen = answer.dont_ask_again;
let action = match (answer.choice, self.pending_tab_id) {
(ConfirmChoice::Destructive, Some(tab_id)) => CloseConfirmAction::Close {
tab_id,
pane_id: self.pending_pane_id,
},
_ => CloseConfirmAction::Cancel,
};
self.hide();
action
}
/// Whether the last answer ticked "Don't ask again" (read once by the
/// caller after a Close, then cleared).
pub(crate) fn take_dont_ask_again(&mut self) -> bool {
std::mem::take(&mut self.dont_ask_again_chosen)
}
/// The command the open dialog names.
#[cfg(test)]
pub(crate) fn command_name(&self) -> &str {
&self.command_name
}
}
impl crate::traits::OverlayComponent for CloseConfirmationUI {
type Action = CloseConfirmAction;
fn show(&mut self, ctx: &egui::Context) -> Self::Action {
CloseConfirmationUI::show(self, ctx)
}
fn is_visible(&self) -> bool {
self.is_visible()
}
fn set_visible(&mut self, visible: bool) {
if !visible {
self.hide();
}
// Note: setting visible=true requires additional state (tab_id, command_name, etc.).
// Use show_for_tab() or show_for_pane() to open this dialog.
}
}
#[cfg(test)]
mod tests {
use super::{CloseConfirmAction, CloseConfirmationUI};
/// Render one frame with an Enter key press under a headless egui
/// context and return the action the dialog reported.
fn show_with_enter(ui: &mut CloseConfirmationUI) -> CloseConfirmAction {
let ctx = egui::Context::default();
ctx.begin_pass(egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(1200.0, 800.0),
)),
events: vec![egui::Event::Key {
key: egui::Key::Enter,
physical_key: None,
pressed: true,
repeat: false,
modifiers: Default::default(),
}],
..Default::default()
});
let action = ui.show(&ctx);
ctx.end_pass().textures_delta.clear();
action
}
/// MD5: Enter is the safe choice everywhere — never the destructive
/// Close. egui also fake-clicks a focused button on Enter, so this
/// must win over any same-frame button activation.
#[test]
fn enter_cancels_never_closes_tab() {
let mut ui = CloseConfirmationUI::new();
ui.show_for_tab(7, "work", "sleep 100");
assert_eq!(show_with_enter(&mut ui), CloseConfirmAction::Cancel);
assert!(!ui.is_visible());
}
/// The pane flavor of the same dialog (close-job confirm) shares the
/// Enter rule.
#[test]
fn enter_cancels_never_closes_pane() {
let mut ui = CloseConfirmationUI::new();
ui.show_for_pane(7, 2, "work", "cargo build");
assert_eq!(show_with_enter(&mut ui), CloseConfirmAction::Cancel);
assert!(!ui.is_visible());
}
}