mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
feat: add notifaction builder
This commit is contained in:
85
src/builder.rs
Normal file
85
src/builder.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use crate::{
|
||||
context::LibnotifyContext,
|
||||
notification::{HintValue, Notification, Timeout, Urgency},
|
||||
};
|
||||
|
||||
pub struct NotificationBuilder<'a> {
|
||||
notification: Notification,
|
||||
_context: &'a LibnotifyContext,
|
||||
}
|
||||
|
||||
impl<'a> NotificationBuilder<'a> {
|
||||
pub fn new<T, Y>(
|
||||
summary: &str,
|
||||
body: T,
|
||||
icon: Y,
|
||||
context: &'a LibnotifyContext,
|
||||
) -> Result<Self, Box<dyn std::error::Error>>
|
||||
where
|
||||
T: Into<Option<&'a str>>,
|
||||
Y: Into<Option<&'a str>>,
|
||||
{
|
||||
let notification = Notification::new(summary, body, icon)?;
|
||||
|
||||
Ok(Self {
|
||||
notification,
|
||||
_context: context,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_app_name(mut self, app_name: &str) -> Result<Self, std::ffi::NulError> {
|
||||
self.notification.set_app_name(app_name)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn set_app_icon(mut self, app_icon: &str) -> Result<Self, std::ffi::NulError> {
|
||||
self.notification.set_app_icon(app_icon)?;
|
||||
Ok(self)
|
||||
}
|
||||
pub fn set_category(mut self, category: &str) -> Result<Self, std::ffi::NulError> {
|
||||
self.notification.set_category(category)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn set_hint(
|
||||
mut self,
|
||||
key: &str,
|
||||
value: HintValue,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
self.notification.set_hint(key, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn set_timeout(mut self, timeout: Timeout) -> Self {
|
||||
self.notification.set_timeout(timeout);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_urgency(mut self, urgency: Urgency) -> Self {
|
||||
self.notification.set_urgency(urgency);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_action<F>(
|
||||
mut self,
|
||||
action: &str,
|
||||
label: &str,
|
||||
callback: F,
|
||||
) -> Result<Self, Box<dyn std::error::Error>>
|
||||
where
|
||||
F: Fn(&str) + 'static,
|
||||
{
|
||||
self.notification.add_action(action, label, callback)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
self.notification.show()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
self.notification.close()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
pub mod builder;
|
||||
pub mod context;
|
||||
pub mod functions;
|
||||
mod libnotify;
|
||||
|
||||
@@ -162,7 +162,7 @@ impl<'a> Notification {
|
||||
/// `set_app_icon()`.
|
||||
///
|
||||
/// Available since: 0.8.4
|
||||
pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), std::ffi::NulError> {
|
||||
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
||||
unsafe { notify_notification_set_app_icon(self.inner, app_icon_c.as_ptr()) };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user