mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
add open file menu item, group file items in section
This commit is contained in:
parent
330075164d
commit
fb9ef36893
5 changed files with 117 additions and 8 deletions
70
src/app/browser/window/action/open.rs
Normal file
70
src/app/browser/window/action/open.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use gtk::{
|
||||
gio::SimpleAction,
|
||||
glib::uuid_string_random,
|
||||
prelude::{ActionExt, ToVariant},
|
||||
};
|
||||
|
||||
// Defaults
|
||||
|
||||
/// C-compatible variant type
|
||||
const DEFAULT_STATE: i32 = -1;
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Open` action
|
||||
pub struct Open {
|
||||
pub simple_action: SimpleAction,
|
||||
}
|
||||
|
||||
impl Default for Open {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Open {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
simple_action: SimpleAction::new_stateful(
|
||||
&uuid_string_random(),
|
||||
None,
|
||||
&DEFAULT_STATE.to_variant(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Change action [state](https://docs.gtk.org/gio/method.SimpleAction.set_state.html)
|
||||
/// * set `DEFAULT_STATE` on `None`
|
||||
pub fn change_state(&self, state: Option<i32>) {
|
||||
self.simple_action.change_state(
|
||||
&match state {
|
||||
Some(value) => value,
|
||||
None => DEFAULT_STATE,
|
||||
}
|
||||
.to_variant(),
|
||||
)
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
/// Define callback function for
|
||||
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||
pub fn connect_activate(&self, callback: impl Fn(Option<i32>) + 'static) {
|
||||
self.simple_action.connect_activate(move |this, _| {
|
||||
let state = this
|
||||
.state()
|
||||
.expect("State value required")
|
||||
.get::<i32>()
|
||||
.expect("Parameter type does not match `i32`");
|
||||
|
||||
callback(if state == DEFAULT_STATE {
|
||||
None
|
||||
} else {
|
||||
Some(state)
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue