Remove Toast and DismissToast internal actions

This commit is contained in:
Antonio Scandurra 2023-04-27 14:43:10 +02:00
parent 2950344c25
commit b6437d6d9e
4 changed files with 75 additions and 73 deletions

View file

@ -443,7 +443,7 @@ impl Copilot {
} }
} }
fn sign_in(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { pub fn sign_in(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
if let CopilotServer::Running(server) = &mut self.server { if let CopilotServer::Running(server) = &mut self.server {
let task = match &server.sign_in_status { let task = match &server.sign_in_status {
SignInStatus::Authorized { .. } | SignInStatus::Unauthorized { .. } => { SignInStatus::Authorized { .. } | SignInStatus::Unauthorized { .. } => {

View file

@ -10,12 +10,13 @@ use gpui::{
ViewHandle, ViewHandle,
}; };
use settings::{settings_file::SettingsFile, Settings}; use settings::{settings_file::SettingsFile, Settings};
use util::ResultExt;
use workspace::{ use workspace::{
item::ItemHandle, notifications::simple_message_notification::OsOpen, DismissToast, item::ItemHandle, notifications::simple_message_notification::OsOpen, StatusItemView, Toast,
StatusItemView, Workspace,
}; };
use copilot::{Copilot, Reinstall, SignIn, SignOut, Status}; use copilot::{Copilot, Reinstall, SignOut, Status};
const COPILOT_SETTINGS_URL: &str = "https://github.com/settings/copilot"; const COPILOT_SETTINGS_URL: &str = "https://github.com/settings/copilot";
const COPILOT_STARTING_TOAST_ID: usize = 1337; const COPILOT_STARTING_TOAST_ID: usize = 1337;
@ -101,41 +102,43 @@ pub fn init(cx: &mut AppContext) {
match status { match status {
Status::Starting { task } => { Status::Starting { task } => {
cx.dispatch_action(workspace::Toast::new( let Some(workspace) = cx.root_view().clone().downcast::<Workspace>() else {
COPILOT_STARTING_TOAST_ID, return;
"Copilot is starting...", };
));
let window_id = cx.window_id(); workspace.update(cx, |workspace, cx| {
let task = task.to_owned(); workspace.show_toast(
cx.spawn(|handle, mut cx| async move { Toast::new(COPILOT_STARTING_TOAST_ID, "Copilot is starting..."),
cx,
)
});
let workspace = workspace.downgrade();
cx.spawn(|_, mut cx| async move {
task.await; task.await;
cx.update(|cx| { if let Some(copilot) = cx.read(Copilot::global) {
if let Some(copilot) = Copilot::global(cx) { workspace
let status = copilot.read(cx).status(); .update(&mut cx, |workspace, cx| match copilot.read(cx).status() {
match status { Status::Authorized => workspace.show_toast(
Status::Authorized => cx.dispatch_action_at( Toast::new(COPILOT_STARTING_TOAST_ID, "Copilot has started!"),
window_id, cx,
handle.id(),
workspace::Toast::new(
COPILOT_STARTING_TOAST_ID,
"Copilot has started!",
),
), ),
_ => { _ => {
cx.dispatch_action_at( workspace.dismiss_toast(COPILOT_STARTING_TOAST_ID, cx);
window_id, copilot
handle.id(), .update(cx, |copilot, cx| copilot.sign_in(cx))
DismissToast::new(COPILOT_STARTING_TOAST_ID), .detach_and_log_err(cx);
);
cx.dispatch_action_at(window_id, handle.id(), SignIn)
}
}
} }
}) })
.log_err();
}
}) })
.detach(); .detach();
} }
_ => cx.dispatch_action(SignIn), _ => {
copilot
.update(cx, |copilot, cx| copilot.sign_in(cx))
.detach_and_log_err(cx);
}
} }
}) })
} }
@ -219,12 +222,22 @@ impl View for CopilotButton {
let status = status.clone(); let status = status.clone();
move |_, _, cx| match status { move |_, _, cx| match status {
Status::Authorized => cx.dispatch_action(DeployCopilotMenu), Status::Authorized => cx.dispatch_action(DeployCopilotMenu),
Status::Error(ref e) => cx.dispatch_action(workspace::Toast::new_action( Status::Error(ref e) => {
if let Some(workspace) = cx.root_view().clone().downcast::<Workspace>()
{
workspace.update(cx, |workspace, cx| {
workspace.show_toast(
Toast::new_action(
COPILOT_ERROR_TOAST_ID, COPILOT_ERROR_TOAST_ID,
format!("Copilot can't be started: {}", e), format!("Copilot can't be started: {}", e),
"Reinstall Copilot", "Reinstall Copilot",
Reinstall, Reinstall,
)), ),
cx,
);
});
}
}
_ => cx.dispatch_action(DeployCopilotStartMenu), _ => cx.dispatch_action(DeployCopilotStartMenu),
} }
}) })

