[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};
6pub trait ActionMapExtManual: IsA<ActionMap> {
7    #[doc(alias = "g_action_map_add_action_entries")]
8    fn add_action_entries(&self, entries: impl IntoIterator<Item = ActionEntry<Self>>) {
9        for entry in entries.into_iter() {
10            let action = if let Some(state) = entry.state() {
11                SimpleAction::new_stateful(entry.name(), entry.parameter_type(), state)
12            } else {
13                SimpleAction::new(entry.name(), entry.parameter_type())
14            };
15            let action_map = self.as_ref();
16            if let Some(callback) = entry.activate {
17                action.connect_activate(clone!(
18                    #[weak]
19                    action_map,
20                    move |action, state| {
21                        // safe to unwrap as O: IsA<ActionMap>
22                        callback(action_map.downcast_ref::<Self>().unwrap(), action, state);
23                    }
24                ));
25            }
26            if let Some(callback) = entry.change_state {
27                action.connect_change_state(clone!(
28                    #[weak]
29                    action_map,
30                    move |action, state| {
31                        // safe to unwrap as O: IsA<ActionMap>
32                        callback(action_map.downcast_ref::<Self>().unwrap(), action, state);
33                    }
34                ));
35            }
36            self.as_ref().add_action(&action);
37        }
38    }
39}
40
41impl<O: IsA<ActionMap>> ActionMapExtManual for O {}