feat: add notifaction builder

This commit is contained in:
2026-10-01 20:48:56 +02:00
parent 7966726534
commit 744efe8ec9
3 changed files with 87 additions and 1 deletions

85
src/builder.rs Normal file
View 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(())
}
}

View File

@@ -3,6 +3,7 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
pub mod builder;
pub mod context;
pub mod functions;
mod libnotify;

View File

@@ -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()) };