Skip to main content

yse_ui/widgets/
menu.rs

1//! Actions, menu bar, menus, and toolbars.
2
3use crate::action::ActionState;
4use crate::bridge as ffi;
5use crate::bridge::Void;
6use crate::component::Component;
7use crate::widgets::{IntoWidget, widget_wrapper};
8use std::rc::Rc;
9use yse_model::{EventStream, Signal, Sink};
10
11/// A retained command that can appear in menus or toolbars.
12pub struct Action {
13    inner: Rc<ActionState>,
14}
15
16impl Clone for Action {
17    fn clone(&self) -> Self {
18        Self {
19            inner: self.inner.clone(),
20        }
21    }
22}
23
24impl Action {
25    /// A stream of trigger events.
26    pub fn triggered(&self) -> EventStream<()> {
27        if self.inner.triggered_sink.borrow().is_none() {
28            unsafe {
29                ffi::action_set_triggered_cb(
30                    self.inner.raw(),
31                    &*self.inner as *const ActionState as *mut Void,
32                );
33            }
34            *self.inner.triggered_sink.borrow_mut() = Some(Rc::new(Sink::new()));
35        }
36        self.inner
37            .triggered_sink
38            .borrow()
39            .as_ref()
40            .unwrap()
41            .stream()
42    }
43
44    /// Register a trigger handler.
45    pub fn on_trigger<F>(&self, f: F) -> &Self
46    where
47        F: FnMut(&()) + 'static,
48    {
49        self.inner
50            .owner
51            .borrow_mut()
52            .add(self.triggered().observe(f));
53        self
54    }
55
56    /// Enable or disable the action.
57    pub fn set_enabled(&self, enabled: bool) {
58        self.inner.set_enabled(enabled);
59    }
60
61    /// Bind the enabled state to a signal.
62    pub fn bind_enabled(&self, signal: &Signal<bool>) {
63        let weak = Rc::downgrade(&self.inner);
64        let subscription = signal.observe(move |enabled| {
65            if let Some(this) = weak.upgrade() {
66                this.set_enabled(*enabled);
67            }
68        });
69        self.inner.owner.borrow_mut().add(subscription);
70    }
71
72    /// Set the action text.
73    pub fn set_text(&self, text: impl Into<String>) {
74        if self.inner.is_alive() {
75            unsafe { ffi::action_set_text(self.inner.raw(), &text.into()) };
76        }
77    }
78
79    /// Use a platform icon-theme name, e.g. `"document-open"` from Breeze.
80    pub fn set_icon(&self, theme_name: impl Into<String>) {
81        if self.inner.is_alive() {
82            unsafe { ffi::action_set_icon(self.inner.raw(), &theme_name.into()) };
83        }
84    }
85
86    /// Set an icon-theme name and return the action for chaining.
87    pub fn icon(self, theme_name: impl Into<String>) -> Self {
88        self.set_icon(theme_name);
89        self
90    }
91
92    /// Set a keyboard shortcut, e.g. `"Ctrl+Q"`.
93    pub fn set_shortcut(&self, shortcut: impl Into<String>) {
94        if self.inner.is_alive() {
95            unsafe { ffi::action_set_shortcut(self.inner.raw(), &shortcut.into()) };
96        }
97    }
98
99    /// Set a keyboard shortcut and return the action, for chaining.
100    pub fn shortcut(self, shortcut: impl Into<String>) -> Self {
101        self.set_shortcut(shortcut);
102        self
103    }
104
105    /// Make the action checkable (shows a check mark when toggled).
106    pub fn set_checkable(&self, checkable: bool) {
107        if self.inner.is_alive() {
108            unsafe { ffi::action_set_checkable(self.inner.raw(), checkable) };
109        }
110    }
111
112    /// Set the checked state of a checkable action.
113    pub fn set_checked(&self, checked: bool) {
114        if self.inner.is_alive() {
115            unsafe { ffi::action_set_checked(self.inner.raw(), checked) };
116        }
117    }
118
119    /// The current checked state.
120    pub fn checked(&self) -> bool {
121        unsafe { ffi::action_checked(self.inner.raw()) }
122    }
123
124    /// Trigger the action programmatically (used by tests and smoke runs).
125    pub fn trigger(&self) {
126        if self.inner.is_alive() {
127            unsafe { ffi::action_trigger(self.inner.raw()) };
128        }
129    }
130}
131
132/// A menu bar attached to a window.
133pub struct MenuBar {
134    pub(crate) inner: Rc<Component>,
135}
136
137impl MenuBar {
138    /// Create a menu in this bar.
139    pub fn menu(&self, title: impl Into<String>) -> Menu {
140        Menu {
141            inner: unsafe {
142                Component::from_raw_child(
143                    ffi::menu_new(&title.into(), self.inner.raw()),
144                    &self.inner,
145                )
146            },
147        }
148    }
149
150    /// Create a menu and hand it to `f`; `f`'s value (typically action
151    /// handles) is passed through so menus build inline.
152    pub fn menu_with<R, F: FnOnce(&Menu) -> R>(&self, title: impl Into<String>, f: F) -> R {
153        let menu = self.menu(title);
154        f(&menu)
155    }
156}
157
158widget_wrapper!(MenuBar);
159
160/// A menu of actions.
161pub struct Menu {
162    pub(crate) inner: Rc<Component>,
163}
164
165impl Menu {
166    /// Create an action in this menu.
167    pub fn action(&self, text: impl Into<String>) -> Action {
168        let inner =
169            unsafe { ActionState::from_raw(ffi::action_new(&text.into(), self.inner.raw())) };
170        self.inner.retain(inner.clone());
171        Action { inner }
172    }
173
174    /// Add a separator.
175    pub fn separator(&self) -> &Self {
176        if self.inner.is_alive() {
177            unsafe { ffi::menu_add_separator(self.inner.raw()) };
178        }
179        self
180    }
181
182    /// Create a nested submenu in this menu.
183    pub fn menu(&self, title: impl Into<String>) -> Menu {
184        Menu {
185            inner: unsafe {
186                Component::from_raw_child(
187                    ffi::menu_add_submenu(self.inner.raw(), &title.into()),
188                    &self.inner,
189                )
190            },
191        }
192    }
193
194    /// Open the menu as a popup at the current cursor position. The menu
195    /// stays open until dismissed; keep a handle alive while it is shown.
196    pub fn popup(&self) {
197        if self.inner.is_alive() {
198            unsafe { ffi::menu_popup(self.inner.raw()) };
199        }
200    }
201}
202
203widget_wrapper!(Menu);
204
205/// A toolbar attached to a window.
206pub struct ToolBar {
207    pub(crate) inner: Rc<Component>,
208}
209
210impl ToolBar {
211    /// Add an action to this toolbar.
212    pub fn add(&self, action: &Action) {
213        if self.inner.is_alive() {
214            unsafe { ffi::toolbar_add_action(self.inner.raw(), action.inner.raw()) };
215        }
216    }
217}
218
219widget_wrapper!(ToolBar);