Add with_type_id to support set a struct as a Notification unique id. (#151)
This commit is contained in:
parent
981d1391e6
commit
36475bac76
2 changed files with 48 additions and 8 deletions
|
|
@ -495,10 +495,13 @@ impl Render for ModalStory {
|
||||||
Button::new("show-notify-warning", cx)
|
Button::new("show-notify-warning", cx)
|
||||||
.label("Notification with Title")
|
.label("Notification with Title")
|
||||||
.on_click(cx.listener(|_, _, cx| {
|
.on_click(cx.listener(|_, _, cx| {
|
||||||
|
struct TestNotification;
|
||||||
|
|
||||||
cx.push_notification(
|
cx.push_notification(
|
||||||
Notification::new(
|
Notification::new(
|
||||||
"你已经成功保存了文件,但是有一些警告信息需要你注意。",
|
"你已经成功保存了文件,但是有一些警告信息需要你注意。",
|
||||||
)
|
)
|
||||||
|
.with_type_id::<TestNotification>()
|
||||||
.title("保存成功")
|
.title("保存成功")
|
||||||
.icon(IconName::Inbox)
|
.icon(IconName::Inbox)
|
||||||
.autohide(false)
|
.autohide(false)
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{any::TypeId, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, prelude::FluentBuilder as _, px, Animation, AnimationExt, ClickEvent, DismissEvent,
|
div, prelude::FluentBuilder as _, px, Animation, AnimationExt, ClickEvent, DismissEvent,
|
||||||
ElementId, EventEmitter, InteractiveElement as _, IntoElement, ParentElement as _, Render,
|
EventEmitter, InteractiveElement as _, IntoElement, ParentElement as _, Render, SharedString,
|
||||||
SharedString, StatefulInteractiveElement, Styled, View, ViewContext, VisualContext,
|
StatefulInteractiveElement, Styled, View, ViewContext, VisualContext, WindowContext,
|
||||||
WindowContext,
|
|
||||||
};
|
};
|
||||||
use smol::Timer;
|
use smol::Timer;
|
||||||
|
|
||||||
|
|
@ -20,12 +19,36 @@ pub enum NotificationType {
|
||||||
Error,
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub(crate) enum NotificationId {
|
||||||
|
Type(TypeId),
|
||||||
|
Id(SharedString),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TypeId> for NotificationId {
|
||||||
|
fn from(type_id: TypeId) -> Self {
|
||||||
|
Self::Type(type_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SharedString> for NotificationId {
|
||||||
|
fn from(id: SharedString) -> Self {
|
||||||
|
Self::Id(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&'static str> for NotificationId {
|
||||||
|
fn from(id: &'static str) -> Self {
|
||||||
|
Self::Id(id.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Notification {
|
pub struct Notification {
|
||||||
/// The id is used make the notification unique.
|
/// The id is used make the notification unique.
|
||||||
/// Then you push a notification with the same id, the previous notification will be replaced.
|
/// Then you push a notification with the same id, the previous notification will be replaced.
|
||||||
///
|
///
|
||||||
/// None means the notification will be added to the end of the list.
|
/// None means the notification will be added to the end of the list.
|
||||||
id: ElementId,
|
id: NotificationId,
|
||||||
type_: NotificationType,
|
type_: NotificationType,
|
||||||
title: Option<SharedString>,
|
title: Option<SharedString>,
|
||||||
content: SharedString,
|
content: SharedString,
|
||||||
|
|
@ -63,10 +86,10 @@ impl Notification {
|
||||||
///
|
///
|
||||||
/// default width is 320px.
|
/// default width is 320px.
|
||||||
pub fn new(content: impl Into<SharedString>) -> Self {
|
pub fn new(content: impl Into<SharedString>) -> Self {
|
||||||
let id = uuid::Uuid::new_v4().to_string();
|
let id: SharedString = uuid::Uuid::new_v4().to_string().into();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
id: SharedString::from(id).into(),
|
id: id.into(),
|
||||||
title: None,
|
title: None,
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
type_: NotificationType::Info,
|
type_: NotificationType::Info,
|
||||||
|
|
@ -76,11 +99,25 @@ impl Notification {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_id(mut self, id: impl Into<ElementId>) -> Self {
|
/// Set the id of the notification, used to uniquely identify the notification.
|
||||||
|
pub fn with_id(mut self, id: impl Into<SharedString>) -> Self {
|
||||||
|
let id: SharedString = id.into();
|
||||||
self.id = id.into();
|
self.id = id.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the type id of the notification, used to uniquely identify the notification.
|
||||||
|
///
|
||||||
|
/// ```rs
|
||||||
|
/// struct MyNotificationKind;
|
||||||
|
/// let notification = Notification::new("Hello").with_type_id::<MyNotificationKind>();
|
||||||
|
/// ```
|
||||||
|
pub fn with_type_id<T: Sized + 'static>(mut self) -> Self {
|
||||||
|
let type_id = TypeId::of::<T>();
|
||||||
|
self.id = type_id.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the title of the notification, default is None.
|
/// Set the title of the notification, default is None.
|
||||||
///
|
///
|
||||||
/// If tilte is None, the notification will not have a title.
|
/// If tilte is None, the notification will not have a title.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue