mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
docs: write super cool documentation
This commit is contained in:
@@ -116,6 +116,7 @@ pub fn get_server_caps() -> Option<Vec<String>> {
|
|||||||
if vec.is_empty() { None } else { Some(vec) }
|
if vec.is_empty() { None } else { Some(vec) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Contains server information. Refer to [libnotify get_server_info docs](https://gnome.pages.gitlab.gnome.org/libnotify/func.get_server_info.html) to better understand specific fields
|
||||||
pub struct ServerInfo {
|
pub struct ServerInfo {
|
||||||
/// A location to store the server name, or `None`
|
/// A location to store the server name, or `None`
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
/// Same as [libnotify global functions](https://gnome.pages.gitlab.gnome.org/libnotify/index.html#functions)
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
mod libnotify;
|
mod libnotify;
|
||||||
|
/// Same as [libnotify notification](https://gnome.pages.gitlab.gnome.org/libnotify/class.Notification.html)
|
||||||
pub mod notification;
|
pub mod notification;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
use super::libnotify::*;
|
use super::libnotify::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
/// Notification urgency level. Refer to [libnotify docs](https://gnome.pages.gitlab.gnome.org/libnotify/enum.Urgency.html) for more info
|
||||||
pub enum Urgency {
|
pub enum Urgency {
|
||||||
|
/// Low urgency. Used for unimportant notifications
|
||||||
Low,
|
Low,
|
||||||
|
/// Normal urgency. Used for most standard notifications
|
||||||
Normal,
|
Normal,
|
||||||
|
/// Critical urgency. Used for very important notifications
|
||||||
Critical,
|
Critical,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
/// Reason why notification was closed. Refer to [libnotify docs](https://gnome.pages.gitlab.gnome.org/libnotify/enum.ClosedReason.html)
|
||||||
pub enum ClosedReason {
|
pub enum ClosedReason {
|
||||||
/// Notification not closed
|
/// Notification not closed
|
||||||
Unset,
|
Unset,
|
||||||
@@ -29,24 +34,39 @@ pub enum ClosedReason {
|
|||||||
/// Used in set hint.
|
/// Used in set hint.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum HintValue {
|
pub enum HintValue {
|
||||||
|
#[allow(missing_docs)]
|
||||||
String(String),
|
String(String),
|
||||||
|
#[allow(missing_docs)]
|
||||||
Int32(i32),
|
Int32(i32),
|
||||||
|
#[allow(missing_docs)]
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The timeout in milliseconds
|
/// Notification timeout, until close
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum Timeout {
|
pub enum Timeout {
|
||||||
|
#[allow(missing_docs)]
|
||||||
Default,
|
Default,
|
||||||
|
#[allow(missing_docs)]
|
||||||
Never,
|
Never,
|
||||||
|
/// Custom timeout in milliseconds
|
||||||
Custom(i32),
|
Custom(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents one notification. Wraps most of [libnotify::notifaction](https://gnome.pages.gitlab.gnome.org/libnotify/class.Notification.html) functions.
|
||||||
pub struct Notification {
|
pub struct Notification {
|
||||||
inner: *mut NotifyNotification,
|
inner: *mut NotifyNotification,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Notification {
|
impl<'a> Notification {
|
||||||
|
/// Create new notification.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// This function will return an error if the supplied bytes contain an
|
||||||
|
/// internal 0 byte. The [`NulError`](https://doc.rust-lang.org/stable/alloc/ffi/c_str/struct.NulError.html) returned will contain the bytes as well as
|
||||||
|
/// the position of the nul byte.
|
||||||
|
///
|
||||||
|
/// On libnotify fail it will return Err(&str.into). May change in the future.
|
||||||
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, Box<dyn std::error::Error>>
|
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, Box<dyn std::error::Error>>
|
||||||
where
|
where
|
||||||
T: Into<Option<&'a str>>,
|
T: Into<Option<&'a str>>,
|
||||||
@@ -73,6 +93,15 @@ impl<'a> Notification {
|
|||||||
Ok(Self { inner })
|
Ok(Self { inner })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add callback to notification.
|
||||||
|
///
|
||||||
|
/// # Args
|
||||||
|
/// `action` – action identifier. `default` will work for most use cases. Other text will be
|
||||||
|
/// displayed as button names.
|
||||||
|
///
|
||||||
|
/// `label` – label for user
|
||||||
|
///
|
||||||
|
/// `callback` - any closure that accepts &str as an argument, where &str is action name.
|
||||||
pub fn add_action<F>(
|
pub fn add_action<F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
action: &str,
|
action: &str,
|
||||||
|
|||||||
13
src/lib.rs
13
src/lib.rs
@@ -1,9 +1,20 @@
|
|||||||
#![cfg_attr(not(doctest), doc = include_str!("./../README.md"))]
|
//! This crate exposes two modules:
|
||||||
|
//!
|
||||||
|
//! # Api
|
||||||
|
//! This module contains a basic wrapper for libnotify calls. It just translates data types, etc,
|
||||||
|
//! so usage is basically the same as libnotify.
|
||||||
|
//!
|
||||||
|
//! # Wrappers
|
||||||
|
//! This module is a higher level abstraction over api. Recommended to use over api.
|
||||||
|
//!
|
||||||
#![allow(non_upper_case_globals)]
|
#![allow(non_upper_case_globals)]
|
||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
|
/// Basic libnotify rust wrapper
|
||||||
pub mod api;
|
pub mod api;
|
||||||
|
/// Higher level abstraction over api
|
||||||
pub mod wrappers;
|
pub mod wrappers;
|
||||||
|
|
||||||
pub use wrappers::{builder, context};
|
pub use wrappers::{builder, context};
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ use crate::{
|
|||||||
wrappers::context::LibnotifyContext,
|
wrappers::context::LibnotifyContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Higher level abstraction of `Notification`. Allow for builder pattern notification construction.
|
||||||
|
/// Requires `LibnotifyContext`
|
||||||
pub struct NotificationBuilder<'a> {
|
pub struct NotificationBuilder<'a> {
|
||||||
notification: Notification,
|
notification: Notification,
|
||||||
_context: &'a LibnotifyContext,
|
_context: &'a LibnotifyContext,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NotificationBuilder<'a> {
|
impl<'a> NotificationBuilder<'a> {
|
||||||
|
/// Create new NotificationBuilder, with given context.
|
||||||
pub fn new<T, Y>(
|
pub fn new<T, Y>(
|
||||||
summary: &str,
|
summary: &str,
|
||||||
body: T,
|
body: T,
|
||||||
@@ -27,20 +30,25 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets app name for this specific notification.
|
||||||
pub fn set_app_name(mut self, app_name: &str) -> Result<Self, std::ffi::NulError> {
|
pub fn set_app_name(mut self, app_name: &str) -> Result<Self, std::ffi::NulError> {
|
||||||
self.notification.set_app_name(app_name)?;
|
self.notification.set_app_name(app_name)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets app icon fot this specific notification.
|
||||||
pub fn set_app_icon(mut self, app_icon: &str) -> Result<Self, std::ffi::NulError> {
|
pub fn set_app_icon(mut self, app_icon: &str) -> Result<Self, std::ffi::NulError> {
|
||||||
self.notification.set_app_icon(app_icon)?;
|
self.notification.set_app_icon(app_icon)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets category for this specific notification
|
||||||
pub fn set_category(mut self, category: &str) -> Result<Self, std::ffi::NulError> {
|
pub fn set_category(mut self, category: &str) -> Result<Self, std::ffi::NulError> {
|
||||||
self.notification.set_category(category)?;
|
self.notification.set_category(category)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set hint for this specific notification
|
||||||
pub fn set_hint(
|
pub fn set_hint(
|
||||||
mut self,
|
mut self,
|
||||||
key: &str,
|
key: &str,
|
||||||
@@ -50,16 +58,19 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set timeout until close
|
||||||
pub fn set_timeout(mut self, timeout: Timeout) -> Self {
|
pub fn set_timeout(mut self, timeout: Timeout) -> Self {
|
||||||
self.notification.set_timeout(timeout);
|
self.notification.set_timeout(timeout);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set notification urgency level
|
||||||
pub fn set_urgency(mut self, urgency: Urgency) -> Self {
|
pub fn set_urgency(mut self, urgency: Urgency) -> Self {
|
||||||
self.notification.set_urgency(urgency);
|
self.notification.set_urgency(urgency);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add callback
|
||||||
pub fn add_action<F>(
|
pub fn add_action<F>(
|
||||||
mut self,
|
mut self,
|
||||||
action: &str,
|
action: &str,
|
||||||
@@ -73,11 +84,15 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Show notification
|
||||||
|
///
|
||||||
|
/// Uses `&self` borrow, so you can use this object later, after showing.
|
||||||
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.notification.show()?;
|
self.notification.show()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Close notification synchronously.
|
||||||
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.notification.close()?;
|
self.notification.close()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
use crate::api::functions::{init, is_initted, set_app_icon, set_app_name, uninit};
|
use crate::api::functions::{init, is_initted, set_app_icon, set_app_name, uninit};
|
||||||
|
|
||||||
|
/// Notification context. Handles libnotify initializing and uninitializing.
|
||||||
pub struct LibnotifyContext {}
|
pub struct LibnotifyContext {}
|
||||||
|
|
||||||
static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool =
|
static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool =
|
||||||
std::sync::atomic::AtomicBool::new(false);
|
std::sync::atomic::AtomicBool::new(false);
|
||||||
|
|
||||||
impl LibnotifyContext {
|
impl LibnotifyContext {
|
||||||
|
/// Try initializing libnotify
|
||||||
|
///
|
||||||
|
/// # Error
|
||||||
|
/// Returns an error, if libnotify cannot be initialized or if there already exists a context
|
||||||
|
/// object
|
||||||
pub fn new(app_name: &str) -> Result<LibnotifyContext, Box<dyn std::error::Error>> {
|
pub fn new(app_name: &str) -> Result<LibnotifyContext, Box<dyn std::error::Error>> {
|
||||||
if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
||||||
return Err("You can create only one context at a time".into());
|
return Err("You can create only one context at a time".into());
|
||||||
@@ -18,11 +24,13 @@ impl LibnotifyContext {
|
|||||||
Ok(Self {})
|
Ok(Self {})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets global app name
|
||||||
pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> {
|
||||||
set_app_name(app_name)?;
|
set_app_name(app_name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets global app icon
|
||||||
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> {
|
||||||
set_app_icon(app_icon)?;
|
set_app_icon(app_icon)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
|
/// Contains notification builder
|
||||||
pub mod builder;
|
pub mod builder;
|
||||||
|
/// Containx libnotify context
|
||||||
pub mod context;
|
pub mod context;
|
||||||
|
|||||||
Reference in New Issue
Block a user