From 7966726534b321136e9426cda1ed78117fbf6c2d Mon Sep 17 00:00:00 2001 From: szymon Date: Thu, 1 Oct 2026 16:31:16 +0200 Subject: [PATCH] feat: add libnotify context --- src/context.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 41 insertions(+) create mode 100644 src/context.rs diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..fb0ab63 --- /dev/null +++ b/src/context.rs @@ -0,0 +1,40 @@ +use crate::functions::{init, is_initted, set_app_icon, set_app_name, uninit}; + +pub struct LibnotifyContext {} + +static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + +impl LibnotifyContext { + pub fn new(app_name: &str) -> Result> { + if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) { + return Err("You can create only one context at a time".into()); + } + + if !is_initted() { + init(app_name)?; + } + + Ok(Self {}) + } + + pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> { + set_app_name(app_name)?; + Ok(()) + } + + pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> { + set_app_icon(app_icon)?; + Ok(()) + } +} + +impl Drop for LibnotifyContext { + fn drop(&mut self) { + if is_initted() { + uninit(); + + IS_CONTEXT_CREATED.store(false, std::sync::atomic::Ordering::SeqCst); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index bd067f8..bed7767 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] +pub mod context; pub mod functions; mod libnotify; pub mod notification;