From 3ee6a03c3054e34aff98aa42dc0feb41edf61d11 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 26 Jan 2025 13:07:20 +0200 Subject: [PATCH] reorganize home action, add middle click handler --- src/app/browser/window/tab/item.rs | 41 +++++++++----- src/app/browser/window/tab/item/action.rs | 6 ++ .../browser/window/tab/item/action/home.rs | 13 +++++ .../window/tab/item/page/navigation.rs | 7 +-- .../window/tab/item/page/navigation/home.rs | 56 +++++++++++++++---- 5 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 src/app/browser/window/tab/item/action/home.rs diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index 5ff1c090..5189cf13 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -56,9 +56,10 @@ impl Item { let id = Rc::new(uuid_string_random()); // Init components - let action = Rc::new(Action::new()); + tab_action.simple_action_group.add_action(&action.home); + tab_action .simple_action_group .add_action(&action.history.back); @@ -85,16 +86,20 @@ impl Item { // Update tab loading indicator let client = Rc::new(Client::init(&page, &widget.tab_page)); - // Init events - - if let Some(text) = request { - page.navigation.request.widget.entry.set_text(text); - if is_load { - client.handle(text, true); + // Connect events + action.home.connect_activate({ + let client = client.clone(); + let page = page.clone(); + move |this, _| { + this.set_enabled(false); + if let Some(uri) = page.navigation.request.home() { + let request = uri.to_string(); + page.navigation.request.widget.entry.set_text(&request); + client.handle(&request, true); + } } - } + }); - // Show identity selection for item action.ident.connect_activate({ let browser_action = browser_action.clone(); let page = page.clone(); @@ -102,9 +107,7 @@ impl Item { let profile = profile.clone(); let window_action = window_action.clone(); move || { - // Request should match valid URI for all drivers supported if let Some(uri) = page.navigation.request.uri() { - // Route by scheme let scheme = uri.scheme(); if scheme == "gemini" || scheme == "titan" { return identity::default( @@ -115,12 +118,10 @@ impl Item { .present(Some(&parent)); } } - // Show dialog with unsupported request message identity::unsupported().present(Some(&parent)); } }); - // Load new request for item action.load.connect_activate({ let page = page.clone(); let client = client.clone(); @@ -132,6 +133,13 @@ impl Item { } }); + // Handle immediately on request + if let Some(text) = request { + page.navigation.request.widget.entry.set_text(text); + if is_load { + client.handle(text, true); + } + } // Done Self { id, @@ -144,6 +152,13 @@ impl Item { // Actions pub fn update(&self) { + // Update self actions + self.action + .home + .set_enabled(self.page.navigation.request.home().is_some_and(|home| { + home.to_string() != self.page.navigation.request.widget.entry.text() + })); + // Update child components self.page.update(); } diff --git a/src/app/browser/window/tab/item/action.rs b/src/app/browser/window/tab/item/action.rs index e93b5f7e..2e604f50 100644 --- a/src/app/browser/window/tab/item/action.rs +++ b/src/app/browser/window/tab/item/action.rs @@ -1,8 +1,11 @@ mod history; +mod home; mod ident; mod load; +use gtk::gio::SimpleAction; use history::History; +use home::Home; use ident::Ident; use load::Load; @@ -11,6 +14,7 @@ use std::rc::Rc; /// [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html) wrapper for `Browser` actions pub struct Action { pub history: Rc, + pub home: SimpleAction, pub ident: Rc, pub load: Rc, } @@ -28,6 +32,7 @@ impl Action { pub fn new() -> Self { let ident = Rc::new(Ident::new()); let load = Rc::new(Load::new()); + let home = SimpleAction::home(); let history = Rc::new(History::build({ let load = load.clone(); @@ -36,6 +41,7 @@ impl Action { Self { history, + home, ident, load, } diff --git a/src/app/browser/window/tab/item/action/home.rs b/src/app/browser/window/tab/item/action/home.rs new file mode 100644 index 00000000..283b971a --- /dev/null +++ b/src/app/browser/window/tab/item/action/home.rs @@ -0,0 +1,13 @@ +use gtk::{gio::SimpleAction, glib::uuid_string_random}; + +pub trait Home { + fn home() -> Self; +} + +impl Home for SimpleAction { + fn home() -> Self { + let home = SimpleAction::new(&uuid_string_random(), None); + home.set_enabled(false); + home + } +} diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index 1226074a..b461c9df 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -38,10 +38,10 @@ impl Navigation { ) -> Self { // init children components - let home = Button::home(window_action); let history = Box::history((window_action, tab_action, item_action)); let reload = Button::reload(window_action); let request = Rc::new(Request::build((browser_action, item_action))); + let home = Button::home((window_action, tab_action, item_action), &request); let bookmark = Button::bookmark(window_action); // init main widget @@ -80,11 +80,6 @@ impl Navigation { .get(&self.request.strip_prefix()) .is_some(), ); - self.home.set_sensitive( - self.request - .home() - .is_some_and(|home| home.to_string() != request), - ); } pub fn clean( diff --git a/src/app/browser/window/tab/item/page/navigation/home.rs b/src/app/browser/window/tab/item/page/navigation/home.rs index 454d0e71..20f93e70 100644 --- a/src/app/browser/window/tab/item/page/navigation/home.rs +++ b/src/app/browser/window/tab/item/page/navigation/home.rs @@ -1,21 +1,55 @@ -use super::WindowAction; -use gtk::{prelude::ActionExt, Button}; +use super::{ItemAction, Request, TabAction, WindowAction}; +use crate::app::browser::window::action::Position; +use gtk::{ + gdk::BUTTON_MIDDLE, + prelude::{ActionExt, WidgetExt}, + Button, GestureClick, +}; use std::rc::Rc; pub trait Home { - fn home(action: &Rc) -> Self; + fn home( + action: (&Rc, &Rc, &Rc), + request: &Rc, + ) -> Self; } impl Home for Button { - fn home(action: &Rc) -> Self { - Button::builder() - .action_name(format!( - "{}.{}", - action.id, - action.home.simple_action.name() - )) // @TODO + fn home( + (window_action, tab_action, item_action): ( + &Rc, + &Rc, + &Rc, + ), + request: &Rc, + ) -> Self { + let button = Button::builder() + .action_name(format!("{}.{}", tab_action.id, item_action.home.name())) .icon_name("go-home-symbolic") .tooltip_text("Home") - .build() + .build(); + + // Navigate home in the new tab (feature) + let button_middle_controller = GestureClick::builder().button(BUTTON_MIDDLE).build(); + + button_middle_controller.connect_pressed({ + let request = request.clone(); + let window_action = window_action.clone(); + move |_, _, _, _| { + if let Some(uri) = request.home() { + window_action.append.activate_stateful_once( + Position::After, + Some(uri.to_string()), + false, + true, + false, + true, + ); + } + } + }); + + button.add_controller(button_middle_controller); + button } }