Skip to main content

Notification

Struct Notification 

Source
pub struct Notification { /* private fields */ }
Expand description

Represents one notification. Wraps most of libnotify::notifaction functions.

Implementations§

Source§

impl<'a> Notification

Source

pub fn new<T, Y>( summary: &str, body: T, icon: Y, ) -> Result<Self, LibnotifyError>
where T: Into<Option<&'a str>>, Y: Into<Option<&'a str>>,

Create new notification.

§Errors

This function can return LibnotifyError::{NewNotificationError, NulError}

§Example

use libnotify_rs::api::{
    functions::{init, uninit},
    notification::Notification,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("Test app")?;
    let notification = Notification::new("My notification", "Yap yap", None)?;
    // ...
    uninit();
    Ok(())
}
Source

pub fn add_action<F>( &mut self, action: &str, label: &str, callback: F, ) -> Result<(), LibnotifyError>
where F: Fn(&str) + 'static,

Add callback to notification.

This function needs a glib mainloop

§Args

action – action identifier. default will work for most use cases. Other text will be displayed as button names.

label – label for user

callback - any closure that accepts &str as an argument, where &str is action name.

§Errors

This function can return LibnotifyError::NulError

§Example
use glib::MainLoop;
use libnotify_rs::{
    api::functions::{init, uninit},
    api::notification::Notification,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("Coffee reminder")?;
    let mut notify = Notification::new("Drink coffee", "Click me when you're done drinking", None)?;

    // We need to have  a glib main loop in order to receive callback actions
    let main_loop = MainLoop::new(None, false);
    let loop_clone = main_loop.clone();

    notify.add_action("default", "I'm drinking!", move |action_name| {
        println!("User drunk his coffee!");
        println!("Action was named: {}", action_name);
        loop_clone.quit();
    })?;

    notify.show()?;
    main_loop.run();

    println!("Glib mainloop has ended");

    uninit();

    Ok(())
}
Source

pub fn clear_actions(&mut self)

Clears all actions from the notification

§Example
use libnotify_rs::api::{
    functions::{init, uninit},
    notification::Notification,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("Test app")?;
    let mut notification = Notification::new("My notification", "Yap yap", None)?;
    notification.add_action("default", "yap", |action| {
        // do stuff
    })?;

    notification.add_action("click", "yappers", |action| {
        // do some other stuff
    })?;

    notification.clear_actions(); // now there are no callbacks!

    uninit();
    Ok(())
}
Source

pub fn clear_hints(&mut self)

Clears all hints from the notification

Source

pub fn close(&self) -> Result<(), LibnotifyError>

Synchronously tells the notification server to hide the notification on the screen

§Errors

This function can return LibnotifyError::{AllocationError, GerrorError}

§Example
use libnotify_rs::api::{functions::init, notification::Notification};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("Test app")?;
    let notification = Notification::new("My notification", "Yap yap", None)?;
    notification.show()?;
    std::thread::sleep(std::time::Duration::from_secs(5)); // To do this you can just set duration,
    // but this is an example
    notification.close()?; // After 5 seconds no notification

    Ok(())
}
Source

pub fn get_closed_reason(&self) -> ClosedReason

Returns the closed reason code for the notification.

This is valid only after the Notification::closed signal is emitted.

This function needs a glib mainloop

§Example
use glib::MainLoop;
use libnotify_rs::{
    api::functions::{init, uninit},
    api::notification::Notification,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("Coffee reminder")?;
    let mut notify = Notification::new("Drink coffee", "Click me when you're done drinking", None)?;

    // We need to have  a glib main loop in order to receive closed_reason
    let main_loop = MainLoop::new(None, false);
    let loop_clone = main_loop.clone();

    notify.show()?;
    main_loop.run();

    // Thanks to the main loop we can also read why the notification was closed
    println!(
        "Notification closed, because: {:?}",
        notify.get_closed_reason()
    );

    Ok(())
}
Source

pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), LibnotifyError>

Sets the application icon for the notification.

Refer to the documentation for icon names.

If this function is not called, the application icon will be set from the value set via set_app_icon(). Usage is the same.

Available since: 0.8.4

§Errors

This function can return LibnotifyError::{NulError}

Source

pub fn set_app_name(&mut self, app_name: &str) -> Result<(), LibnotifyError>

Sets the application name for the notification.

If this function is not called, the application name will be set from the value used in init() or overridden with set_app_name(). Usage is the same.

Available since: 0.7.3

§Errors

This function can return LibnotifyError::{NulError}

Source

pub fn set_category(&mut self, category: &str) -> Result<(), LibnotifyError>

Sets the category of this notification.

This can be used by the notification server to filter or display the data in a certain way

The only reference to what this even is, is this thread. If you know how to use this bad boy, have fun :D

§Errors

This function can return LibnotifyError::{NulError}

Source

pub fn set_hint( &mut self, key: &str, value: HintValue, ) -> Result<(), LibnotifyError>

Sets a hint for key with value value

Available since: 0.6

The only reference to what this even is, is this thread. If you know how to use this bad boy, have fun :D

§Errors

This function can return LibnotifyError::{NulError, AllocationError}

Source

pub fn set_timeout(&mut self, timeout: Timeout)

Sets the timeout of the notification.

Note that the timeout may be ignored by the server.

§Example
use libnotify_rs::api::{
    functions::{init, uninit},
    notification::{self, Notification},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("I love coffee")?;
    let mut notify = Notification::new(
        "Remember to drink coffe!",
        "Coffee is very important for your mental health!",
        None,
    )?;

    notify.set_timeout(notification::Timeout::Custom(1000 * 10)); // 10 secs
    Ok(())
}
Source

pub fn set_urgency(&mut self, urgency: Urgency)

Sets the urgency level of this notification

§Example
use libnotify_rs::api::{
    functions::{init, uninit},
    notification::{self, Notification},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("I love coffee")?;
    let mut notify = Notification::new(
        "Remember to drink coffe!",
        "Coffee is very important for your mental health!",
        None,
    )?;

    notify.set_urgency(notification::Urgency::Critical); // coffee is very important
    Ok(())
}
Source

pub fn show(&self) -> Result<(), LibnotifyError>

Tells the notification server to display the notification on the screen

§Errors

This function can return LibnotifyError::{GerrorError, AllocationError}

§Example
use libnotify_rs::api::{
    functions::{init, uninit},
    notification::{self, Notification},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("I love coffee")?;
    let mut notify = Notification::new(
        "Remember to drink coffe!",
        "Coffee is very important for your mental health!",
        None,
    )?;

    notify.show()?;

    Ok(())
}
Source

pub fn update<'b, F, I>( &mut self, summary: &str, body: F, icon: I, ) -> Result<(), LibnotifyError>
where F: Into<Option<&'b str>>, I: Into<Option<&'b str>>,

Updates the notification text and icon.

This won’t send the update out and display it on the screen. For that, you will need to call show().

§Errors

This function can return LibnotifyError::{NulError}

§Example
use libnotify_rs::api::{functions::init, notification::Notification};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    init("Test app")?;
    let mut notification = Notification::new("My notification", "Yap yap", None)?;
    notification.update("My new summary", "My new body", None)?;

    Ok(())
}

Trait Implementations§

Source§

impl Drop for Notification

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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> 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, 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.