mux: introduce can_close_without_prompting concept to model

This is defined as a trait method on Pane (default: false), and has the
obvious transitive equivalent methods in Tab and Window (eg: if all
contained items are `can_close_without_prompting`, then that container
is also `can_close_without_prompting`).

The intent is to avoid bothering the user to confirm closing a window
when the content is not stateful and doesn't warrant it.

For example: the window that is displayed in the event of a
configuration error really shouldn't prompt to the user to confirm
closing it.

All termwiztermtab panes are `can_close_without_prompting==true`
to effect this policy.

In the future, we could teach LocalPane to lookup the session leader
process against a list of "uninteresting" or "stateless" processes
and return an appropriate result (as suggested in
https://github.com/wez/wezterm/issues/280).  That functionality
is NOT part of this commit.
This commit is contained in:
Wez Furlong committed 2021-02-27 09:03:13 -08:00
1 parent 02828c708a
commit 823213703c
8 files changed
+56 -10

No files matched your search

+1
View File
@@ -32,6 +32,7 @@ brief notes about them may accumulate here.
* Windows: the portable .zip file download now includes ANGLE EGL, just like the setup.exe installer has done since version 20201031-154415-9614e117
* Windows: Fixed [ToggleFullScreen](config/lua/keyassignment/ToggleFullScreen.md) so that it once again toggles between full screen and normal placement. [#177](https://github.com/wez/wezterm/issues/177)
* New: [exit_behavior](config/lua/config/exit_behavior.md) config option to keep panes open after the program has completed. [#499](https://github.com/wez/wezterm/issues/499)
* Closing the configuration error window no longer requires confirmation
### 20210203-095643-70a364eb
+5
View File
@@ -123,6 +123,11 @@ pub trait Pane: Downcast {
/// Called to advise on whether this tab has focus
fn focus_changed(&self, _focused: bool) {}
/// Certain panes are OK to be closed with impunity (no prompts)
fn can_close_without_prompting(&self) -> bool {
false
}
/// Performs a search.
/// If the result is empty then there are no matches.
/// Otherwise, the result shall contain all possible matches.
+10
View File
@@ -1200,6 +1200,16 @@ impl Tab {
}
}
pub fn can_close_without_prompting(&self) -> bool {
let panes = self.iter_panes();
for pos in &panes {
if !pos.pane.can_close_without_prompting() {
return false;
}
}
true
}
pub fn is_dead(&self) -> bool {
let panes = self.iter_panes();
let mut dead_count = 0;
+4
View File
@@ -156,6 +156,10 @@ impl Pane for TermWizTerminalPane {
self.terminal.borrow_mut().get_title().to_string()
}
fn can_close_without_prompting(&self) -> bool {
true
}
fn send_paste(&self, text: &str) -> anyhow::Result<()> {
let paste = InputEvent::Paste(text.to_string());
self.input_tx.send(paste)?;
+9
View File
@@ -73,6 +73,15 @@ impl Window {
self.tabs.get(idx)
}
pub fn can_close_without_prompting(&self) -> bool {
for tab in &self.tabs {
if !tab.can_close_without_prompting() {
return false;
}
}
true
}
pub fn idx_by_id(&self, id: TabId) -> Option<usize> {
for (idx, t) in self.tabs.iter().enumerate() {
if t.tab_id() == id {
@@ -199,7 +199,7 @@ pub fn confirm_close_tab(
})
.detach();
}
TermWindow::schedule_cancel_overlay(window, tab_id);
TermWindow::schedule_cancel_overlay(window, tab_id, None);
Ok(())
}
@@ -220,7 +220,7 @@ pub fn confirm_close_window(
})
.detach();
}
TermWindow::schedule_cancel_overlay(window, tab_id);
TermWindow::schedule_cancel_overlay(window, tab_id, None);
Ok(())
}
@@ -238,7 +238,7 @@ pub fn confirm_quit_program(
})
.detach();
}
TermWindow::schedule_cancel_overlay(window, tab_id);
TermWindow::schedule_cancel_overlay(window, tab_id, None);
Ok(())
}
+3 -1
View File
@@ -39,9 +39,11 @@ where
let window = term_window.window.clone().unwrap();
let overlay_pane_id = tw_tab.pane_id();
let future = promise::spawn::spawn_into_new_thread(move || {
let res = func(tab_id, tw_term);
TermWindow::schedule_cancel_overlay(window, tab_id);
TermWindow::schedule_cancel_overlay(window, tab_id, Some(overlay_pane_id));
res
});
+21 -6
View File
@@ -302,6 +302,14 @@ impl WindowCallbacks for TermWindow {
};
let mux_window_id = self.mux_window_id;
let can_close = mux
.get_window(mux_window_id)
.map_or(false, |w| w.can_close_without_prompting());
if can_close {
mux.kill_window(self.mux_window_id);
return true;
}
let window = self.window.clone().unwrap();
let (overlay, future) = start_overlay(self, &tab, move |tab_id, term| {
confirm_close_window(term, mux_window_id, window, tab_id)
@@ -2455,7 +2463,7 @@ impl TermWindow {
};
let pane_id = pane.pane_id();
if confirm {
if confirm && !pane.can_close_without_prompting() {
let window = self.window.clone().unwrap();
let (overlay, future) = start_overlay_pane(self, &pane, move |pane_id, term| {
confirm_close_pane(pane_id, term, mux_window_id, window)
@@ -2475,7 +2483,7 @@ impl TermWindow {
};
let tab_id = tab.tab_id();
let mux_window_id = self.mux_window_id;
if confirm {
if confirm && !tab.can_close_without_prompting() {
let window = self.window.clone().unwrap();
let (overlay, future) = start_overlay(self, &tab, move |tab_id, term| {
confirm_close_tab(tab_id, term, mux_window_id, window)
@@ -4055,8 +4063,15 @@ impl TermWindow {
}
}
/// Removes any overlay for the specified tab
fn cancel_overlay_for_tab(&self, tab_id: TabId) {
/// if pane_id.is_none(), removes any overlay for the specified tab.
/// Otherwise: if the overlay is the specified pane for that tab, remove it.
fn cancel_overlay_for_tab(&self, tab_id: TabId, pane_id: Option<PaneId>) {
if pane_id.is_some() {
let current = self.tab_state(tab_id).overlay.as_ref().map(|o| o.pane_id());
if current != pane_id {
return;
}
}
if let Some(pane) = self.tab_state(tab_id).overlay.take() {
Mux::get().unwrap().remove_pane(pane.pane_id());
}
@@ -4065,10 +4080,10 @@ impl TermWindow {
}
}
pub fn schedule_cancel_overlay(window: Window, tab_id: TabId) {
pub fn schedule_cancel_overlay(window: Window, tab_id: TabId, pane_id: Option<PaneId>) {
window.apply(move |myself, _| {
if let Some(myself) = myself.downcast_mut::<Self>() {
myself.cancel_overlay_for_tab(tab_id);
myself.cancel_overlay_for_tab(tab_id, pane_id);
}
Ok(())
});