From 2b472eb6189dae7737031b17af4650c8d45df7f9 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 26 Jan 2025 10:06:05 +0200 Subject: [PATCH] implement middle click controller for history navigation buttons --- src/app/browser/window/tab.rs | 5 +- src/app/browser/window/tab/item.rs | 15 ++---- .../browser/window/tab/item/action/history.rs | 8 +-- src/app/browser/window/tab/item/page.rs | 9 ++-- .../window/tab/item/page/navigation.rs | 8 +-- .../tab/item/page/navigation/history.rs | 10 ++-- .../tab/item/page/navigation/history/back.rs | 52 ++++++++++++++++--- .../item/page/navigation/history/forward.rs | 52 ++++++++++++++++--- 8 files changed, 115 insertions(+), 44 deletions(-) diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index 0389df59..35af2694 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -11,10 +11,7 @@ pub use item::Item; use menu::Menu; use widget::Widget; -use crate::app::browser::{ - window::action::{Action as WindowAction, Position}, - Action as BrowserAction, -}; +use super::{Action as WindowAction, BrowserAction, Position}; use crate::Profile; use gtk::{ glib::{DateTime, GString, Propagation}, diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index d91145eb..af562e1f 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -5,17 +5,14 @@ mod identity; pub mod page; mod widget; -use crate::app::browser::{ - window::action::{Action as WindowAction, Position}, - Action as BrowserAction, -}; +use super::{Action as TabAction, BrowserAction, Position, WindowAction}; use crate::Profile; use action::Action; use adw::TabView; use client::Client; use gtk::{ glib::{uuid_string_random, GString}, - prelude::{ActionExt, ActionMapExt, Cast, EditableExt}, + prelude::{ActionMapExt, Cast, EditableExt}, }; use page::Page; use sqlite::Transaction; @@ -44,7 +41,7 @@ impl Item { (browser_action, window_action, tab_action): ( &Rc, &Rc, - &Rc, + &Rc, ), (position, request, is_pinned, is_selected, is_attention, is_load): ( Position, @@ -73,11 +70,7 @@ impl Item { let page = Rc::new(Page::build( &id, profile, - (browser_action, window_action, &action), - ( - &format!("{}.{}", &tab_action.id, action.history.back.name()), - &format!("{}.{}", &tab_action.id, action.history.forward.name()), - ), + (browser_action, window_action, tab_action, &action), )); let widget = Rc::new(Widget::build( diff --git a/src/app/browser/window/tab/item/action/history.rs b/src/app/browser/window/tab/item/action/history.rs index 14dab83e..a02229b0 100644 --- a/src/app/browser/window/tab/item/action/history.rs +++ b/src/app/browser/window/tab/item/action/history.rs @@ -69,11 +69,11 @@ impl History { self.forward.set_enabled(self.forward(false).is_some()); } - pub fn back(&self, follow_to_index: bool) -> Option { - self.memory.back(follow_to_index) + pub fn back(&self, is_follow_to_index: bool) -> Option { + self.memory.back(is_follow_to_index) } - pub fn forward(&self, follow_to_index: bool) -> Option { - self.memory.next(follow_to_index) + pub fn forward(&self, is_follow_to_index: bool) -> Option { + self.memory.next(is_follow_to_index) } } diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index d296d1bc..df95651d 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -13,7 +13,7 @@ use navigation::Navigation; use search::Search; use widget::Widget; -use super::{Action as ItemAction, BrowserAction, Profile, WindowAction}; +use super::{Action as ItemAction, BrowserAction, Profile, TabAction, WindowAction}; use gtk::{glib::GString, prelude::EditableExt}; use sqlite::Transaction; @@ -40,12 +40,12 @@ impl Page { pub fn build( id: &Rc, profile: &Rc, - (browser_action, window_action, item_action): ( + (browser_action, window_action, tab_action, item_action): ( &Rc, &Rc, + &Rc, &Rc, ), - (back_action_name, forward_action_name): (&str, &str), ) -> Self { // Init components let content = Rc::new(Content::build((window_action, item_action))); @@ -54,8 +54,7 @@ impl Page { let navigation = Rc::new(Navigation::build( profile, - (browser_action, window_action, item_action), - (back_action_name, forward_action_name), + (browser_action, window_action, tab_action, item_action), )); let input = Rc::new(Input::new()); diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index 591630f2..1226074a 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -6,7 +6,7 @@ mod reload; mod request; mod widget; -use super::{BrowserAction, ItemAction, Profile, WindowAction}; +use super::{BrowserAction, ItemAction, Profile, TabAction, WindowAction}; use bookmark::Bookmark; use gtk::{prelude::WidgetExt, Box, Button}; use history::History; @@ -29,17 +29,17 @@ pub struct Navigation { impl Navigation { pub fn build( profile: &Rc, - (browser_action, window_action, item_action): ( + (browser_action, window_action, tab_action, item_action): ( &Rc, &Rc, + &Rc, &Rc, ), - (back_action_name, forward_action_name): (&str, &str), ) -> Self { // init children components let home = Button::home(window_action); - let history = Box::history(back_action_name, forward_action_name); + 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 bookmark = Button::bookmark(window_action); diff --git a/src/app/browser/window/tab/item/page/navigation/history.rs b/src/app/browser/window/tab/item/page/navigation/history.rs index d64c7e94..d423e0f5 100644 --- a/src/app/browser/window/tab/item/page/navigation/history.rs +++ b/src/app/browser/window/tab/item/page/navigation/history.rs @@ -4,14 +4,16 @@ pub mod forward; pub use back::Back; pub use forward::Forward; +use super::{ItemAction, TabAction, WindowAction}; use gtk::{prelude::BoxExt, Box, Button, Orientation}; +use std::rc::Rc; pub trait History { - fn history(back_action_name: &str, forward_action_name: &str) -> Self; + fn history(action: (&Rc, &Rc, &Rc)) -> Self; } impl History for Box { - fn history(back_action_name: &str, forward_action_name: &str) -> Self { + fn history(action: (&Rc, &Rc, &Rc)) -> Self { let g_box = Box::builder() .orientation(Orientation::Horizontal) .css_classes([ @@ -19,8 +21,8 @@ impl History for Box { ]) .build(); - g_box.append(&Button::back(back_action_name)); - g_box.append(&Button::forward(forward_action_name)); + g_box.append(&Button::back(action)); + g_box.append(&Button::forward(action)); g_box } } diff --git a/src/app/browser/window/tab/item/page/navigation/history/back.rs b/src/app/browser/window/tab/item/page/navigation/history/back.rs index cce1bbc8..fecafd0b 100644 --- a/src/app/browser/window/tab/item/page/navigation/history/back.rs +++ b/src/app/browser/window/tab/item/page/navigation/history/back.rs @@ -1,15 +1,55 @@ -use gtk::Button; +use super::{ItemAction, 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 Back { - fn back(action_name: &str) -> Self; + fn back(action: (&Rc, &Rc, &Rc)) -> Self; } impl Back for Button { - fn back(action_name: &str) -> Self { - Button::builder() - .action_name(action_name) + fn back( + (window_action, tab_action, item_action): ( + &Rc, + &Rc, + &Rc, + ), + ) -> Self { + // Init main widget + let button = Button::builder() + .action_name(format!( + "{}.{}", + tab_action.id, + item_action.history.back.name() + )) .icon_name("go-previous-symbolic") .tooltip_text("Back") - .build() + .build(); + + // Ability to open previous history record in the new tab (without change current page state) + let new_tab_controller = GestureClick::builder().button(BUTTON_MIDDLE).build(); + + new_tab_controller.connect_pressed({ + let item_action = item_action.clone(); + let window_action = window_action.clone(); + move |_, _, _, _| { + if let Some(request) = item_action.history.back(false) { + window_action.append.activate_stateful_once( + Position::After, + Some(request.to_string()), + false, + true, + false, + true, + ); + } + } + }); + button.add_controller(new_tab_controller); + button } } diff --git a/src/app/browser/window/tab/item/page/navigation/history/forward.rs b/src/app/browser/window/tab/item/page/navigation/history/forward.rs index c560db98..28ed02bb 100644 --- a/src/app/browser/window/tab/item/page/navigation/history/forward.rs +++ b/src/app/browser/window/tab/item/page/navigation/history/forward.rs @@ -1,15 +1,55 @@ -use gtk::Button; +use super::{ItemAction, 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 Forward { - fn forward(action_name: &str) -> Self; + fn forward(action: (&Rc, &Rc, &Rc)) -> Self; } impl Forward for Button { - fn forward(action_name: &str) -> Self { - Button::builder() - .action_name(action_name) + fn forward( + (window_action, tab_action, item_action): ( + &Rc, + &Rc, + &Rc, + ), + ) -> Self { + // Init main widget + let button = Button::builder() + .action_name(format!( + "{}.{}", + tab_action.id, + item_action.history.forward.name() + )) .icon_name("go-next-symbolic") .tooltip_text("Forward") - .build() + .build(); + + // Ability to open next history record in the new tab (without change current page state) + let new_tab_controller = GestureClick::builder().button(BUTTON_MIDDLE).build(); + + new_tab_controller.connect_pressed({ + let item_action = item_action.clone(); + let window_action = window_action.clone(); + move |_, _, _, _| { + if let Some(request) = item_action.history.forward(false) { + window_action.append.activate_stateful_once( + Position::After, + Some(request.to_string()), + false, + true, + false, + true, + ); + } + } + }); + button.add_controller(new_tab_controller); + button } }