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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::ffi;
#[cfg(feature = "v1_4")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
use glib::prelude::*;
use glib::translate::*;

glib::wrapper! {
    /// The goal of [`MenuManager`][crate::MenuManager] is to simplify the process of merging multiple
    /// GtkBuilder .ui files containing menus into a single representation of the
    /// application menus. Additionally, it provides the ability to "unmerge"
    /// previously merged menus.
    ///
    /// This allows for an application to have plugins which seemlessly extends
    /// the core application menus.
    ///
    /// Implementation notes:
    ///
    /// To make this work, we don't use the GMenu instances created by a GtkBuilder
    /// instance. Instead, we create the menus ourself and recreate section and
    /// submenu links. This allows the [`MenuManager`][crate::MenuManager] to be in full control of
    /// the generated menus.
    ///
    /// [`menu_by_id()`][Self::menu_by_id()] will always return a [`gio::Menu`][crate::gio::Menu], however
    /// that menu may contain no children until something has extended it later
    /// on during the application process.
    ///
    /// # Implements
    ///
    /// [`trait@glib::ObjectExt`]
    #[doc(alias = "PanelMenuManager")]
    pub struct MenuManager(Object<ffi::PanelMenuManager, ffi::PanelMenuManagerClass>);

    match fn {
        type_ => || ffi::panel_menu_manager_get_type(),
    }
}

impl MenuManager {
    #[doc(alias = "panel_menu_manager_new")]
    pub fn new() -> MenuManager {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::panel_menu_manager_new()) }
    }

    /// Locates a menu item that matches `id` and sets the position within
    /// the resulting [`gio::Menu`][crate::gio::Menu] to `position`.
    ///
    /// If no match is found, [`None`] is returned.
    /// ## `id`
    /// the identifier of the menu item
    ///
    /// # Returns
    ///
    /// a [`gio::Menu`][crate::gio::Menu] if successful; otherwise
    ///  [`None`] and `position` is unset.
    ///
    /// ## `position`
    /// the position within the resulting menu
    #[cfg(feature = "v1_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
    #[doc(alias = "panel_menu_manager_find_item_by_id")]
    pub fn find_item_by_id(&self, id: &str) -> (Option<gio::Menu>, u32) {
        unsafe {
            let mut position = std::mem::MaybeUninit::uninit();
            let ret = from_glib_none(ffi::panel_menu_manager_find_item_by_id(
                self.to_glib_none().0,
                id.to_glib_none().0,
                position.as_mut_ptr(),
            ));
            (ret, position.assume_init())
        }
    }

    ///
    /// # Returns
    ///
    /// A [`gio::Menu`][crate::gio::Menu].
    #[cfg(feature = "v1_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
    #[doc(alias = "panel_menu_manager_get_menu_by_id")]
    #[doc(alias = "get_menu_by_id")]
    pub fn menu_by_id(&self, menu_id: &str) -> gio::Menu {
        unsafe {
            from_glib_none(ffi::panel_menu_manager_get_menu_by_id(
                self.to_glib_none().0,
                menu_id.to_glib_none().0,
            ))
        }
    }

    /// Gets the known menu ids as a string array.
    #[cfg(feature = "v1_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
    #[doc(alias = "panel_menu_manager_get_menu_ids")]
    #[doc(alias = "get_menu_ids")]
    pub fn menu_ids(&self) -> Vec<glib::GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_none(ffi::panel_menu_manager_get_menu_ids(
                self.to_glib_none().0,
            ))
        }
    }

    /// Note that `menu_model` is not retained, a copy of it is made.
    /// ## `menu_id`
    /// the identifier of the menu
    /// ## `menu_model`
    /// the menu model to merge
    ///
    /// # Returns
    ///
    /// the merge-id which can be used with [`remove()`][Self::remove()]
    #[cfg(feature = "v1_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
    #[doc(alias = "panel_menu_manager_merge")]
    pub fn merge(&self, menu_id: &str, menu_model: &impl IsA<gio::MenuModel>) -> u32 {
        unsafe {
            ffi::panel_menu_manager_merge(
                self.to_glib_none().0,
                menu_id.to_glib_none().0,
                menu_model.as_ref().to_glib_none().0,
            )
        }
    }

    /// This removes items from menus that were added as part of a previous
    /// menu merge. Use the value returned from [`merge()`][Self::merge()] as
    /// the `merge_id`.
    /// ## `merge_id`
    /// A previously registered merge id
    #[cfg(feature = "v1_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
    #[doc(alias = "panel_menu_manager_remove")]
    pub fn remove(&self, merge_id: u32) {
        unsafe {
            ffi::panel_menu_manager_remove(self.to_glib_none().0, merge_id);
        }
    }

    /// Overwrites an attribute for a menu that was created by
    /// [`MenuManager`][crate::MenuManager].
    ///
    /// This can be useful when you want to update an attribute such as
    /// "accel" when an accelerator has changed due to user mappings.
    /// ## `menu`
    /// the menu that was retreived with [`menu_by_id()`][Self::menu_by_id()]
    /// ## `position`
    /// the index of the item in the menu
    /// ## `attribute`
    /// the attribute to change
    /// ## `value`
    /// the new value for the attribute
    #[cfg(feature = "v1_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_4")))]
    #[doc(alias = "panel_menu_manager_set_attribute_string")]
    pub fn set_attribute_string(
        &self,
        menu: &gio::Menu,
        position: u32,
        attribute: &str,
        value: &str,
    ) {
        unsafe {
            ffi::panel_menu_manager_set_attribute_string(
                self.to_glib_none().0,
                menu.to_glib_none().0,
                position,
                attribute.to_glib_none().0,
                value.to_glib_none().0,
            );
        }
    }
}

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