mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-09-14 16:02:05 +00:00
apply escape action to the current page only
This commit is contained in:
parent
04a34c91ca
commit
3fda9c851a
@ -70,8 +70,8 @@ impl Browser {
|
|||||||
action.escape.connect_activate({
|
action.escape.connect_activate({
|
||||||
let widget = widget.clone();
|
let widget = widget.clone();
|
||||||
let window = window.clone();
|
let window = window.clone();
|
||||||
move || {
|
move |tab_item_id| {
|
||||||
window.tab.escape(None); // current tab
|
window.escape(tab_item_id);
|
||||||
widget.application_window.set_focus(gtk::Window::NONE);
|
widget.application_window.set_focus(gtk::Window::NONE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
use gtk::{gio::SimpleAction, glib::uuid_string_random, prelude::ActionExt};
|
use gtk::{
|
||||||
|
gio::SimpleAction,
|
||||||
|
glib::{uuid_string_random, GString},
|
||||||
|
prelude::{ActionExt, ToVariant},
|
||||||
|
};
|
||||||
|
|
||||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Escape` action of `Browser` group
|
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Escape` action of `Browser` group
|
||||||
pub struct Escape {
|
pub struct Escape {
|
||||||
@ -11,23 +15,65 @@ impl Escape {
|
|||||||
/// Create new `Self`
|
/// Create new `Self`
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
simple_action: SimpleAction::new(&uuid_string_random(), None),
|
simple_action: SimpleAction::new_stateful(
|
||||||
|
&uuid_string_random(),
|
||||||
|
None,
|
||||||
|
&String::new().to_variant(),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||||
/// with formatted for this action [Variant](https://docs.gtk.org/glib/struct.Variant.html) value
|
/// * this action reset previous state for action after activation
|
||||||
pub fn activate(&self) {
|
pub fn activate_stateful_once(&self, tab_item_id: Option<GString>) {
|
||||||
self.simple_action.activate(None); // @TODO custom value
|
// Save current state in memory
|
||||||
|
let _tab_item_id = state(&self.simple_action);
|
||||||
|
|
||||||
|
// Apply requested state
|
||||||
|
self.change_state(tab_item_id);
|
||||||
|
|
||||||
|
// Activate action
|
||||||
|
self.simple_action.activate(None);
|
||||||
|
|
||||||
|
// Return previous state
|
||||||
|
self.change_state(_tab_item_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<GString>) {
|
||||||
|
self.simple_action.change_state(
|
||||||
|
&match state {
|
||||||
|
Some(value) => value.to_string(),
|
||||||
|
None => String::new(),
|
||||||
|
}
|
||||||
|
.to_variant(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
|
|
||||||
/// Define callback function for
|
/// Define callback function for
|
||||||
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||||
pub fn connect_activate(&self, callback: impl Fn() + 'static) {
|
pub fn connect_activate(&self, callback: impl Fn(Option<GString>) + 'static) {
|
||||||
self.simple_action.connect_activate(move |_, _| callback());
|
self.simple_action
|
||||||
|
.connect_activate(move |this, _| callback(state(this)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shared helper to get C-based action state in Optional format
|
||||||
|
fn state(this: &SimpleAction) -> Option<GString> {
|
||||||
|
let state = this
|
||||||
|
.state()
|
||||||
|
.expect("State value required")
|
||||||
|
.get::<String>()
|
||||||
|
.expect("Parameter type does not match `String`");
|
||||||
|
|
||||||
|
if state.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(state.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,6 +119,10 @@ impl Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
pub fn escape(&self, tab_item_id: Option<GString>) {
|
||||||
|
self.tab.escape(tab_item_id);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update(&self, tab_item_id: Option<GString>) {
|
pub fn update(&self, tab_item_id: Option<GString>) {
|
||||||
self.tab.update(tab_item_id);
|
self.tab.update(tab_item_id);
|
||||||
}
|
}
|
||||||
|
@ -176,12 +176,21 @@ impl Tab {
|
|||||||
self.widget.close_all();
|
self.widget.close_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle search widget
|
// Toggle escape action for specified or current item
|
||||||
pub fn escape(&self, page_position: Option<i32>) {
|
pub fn escape(&self, item_id: Option<GString>) {
|
||||||
if let Some(item) = self.item(page_position) {
|
match item_id {
|
||||||
|
Some(id) => {
|
||||||
|
if let Some(item) = self.index.borrow().get(&id) {
|
||||||
|
item.page.escape()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
if let Some(item) = self.item(None) {
|
||||||
item.page.escape();
|
item.page.escape();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Toggle search widget
|
// Toggle search widget
|
||||||
pub fn find(&self, page_position: Option<i32>) {
|
pub fn find(&self, page_position: Option<i32>) {
|
||||||
|
@ -176,7 +176,9 @@ impl Page {
|
|||||||
const DEFAULT_MAX_REDIRECT_COUNT: usize = 10;
|
const DEFAULT_MAX_REDIRECT_COUNT: usize = 10;
|
||||||
|
|
||||||
// Move focus out from navigation entry
|
// Move focus out from navigation entry
|
||||||
self.browser_action.escape.activate();
|
self.browser_action
|
||||||
|
.escape
|
||||||
|
.activate_stateful_once(Some(self.id.as_str().into()));
|
||||||
|
|
||||||
// Initially disable find action
|
// Initially disable find action
|
||||||
self.window_action.find.simple_action.set_enabled(false);
|
self.window_action.find.simple_action.set_enabled(false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user