Skip to main content

Notification

Struct Notification 

Source
pub struct Notification<'a> {
    pub app_id: &'a str,
    pub title: &'a str,
    pub message: &'a str,
    pub icon: MessageIcon,
    pub timeout: i32,
}
Expand description

Notification.

Shows a brief message to the user without blocking their interaction with the application.

// Define a unique application identifier for the notification system.
const APP_ID: &str = "com.example.myapp";

// Invoke setup at application initialization to ensure the application
// is registered and ready to show notifications later.
rustydialogs::Notification::setup(APP_ID);

rustydialogs::Notification {
	app_id: APP_ID,
	title: "Task Complete",
	message: "All files were processed successfully.",
	icon: rustydialogs::MessageIcon::Info,
	timeout: rustydialogs::Notification::SHORT_TIMEOUT,
}.show();

Fields§

§app_id: &'a str

Application identifier used by notification backends.

This is a best-effort hint: some backends may ignore it, and some only honor the first value seen by the process/session.

§title: &'a str

The title of the notification.

§message: &'a str

The message to display in the notification.

§icon: MessageIcon

The icon to show in the notification.

§timeout: i32

Timeout in milliseconds after which the notification should automatically close.

A value less than or equal to 0 means that the notification will not automatically close.

This is a best-effort hint: some backends may ignore it and use their own default timeout, or may not support timeouts at all.

Implementations§

Source§

impl<'a> Notification<'a>

Source

pub const SHORT_TIMEOUT: i32 = 5000

Short timeout duration in milliseconds for notification popups.

Source

pub const LONG_TIMEOUT: i32 = 10000

Long timeout duration in milliseconds for notification popups.

Source

pub fn setup(app_id: &str)

Perform any necessary setup for notifications, such as registering the application with the notification system.

This step is optional, when skipped the library will attempt to perform any necessary setup automatically when showing the first notification, but this method can be used to ensure that the setup is done at a specific time in the application lifecycle.

§Windows

By default, this initializes a process-wide tray icon used for balloon notifications.

When using the winrt-toast backend, this creates a Start Menu shortcut for the application with the provided application identifier, which is required for showing toast notifications on Windows. It is recommended to call this method during application initialization before showing any notifications or the first notification may be skipped due to delays in the shortcut creation process.

§Linux

When using the libnotify backend, this registers the application with the notification system using the provided application identifier.

Examples found in repository?
examples/notification.rs (line 6)
3fn main() {
4	// winrt-toast: Takes almost three seconds to show the first notification due to registration delays.
5	// All Notifications shown before the registration is complete will be ignored.
6	rustydialogs::Notification::setup(APP_ID);
7
8	let i = std::process::id() / 4;
9
10	let icon = match i % 4 {
11		0 => rustydialogs::MessageIcon::Info,
12		1 => rustydialogs::MessageIcon::Warning,
13		2 => rustydialogs::MessageIcon::Error,
14		_ => rustydialogs::MessageIcon::Question,
15	};
16
17	let notify = rustydialogs::Notification {
18		app_id: APP_ID,
19		title: "Rusty Dialogs",
20		message: "This is a native notification.",
21		icon,
22		timeout: rustydialogs::Notification::SHORT_TIMEOUT,
23	};
24
25	notify.show();
26	println!("Notification shown");
27
28	// winrt-toast: Wait a bit to ensure the notification is visible before the program exits.
29	std::thread::sleep(std::time::Duration::from_millis(100));
30}
Source

pub fn show(&self)

Show the notification.

Examples found in repository?
examples/tests.rs (line 369)
366	fn notify(p: &rustydialogs::Notification<'_>) {
367		println!("\n{} Confirm {} appeared.", Color("Step:", "255;214;102"), Color(format_args!("{:?}", p.icon), "255;214;102"));
368		loop {
369			p.show();
370			if confirm("Confirm notification? [Y/n]: ", true) {
371				println!("  Result: {}", Color("PASS", "123;201;111"));
372				break;
373			}
374			println!("  Result: {}", Color("FAIL", "255;107;107"));
375			if !confirm("  Test failed, retry? [Y/n]: ", true) {
376				break;
377			}
378		}
379	}
More examples
Hide additional examples
examples/notification.rs (line 25)
3fn main() {
4	// winrt-toast: Takes almost three seconds to show the first notification due to registration delays.
5	// All Notifications shown before the registration is complete will be ignored.
6	rustydialogs::Notification::setup(APP_ID);
7
8	let i = std::process::id() / 4;
9
10	let icon = match i % 4 {
11		0 => rustydialogs::MessageIcon::Info,
12		1 => rustydialogs::MessageIcon::Warning,
13		2 => rustydialogs::MessageIcon::Error,
14		_ => rustydialogs::MessageIcon::Question,
15	};
16
17	let notify = rustydialogs::Notification {
18		app_id: APP_ID,
19		title: "Rusty Dialogs",
20		message: "This is a native notification.",
21		icon,
22		timeout: rustydialogs::Notification::SHORT_TIMEOUT,
23	};
24
25	notify.show();
26	println!("Notification shown");
27
28	// winrt-toast: Wait a bit to ensure the notification is visible before the program exits.
29	std::thread::sleep(std::time::Duration::from_millis(100));
30}

Trait Implementations§

Source§

impl<'a> Clone for Notification<'a>

Source§

fn clone(&self) -> Notification<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Notification<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Copy for Notification<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Notification<'a>

§

impl<'a> RefUnwindSafe for Notification<'a>

§

impl<'a> Send for Notification<'a>

§

impl<'a> Sync for Notification<'a>

§

impl<'a> Unpin for Notification<'a>

§

impl<'a> UnsafeUnpin for Notification<'a>

§

impl<'a> UnwindSafe for Notification<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.