View file

@ -1,9 +1,7 @@
use std::{any::TypeId, ops::DerefMut}; use crate::{Toast, Workspace};
use collections::HashSet; use collections::HashSet;
use gpui::{AnyViewHandle, AppContext, Entity, View, ViewContext, ViewHandle}; use gpui::{AnyViewHandle, AppContext, Entity, View, ViewContext, ViewHandle};
use std::{any::TypeId, ops::DerefMut};
use crate::Workspace;
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.set_global(NotificationTracker::new()); cx.set_global(NotificationTracker::new());
@ -113,6 +111,28 @@ impl Workspace {
self.dismiss_notification_internal(type_id, id, cx) self.dismiss_notification_internal(type_id, id, cx)
} }
pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
self.dismiss_notification::<simple_message_notification::MessageNotification>(toast.id, cx);
self.show_notification(toast.id, cx, |cx| {
cx.add_view(|_cx| match &toast.click {
Some((click_msg, action)) => {
simple_message_notification::MessageNotification::new_boxed_action(
toast.msg.clone(),
action.boxed_clone(),
click_msg.clone(),
)
}
None => {
simple_message_notification::MessageNotification::new_message(toast.msg.clone())
}
})
})
}
pub fn dismiss_toast(&mut self, id: usize, cx: &mut ViewContext<Self>) {
self.dismiss_notification::<simple_message_notification::MessageNotification>(id, cx);
}
fn dismiss_notification_internal( fn dismiss_notification_internal(
&mut self, &mut self,
type_id: TypeId, type_id: TypeId,

View file

@ -220,17 +220,6 @@ impl Clone for Toast {
} }
} }
#[derive(Clone, PartialEq)]
pub struct DismissToast {
id: usize,
}
impl DismissToast {
pub fn new(id: usize) -> Self {
DismissToast { id }
}
}
pub type WorkspaceId = i64; pub type WorkspaceId = i64;
impl_internal_actions!( impl_internal_actions!(
@ -244,8 +233,6 @@ impl_internal_actions!(
SplitWithItem, SplitWithItem,
SplitWithProjectEntry, SplitWithProjectEntry,
OpenProjectEntryInPane, OpenProjectEntryInPane,
Toast,
DismissToast
] ]
); );
impl_actions!(workspace, [ActivatePane]); impl_actions!(workspace, [ActivatePane]);
@ -431,24 +418,6 @@ pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
.detach(); .detach();
}); });
cx.add_action(|workspace: &mut Workspace, alert: &Toast, cx| {
workspace.dismiss_notification::<MessageNotification>(alert.id, cx);
workspace.show_notification(alert.id, cx, |cx| {
cx.add_view(|_cx| match &alert.click {
Some((click_msg, action)) => MessageNotification::new_boxed_action(
alert.msg.clone(),
action.boxed_clone(),
click_msg.clone(),
),
None => MessageNotification::new_message(alert.msg.clone()),
})
})
});
cx.add_action(|workspace: &mut Workspace, alert: &DismissToast, cx| {
workspace.dismiss_notification::<MessageNotification>(alert.id, cx);
});
let client = &app_state.client; let client = &app_state.client;
client.add_view_request_handler(Workspace::handle_follow); client.add_view_request_handler(Workspace::handle_follow);
client.add_view_message_handler(Workspace::handle_unfollow); client.add_view_message_handler(Workspace::handle_unfollow);