Add convenience method to context for showing a notification

This commit is contained in:
Mika Attila 2015-11-03 10:06:33 +01:00
parent 69a2d18218
commit a74f4c9888
2 changed files with 36 additions and 15 deletions

View File

@ -1,11 +1,11 @@
extern crate libnotify; extern crate libnotify;
fn main() { fn main() {
let notify = libnotify::Context::new("hello").unwrap_or_else(|e| { // Create a libnotify context
panic!("{}", e); let notify = libnotify::Context::new("myapp").unwrap();
}); // Create a new notification and show it
let body_text = Some("This is the optional body text."); let n = notify.new_notification("Summary", Some("Optional Body"), None).unwrap();
let n = notify.new_notification("This is the summary.", body_text, None) n.show().unwrap();
.unwrap_or_else(|e| panic!("{}", e)); // You can also use the .show() convenience method on the context
n.show().unwrap_or_else(|e| panic!("{}", e)); notify.show("I am another notification", None, None).unwrap();
} }

View File

@ -4,13 +4,15 @@
//! extern crate libnotify; //! extern crate libnotify;
//! //!
//! fn main() { //! fn main() {
//! let notify = libnotify::Context::new("hello").unwrap_or_else(|e| { //! // Create a libnotify context
//! panic!("{}", e); //! let notify = libnotify::Context::new("myapp").unwrap();
//! }); //! // Create a new notification and show it
//! let body_text = Some("This is the optional body text."); //! let n = notify.new_notification("Summary",
//! let n = notify.new_notification("This is the summary.", body_text, None) //! Some("Optional Body"),
//! .unwrap_or_else(|e| panic!("{}", e)); //! None).unwrap();
//! n.show().unwrap_or_else(|e| panic!("{}", e)); //! n.show().unwrap();
//! // You can also use the .show() convenience method on the context
//! notify.show("I am another notification", None, None).unwrap();
//! } //! }
//! //!
//! ``` //! ```
@ -81,6 +83,12 @@ impl From<ffi::NulError> for NotificationCreationError {
} }
} }
impl Error for NotificationCreationError {
fn description(&self) -> &str {
"notification creation error"
}
}
/// The context which within libnotify operates. /// The context which within libnotify operates.
/// ///
/// Only one context can exist at a time. /// Only one context can exist at a time.
@ -145,6 +153,19 @@ impl Context {
}) })
} }
} }
/// Show a notification.
///
/// This is a convenience method that creates a new notification,
/// and shows it in one step.
pub fn show(&self,
summary: &str,
body: Option<&str>,
icon: Option<&str>)
-> Result<(), Box<Error>> {
let notif = try!(self.new_notification(summary, body, icon));
try!(notif.show());
Ok(())
}
} }
impl Drop for Context { impl Drop for Context {
@ -194,6 +215,6 @@ impl fmt::Display for NotificationShowError {
impl Error for NotificationShowError { impl Error for NotificationShowError {
fn description(&self) -> &str { fn description(&self) -> &str {
"Notification show error" "notification show error"
} }
} }