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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use gtk::prelude::ToVariant;

/// Trait used to specify the group name in [`ActionName`].
pub trait ActionGroupName {
    /// Returns the group name.
    fn group_name() -> &'static str;
}

/// Trait for marking stateless actions.
pub trait EmptyType {}

impl EmptyType for () {}

/// Define the name of an action.
pub trait ActionName {
    /// The group of this action.
    type Group: ActionGroupName;

    /// Target value type for passing values to this action.
    ///
    /// Use [`()`] for actions without target value.
    type Target;

    /// State type of this action.
    ///
    /// Use [`()`] for stateless actions.
    type State;

    /// Returns the actions name.
    fn name() -> &'static str;

    /// The full action name (group.action).
    fn action_name() -> String {
        format!("{}.{}", Self::Group::group_name(), Self::name())
    }
}

/// Type safe interface for [`gtk::prelude::ActionableExt`].
pub trait ActionablePlus {
    /// Set a new stateful action with a default state value.
    fn set_action<A: ActionName>(&self, value: A::Target)
    where
        A::Target: ToVariant;

    /// Set a new stateless action.
    fn set_stateless_action<A: ActionName>(&self, unit_type: &())
    where
        A::Target: EmptyType;
}

impl<W: gtk::prelude::ActionableExt> ActionablePlus for W {
    fn set_action<A: ActionName>(&self, value: A::Target)
    where
        A::Target: ToVariant,
    {
        self.set_action_name(Some(A::action_name().as_str()));
        self.set_action_target_value(Some(&value.to_variant()));
    }

    fn set_stateless_action<A: ActionName>(&self, _unit_type: &())
    where
        A::Target: EmptyType,
    {
        self.set_action_name(Some(A::action_name().as_str()));
    }
}

/// Safe interface for [`gtk::prelude::GtkApplicationExt`].
pub trait AccelsPlus {
    /// Set keyboard accelerator for a certain action.
    fn set_accelerators_for_action<A: ActionName>(&self, value: &[&str])
    where
        A::Target: EmptyType;
}

impl<W: gtk::prelude::GtkApplicationExt> AccelsPlus for W {
    fn set_accelerators_for_action<A: ActionName>(&self, accel_codes: &[&str])
    where
        A::Target: EmptyType,
    {
        self.set_accels_for_action(A::action_name().as_str(), accel_codes);
    }
}