feat(notification): add EmbedderMsg::ShowNotification (#36055)

Signed-off-by: Jason Tsai <git@pews.dev>
This commit is contained in:
Jason Tsai 2025-03-24 21:01:36 +08:00 committed by GitHub
parent c09eed759b
commit e9ed5dd023
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 151 additions and 20 deletions

View file

@ -14,6 +14,7 @@ mod webdriver;
use std::fmt::{Debug, Error, Formatter};
use std::path::PathBuf;
use std::sync::Arc;
use base::id::{PipelineId, WebViewId};
use crossbeam_channel::Sender;
@ -23,6 +24,7 @@ pub use keyboard_types::{KeyboardEvent, Modifiers};
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use num_derive::FromPrimitive;
use pixels::Image;
use serde::{Deserialize, Serialize};
use servo_url::ServoUrl;
use strum_macros::IntoStaticStr;
@ -326,6 +328,8 @@ pub enum EmbedderMsg {
/// Required because the constellation can have pending calls to make
/// (e.g. SetFrameTree) at the time that we send it an ExitMsg.
ShutdownComplete,
/// Request to display a notification.
ShowNotification(Option<WebViewId>, Notification),
}
impl Debug for EmbedderMsg {
@ -582,3 +586,52 @@ pub enum LoadStatus {
/// See <https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState>
Complete,
}
/// Data that could be used to display a desktop notification to the end user
/// when the [Notification API](<https://notifications.spec.whatwg.org/#notifications>) is called.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Notification {
/// Title of the notification.
pub title: String,
/// Body string of the notification.
pub body: String,
/// An identifier tag for the notification. Notification with the same tag
/// can be replaced by another to avoid users' screen being filled up with similar notifications.
pub tag: String,
/// The tag for the language used in the notification's title, body, and the title of each its actions. [RFC 5646](https://datatracker.ietf.org/doc/html/rfc5646)
pub language: String,
/// A boolean value indicates the notification should remain readily available
/// until the end user activates or dismisses the notification.
pub require_interaction: bool,
/// When `true`, indicates no sounds or vibrations should be made. When `None`,
/// the device's default settings should be respected.
pub silent: Option<bool>,
/// The URL of an icon. The icon will be displayed as part of the notification.
pub icon_url: Option<ServoUrl>,
/// Icon's raw image data and metadata.
pub icon_resource: Option<Arc<Image>>,
/// The URL of a badge. The badge is used when there is no enough space to display the notification,
/// such as on a mobile device's notification bar.
pub badge_url: Option<ServoUrl>,
/// Badge's raw image data and metadata.
pub badge_resource: Option<Arc<Image>>,
/// The URL of an image. The image will be displayed as part of the notification.
pub image_url: Option<ServoUrl>,
/// Image's raw image data and metadata.
pub image_resource: Option<Arc<Image>>,
/// Actions available for users to choose from for interacting with the notification.
pub actions: Vec<NotificationAction>,
}
/// Actions available for users to choose from for interacting with the notification.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NotificationAction {
/// A string that identifies the action.
pub name: String,
/// The title string of the action to be shown to the user.
pub title: String,
/// The URL of an icon. The icon will be displayed with the action.
pub icon_url: Option<ServoUrl>,
/// Icon's raw image data and metadata.
pub icon_resource: Option<Arc<Image>>,
}