Show prompt when closing last window while there's an active call

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-10-11 14:52:47 +02:00
parent feb17c29ec
commit 29c3b81a0a
4 changed files with 54 additions and 8 deletions

View file

@ -794,6 +794,16 @@ impl AsyncAppContext {
self.update(|cx| cx.activate_window(window_id)) self.update(|cx| cx.activate_window(window_id))
} }
pub fn prompt(
&mut self,
window_id: usize,
level: PromptLevel,
msg: &str,
answers: &[&str],
) -> oneshot::Receiver<usize> {
self.update(|cx| cx.prompt(window_id, level, msg, answers))
}
pub fn platform(&self) -> Arc<dyn Platform> { pub fn platform(&self) -> Arc<dyn Platform> {
self.0.borrow().platform() self.0.borrow().platform()
} }

View file

@ -17,7 +17,7 @@ use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
use collections::{hash_map, HashMap, HashSet}; use collections::{hash_map, HashMap, HashSet};
use dock::{DefaultItemFactory, Dock, ToggleDockButton}; use dock::{DefaultItemFactory, Dock, ToggleDockButton};
use drag_and_drop::DragAndDrop; use drag_and_drop::DragAndDrop;
use futures::{channel::oneshot, FutureExt}; use futures::{channel::oneshot, FutureExt, StreamExt};
use gpui::{ use gpui::{
actions, actions,
elements::*, elements::*,
@ -1231,7 +1231,7 @@ impl Workspace {
_: &CloseWindow, _: &CloseWindow,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
let prepare = self.prepare_to_close(cx); let prepare = self.prepare_to_close(false, cx);
Some(cx.spawn(|this, mut cx| async move { Some(cx.spawn(|this, mut cx| async move {
if prepare.await? { if prepare.await? {
this.update(&mut cx, |_, cx| { this.update(&mut cx, |_, cx| {
@ -1243,8 +1243,42 @@ impl Workspace {
})) }))
} }
pub fn prepare_to_close(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<bool>> { pub fn prepare_to_close(
self.save_all_internal(true, cx) &mut self,
quitting: bool,
cx: &mut ViewContext<Self>,
) -> Task<Result<bool>> {
let active_call = ActiveCall::global(cx);
let window_id = cx.window_id();
let workspace_count = cx
.window_ids()
.flat_map(|window_id| cx.root_view::<Workspace>(window_id))
.count();
cx.spawn(|this, mut cx| async move {
if !quitting
&& workspace_count == 1
&& active_call.read_with(&cx, |call, _| call.room().is_some())
{
let answer = cx
.prompt(
window_id,
PromptLevel::Warning,
"Do you want to leave the current call?",
&["Close window and hang up", "Cancel"],
)
.next()
.await;
if answer == Some(1) {
return anyhow::Ok(false);
} else {
active_call.update(&mut cx, |call, cx| call.hang_up(cx))?;
}
}
Ok(this
.update(&mut cx, |this, cx| this.save_all_internal(true, cx))
.await?)
})
} }
fn save_all(&mut self, _: &SaveAll, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> { fn save_all(&mut self, _: &SaveAll, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
@ -2944,7 +2978,7 @@ mod tests {
// When there are no dirty items, there's nothing to do. // When there are no dirty items, there's nothing to do.
let item1 = cx.add_view(&workspace, |_| TestItem::new()); let item1 = cx.add_view(&workspace, |_| TestItem::new());
workspace.update(cx, |w, cx| w.add_item(Box::new(item1.clone()), cx)); workspace.update(cx, |w, cx| w.add_item(Box::new(item1.clone()), cx));
let task = workspace.update(cx, |w, cx| w.prepare_to_close(cx)); let task = workspace.update(cx, |w, cx| w.prepare_to_close(false, cx));
assert!(task.await.unwrap()); assert!(task.await.unwrap());
// When there are dirty untitled items, prompt to save each one. If the user // When there are dirty untitled items, prompt to save each one. If the user
@ -2964,7 +2998,7 @@ mod tests {
w.add_item(Box::new(item2.clone()), cx); w.add_item(Box::new(item2.clone()), cx);
w.add_item(Box::new(item3.clone()), cx); w.add_item(Box::new(item3.clone()), cx);
}); });
let task = workspace.update(cx, |w, cx| w.prepare_to_close(cx)); let task = workspace.update(cx, |w, cx| w.prepare_to_close(false, cx));
cx.foreground().run_until_parked(); cx.foreground().run_until_parked();
cx.simulate_prompt_answer(window_id, 2 /* cancel */); cx.simulate_prompt_answer(window_id, 2 /* cancel */);
cx.foreground().run_until_parked(); cx.foreground().run_until_parked();

View file

@ -344,7 +344,9 @@ fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) {
// If the user cancels any save prompt, then keep the app open. // If the user cancels any save prompt, then keep the app open.
for workspace in workspaces { for workspace in workspaces {
if !workspace if !workspace
.update(&mut cx, |workspace, cx| workspace.prepare_to_close(cx)) .update(&mut cx, |workspace, cx| {
workspace.prepare_to_close(true, cx)
})
.await? .await?
{ {
return Ok(()); return Ok(());

View file

@ -2,7 +2,7 @@ import Theme from "../themes/common/theme";
import { backgroundColor, borderColor, text } from "./components"; import { backgroundColor, borderColor, text } from "./components";
export default function incomingCallNotification(theme: Theme): Object { export default function incomingCallNotification(theme: Theme): Object {
const avatarSize = 32; const avatarSize = 48;
return { return {
windowHeight: 74, windowHeight: 74,
windowWidth: 380, windowWidth: 380,