[go: up one dir, main page]

gio/
action_map.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{clone, prelude::*};
4
5use crate::{prelude::*, ActionEntry, ActionMap, SimpleAction};
6
7mod sealed {
8    pub trait Sealed {}
9    impl<T: super::IsA<super::ActionMap>> Sealed for T {}
10}
11
12pub trait ActionMapExtManual: sealed::Sealed + IsA<ActionMap> {
13    #[doc(alias = "g_action_map_add_action_entries")]
14    fn add_action_entries(&self, entries: impl IntoIterator<Item = ActionEntry<Self>>) {
15        for entry in entries.into_iter() {
16            let action = if let Some(state) = entry.state() {
17                SimpleAction::new_stateful(entry.name(), entry.parameter_type(), state)
18            } else {
19                SimpleAction::new(entry.name(), entry.parameter_type())
20            };
21            let action_map = self.as_ref();
22            if let Some(callback) = entry.activate {
23                action.connect_activate(clone!(
24                    #[weak]
25                    action_map,
26                    move |action, state| {
27                        // safe to unwrap as O: IsA<ActionMap>
28                        callback(action_map.downcast_ref::<Self>().unwrap(), action, state);
29                    }
30                ));
31            }
32            if let Some(callback) = entry.change_state {
33                action.connect_change_state(clone!(
34                    #[weak]
35                    action_map,
36                    move |action, state| {
37                        // safe to unwrap as O: IsA<ActionMap>
38                        callback(action_map.downcast_ref::<Self>().unwrap(), action, state);
39                    }
40                ));
41            }
42            self.as_ref().add_action(&action);
43        }
44    }
45}
46
47impl<O: IsA<ActionMap>> ActionMapExtManual for O {}