Skip to content

Commit

Permalink
implement middle click controller for history navigation buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Jan 26, 2025
1 parent b9b91fb commit 2b472eb
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 44 deletions.
5 changes: 1 addition & 4 deletions src/app/browser/window/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
15 changes: 4 additions & 11 deletions src/app/browser/window/tab/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +41,7 @@ impl Item {
(browser_action, window_action, tab_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<super::Action>,
&Rc<TabAction>,
),
(position, request, is_pinned, is_selected, is_attention, is_load): (
Position,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions src/app/browser/window/tab/item/action/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ impl History {
self.forward.set_enabled(self.forward(false).is_some());
}

pub fn back(&self, follow_to_index: bool) -> Option<GString> {
self.memory.back(follow_to_index)
pub fn back(&self, is_follow_to_index: bool) -> Option<GString> {
self.memory.back(is_follow_to_index)
}

pub fn forward(&self, follow_to_index: bool) -> Option<GString> {
self.memory.next(follow_to_index)
pub fn forward(&self, is_follow_to_index: bool) -> Option<GString> {
self.memory.next(is_follow_to_index)
}
}
9 changes: 4 additions & 5 deletions src/app/browser/window/tab/item/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,12 +40,12 @@ impl Page {
pub fn build(
id: &Rc<GString>,
profile: &Rc<Profile>,
(browser_action, window_action, item_action): (
(browser_action, window_action, tab_action, item_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<TabAction>,
&Rc<ItemAction>,
),
(back_action_name, forward_action_name): (&str, &str),
) -> Self {
// Init components
let content = Rc::new(Content::build((window_action, item_action)));
Expand All @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions src/app/browser/window/tab/item/page/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,17 +29,17 @@ pub struct Navigation {
impl Navigation {
pub fn build(
profile: &Rc<Profile>,
(browser_action, window_action, item_action): (
(browser_action, window_action, tab_action, item_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<TabAction>,
&Rc<ItemAction>,
),
(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);
Expand Down
10 changes: 6 additions & 4 deletions src/app/browser/window/tab/item/page/navigation/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ 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<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>)) -> Self;
}

impl History for Box {
fn history(back_action_name: &str, forward_action_name: &str) -> Self {
fn history(action: (&Rc<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>)) -> Self {
let g_box = Box::builder()
.orientation(Orientation::Horizontal)
.css_classes([
"linked", // merge childs
])
.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
}
}
52 changes: 46 additions & 6 deletions src/app/browser/window/tab/item/page/navigation/history/back.rs
Original file line number Diff line number Diff line change
@@ -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<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>)) -> 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<WindowAction>,
&Rc<TabAction>,
&Rc<ItemAction>,
),
) -> 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
}
}
52 changes: 46 additions & 6 deletions src/app/browser/window/tab/item/page/navigation/history/forward.rs
Original file line number Diff line number Diff line change
@@ -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<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>)) -> 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<WindowAction>,
&Rc<TabAction>,
&Rc<ItemAction>,
),
) -> 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
}
}

0 comments on commit 2b472eb

Please sign in to comment.