Refactor workspace notifications to use explicit NotificationId
type (#10342)
This PR reworks the way workspace notifications are identified to use a new `NotificationId` type. A `NotificationId` is bound to a given type that is used as a unique identifier. Generally this will be a unit struct that can be used to uniquely identify this notification. A `NotificationId` can also accept an optional `ElementId` in order to distinguish between different notifications of the same type. This system avoids the issue we had previously of selecting `usize` IDs somewhat arbitrarily and running the risk of having two independent notifications collide (and thus interfere with each other). This also fixes a bug where multiple suggestion notifications for the same extension could be live at once Fixes https://github.com/zed-industries/zed/issues/10320. Release Notes: - Fixed a bug where multiple extension suggestions for the same extension could be shown at once ([#10320](https://github.com/zed-industries/zed/issues/10320)). --------- Co-authored-by: Max <max@zed.dev>
This commit is contained in:
parent
322f68f3d6
commit
8cbdd9e0fa
12 changed files with 212 additions and 98 deletions
|
@ -46,6 +46,7 @@ use ui::{
|
||||||
};
|
};
|
||||||
use util::{paths::CONVERSATIONS_DIR, post_inc, ResultExt, TryFutureExt};
|
use util::{paths::CONVERSATIONS_DIR, post_inc, ResultExt, TryFutureExt};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
dock::{DockPosition, Panel, PanelEvent},
|
dock::{DockPosition, Panel, PanelEvent},
|
||||||
searchable::Direction,
|
searchable::Direction,
|
||||||
|
@ -418,10 +419,14 @@ impl AssistantPanel {
|
||||||
if pending_assist.inline_assistant.is_none() {
|
if pending_assist.inline_assistant.is_none() {
|
||||||
if let Some(workspace) = this.workspace.upgrade() {
|
if let Some(workspace) = this.workspace.upgrade() {
|
||||||
workspace.update(cx, |workspace, cx| {
|
workspace.update(cx, |workspace, cx| {
|
||||||
workspace.show_toast(
|
struct InlineAssistantError;
|
||||||
Toast::new(inline_assist_id, error),
|
|
||||||
cx,
|
let id =
|
||||||
);
|
NotificationId::identified::<InlineAssistantError>(
|
||||||
|
inline_assist_id,
|
||||||
|
);
|
||||||
|
|
||||||
|
workspace.show_toast(Toast::new(id, error), cx);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ use util::{
|
||||||
http::{HttpClient, HttpClientWithUrl},
|
http::{HttpClient, HttpClientWithUrl},
|
||||||
ResultExt,
|
ResultExt,
|
||||||
};
|
};
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::Workspace;
|
use workspace::Workspace;
|
||||||
|
|
||||||
const SHOULD_SHOW_UPDATE_NOTIFICATION_KEY: &str = "auto-updater-should-show-updated-notification";
|
const SHOULD_SHOW_UPDATE_NOTIFICATION_KEY: &str = "auto-updater-should-show-updated-notification";
|
||||||
|
@ -262,9 +263,11 @@ pub fn notify_of_any_new_update(cx: &mut ViewContext<Workspace>) -> Option<()> {
|
||||||
let should_show_notification = should_show_notification.await?;
|
let should_show_notification = should_show_notification.await?;
|
||||||
if should_show_notification {
|
if should_show_notification {
|
||||||
workspace.update(&mut cx, |workspace, cx| {
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
workspace.show_notification(0, cx, |cx| {
|
workspace.show_notification(
|
||||||
cx.new_view(|_| UpdateNotification::new(version))
|
NotificationId::unique::<UpdateNotification>(),
|
||||||
});
|
cx,
|
||||||
|
|cx| cx.new_view(|_| UpdateNotification::new(version)),
|
||||||
|
);
|
||||||
updater
|
updater
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.set_should_show_update_notification(false, cx)
|
.set_should_show_update_notification(false, cx)
|
||||||
|
|
|
@ -22,6 +22,7 @@ use std::{
|
||||||
};
|
};
|
||||||
use ui::{prelude::*, Label};
|
use ui::{prelude::*, Label};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
item::{FollowableItem, Item, ItemEvent, ItemHandle},
|
item::{FollowableItem, Item, ItemEvent, ItemHandle},
|
||||||
register_followable_item,
|
register_followable_item,
|
||||||
|
@ -269,7 +270,15 @@ impl ChannelView {
|
||||||
cx.write_to_clipboard(ClipboardItem::new(link));
|
cx.write_to_clipboard(ClipboardItem::new(link));
|
||||||
self.workspace
|
self.workspace
|
||||||
.update(cx, |workspace, cx| {
|
.update(cx, |workspace, cx| {
|
||||||
workspace.show_toast(Toast::new(0, "Link copied to clipboard"), cx);
|
struct CopyLinkForPositionToast;
|
||||||
|
|
||||||
|
workspace.show_toast(
|
||||||
|
Toast::new(
|
||||||
|
NotificationId::unique::<CopyLinkForPositionToast>(),
|
||||||
|
"Link copied to clipboard",
|
||||||
|
),
|
||||||
|
cx,
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ use std::{sync::Arc, time::Duration};
|
||||||
use time::{OffsetDateTime, UtcOffset};
|
use time::{OffsetDateTime, UtcOffset};
|
||||||
use ui::{h_flex, prelude::*, v_flex, Avatar, Button, Icon, IconButton, IconName, Label, Tooltip};
|
use ui::{h_flex, prelude::*, v_flex, Avatar, Button, Icon, IconButton, IconName, Label, Tooltip};
|
||||||
use util::{ResultExt, TryFutureExt};
|
use util::{ResultExt, TryFutureExt};
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
dock::{DockPosition, Panel, PanelEvent},
|
dock::{DockPosition, Panel, PanelEvent},
|
||||||
Workspace,
|
Workspace,
|
||||||
|
@ -534,8 +535,10 @@ impl NotificationPanel {
|
||||||
|
|
||||||
self.workspace
|
self.workspace
|
||||||
.update(cx, |workspace, cx| {
|
.update(cx, |workspace, cx| {
|
||||||
workspace.dismiss_notification::<NotificationToast>(0, cx);
|
let id = NotificationId::unique::<NotificationToast>();
|
||||||
workspace.show_notification(0, cx, |cx| {
|
|
||||||
|
workspace.dismiss_notification(&id, cx);
|
||||||
|
workspace.show_notification(id, cx, |cx| {
|
||||||
let workspace = cx.view().downgrade();
|
let workspace = cx.view().downgrade();
|
||||||
cx.new_view(|_| NotificationToast {
|
cx.new_view(|_| NotificationToast {
|
||||||
notification_id,
|
notification_id,
|
||||||
|
@ -554,7 +557,8 @@ impl NotificationPanel {
|
||||||
self.current_notification_toast.take();
|
self.current_notification_toast.take();
|
||||||
self.workspace
|
self.workspace
|
||||||
.update(cx, |workspace, cx| {
|
.update(cx, |workspace, cx| {
|
||||||
workspace.dismiss_notification::<NotificationToast>(0, cx)
|
let id = NotificationId::unique::<NotificationToast>();
|
||||||
|
workspace.dismiss_notification(&id, cx)
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ use language::{
|
||||||
use settings::{update_settings_file, Settings, SettingsStore};
|
use settings::{update_settings_file, Settings, SettingsStore};
|
||||||
use std::{path::Path, sync::Arc};
|
use std::{path::Path, sync::Arc};
|
||||||
use util::{paths, ResultExt};
|
use util::{paths, ResultExt};
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
create_and_open_local_file,
|
create_and_open_local_file,
|
||||||
item::ItemHandle,
|
item::ItemHandle,
|
||||||
|
@ -25,8 +26,10 @@ use workspace::{
|
||||||
use zed_actions::OpenBrowser;
|
use zed_actions::OpenBrowser;
|
||||||
|
|
||||||
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_ERROR_TOAST_ID: usize = 1338;
|
struct CopilotStartingToast;
|
||||||
|
|
||||||
|
struct CopilotErrorToast;
|
||||||
|
|
||||||
pub struct CopilotButton {
|
pub struct CopilotButton {
|
||||||
editor_subscription: Option<(Subscription, usize)>,
|
editor_subscription: Option<(Subscription, usize)>,
|
||||||
|
@ -74,7 +77,7 @@ impl Render for CopilotButton {
|
||||||
.update(cx, |workspace, cx| {
|
.update(cx, |workspace, cx| {
|
||||||
workspace.show_toast(
|
workspace.show_toast(
|
||||||
Toast::new(
|
Toast::new(
|
||||||
COPILOT_ERROR_TOAST_ID,
|
NotificationId::unique::<CopilotErrorToast>(),
|
||||||
format!("Copilot can't be started: {}", e),
|
format!("Copilot can't be started: {}", e),
|
||||||
)
|
)
|
||||||
.on_click(
|
.on_click(
|
||||||
|
@ -350,7 +353,10 @@ pub fn initiate_sign_in(cx: &mut WindowContext) {
|
||||||
|
|
||||||
let Ok(workspace) = workspace.update(cx, |workspace, cx| {
|
let Ok(workspace) = workspace.update(cx, |workspace, cx| {
|
||||||
workspace.show_toast(
|
workspace.show_toast(
|
||||||
Toast::new(COPILOT_STARTING_TOAST_ID, "Copilot is starting..."),
|
Toast::new(
|
||||||
|
NotificationId::unique::<CopilotStartingToast>(),
|
||||||
|
"Copilot is starting...",
|
||||||
|
),
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
workspace.weak_handle()
|
workspace.weak_handle()
|
||||||
|
@ -364,11 +370,17 @@ pub fn initiate_sign_in(cx: &mut WindowContext) {
|
||||||
workspace
|
workspace
|
||||||
.update(&mut cx, |workspace, cx| match copilot.read(cx).status() {
|
.update(&mut cx, |workspace, cx| match copilot.read(cx).status() {
|
||||||
Status::Authorized => workspace.show_toast(
|
Status::Authorized => workspace.show_toast(
|
||||||
Toast::new(COPILOT_STARTING_TOAST_ID, "Copilot has started!"),
|
Toast::new(
|
||||||
|
NotificationId::unique::<CopilotStartingToast>(),
|
||||||
|
"Copilot has started!",
|
||||||
|
),
|
||||||
cx,
|
cx,
|
||||||
),
|
),
|
||||||
_ => {
|
_ => {
|
||||||
workspace.dismiss_toast(COPILOT_STARTING_TOAST_ID, cx);
|
workspace.dismiss_toast(
|
||||||
|
&NotificationId::unique::<CopilotStartingToast>(),
|
||||||
|
cx,
|
||||||
|
);
|
||||||
copilot
|
copilot
|
||||||
.update(cx, |copilot, cx| copilot.sign_in(cx))
|
.update(cx, |copilot, cx| copilot.sign_in(cx))
|
||||||
.detach_and_log_err(cx);
|
.detach_and_log_err(cx);
|
||||||
|
|
|
@ -129,6 +129,7 @@ use ui::{
|
||||||
Tooltip,
|
Tooltip,
|
||||||
};
|
};
|
||||||
use util::{defer, maybe, post_inc, RangeExt, ResultExt, TryFutureExt};
|
use util::{defer, maybe, post_inc, RangeExt, ResultExt, TryFutureExt};
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::Toast;
|
use workspace::Toast;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
searchable::SearchEvent, ItemNavHistory, SplitDirection, ViewId, Workspace, WorkspaceId,
|
searchable::SearchEvent, ItemNavHistory, SplitDirection, ViewId, Workspace, WorkspaceId,
|
||||||
|
@ -8922,7 +8923,12 @@ impl Editor {
|
||||||
|
|
||||||
if let Some(workspace) = self.workspace() {
|
if let Some(workspace) = self.workspace() {
|
||||||
workspace.update(cx, |workspace, cx| {
|
workspace.update(cx, |workspace, cx| {
|
||||||
workspace.show_toast(Toast::new(0x156a5f9ee, message), cx)
|
struct CopyPermalinkToLine;
|
||||||
|
|
||||||
|
workspace.show_toast(
|
||||||
|
Toast::new(NotificationId::unique::<CopyPermalinkToLine>(), message),
|
||||||
|
cx,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8943,7 +8949,12 @@ impl Editor {
|
||||||
|
|
||||||
if let Some(workspace) = self.workspace() {
|
if let Some(workspace) = self.workspace() {
|
||||||
workspace.update(cx, |workspace, cx| {
|
workspace.update(cx, |workspace, cx| {
|
||||||
workspace.show_toast(Toast::new(0x45a8978, message), cx)
|
struct OpenPermalinkToLine;
|
||||||
|
|
||||||
|
workspace.show_toast(
|
||||||
|
Toast::new(NotificationId::unique::<OpenPermalinkToLine>(), message),
|
||||||
|
cx,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,10 @@ use std::sync::{Arc, OnceLock};
|
||||||
use db::kvp::KEY_VALUE_STORE;
|
use db::kvp::KEY_VALUE_STORE;
|
||||||
use editor::Editor;
|
use editor::Editor;
|
||||||
use extension::ExtensionStore;
|
use extension::ExtensionStore;
|
||||||
use gpui::{Entity, Model, VisualContext};
|
use gpui::{Model, VisualContext};
|
||||||
use language::Buffer;
|
use language::Buffer;
|
||||||
use ui::ViewContext;
|
use ui::{SharedString, ViewContext};
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{notifications::simple_message_notification, Workspace};
|
use workspace::{notifications::simple_message_notification, Workspace};
|
||||||
|
|
||||||
fn suggested_extensions() -> &'static HashMap<&'static str, Arc<str>> {
|
fn suggested_extensions() -> &'static HashMap<&'static str, Arc<str>> {
|
||||||
|
@ -140,7 +141,13 @@ pub(crate) fn suggest(buffer: Model<Buffer>, cx: &mut ViewContext<Workspace>) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
workspace.show_notification(buffer.entity_id().as_u64() as usize, cx, |cx| {
|
struct ExtensionSuggestionNotification;
|
||||||
|
|
||||||
|
let notification_id = NotificationId::identified::<ExtensionSuggestionNotification>(
|
||||||
|
SharedString::from(extension_id.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
workspace.show_notification(notification_id, cx, |cx| {
|
||||||
cx.new_view(move |_cx| {
|
cx.new_view(move |_cx| {
|
||||||
simple_message_notification::MessageNotification::new(format!(
|
simple_message_notification::MessageNotification::new(format!(
|
||||||
"Do you want to install the recommended '{}' extension for '{}' files?",
|
"Do you want to install the recommended '{}' extension for '{}' files?",
|
||||||
|
|
|
@ -17,6 +17,7 @@ use regex::Regex;
|
||||||
use serde_derive::Serialize;
|
use serde_derive::Serialize;
|
||||||
use ui::{prelude::*, Button, ButtonStyle, IconPosition, Tooltip};
|
use ui::{prelude::*, Button, ButtonStyle, IconPosition, Tooltip};
|
||||||
use util::{http::HttpClient, ResultExt};
|
use util::{http::HttpClient, ResultExt};
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{DismissDecision, ModalView, Toast, Workspace};
|
use workspace::{DismissDecision, ModalView, Toast, Workspace};
|
||||||
|
|
||||||
use crate::{system_specs::SystemSpecs, GiveFeedback, OpenZedRepo};
|
use crate::{system_specs::SystemSpecs, GiveFeedback, OpenZedRepo};
|
||||||
|
@ -127,11 +128,11 @@ impl FeedbackModal {
|
||||||
let is_local_project = project.read(cx).is_local();
|
let is_local_project = project.read(cx).is_local();
|
||||||
|
|
||||||
if !is_local_project {
|
if !is_local_project {
|
||||||
const TOAST_ID: usize = 0xdeadbeef;
|
struct FeedbackInRemoteProject;
|
||||||
|
|
||||||
workspace.show_toast(
|
workspace.show_toast(
|
||||||
Toast::new(
|
Toast::new(
|
||||||
TOAST_ID,
|
NotificationId::unique::<FeedbackInRemoteProject>(),
|
||||||
"You can only submit feedback in your own project.",
|
"You can only submit feedback in your own project.",
|
||||||
),
|
),
|
||||||
cx,
|
cx,
|
||||||
|
|
|
@ -13,6 +13,7 @@ use ui::{
|
||||||
LabelSize, ListItem, ListItemSpacing, Selectable,
|
LabelSize, ListItem, ListItemSpacing, Selectable,
|
||||||
};
|
};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
use workspace::{ModalView, Toast, Workspace};
|
use workspace::{ModalView, Toast, Workspace};
|
||||||
|
|
||||||
actions!(branches, [OpenRecent]);
|
actions!(branches, [OpenRecent]);
|
||||||
|
@ -125,9 +126,11 @@ impl BranchListDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display_error_toast(&self, message: String, cx: &mut WindowContext<'_>) {
|
fn display_error_toast(&self, message: String, cx: &mut WindowContext<'_>) {
|
||||||
const GIT_CHECKOUT_FAILURE_ID: usize = 2048;
|
|
||||||
self.workspace.update(cx, |model, ctx| {
|
self.workspace.update(cx, |model, ctx| {
|
||||||
model.show_toast(Toast::new(GIT_CHECKOUT_FAILURE_ID, message), ctx)
|
struct GitCheckoutFailure;
|
||||||
|
let id = NotificationId::unique::<GitCheckoutFailure>();
|
||||||
|
|
||||||
|
model.show_toast(Toast::new(id, message), ctx)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,34 @@ pub fn init(cx: &mut AppContext) {
|
||||||
cx.set_global(NotificationTracker::new());
|
cx.set_global(NotificationTracker::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct NotificationId {
|
||||||
|
/// A [`TypeId`] used to uniquely identify this notification.
|
||||||
|
type_id: TypeId,
|
||||||
|
/// A supplementary ID used to distinguish between multiple
|
||||||
|
/// notifications that have the same [`type_id`](Self::type_id);
|
||||||
|
id: Option<ElementId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NotificationId {
|
||||||
|
/// Returns a unique [`NotificationId`] for the given type.
|
||||||
|
pub fn unique<T: 'static>() -> Self {
|
||||||
|
Self {
|
||||||
|
type_id: TypeId::of::<T>(),
|
||||||
|
id: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a [`NotificationId`] for the given type that is also identified
|
||||||
|
/// by the provided ID.
|
||||||
|
pub fn identified<T: 'static>(id: impl Into<ElementId>) -> Self {
|
||||||
|
Self {
|
||||||
|
type_id: TypeId::of::<T>(),
|
||||||
|
id: Some(id.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Notification: EventEmitter<DismissEvent> + Render {}
|
pub trait Notification: EventEmitter<DismissEvent> + Render {}
|
||||||
|
|
||||||
impl<V: EventEmitter<DismissEvent> + Render> Notification for V {}
|
impl<V: EventEmitter<DismissEvent> + Render> Notification for V {}
|
||||||
|
@ -41,13 +69,13 @@ impl From<&dyn NotificationHandle> for AnyView {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct NotificationTracker {
|
pub(crate) struct NotificationTracker {
|
||||||
notifications_sent: HashMap<TypeId, Vec<usize>>,
|
notifications_sent: HashMap<TypeId, Vec<NotificationId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Global for NotificationTracker {}
|
impl Global for NotificationTracker {}
|
||||||
|
|
||||||
impl std::ops::Deref for NotificationTracker {
|
impl std::ops::Deref for NotificationTracker {
|
||||||
type Target = HashMap<TypeId, Vec<usize>>;
|
type Target = HashMap<TypeId, Vec<NotificationId>>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.notifications_sent
|
&self.notifications_sent
|
||||||
|
@ -71,45 +99,46 @@ impl NotificationTracker {
|
||||||
impl Workspace {
|
impl Workspace {
|
||||||
pub fn has_shown_notification_once<V: Notification>(
|
pub fn has_shown_notification_once<V: Notification>(
|
||||||
&self,
|
&self,
|
||||||
id: usize,
|
id: &NotificationId,
|
||||||
cx: &ViewContext<Self>,
|
cx: &ViewContext<Self>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
cx.global::<NotificationTracker>()
|
cx.global::<NotificationTracker>()
|
||||||
.get(&TypeId::of::<V>())
|
.get(&TypeId::of::<V>())
|
||||||
.map(|ids| ids.contains(&id))
|
.map(|ids| ids.contains(id))
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_notification_once<V: Notification>(
|
pub fn show_notification_once<V: Notification>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: usize,
|
id: NotificationId,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
|
build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
|
||||||
) {
|
) {
|
||||||
if !self.has_shown_notification_once::<V>(id, cx) {
|
if !self.has_shown_notification_once::<V>(&id, cx) {
|
||||||
let tracker = cx.global_mut::<NotificationTracker>();
|
let tracker = cx.global_mut::<NotificationTracker>();
|
||||||
let entry = tracker.entry(TypeId::of::<V>()).or_default();
|
let entry = tracker.entry(TypeId::of::<V>()).or_default();
|
||||||
entry.push(id);
|
entry.push(id.clone());
|
||||||
self.show_notification::<V>(id, cx, build_notification)
|
self.show_notification::<V>(id, cx, build_notification)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_notification<V: Notification>(
|
pub fn show_notification<V: Notification>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: usize,
|
id: NotificationId,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
|
build_notification: impl FnOnce(&mut ViewContext<Self>) -> View<V>,
|
||||||
) {
|
) {
|
||||||
let type_id = TypeId::of::<V>();
|
self.dismiss_notification_internal(&id, cx);
|
||||||
self.dismiss_notification_internal(type_id, id, cx);
|
|
||||||
|
|
||||||
let notification = build_notification(cx);
|
let notification = build_notification(cx);
|
||||||
cx.subscribe(¬ification, move |this, _, _: &DismissEvent, cx| {
|
cx.subscribe(¬ification, {
|
||||||
this.dismiss_notification_internal(type_id, id, cx);
|
let id = id.clone();
|
||||||
|
move |this, _, _: &DismissEvent, cx| {
|
||||||
|
this.dismiss_notification_internal(&id, cx);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
self.notifications
|
self.notifications.push((id, Box::new(notification)));
|
||||||
.push((type_id, id, Box::new(notification)));
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,21 +146,25 @@ impl Workspace {
|
||||||
where
|
where
|
||||||
E: std::fmt::Debug,
|
E: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
self.show_notification(0, cx, |cx| {
|
struct WorkspaceErrorNotification;
|
||||||
cx.new_view(|_cx| {
|
|
||||||
simple_message_notification::MessageNotification::new(format!("Error: {err:?}"))
|
self.show_notification(
|
||||||
})
|
NotificationId::unique::<WorkspaceErrorNotification>(),
|
||||||
});
|
cx,
|
||||||
|
|cx| {
|
||||||
|
cx.new_view(|_cx| {
|
||||||
|
simple_message_notification::MessageNotification::new(format!("Error: {err:?}"))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dismiss_notification<V: Notification>(&mut self, id: usize, cx: &mut ViewContext<Self>) {
|
pub fn dismiss_notification(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
|
||||||
let type_id = TypeId::of::<V>();
|
self.dismiss_notification_internal(id, cx)
|
||||||
|
|
||||||
self.dismiss_notification_internal(type_id, id, cx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
|
pub fn show_toast(&mut self, toast: Toast, cx: &mut ViewContext<Self>) {
|
||||||
self.dismiss_notification::<simple_message_notification::MessageNotification>(toast.id, cx);
|
self.dismiss_notification(&toast.id, cx);
|
||||||
self.show_notification(toast.id, cx, |cx| {
|
self.show_notification(toast.id, cx, |cx| {
|
||||||
cx.new_view(|_cx| match toast.on_click.as_ref() {
|
cx.new_view(|_cx| match toast.on_click.as_ref() {
|
||||||
Some((click_msg, on_click)) => {
|
Some((click_msg, on_click)) => {
|
||||||
|
@ -145,25 +178,19 @@ impl Workspace {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dismiss_toast(&mut self, id: usize, cx: &mut ViewContext<Self>) {
|
pub fn dismiss_toast(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
|
||||||
self.dismiss_notification::<simple_message_notification::MessageNotification>(id, cx);
|
self.dismiss_notification(id, cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dismiss_notification_internal(
|
fn dismiss_notification_internal(&mut self, id: &NotificationId, cx: &mut ViewContext<Self>) {
|
||||||
&mut self,
|
self.notifications.retain(|(existing_id, _)| {
|
||||||
type_id: TypeId,
|
if existing_id == id {
|
||||||
id: usize,
|
cx.notify();
|
||||||
cx: &mut ViewContext<Self>,
|
false
|
||||||
) {
|
} else {
|
||||||
self.notifications
|
true
|
||||||
.retain(|(existing_type_id, existing_id, _)| {
|
}
|
||||||
if (*existing_type_id, *existing_id) == (type_id, id) {
|
});
|
||||||
cx.notify();
|
|
||||||
false
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,6 +83,7 @@ use util::ResultExt;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
pub use workspace_settings::{AutosaveSetting, TabBarSettings, WorkspaceSettings};
|
pub use workspace_settings::{AutosaveSetting, TabBarSettings, WorkspaceSettings};
|
||||||
|
|
||||||
|
use crate::notifications::NotificationId;
|
||||||
use crate::persistence::{
|
use crate::persistence::{
|
||||||
model::{DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup},
|
model::{DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup},
|
||||||
SerializedAxis,
|
SerializedAxis,
|
||||||
|
@ -191,16 +192,14 @@ impl_actions!(
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Toast {
|
pub struct Toast {
|
||||||
id: usize,
|
id: NotificationId,
|
||||||
msg: Cow<'static, str>,
|
msg: Cow<'static, str>,
|
||||||
#[serde(skip)]
|
|
||||||
on_click: Option<(Cow<'static, str>, Arc<dyn Fn(&mut WindowContext)>)>,
|
on_click: Option<(Cow<'static, str>, Arc<dyn Fn(&mut WindowContext)>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Toast {
|
impl Toast {
|
||||||
pub fn new<I: Into<Cow<'static, str>>>(id: usize, msg: I) -> Self {
|
pub fn new<I: Into<Cow<'static, str>>>(id: NotificationId, msg: I) -> Self {
|
||||||
Toast {
|
Toast {
|
||||||
id,
|
id,
|
||||||
msg: msg.into(),
|
msg: msg.into(),
|
||||||
|
@ -229,7 +228,7 @@ impl PartialEq for Toast {
|
||||||
impl Clone for Toast {
|
impl Clone for Toast {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Toast {
|
Toast {
|
||||||
id: self.id,
|
id: self.id.clone(),
|
||||||
msg: self.msg.clone(),
|
msg: self.msg.clone(),
|
||||||
on_click: self.on_click.clone(),
|
on_click: self.on_click.clone(),
|
||||||
}
|
}
|
||||||
|
@ -555,7 +554,7 @@ pub struct Workspace {
|
||||||
status_bar: View<StatusBar>,
|
status_bar: View<StatusBar>,
|
||||||
modal_layer: View<ModalLayer>,
|
modal_layer: View<ModalLayer>,
|
||||||
titlebar_item: Option<AnyView>,
|
titlebar_item: Option<AnyView>,
|
||||||
notifications: Vec<(TypeId, usize, Box<dyn NotificationHandle>)>,
|
notifications: Vec<(NotificationId, Box<dyn NotificationHandle>)>,
|
||||||
project: Model<Project>,
|
project: Model<Project>,
|
||||||
follower_states: HashMap<View<Pane>, FollowerState>,
|
follower_states: HashMap<View<Pane>, FollowerState>,
|
||||||
last_leaders_by_pane: HashMap<WeakView<Pane>, PeerId>,
|
last_leaders_by_pane: HashMap<WeakView<Pane>, PeerId>,
|
||||||
|
@ -634,18 +633,32 @@ impl Workspace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
project::Event::Notification(message) => this.show_notification(0, cx, |cx| {
|
project::Event::Notification(message) => {
|
||||||
cx.new_view(|_| MessageNotification::new(message.clone()))
|
struct ProjectNotification;
|
||||||
}),
|
|
||||||
|
this.show_notification(
|
||||||
|
NotificationId::unique::<ProjectNotification>(),
|
||||||
|
cx,
|
||||||
|
|cx| cx.new_view(|_| MessageNotification::new(message.clone())),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
project::Event::LanguageServerPrompt(request) => {
|
project::Event::LanguageServerPrompt(request) => {
|
||||||
|
struct LanguageServerPrompt;
|
||||||
|
|
||||||
let mut hasher = DefaultHasher::new();
|
let mut hasher = DefaultHasher::new();
|
||||||
request.lsp_name.as_str().hash(&mut hasher);
|
request.lsp_name.as_str().hash(&mut hasher);
|
||||||
let id = hasher.finish();
|
let id = hasher.finish();
|
||||||
|
|
||||||
this.show_notification(id as usize, cx, |cx| {
|
this.show_notification(
|
||||||
cx.new_view(|_| notifications::LanguageServerPrompt::new(request.clone()))
|
NotificationId::identified::<LanguageServerPrompt>(id as usize),
|
||||||
});
|
cx,
|
||||||
|
|cx| {
|
||||||
|
cx.new_view(|_| {
|
||||||
|
notifications::LanguageServerPrompt::new(request.clone())
|
||||||
|
})
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -2834,7 +2847,7 @@ impl Workspace {
|
||||||
.children(
|
.children(
|
||||||
self.notifications
|
self.notifications
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, _, notification)| notification.to_any()),
|
.map(|(_, notification)| notification.to_any()),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3831,13 +3844,19 @@ fn notify_if_database_failed(workspace: WindowHandle<Workspace>, cx: &mut AsyncA
|
||||||
workspace
|
workspace
|
||||||
.update(cx, |workspace, cx| {
|
.update(cx, |workspace, cx| {
|
||||||
if (*db::ALL_FILE_DB_FAILED).load(std::sync::atomic::Ordering::Acquire) {
|
if (*db::ALL_FILE_DB_FAILED).load(std::sync::atomic::Ordering::Acquire) {
|
||||||
workspace.show_notification_once(0, cx, |cx| {
|
struct DatabaseFailedNotification;
|
||||||
cx.new_view(|_| {
|
|
||||||
MessageNotification::new("Failed to load the database file.")
|
workspace.show_notification_once(
|
||||||
.with_click_message("Click to let us know about this error")
|
NotificationId::unique::<DatabaseFailedNotification>(),
|
||||||
.on_click(|cx| cx.open_url(REPORT_ISSUE_URL))
|
cx,
|
||||||
})
|
|cx| {
|
||||||
});
|
cx.new_view(|_| {
|
||||||
|
MessageNotification::new("Failed to load the database file.")
|
||||||
|
.with_click_message("Click to let us know about this error")
|
||||||
|
.on_click(|cx| cx.open_url(REPORT_ISSUE_URL))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.log_err();
|
.log_err();
|
||||||
|
|
|
@ -34,6 +34,7 @@ use task::{
|
||||||
static_source::{StaticSource, TrackedFile},
|
static_source::{StaticSource, TrackedFile},
|
||||||
};
|
};
|
||||||
use theme::ActiveTheme;
|
use theme::ActiveTheme;
|
||||||
|
use workspace::notifications::NotificationId;
|
||||||
|
|
||||||
use terminal_view::terminal_panel::{self, TerminalPanel};
|
use terminal_view::terminal_panel::{self, TerminalPanel};
|
||||||
use util::{
|
use util::{
|
||||||
|
@ -253,9 +254,11 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
|
||||||
.await
|
.await
|
||||||
.context("error creating CLI symlink")?;
|
.context("error creating CLI symlink")?;
|
||||||
workspace.update(&mut cx, |workspace, cx| {
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
|
struct InstalledZedCli;
|
||||||
|
|
||||||
workspace.show_toast(
|
workspace.show_toast(
|
||||||
Toast::new(
|
Toast::new(
|
||||||
0,
|
NotificationId::unique::<InstalledZedCli>(),
|
||||||
format!(
|
format!(
|
||||||
"Installed `zed` to {}. You can launch {} from your terminal.",
|
"Installed `zed` to {}. You can launch {} from your terminal.",
|
||||||
path.to_string_lossy(),
|
path.to_string_lossy(),
|
||||||
|
@ -274,9 +277,11 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
|
||||||
cx.spawn(|workspace, mut cx| async move {
|
cx.spawn(|workspace, mut cx| async move {
|
||||||
register_zed_scheme(&cx).await?;
|
register_zed_scheme(&cx).await?;
|
||||||
workspace.update(&mut cx, |workspace, cx| {
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
|
struct RegisterZedScheme;
|
||||||
|
|
||||||
workspace.show_toast(
|
workspace.show_toast(
|
||||||
Toast::new(
|
Toast::new(
|
||||||
0,
|
NotificationId::unique::<RegisterZedScheme>(),
|
||||||
format!(
|
format!(
|
||||||
"zed:// links will now open in {}.",
|
"zed:// links will now open in {}.",
|
||||||
ReleaseChannel::global(cx).display_name()
|
ReleaseChannel::global(cx).display_name()
|
||||||
|
@ -555,14 +560,20 @@ fn open_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
|
||||||
workspace
|
workspace
|
||||||
.update(&mut cx, |workspace, cx| {
|
.update(&mut cx, |workspace, cx| {
|
||||||
let Some(log) = log else {
|
let Some(log) = log else {
|
||||||
workspace.show_notification(29, cx, |cx| {
|
struct OpenLogError;
|
||||||
cx.new_view(|_| {
|
|
||||||
MessageNotification::new(format!(
|
workspace.show_notification(
|
||||||
"Unable to access/open log file at path {:?}",
|
NotificationId::unique::<OpenLogError>(),
|
||||||
paths::LOG.as_path()
|
cx,
|
||||||
))
|
|cx| {
|
||||||
})
|
cx.new_view(|_| {
|
||||||
});
|
MessageNotification::new(format!(
|
||||||
|
"Unable to access/open log file at path {:?}",
|
||||||
|
paths::LOG.as_path()
|
||||||
|
))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let project = workspace.project().clone();
|
let project = workspace.project().clone();
|
||||||
|
@ -749,7 +760,9 @@ fn open_local_file(
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
} else {
|
} else {
|
||||||
workspace.show_notification(0, cx, |cx| {
|
struct NoOpenFolders;
|
||||||
|
|
||||||
|
workspace.show_notification(NotificationId::unique::<NoOpenFolders>(), cx, |cx| {
|
||||||
cx.new_view(|_| MessageNotification::new("This project has no folders open."))
|
cx.new_view(|_| MessageNotification::new("This project has no folders open."))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue