1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use mlua::{Lua, UserData};
use notify_rust::Notification;

use crate::create_body;

#[doc(hidden)]
pub fn init(lua: &Lua) -> mlua::Result<mlua::Table> {
    create_body!(lua,
        "new" => lua.create_function(EasyNotification::new_lua)?
    )
}

#[derive(Debug, Clone)]
pub struct EasyNotification(Notification);
impl EasyNotification {
    #[allow(dead_code)]
    pub fn new() -> Self {
        Self(Notification::new())
    }
    pub fn new_lua(_: &Lua, _: ()) -> mlua::Result<Self> {
        Ok(Self(Notification::new()))
    }
}

impl Default for EasyNotification {
    fn default() -> Self {
        Self::new()
    }
}

impl UserData for EasyNotification {
    fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_method_mut("appname", |_, this, appname: String| {
            this.0.appname(&appname);
            Ok(this.clone())
        });
        methods.add_method_mut("summary", |_, this, summary: String| {
            this.0.summary(&summary);
            Ok(this.clone())
        });
        methods.add_method_mut("subtitle", |_, this, subtitle: String| {
            this.0.subtitle(&subtitle);
            Ok(this.clone())
        });
        methods.add_method_mut("body", |_, this, body: String| {
            this.0.body(&body);
            Ok(this.clone())
        });
        methods.add_method_mut("icon", |_, this, icon: String| {
            this.0.icon(&icon);
            Ok(this.clone())
        });
        methods.add_method_mut("show", |_, this, ()| {
            //TODO: do something with the handle
            this.0.show().map_err(|x| mlua::Error::RuntimeError(x.to_string()))?;
            Ok(this.clone())
        });
    }
}