mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
docs: add callbacks example
This commit is contained in:
36
examples/callbacks.rs
Normal file
36
examples/callbacks.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use glib::MainLoop;
|
||||
use libnotify_rs::{
|
||||
functions::{init, uninit},
|
||||
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();
|
||||
|
||||
// Thanks to the main loop we can also read why the notification was closed
|
||||
println!(
|
||||
"Notification closed, because: {:?}",
|
||||
notify.get_closed_reason()
|
||||
);
|
||||
|
||||
println!("Glib mainloop has ended");
|
||||
|
||||
uninit();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user