mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
Compare commits
38 Commits
docs/error
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d1a2f7662 | ||
| 16639df081 | |||
|
|
d839a4a12c | ||
| df49d4d079 | |||
| 4c2acefdfb | |||
| 71c1dc7bad | |||
| f76226242d | |||
| 7aa48def4c | |||
| e78c77f12e | |||
| 7b37916381 | |||
| fdbf332615 | |||
| 8172a5f9fb | |||
| c231911f6b | |||
| d50bac9e0e | |||
| 0503fa8e28 | |||
| 60026e6fea | |||
| b83c75770a | |||
| 8e88f16912 | |||
| db0f9b33a2 | |||
| 4acf6a6b87 | |||
| 1f0def6499 | |||
| 221812e18d | |||
| fe237ce076 | |||
| b1a442b566 | |||
| 6d1be544c5 | |||
| be5d8e098c | |||
| 5c71686b17 | |||
| 2832dff7f1 | |||
| b6e19b5313 | |||
|
|
22b6ee3ad9 | ||
|
|
47e262357f | ||
| 6e0ecda035 | |||
| f10ff06a89 | |||
|
|
f170e8c710 | ||
| 6d8672537f | |||
|
|
4f20307a7b | ||
|
|
edaf6e2787 | ||
| 043b6e93c8 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -336,7 +336,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libnotify-rs"
|
name = "libnotify-rs"
|
||||||
version = "0.3.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "libnotify-rs"
|
name = "libnotify-rs"
|
||||||
version = "0.3.0"
|
version = "1.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors = [ "szymon_jozef"]
|
authors = [ "szymon_jozef"]
|
||||||
description = "Rust bindings for libnotify"
|
description = "Rust bindings for libnotify"
|
||||||
readme = "./README.md"
|
readme = "./README.md"
|
||||||
homepage = "https://github.com/szymon-jozef/libnotify-rs"
|
|
||||||
repository = "https://github.com/szymon-jozef/libnotify-rs"
|
repository = "https://github.com/szymon-jozef/libnotify-rs"
|
||||||
|
documentation = "https://szymon-jozef.github.io/libnotify-rs/libnotify_rs/index.html"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
keywords = [ "bindings", "libnotify" ]
|
keywords = [ "bindings", "libnotify", "notificatons" ]
|
||||||
build = "./build.rs"
|
build = "./build.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -27,11 +27,9 @@ Available at [github pages](https://szymon-jozef.github.io/libnotify-rs/libnotif
|
|||||||
## Add to project
|
## Add to project
|
||||||
Type in:
|
Type in:
|
||||||
```bash
|
```bash
|
||||||
cargo add --git "https://github.com/szymon-jozef/libnotify-rs" --tag <desired version>
|
cargo add libnotify-rs
|
||||||
```
|
```
|
||||||
|
|
||||||
Tag argument is optional, but encouraged.
|
|
||||||
|
|
||||||
### Nix flake
|
### Nix flake
|
||||||
For nix users, there is also a flake inside. If you want to use it add this to your flake:
|
For nix users, there is also a flake inside. If you want to use it add this to your flake:
|
||||||
|
|
||||||
@@ -51,13 +49,15 @@ packages = [
|
|||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
To use this crate you need to have installed:
|
To use this crate you need to have installed:
|
||||||
- [libnotify](https://gitlab.gnome.org/GNOME/libnotify), duh (version 0.8.8 >= is supported)
|
- [libnotify](https://gitlab.gnome.org/GNOME/libnotify), duh (version 0.8.8 >= is tested. Older versions may work, but are unsupported)
|
||||||
- [clang](https://clang.llvm.org/) for bindgen
|
- [clang](https://clang.llvm.org/) for bindgen
|
||||||
- [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/?__goaway_challenge=meta-refresh&__goaway_id=e567f4f3ff21df0fb2b589fc86ed197e&__goaway_referer=https%3A%2F%2Fsearch.brave.com%2F), to find the above
|
- [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/?__goaway_challenge=meta-refresh&__goaway_id=e567f4f3ff21df0fb2b589fc86ed197e&__goaway_referer=https%3A%2F%2Fsearch.brave.com%2F), to find the above
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
Refer to [examples dir](./examples)
|
Refer to [examples dir](./examples)
|
||||||
|
|
||||||
|
Also have a look at [notify-send-rs](https://github.com/szymon-jozef/notify-send-rs). That's a small notify-send clone I made with this lib, as an example.
|
||||||
|
|
||||||
Sneak peek for making a notification:
|
Sneak peek for making a notification:
|
||||||
|
|
||||||
### Without my super cool wrapper (:()
|
### Without my super cool wrapper (:()
|
||||||
|
|||||||
7
build.rs
7
build.rs
@@ -2,7 +2,9 @@ use std::env;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let library = pkg_config::Config::new().probe("libnotify").unwrap();
|
let library = pkg_config::Config::new()
|
||||||
|
.probe("libnotify")
|
||||||
|
.expect("Cannot locate libnotify. Is it installed?");
|
||||||
|
|
||||||
let paths = library
|
let paths = library
|
||||||
.include_paths
|
.include_paths
|
||||||
@@ -17,7 +19,8 @@ fn main() {
|
|||||||
.generate()
|
.generate()
|
||||||
.expect("Unable to generate bindings");
|
.expect("Unable to generate bindings");
|
||||||
|
|
||||||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
let out_path =
|
||||||
|
PathBuf::from(env::var("OUT_DIR").expect("Out $OUT_DIR not set. Perhaps a bindgen error?"));
|
||||||
|
|
||||||
bindings
|
bindings
|
||||||
.write_to_file(out_path.join("bindings.rs"))
|
.write_to_file(out_path.join("bindings.rs"))
|
||||||
|
|||||||
@@ -9,10 +9,26 @@ use super::libnotify::*;
|
|||||||
*(what a cool section comment)
|
*(what a cool section comment)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// Initialized libnotify. This must be called before any other functions.
|
/// Initializes libnotify. This must be called before any other functions.
|
||||||
///
|
///
|
||||||
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
|
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
|
||||||
///
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::{
|
||||||
|
/// functions::init,
|
||||||
|
/// notification:: Notification,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?; // we need to init libnotify __before__ any other libnotify code
|
||||||
|
/// let notification = Notification::new("Test", None, None)?; // Some libnotify call
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{InitError, NulError}
|
/// This function can return LibnotifyError::{InitError, NulError}
|
||||||
pub fn init<'a, T>(app_name: T) -> Result<(), LibnotifyError>
|
pub fn init<'a, T>(app_name: T) -> Result<(), LibnotifyError>
|
||||||
@@ -38,6 +54,19 @@ where
|
|||||||
/// Uninitializes libnotify.
|
/// Uninitializes libnotify.
|
||||||
///
|
///
|
||||||
/// This should be called when the program no longer needs libnotify for the rest of its lifecycle, typically just before exitting.
|
/// This should be called when the program no longer needs libnotify for the rest of its lifecycle, typically just before exitting.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{init, uninit};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// // let notify = ...
|
||||||
|
/// uninit(); // __always__ uninit libnotify after use
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
pub fn uninit() {
|
pub fn uninit() {
|
||||||
unsafe {
|
unsafe {
|
||||||
notify_uninit();
|
notify_uninit();
|
||||||
@@ -45,6 +74,20 @@ pub fn uninit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Gets whether or not libnotify is initialized.
|
/// Gets whether or not libnotify is initialized.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{init, is_initted};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// if !is_initted() { // useful for conditional initing
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
pub fn is_initted() -> bool {
|
pub fn is_initted() -> bool {
|
||||||
unsafe { notify_is_initted() != 0 }
|
unsafe { notify_is_initted() != 0 }
|
||||||
}
|
}
|
||||||
@@ -54,6 +97,20 @@ pub fn is_initted() -> bool {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/// Gets the application name registered.
|
/// Gets the application name registered.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{get_app_name, init};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let app_name = "Test app";
|
||||||
|
/// init(app_name)?;
|
||||||
|
/// let app_name_gotten: String = get_app_name().ok_or::<String>("No name gotten".into())?;
|
||||||
|
/// assert_eq!(app_name, app_name_gotten);
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
pub fn get_app_name() -> Option<String> {
|
pub fn get_app_name() -> Option<String> {
|
||||||
let app_name_c: *const std::os::raw::c_char = unsafe { notify_get_app_name() };
|
let app_name_c: *const std::os::raw::c_char = unsafe { notify_get_app_name() };
|
||||||
|
|
||||||
@@ -70,6 +127,20 @@ pub fn get_app_name() -> Option<String> {
|
|||||||
|
|
||||||
/// Gets the application icon registered.
|
/// Gets the application icon registered.
|
||||||
/// Available since: 0.8.4
|
/// Available since: 0.8.4
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{get_app_icon, get_app_name, init, set_app_icon};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let icon = "gimp";
|
||||||
|
/// set_app_icon(icon)?;
|
||||||
|
/// let icon_gotten = get_app_icon().ok_or::<String>("No app icon set".into())?;
|
||||||
|
/// assert_eq!(icon, icon_gotten);
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn get_app_icon() -> Option<String> {
|
pub fn get_app_icon() -> Option<String> {
|
||||||
let app_icon_c: *const std::os::raw::c_char = unsafe { notify_get_app_icon() };
|
let app_icon_c: *const std::os::raw::c_char = unsafe { notify_get_app_icon() };
|
||||||
|
|
||||||
@@ -86,6 +157,27 @@ pub fn get_app_icon() -> Option<String> {
|
|||||||
|
|
||||||
/// Queries the server capabilities.
|
/// Queries the server capabilities.
|
||||||
/// Synchronously queries the server for its capabilities and returns them in a list.
|
/// Synchronously queries the server for its capabilities and returns them in a list.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{self, uninit};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// functions::init("Test app")?;
|
||||||
|
///
|
||||||
|
/// if let Some(caps) = functions::get_server_caps() {
|
||||||
|
/// println!("=== Server caps ===");
|
||||||
|
/// for s in caps {
|
||||||
|
/// println!("{}", s);
|
||||||
|
/// }
|
||||||
|
/// } else {
|
||||||
|
/// eprintln!("No server caps");
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
pub fn get_server_caps() -> Option<Vec<String>> {
|
pub fn get_server_caps() -> Option<Vec<String>> {
|
||||||
let mut vec: Vec<String> = vec![];
|
let mut vec: Vec<String> = vec![];
|
||||||
|
|
||||||
@@ -135,6 +227,41 @@ pub struct ServerInfo {
|
|||||||
|
|
||||||
/// Queries the server for information.
|
/// Queries the server for information.
|
||||||
/// Synchronously queries the server for its information, specifically, the name, vendor, server version, and the version of the notifications specification that it is compliant with.
|
/// Synchronously queries the server for its information, specifically, the name, vendor, server version, and the version of the notifications specification that it is compliant with.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// use libnotify_rs::api::functions::{self, uninit};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// functions::init("Test app")?;
|
||||||
|
///
|
||||||
|
/// if let Some(info) = functions::get_server_info() {
|
||||||
|
/// println!("=== Server info ===");
|
||||||
|
/// if let Some(name) = info.name {
|
||||||
|
/// println!("Server name: {}", name);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// if let Some(vendor) = info.vendor {
|
||||||
|
/// println!("Server vendor: {}", vendor);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// if let Some(version) = info.version {
|
||||||
|
/// println!("Server version: {}", version);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// if let Some(spec_version) = info.spec_version {
|
||||||
|
/// println!("Server spec version: {}", spec_version);
|
||||||
|
/// }
|
||||||
|
/// } else {
|
||||||
|
/// eprintln!("No server info");
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// uninit();
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn get_server_info() -> Option<ServerInfo> {
|
pub fn get_server_info() -> Option<ServerInfo> {
|
||||||
let mut ret_name = std::ptr::null_mut() as *mut std::os::raw::c_char;
|
let mut ret_name = std::ptr::null_mut() as *mut std::os::raw::c_char;
|
||||||
let mut ret_vendor = std::ptr::null_mut() as *mut std::os::raw::c_char;
|
let mut ret_vendor = std::ptr::null_mut() as *mut std::os::raw::c_char;
|
||||||
@@ -169,6 +296,19 @@ pub fn get_server_info() -> Option<ServerInfo> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::NulError
|
/// This function can return LibnotifyError::NulError
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{get_app_name, init, set_app_name};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let app_name = "morbius app";
|
||||||
|
/// set_app_name(app_name)?;
|
||||||
|
/// assert_eq!(app_name, get_app_name().unwrap());
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn set_app_name(app_name: &str) -> Result<(), LibnotifyError> {
|
pub fn set_app_name(app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_name_c = std::ffi::CString::new(app_name)?;
|
let app_name_c = std::ffi::CString::new(app_name)?;
|
||||||
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
|
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
|
||||||
@@ -183,6 +323,19 @@ pub fn set_app_name(app_name: &str) -> Result<(), LibnotifyError> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::NulError
|
/// This function can return LibnotifyError::NulError
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::functions::{get_app_icon, init, set_app_icon};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let app_icon = "morbius app";
|
||||||
|
/// set_app_icon(app_icon)?;
|
||||||
|
/// assert_eq!(app_icon, get_app_icon().unwrap());
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn set_app_icon(app_icon: &str) -> Result<(), LibnotifyError> {
|
pub fn set_app_icon(app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
||||||
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
|
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
|
||||||
|
|||||||
@@ -66,6 +66,23 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{NewNotificationError, NulError}
|
/// This function can return LibnotifyError::{NewNotificationError, NulError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// use libnotify_rs::api::{
|
||||||
|
/// functions::{init, uninit},
|
||||||
|
/// notification::Notification,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let notification = Notification::new("My notification", "Yap yap", None)?;
|
||||||
|
/// // ...
|
||||||
|
/// uninit();
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, LibnotifyError>
|
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, LibnotifyError>
|
||||||
where
|
where
|
||||||
T: Into<Option<&'a str>>,
|
T: Into<Option<&'a str>>,
|
||||||
@@ -94,6 +111,8 @@ impl<'a> Notification {
|
|||||||
|
|
||||||
/// Add callback to notification.
|
/// Add callback to notification.
|
||||||
///
|
///
|
||||||
|
/// This function needs a [glib mainloop](https://docs.gtk.org/glib/main-loop.html)
|
||||||
|
///
|
||||||
/// # Args
|
/// # Args
|
||||||
/// `action` – action identifier. `default` will work for most use cases. Other text will be
|
/// `action` – action identifier. `default` will work for most use cases. Other text will be
|
||||||
/// displayed as button names.
|
/// displayed as button names.
|
||||||
@@ -104,6 +123,39 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::NulError
|
/// This function can return LibnotifyError::NulError
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run(There's no notification deamon on CI and no one to click the notification)
|
||||||
|
/// use glib::MainLoop;
|
||||||
|
/// use libnotify_rs::{
|
||||||
|
/// api::functions::{init, uninit},
|
||||||
|
/// api::notification::Notification,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Coffee reminder")?;
|
||||||
|
/// let mut notify = Notification::new("Drink coffee", "Click me when you're done drinking", None)?;
|
||||||
|
///
|
||||||
|
/// // We need to have a glib main loop in order to receive callback actions
|
||||||
|
/// let main_loop = MainLoop::new(None, false);
|
||||||
|
/// let loop_clone = main_loop.clone();
|
||||||
|
///
|
||||||
|
/// notify.add_action("default", "I'm drinking!", move |action_name| {
|
||||||
|
/// println!("User drunk his coffee!");
|
||||||
|
/// println!("Action was named: {}", action_name);
|
||||||
|
/// loop_clone.quit();
|
||||||
|
/// })?;
|
||||||
|
///
|
||||||
|
/// notify.show()?;
|
||||||
|
/// main_loop.run();
|
||||||
|
///
|
||||||
|
/// println!("Glib mainloop has ended");
|
||||||
|
///
|
||||||
|
/// uninit();
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn add_action<F>(
|
pub fn add_action<F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
action: &str,
|
action: &str,
|
||||||
@@ -136,6 +188,32 @@ impl<'a> Notification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Clears all actions from the notification
|
/// Clears all actions from the notification
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::{
|
||||||
|
/// functions::{init, uninit},
|
||||||
|
/// notification::Notification,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let mut notification = Notification::new("My notification", "Yap yap", None)?;
|
||||||
|
/// notification.add_action("default", "yap", |action| {
|
||||||
|
/// // do stuff
|
||||||
|
/// })?;
|
||||||
|
///
|
||||||
|
/// notification.add_action("click", "yappers", |action| {
|
||||||
|
/// // do some other stuff
|
||||||
|
/// })?;
|
||||||
|
///
|
||||||
|
/// notification.clear_actions(); // now there are no callbacks!
|
||||||
|
///
|
||||||
|
/// uninit();
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
pub fn clear_actions(&mut self) {
|
pub fn clear_actions(&mut self) {
|
||||||
unsafe { notify_notification_clear_actions(self.inner) }
|
unsafe { notify_notification_clear_actions(self.inner) }
|
||||||
}
|
}
|
||||||
@@ -149,6 +227,22 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{AllocationError, GerrorError}
|
/// This function can return LibnotifyError::{AllocationError, GerrorError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run(Not notification deamon on CI)
|
||||||
|
/// use libnotify_rs::api::{functions::init, notification::Notification};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let notification = Notification::new("My notification", "Yap yap", None)?;
|
||||||
|
/// notification.show()?;
|
||||||
|
/// std::thread::sleep(std::time::Duration::from_secs(5)); // To do this you can just set duration,
|
||||||
|
/// // but this is an example
|
||||||
|
/// notification.close()?; // After 5 seconds no notification
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn close(&self) -> Result<(), LibnotifyError> {
|
pub fn close(&self) -> Result<(), LibnotifyError> {
|
||||||
let mut gerror = std::ptr::null_mut() as *mut GError;
|
let mut gerror = std::ptr::null_mut() as *mut GError;
|
||||||
|
|
||||||
@@ -179,6 +273,37 @@ impl<'a> Notification {
|
|||||||
/// Returns the closed reason code for the notification.
|
/// Returns the closed reason code for the notification.
|
||||||
///
|
///
|
||||||
/// This is valid only after the Notification::closed signal is emitted.
|
/// This is valid only after the Notification::closed signal is emitted.
|
||||||
|
///
|
||||||
|
/// This function needs a [glib mainloop](https://docs.gtk.org/glib/main-loop.html)
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run(No notificaton daemon on CI)
|
||||||
|
/// use glib::MainLoop;
|
||||||
|
/// use libnotify_rs::{
|
||||||
|
/// api::functions::{init, uninit},
|
||||||
|
/// api::notification::Notification,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Coffee reminder")?;
|
||||||
|
/// let mut notify = Notification::new("Drink coffee", "Click me when you're done drinking", None)?;
|
||||||
|
///
|
||||||
|
/// // We need to have a glib main loop in order to receive closed_reason
|
||||||
|
/// let main_loop = MainLoop::new(None, false);
|
||||||
|
/// let loop_clone = main_loop.clone();
|
||||||
|
///
|
||||||
|
/// notify.show()?;
|
||||||
|
/// main_loop.run();
|
||||||
|
///
|
||||||
|
/// // Thanks to the main loop we can also read why the notification was closed
|
||||||
|
/// println!(
|
||||||
|
/// "Notification closed, because: {:?}",
|
||||||
|
/// notify.get_closed_reason()
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///```
|
||||||
pub fn get_closed_reason(&self) -> ClosedReason {
|
pub fn get_closed_reason(&self) -> ClosedReason {
|
||||||
match unsafe { notify_notification_get_closed_reason(self.inner) } {
|
match unsafe { notify_notification_get_closed_reason(self.inner) } {
|
||||||
NotifyClosedReason_NOTIFY_CLOSED_REASON_UNSET => ClosedReason::Unset,
|
NotifyClosedReason_NOTIFY_CLOSED_REASON_UNSET => ClosedReason::Unset,
|
||||||
@@ -192,8 +317,11 @@ impl<'a> Notification {
|
|||||||
|
|
||||||
/// Sets the application icon for the notification.
|
/// Sets the application icon for the notification.
|
||||||
///
|
///
|
||||||
|
/// Refer to the [documentation](https://specifications.freedesktop.org/icon-naming/latest/) for
|
||||||
|
/// icon names.
|
||||||
|
///
|
||||||
/// If this function is not called, the application icon will be set from the value set via
|
/// If this function is not called, the application icon will be set from the value set via
|
||||||
/// `set_app_icon()`.
|
/// `set_app_icon()`. Usage is the same.
|
||||||
///
|
///
|
||||||
/// Available since: 0.8.4
|
/// Available since: 0.8.4
|
||||||
///
|
///
|
||||||
@@ -208,7 +336,7 @@ impl<'a> Notification {
|
|||||||
|
|
||||||
/// Sets the application name for the notification.
|
/// Sets the application name for the notification.
|
||||||
///
|
///
|
||||||
/// If this function is not called, the application name will be set from the value used in init() or overridden with set_app_name().
|
/// If this function is not called, the application name will be set from the value used in init() or overridden with set_app_name(). Usage is the same.
|
||||||
///
|
///
|
||||||
/// Available since: 0.7.3
|
/// Available since: 0.7.3
|
||||||
///
|
///
|
||||||
@@ -225,6 +353,8 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// This can be used by the notification server to filter or display the data in a certain way
|
/// This can be used by the notification server to filter or display the data in a certain way
|
||||||
///
|
///
|
||||||
|
/// The only reference to what this even is, is [this thread](https://unix.stackexchange.com/questions/251243/what-do-a-notify-send-notification-category-hint-and-version-parameters-mean). If you know how to use this bad boy, have fun :D
|
||||||
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{NulError}
|
/// This function can return LibnotifyError::{NulError}
|
||||||
pub fn set_category(&mut self, category: &str) -> Result<(), LibnotifyError> {
|
pub fn set_category(&mut self, category: &str) -> Result<(), LibnotifyError> {
|
||||||
@@ -238,6 +368,9 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// Available since: 0.6
|
/// Available since: 0.6
|
||||||
///
|
///
|
||||||
|
///
|
||||||
|
/// The only reference to what this even is, is [this thread](https://unix.stackexchange.com/questions/251243/what-do-a-notify-send-notification-category-hint-and-version-parameters-mean). If you know how to use this bad boy, have fun :D
|
||||||
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{NulError, AllocationError}
|
/// This function can return LibnotifyError::{NulError, AllocationError}
|
||||||
pub fn set_hint(&mut self, key: &str, value: HintValue) -> Result<(), LibnotifyError> {
|
pub fn set_hint(&mut self, key: &str, value: HintValue) -> Result<(), LibnotifyError> {
|
||||||
@@ -266,6 +399,26 @@ impl<'a> Notification {
|
|||||||
/// Sets the timeout of the notification.
|
/// Sets the timeout of the notification.
|
||||||
///
|
///
|
||||||
/// Note that the timeout may be ignored by the server.
|
/// Note that the timeout may be ignored by the server.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::{
|
||||||
|
/// functions::{init, uninit},
|
||||||
|
/// notification::{self, Notification},
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("I love coffee")?;
|
||||||
|
/// let mut notify = Notification::new(
|
||||||
|
/// "Remember to drink coffe!",
|
||||||
|
/// "Coffee is very important for your mental health!",
|
||||||
|
/// None,
|
||||||
|
/// )?;
|
||||||
|
///
|
||||||
|
/// notify.set_timeout(notification::Timeout::Custom(1000 * 10)); // 10 secs
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn set_timeout(&mut self, timeout: Timeout) {
|
pub fn set_timeout(&mut self, timeout: Timeout) {
|
||||||
let timeout_c = match timeout {
|
let timeout_c = match timeout {
|
||||||
Timeout::Default => NOTIFY_EXPIRES_DEFAULT,
|
Timeout::Default => NOTIFY_EXPIRES_DEFAULT,
|
||||||
@@ -277,6 +430,25 @@ impl<'a> Notification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the urgency level of this notification
|
/// Sets the urgency level of this notification
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::{
|
||||||
|
/// functions::{init, uninit},
|
||||||
|
/// notification::{self, Notification},
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("I love coffee")?;
|
||||||
|
/// let mut notify = Notification::new(
|
||||||
|
/// "Remember to drink coffe!",
|
||||||
|
/// "Coffee is very important for your mental health!",
|
||||||
|
/// None,
|
||||||
|
/// )?;
|
||||||
|
///
|
||||||
|
/// notify.set_urgency(notification::Urgency::Critical); // coffee is very important
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn set_urgency(&mut self, urgency: Urgency) {
|
pub fn set_urgency(&mut self, urgency: Urgency) {
|
||||||
let urgency: u32 = match urgency {
|
let urgency: u32 = match urgency {
|
||||||
Urgency::Low => NotifyUrgency_NOTIFY_URGENCY_LOW,
|
Urgency::Low => NotifyUrgency_NOTIFY_URGENCY_LOW,
|
||||||
@@ -291,6 +463,27 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{GerrorError, AllocationError}
|
/// This function can return LibnotifyError::{GerrorError, AllocationError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run(No notification daemon on CI)
|
||||||
|
/// use libnotify_rs::api::{
|
||||||
|
/// functions::{init, uninit},
|
||||||
|
/// notification::{self, Notification},
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("I love coffee")?;
|
||||||
|
/// let mut notify = Notification::new(
|
||||||
|
/// "Remember to drink coffe!",
|
||||||
|
/// "Coffee is very important for your mental health!",
|
||||||
|
/// None,
|
||||||
|
/// )?;
|
||||||
|
///
|
||||||
|
/// notify.show()?;
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn show(&self) -> Result<(), LibnotifyError> {
|
pub fn show(&self) -> Result<(), LibnotifyError> {
|
||||||
let mut gerror = std::ptr::null_mut() as *mut GError;
|
let mut gerror = std::ptr::null_mut() as *mut GError;
|
||||||
|
|
||||||
@@ -323,6 +516,19 @@ impl<'a> Notification {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{NulError}
|
/// This function can return LibnotifyError::{NulError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::api::{functions::init, notification::Notification};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// init("Test app")?;
|
||||||
|
/// let mut notification = Notification::new("My notification", "Yap yap", None)?;
|
||||||
|
/// notification.update("My new summary", "My new body", None)?;
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn update<'b, F, I>(
|
pub fn update<'b, F, I>(
|
||||||
&mut self,
|
&mut self,
|
||||||
summary: &str,
|
summary: &str,
|
||||||
@@ -362,6 +568,7 @@ impl Drop for Notification {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This function translates c callback to rust. Used in `add_action`
|
||||||
unsafe extern "C" fn action_trampoline<F>(
|
unsafe extern "C" fn action_trampoline<F>(
|
||||||
_notification: *mut NotifyNotification,
|
_notification: *mut NotifyNotification,
|
||||||
action: *mut ::std::os::raw::c_char,
|
action: *mut ::std::os::raw::c_char,
|
||||||
@@ -379,6 +586,7 @@ unsafe extern "C" fn action_trampoline<F>(
|
|||||||
callback(&action);
|
callback(&action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This function tells c how to free memory of our callback
|
||||||
unsafe extern "C" fn drop_box<F>(data: gpointer) {
|
unsafe extern "C" fn drop_box<F>(data: gpointer) {
|
||||||
unsafe { drop(Box::from_raw(data as *mut F)) };
|
unsafe { drop(Box::from_raw(data as *mut F)) };
|
||||||
}
|
}
|
||||||
@@ -414,4 +622,7 @@ mod tests {
|
|||||||
.is_null()
|
.is_null()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// I meant to write more tests, but I'm to lazy :3
|
||||||
|
// Doctests should suffice
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,16 @@
|
|||||||
//!
|
//!
|
||||||
//! # Features
|
//! # Features
|
||||||
//!
|
//!
|
||||||
//! - clap – enables [clap] support for `Urgency`
|
//! - clap – enables [clap](https://docs.rs/clap/latest/clap/) support for `Urgency`
|
||||||
//!
|
//!
|
||||||
#![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)]
|
#![warn(missing_docs)]
|
||||||
|
#![warn(clippy::missing_errors_doc)]
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
compile_error!("Libnotify-rs only supports linux");
|
||||||
|
|
||||||
/// Basic libnotify rust wrapper
|
/// Basic libnotify rust wrapper
|
||||||
pub mod api;
|
pub mod api;
|
||||||
|
|||||||
@@ -19,6 +19,29 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// This function can return LibnotifyError::{NewNotificationError, NulError}
|
/// This function can return LibnotifyError::{NewNotificationError, NulError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
///
|
||||||
|
/// // Builder is recommended way to interact with libnotify notifications
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
///
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?;
|
||||||
|
///
|
||||||
|
/// // you don't need to uninitialize libnotify, cause context already does that!
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
pub fn new<T, Y>(
|
pub fn new<T, Y>(
|
||||||
summary: &str,
|
summary: &str,
|
||||||
body: T,
|
body: T,
|
||||||
@@ -41,6 +64,17 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{NulError}
|
/// This function can return LibnotifyError::{NulError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```ignore(Small example)
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .set_app_name("Other app name");
|
||||||
|
/// ```
|
||||||
pub fn set_app_name<T: Into<Option<&'a str>>>(
|
pub fn set_app_name<T: Into<Option<&'a str>>>(
|
||||||
mut self,
|
mut self,
|
||||||
app_name: T,
|
app_name: T,
|
||||||
@@ -56,6 +90,17 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{NulError}
|
/// This function can return LibnotifyError::{NulError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```ignore(Small example)
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .set_app_icon("STOCK-HOME")?;
|
||||||
|
/// ```
|
||||||
pub fn set_app_icon<T: Into<Option<&'a str>>>(
|
pub fn set_app_icon<T: Into<Option<&'a str>>>(
|
||||||
mut self,
|
mut self,
|
||||||
app_icon: T,
|
app_icon: T,
|
||||||
@@ -91,6 +136,25 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set timeout until close
|
/// Set timeout until close
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
///
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .set_timeout(libnotify_rs::api::notification::Timeout::Never);
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn set_timeout<T: Into<Option<Timeout>>>(mut self, timeout: T) -> Self {
|
pub fn set_timeout<T: Into<Option<Timeout>>>(mut self, timeout: T) -> Self {
|
||||||
if let Some(timeout) = timeout.into() {
|
if let Some(timeout) = timeout.into() {
|
||||||
self.notification.set_timeout(timeout);
|
self.notification.set_timeout(timeout);
|
||||||
@@ -100,6 +164,25 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set notification urgency level
|
/// Set notification urgency level
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
///
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .set_urgency(libnotify_rs::api::notification::Urgency::Critical);
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn set_urgency<T: Into<Option<Urgency>>>(mut self, urgency: T) -> Self {
|
pub fn set_urgency<T: Into<Option<Urgency>>>(mut self, urgency: T) -> Self {
|
||||||
if let Some(urgency) = urgency.into() {
|
if let Some(urgency) = urgency.into() {
|
||||||
self.notification.set_urgency(urgency);
|
self.notification.set_urgency(urgency);
|
||||||
@@ -112,6 +195,28 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::NulError
|
/// This function can return LibnotifyError::NulError
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
///
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .add_action("default", "super cool action", |action| {
|
||||||
|
/// println!("Action: {} was run!", action);
|
||||||
|
/// });
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn add_action<F>(
|
pub fn add_action<F>(
|
||||||
mut self,
|
mut self,
|
||||||
action: &str,
|
action: &str,
|
||||||
@@ -131,6 +236,31 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{GerrorError, AllocationError}
|
/// This function can return LibnotifyError::{GerrorError, AllocationError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run(No notification daemon on CI)
|
||||||
|
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
///
|
||||||
|
/// /// Builder is recommended way to interact with libnotify notifications
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
///
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .set_urgency(libnotify_rs::api::notification::Urgency::Critical)
|
||||||
|
/// .set_timeout(libnotify_rs::api::notification::Timeout::Never)
|
||||||
|
/// .set_category("Coffee")?;
|
||||||
|
///
|
||||||
|
/// notification_builder.show()?;
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn show(&self) -> Result<(), LibnotifyError> {
|
pub fn show(&self) -> Result<(), LibnotifyError> {
|
||||||
self.notification.show()?;
|
self.notification.show()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -142,6 +272,31 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// This function can return LibnotifyError::{GerrorError, AllocationError}
|
/// This function can return LibnotifyError::{GerrorError, AllocationError}
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```no_run(No notification daemon on CI)
|
||||||
|
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
///
|
||||||
|
/// /// Builder is recommended way to interact with libnotify notifications
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
///
|
||||||
|
/// let notification_builder = NotificationBuilder::new(
|
||||||
|
/// "Remember about your coffee!",
|
||||||
|
/// "Only idiots forget about coffee!",
|
||||||
|
/// None,
|
||||||
|
/// &ctx,
|
||||||
|
/// )?
|
||||||
|
/// .set_urgency(libnotify_rs::api::notification::Urgency::Critical)
|
||||||
|
/// .set_timeout(libnotify_rs::api::notification::Timeout::Never)
|
||||||
|
/// .set_category("Coffee")?;
|
||||||
|
///
|
||||||
|
/// notification_builder.show()?;
|
||||||
|
/// notification_builder.close()?;
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn close(&self) -> Result<(), LibnotifyError> {
|
pub fn close(&self) -> Result<(), LibnotifyError> {
|
||||||
self.notification.close()?;
|
self.notification.close()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -12,9 +12,22 @@ static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool =
|
|||||||
impl LibnotifyContext {
|
impl LibnotifyContext {
|
||||||
/// Try initializing libnotify
|
/// Try initializing libnotify
|
||||||
///
|
///
|
||||||
/// # Error
|
/// # Errors
|
||||||
/// Returns an error, if libnotify cannot be initialized or if there already exists a context
|
/// Returns an error, if libnotify cannot be initialized or if there already exists a context
|
||||||
/// object
|
/// object
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use libnotify_rs::context::LibnotifyContext;
|
||||||
|
///
|
||||||
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?;
|
||||||
|
///
|
||||||
|
/// // you don't need to uninitialize libnotify, cause context already does that!
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
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());
|
||||||
@@ -28,12 +41,19 @@ impl LibnotifyContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sets global app name
|
/// Sets global app name
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// This function can return LibnotifyError::NulError
|
||||||
pub fn set_new_name(&self, app_name: &str) -> Result<(), LibnotifyError> {
|
pub fn set_new_name(&self, app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
set_app_name(app_name)?;
|
set_app_name(app_name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets global app icon
|
/// Sets global app icon
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// This function can return LibnotifyError::NulError
|
||||||
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), LibnotifyError> {
|
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
set_app_icon(app_icon)?;
|
set_app_icon(app_icon)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user