mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-28 20:14:13 +00:00
create separated wrapper for pin action
This commit is contained in:
parent
e47c22ac92
commit
33b22ca30a
30
src/app.rs
30
src/app.rs
@ -8,7 +8,7 @@ use crate::profile::Profile;
|
||||
use adw::Application;
|
||||
use gtk::{
|
||||
gio::SimpleAction,
|
||||
glib::{gformat, uuid_string_random, ExitCode},
|
||||
glib::{uuid_string_random, ExitCode},
|
||||
prelude::{
|
||||
ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt, ToVariant,
|
||||
},
|
||||
@ -43,8 +43,6 @@ impl App {
|
||||
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
|
||||
let action_page_reload =
|
||||
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
|
||||
let action_page_pin =
|
||||
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
|
||||
|
||||
// Init GTK
|
||||
let gobject = Application::builder()
|
||||
@ -60,7 +58,6 @@ impl App {
|
||||
action_page_history_back.clone(),
|
||||
action_page_history_forward.clone(),
|
||||
action_page_reload.clone(),
|
||||
action_page_pin.clone(),
|
||||
));
|
||||
|
||||
// Init events
|
||||
@ -172,7 +169,7 @@ impl App {
|
||||
for (detailed_action_name, &accels) in &[
|
||||
// Browser actions
|
||||
(
|
||||
gformat!(
|
||||
format!(
|
||||
"{}.{}",
|
||||
browser.action().id(),
|
||||
browser.action().close().id()
|
||||
@ -180,7 +177,7 @@ impl App {
|
||||
&["<Primary>Escape"],
|
||||
),
|
||||
(
|
||||
gformat!(
|
||||
format!(
|
||||
"{}.{}",
|
||||
browser.action().id(),
|
||||
browser.action().debug().id()
|
||||
@ -188,7 +185,7 @@ impl App {
|
||||
&["<Primary>i"],
|
||||
),
|
||||
(
|
||||
gformat!(
|
||||
format!(
|
||||
"{}.{}",
|
||||
browser.action().id(),
|
||||
browser.action().update().id()
|
||||
@ -197,28 +194,35 @@ impl App {
|
||||
),
|
||||
// Tab actions
|
||||
(
|
||||
gformat!(
|
||||
format!(
|
||||
"{}.{}",
|
||||
browser.window().action().id(),
|
||||
browser.window().action().append().id()
|
||||
),
|
||||
&["<Primary>t"],
|
||||
),
|
||||
(
|
||||
format!(
|
||||
"{}.{}",
|
||||
browser.window().action().id(),
|
||||
browser.window().action().pin().id()
|
||||
),
|
||||
&["<Primary>p"],
|
||||
),
|
||||
// @TODO
|
||||
(
|
||||
gformat!("win.{}", action_page_reload.name()),
|
||||
format!("win.{}", action_page_reload.name()),
|
||||
&["<Primary>r"],
|
||||
),
|
||||
(
|
||||
gformat!("win.{}", action_page_history_back.name()),
|
||||
format!("win.{}", action_page_history_back.name()),
|
||||
&["<Primary>Left"],
|
||||
),
|
||||
(
|
||||
gformat!("win.{}", action_page_history_forward.name()),
|
||||
format!("win.{}", action_page_history_forward.name()),
|
||||
&["<Primary>Right"],
|
||||
),
|
||||
(gformat!("win.{}", action_page_home.name()), &["<Primary>h"]),
|
||||
(gformat!("win.{}", action_page_pin.name()), &["<Primary>p"]),
|
||||
(format!("win.{}", action_page_home.name()), &["<Primary>h"]),
|
||||
// @TODO page close missed
|
||||
] {
|
||||
gobject.set_accels_for_action(detailed_action_name, &accels);
|
||||
|
@ -36,7 +36,6 @@ impl Browser {
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
) -> Browser {
|
||||
// Init components
|
||||
let action = Rc::new(Action::new());
|
||||
@ -48,7 +47,6 @@ impl Browser {
|
||||
action_page_history_back.clone(),
|
||||
action_page_history_forward.clone(),
|
||||
action_page_reload.clone(),
|
||||
action_page_pin.clone(),
|
||||
));
|
||||
|
||||
// Init widget
|
||||
@ -61,7 +59,6 @@ impl Browser {
|
||||
action_page_history_back.clone(),
|
||||
action_page_history_forward.clone(),
|
||||
action_page_reload.clone(),
|
||||
action_page_pin.clone(),
|
||||
],
|
||||
));
|
||||
|
||||
@ -159,13 +156,6 @@ impl Browser {
|
||||
move |this, _| window.tab_page_reload(page_position_from_action_state(this))
|
||||
});
|
||||
|
||||
action_page_pin.connect_activate({
|
||||
let window = window.clone();
|
||||
move |this, _| {
|
||||
window.tab_pin(page_position_from_action_state(this));
|
||||
}
|
||||
});
|
||||
|
||||
// Return new activated `Self`
|
||||
Self {
|
||||
action,
|
||||
|
@ -33,7 +33,6 @@ impl Window {
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
) -> Self {
|
||||
// Init local actions
|
||||
let action = Rc::new(Action::new());
|
||||
@ -47,7 +46,6 @@ impl Window {
|
||||
action_page_home.clone(),
|
||||
action_page_history_back.clone(),
|
||||
action_page_history_forward.clone(),
|
||||
action_page_pin.clone(),
|
||||
action_page_reload.clone(),
|
||||
);
|
||||
|
||||
@ -61,7 +59,6 @@ impl Window {
|
||||
action_page_history_back,
|
||||
action_page_history_forward,
|
||||
action_page_reload,
|
||||
action_page_pin,
|
||||
// Widgets
|
||||
tab.gobject(),
|
||||
);
|
||||
@ -77,6 +74,11 @@ impl Window {
|
||||
}
|
||||
});
|
||||
|
||||
action.pin().connect_activate({
|
||||
let tab = tab.clone();
|
||||
move |page_position| tab.pin(page_position)
|
||||
});
|
||||
|
||||
// Init struct
|
||||
Self {
|
||||
//header,
|
||||
@ -113,10 +115,6 @@ impl Window {
|
||||
self.tab.close_all();
|
||||
}
|
||||
|
||||
pub fn tab_pin(&self, page_position: Option<i32>) {
|
||||
self.tab.pin(page_position);
|
||||
}
|
||||
|
||||
pub fn update(&self, tab_item_id: Option<GString>) {
|
||||
self.tab.update(tab_item_id);
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
mod append;
|
||||
mod pin;
|
||||
|
||||
use append::Append;
|
||||
use pin::Pin;
|
||||
|
||||
use gtk::{
|
||||
gio::SimpleActionGroup,
|
||||
@ -13,6 +15,7 @@ use std::rc::Rc;
|
||||
pub struct Action {
|
||||
// Actions
|
||||
append: Rc<Append>,
|
||||
pin: Rc<Pin>,
|
||||
// Group
|
||||
id: GString,
|
||||
gobject: SimpleActionGroup,
|
||||
@ -25,6 +28,7 @@ impl Action {
|
||||
pub fn new() -> Self {
|
||||
// Init actions
|
||||
let append = Rc::new(Append::new());
|
||||
let pin = Rc::new(Pin::new());
|
||||
|
||||
// Generate unique group ID
|
||||
let id = uuid_string_random();
|
||||
@ -34,10 +38,12 @@ impl Action {
|
||||
|
||||
// Add action to given group
|
||||
gobject.add_action(append.gobject());
|
||||
gobject.add_action(pin.gobject());
|
||||
|
||||
// Done
|
||||
Self {
|
||||
append,
|
||||
pin,
|
||||
id,
|
||||
gobject,
|
||||
}
|
||||
@ -50,6 +56,11 @@ impl Action {
|
||||
&self.append
|
||||
}
|
||||
|
||||
/// Get reference `Pin` action
|
||||
pub fn pin(&self) -> &Rc<Pin> {
|
||||
&self.pin
|
||||
}
|
||||
|
||||
/// Get auto-generated name for action group
|
||||
/// * useful for manual relationship with GObjects or as the `detailed_name`
|
||||
/// for [Accels](https://docs.gtk.org/gtk4/method.Application.set_accels_for_action.html) or
|
||||
|
@ -4,7 +4,7 @@ use gtk::{
|
||||
prelude::ActionExt,
|
||||
};
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Append` action of `Browser` group
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Append` action of `Window` group
|
||||
pub struct Append {
|
||||
gobject: SimpleAction,
|
||||
}
|
||||
|
80
src/app/browser/window/action/pin.rs
Normal file
80
src/app/browser/window/action/pin.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use gtk::{
|
||||
gio::SimpleAction,
|
||||
glib::{uuid_string_random, GString},
|
||||
prelude::{ActionExt, ToVariant},
|
||||
};
|
||||
|
||||
// Defaults
|
||||
|
||||
/// C-compatible variant type
|
||||
const DEFAULT_STATE: i32 = -1;
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Pin` action of `Window` group
|
||||
pub struct Pin {
|
||||
gobject: SimpleAction,
|
||||
}
|
||||
|
||||
impl Pin {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
gobject: 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.gobject.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) {
|
||||
let state = self.state();
|
||||
self.gobject.connect_activate(move |_, _| callback(state));
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn state(&self) -> Option<i32> {
|
||||
let state = self
|
||||
.gobject
|
||||
.state()
|
||||
.expect("State value required for this action")
|
||||
.get::<i32>()
|
||||
.expect("Parameter type does not match `i32`");
|
||||
|
||||
if state != DEFAULT_STATE {
|
||||
Some(state)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Get reference to [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) GObject
|
||||
pub fn gobject(&self) -> &SimpleAction {
|
||||
&self.gobject
|
||||
}
|
||||
|
||||
/// Get auto-generated [action name](https://docs.gtk.org/gio/property.SimpleAction.name.html)
|
||||
pub fn id(&self) -> GString {
|
||||
self.gobject.name()
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@ impl Header {
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
// Widgets
|
||||
tab_view: &TabView,
|
||||
) -> Rc<Self> {
|
||||
@ -40,7 +39,6 @@ impl Header {
|
||||
action_page_history_back,
|
||||
action_page_history_forward,
|
||||
action_page_reload,
|
||||
action_page_pin,
|
||||
tab_view,
|
||||
);
|
||||
|
||||
|
@ -29,7 +29,6 @@ impl Bar {
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
view: &TabView,
|
||||
) -> Rc<Self> {
|
||||
// Init components
|
||||
@ -44,7 +43,6 @@ impl Bar {
|
||||
action_page_history_back,
|
||||
action_page_history_forward,
|
||||
action_page_reload,
|
||||
action_page_pin,
|
||||
);
|
||||
|
||||
// Build result
|
||||
|
@ -6,7 +6,6 @@ use crate::app::browser::action::Action as BrowserAction;
|
||||
use crate::app::browser::window::action::Action as WindowAction;
|
||||
use gtk::{
|
||||
gio::{self, SimpleAction},
|
||||
glib::{gformat, GString},
|
||||
prelude::ActionExt,
|
||||
MenuButton,
|
||||
};
|
||||
@ -26,21 +25,24 @@ impl Menu {
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
) -> Rc<Self> {
|
||||
// Main
|
||||
let main = gio::Menu::new();
|
||||
|
||||
// Main > Page
|
||||
let main_page = gio::Menu::new();
|
||||
main_page.append(Some("New"), Some(&gformat!(
|
||||
main_page.append(Some("New"), Some(&format!(
|
||||
"{}.{}",
|
||||
window_action.id(),
|
||||
window_action.append().id()
|
||||
)));
|
||||
|
||||
main_page.append(Some("Reload"), Some(&detailed_action_name(&action_page_reload)));
|
||||
main_page.append(Some("Pin"), Some(&detailed_action_name(&action_page_pin)));
|
||||
main_page.append(Some("Pin"), Some(&format!(
|
||||
"{}.{}",
|
||||
window_action.id(),
|
||||
window_action.pin().id()
|
||||
)));
|
||||
|
||||
// Main > Page > Navigation
|
||||
let main_page_navigation = gio::Menu::new();
|
||||
@ -68,19 +70,19 @@ impl Menu {
|
||||
let main_tool = gio::Menu::new();
|
||||
|
||||
// Debug
|
||||
main_tool.append(Some("Debug"), Some(&gformat!(
|
||||
main_tool.append(Some("Debug"), Some(&format!(
|
||||
"{}.{}",
|
||||
browser_action.id(),
|
||||
browser_action.debug().id()
|
||||
)));
|
||||
|
||||
main_tool.append(Some("Profile"), Some(&gformat!(
|
||||
main_tool.append(Some("Profile"), Some(&format!(
|
||||
"{}.{}",
|
||||
browser_action.id(),
|
||||
browser_action.profile().id()
|
||||
)));
|
||||
|
||||
main_tool.append(Some("About"), Some(&gformat!(
|
||||
main_tool.append(Some("About"), Some(&format!(
|
||||
"{}.{}",
|
||||
browser_action.id(),
|
||||
browser_action.about().id()
|
||||
@ -88,7 +90,7 @@ impl Menu {
|
||||
|
||||
main.append_submenu(Some("Tool"), &main_tool);
|
||||
|
||||
main.append(Some("Quit"), Some(&gformat!(
|
||||
main.append(Some("Quit"), Some(&format!(
|
||||
"{}.{}",
|
||||
browser_action.id(),
|
||||
browser_action.close().id()
|
||||
@ -105,8 +107,8 @@ impl Menu {
|
||||
}
|
||||
|
||||
// Private helpers
|
||||
fn detailed_action_name(action: &SimpleAction) -> GString {
|
||||
gformat!("win.{}", action.name()) // @TODO find the way to ident parent group
|
||||
// without application-wide dependencies import
|
||||
// see also src/app/action.rs
|
||||
fn detailed_action_name(action: &SimpleAction) -> String {
|
||||
format!("win.{}", action.name()) // @TODO find the way to ident parent group
|
||||
// without application-wide dependencies import
|
||||
// see also src/app/action.rs
|
||||
}
|
||||
|
@ -47,7 +47,6 @@ impl Tab {
|
||||
action_page_home: SimpleAction,
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
) -> Rc<Self> {
|
||||
// Init local actions
|
||||
@ -58,12 +57,12 @@ impl Tab {
|
||||
|
||||
// Init context menu
|
||||
let menu = Menu::new(
|
||||
window_action.clone(),
|
||||
action_page_close_all.clone(),
|
||||
action_page_close.clone(),
|
||||
action_page_history_back.clone(),
|
||||
action_page_history_forward.clone(),
|
||||
action_page_home.clone(),
|
||||
action_page_pin.clone(),
|
||||
action_page_reload.clone(),
|
||||
);
|
||||
|
||||
@ -123,9 +122,19 @@ impl Tab {
|
||||
let action_page_history_back = action_page_history_back.clone();
|
||||
let action_page_history_forward = action_page_history_forward.clone();
|
||||
let action_page_home = action_page_home.clone();
|
||||
let action_page_pin = action_page_pin.clone();
|
||||
let window_action = window_action.clone();
|
||||
let action_page_reload = action_page_reload.clone();
|
||||
move |tab_view, tab_page| {
|
||||
// Update actions
|
||||
let state_v2 = match tab_page {
|
||||
// Context menu opened
|
||||
Some(this) => Some(tab_view.page_position(this)),
|
||||
// Context menu closed (reset state to defaults)
|
||||
None => None,
|
||||
}; // @TODO
|
||||
window_action.pin().change_state(state_v2);
|
||||
|
||||
// @TODO old version requires update
|
||||
// Setup state for selected page
|
||||
let state = match tab_page {
|
||||
// Context menu opened
|
||||
@ -134,12 +143,10 @@ impl Tab {
|
||||
None => (-1).to_variant(),
|
||||
};
|
||||
|
||||
// Update actions
|
||||
action_page_close.change_state(&state);
|
||||
action_page_history_back.change_state(&state);
|
||||
action_page_history_forward.change_state(&state);
|
||||
action_page_home.change_state(&state);
|
||||
action_page_pin.change_state(&state);
|
||||
action_page_reload.change_state(&state);
|
||||
}
|
||||
});
|
||||
|
@ -1,3 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::app::browser::window::action::Action as WindowAction;
|
||||
use gtk::{gio::SimpleAction, prelude::ActionExt};
|
||||
|
||||
/// Context menu wrapper
|
||||
@ -12,12 +15,12 @@ impl Menu {
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new(
|
||||
window_action: Rc<WindowAction>,
|
||||
action_page_close_all: SimpleAction,
|
||||
action_page_close: SimpleAction,
|
||||
action_page_history_back: SimpleAction,
|
||||
action_page_history_forward: SimpleAction,
|
||||
action_page_home: SimpleAction,
|
||||
action_page_pin: SimpleAction,
|
||||
action_page_reload: SimpleAction,
|
||||
) -> Self {
|
||||
let main = gtk::gio::Menu::new();
|
||||
@ -27,7 +30,14 @@ impl Menu {
|
||||
Some(&detailed_action_name(action_page_reload)),
|
||||
);
|
||||
|
||||
main.append(Some("Pin"), Some(&detailed_action_name(action_page_pin)));
|
||||
main.append(
|
||||
Some("Pin"),
|
||||
Some(&format!(
|
||||
"{}.{}",
|
||||
window_action.id(),
|
||||
window_action.pin().id()
|
||||
)),
|
||||
);
|
||||
|
||||
let navigation = gtk::gio::Menu::new();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user