pub struct Notification { /* private fields */ }Expand description
Represents one notification. Wraps most of libnotify::notifaction functions.
Implementations§
Source§impl<'a> Notification
impl<'a> Notification
Sourcepub fn new<T, Y>(
summary: &str,
body: T,
icon: Y,
) -> Result<Self, LibnotifyError>
pub fn new<T, Y>( summary: &str, body: T, icon: Y, ) -> Result<Self, LibnotifyError>
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(())
}Sourcepub fn add_action<F>(
&mut self,
action: &str,
label: &str,
callback: F,
) -> Result<(), LibnotifyError>
pub fn add_action<F>( &mut self, action: &str, label: &str, callback: F, ) -> Result<(), LibnotifyError>
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(())
}Sourcepub fn clear_actions(&mut self)
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(())
}Sourcepub fn clear_hints(&mut self)
pub fn clear_hints(&mut self)
Clears all hints from the notification
Sourcepub fn close(&self) -> Result<(), LibnotifyError>
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(())
}Sourcepub fn get_closed_reason(&self) -> ClosedReason
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(())
}Sourcepub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), LibnotifyError>
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}
Sourcepub fn set_app_name(&mut self, app_name: &str) -> Result<(), LibnotifyError>
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}
Sourcepub fn set_category(&mut self, category: &str) -> Result<(), LibnotifyError>
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}
Sourcepub fn set_hint(
&mut self,
key: &str,
value: HintValue,
) -> Result<(), LibnotifyError>
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}
Sourcepub fn set_timeout(&mut self, timeout: Timeout)
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(())
}Sourcepub fn set_urgency(&mut self, urgency: Urgency)
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(())
}Sourcepub fn show(&self) -> Result<(), LibnotifyError>
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(())
}Sourcepub fn update<'b, F, I>(
&mut self,
summary: &str,
body: F,
icon: I,
) -> Result<(), LibnotifyError>
pub fn update<'b, F, I>( &mut self, summary: &str, body: F, icon: I, ) -> Result<(), LibnotifyError>
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(())
}