Add NotificationShowError
This commit is contained in:
parent
3dfc03aebd
commit
94bb46164f
@ -5,8 +5,7 @@ fn main() {
|
||||
panic!("{}", e);
|
||||
});
|
||||
let body_text = Some("This is the optional body text.");
|
||||
let n = notify.new_notification("This is the summary.",
|
||||
body_text,
|
||||
None).unwrap_or_else(|e| panic!("{}", e));
|
||||
n.show().ok().expect("Failed to show notification");
|
||||
let n = notify.new_notification("This is the summary.", body_text, None)
|
||||
.unwrap_or_else(|e| panic!("{}", e));
|
||||
n.show().unwrap_or_else(|e| panic!("{}", e));
|
||||
}
|
||||
|
27
src/lib.rs
27
src/lib.rs
@ -21,9 +21,10 @@ extern crate libnotify_sys as sys;
|
||||
extern crate glib_2_0_sys as glib;
|
||||
extern crate gtypes;
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::marker::PhantomData;
|
||||
use std::fmt;
|
||||
use std::error::Error;
|
||||
|
||||
use gtypes::{TRUE, FALSE};
|
||||
|
||||
@ -158,15 +159,35 @@ pub struct Notification<'a> {
|
||||
impl<'a> Notification<'a> {
|
||||
/// Tells the notification server to display the notification
|
||||
/// on the screen.
|
||||
pub fn show(&'a self) -> Result<(), ()> {
|
||||
pub fn show(&'a self) -> Result<(), NotificationShowError> {
|
||||
unsafe {
|
||||
let mut err: *mut glib::GError = std::ptr::null_mut();
|
||||
sys::notify_notification_show(self.handle, &mut err);
|
||||
if !err.is_null() {
|
||||
let result = Err(NotificationShowError {
|
||||
message: CStr::from_ptr((*err).message).to_string_lossy().into_owned(),
|
||||
});
|
||||
glib::g_error_free(err);
|
||||
return Err(());
|
||||
return result;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NotificationShowError {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for NotificationShowError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Error showing notification: {}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for NotificationShowError {
|
||||
fn description(&self) -> &str {
|
||||
"Notification show error"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user