Skip to content

Commit

Permalink
reorganize reload action, add middle click handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Jan 26, 2025
1 parent 3ee6a03 commit 8b4d184
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/app/browser/window/tab/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Item {
let action = Rc::new(Action::new());

tab_action.simple_action_group.add_action(&action.home);
tab_action.simple_action_group.add_action(&action.reload);

tab_action
.simple_action_group
Expand Down Expand Up @@ -133,6 +134,14 @@ impl Item {
}
});

action.reload.connect_activate({
let page = page.clone();
let client = client.clone();
move |_, _| {
client.handle(&page.navigation.request.widget.entry.text(), false);
}
});

// Handle immediately on request
if let Some(text) = request {
page.navigation.request.widget.entry.set_text(text);
Expand All @@ -159,6 +168,10 @@ impl Item {
home.to_string() != self.page.navigation.request.widget.entry.text()
}));

self.action
.reload
.set_enabled(!self.page.navigation.request.widget.entry.text().is_empty());

// Update child components
self.page.update();
}
Expand Down
5 changes: 5 additions & 0 deletions src/app/browser/window/tab/item/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ mod history;
mod home;
mod ident;
mod load;
mod reload;

use gtk::gio::SimpleAction;
use history::History;
use home::Home;
use ident::Ident;
use load::Load;
use reload::Reload;

use std::rc::Rc;

Expand All @@ -17,6 +19,7 @@ pub struct Action {
pub home: SimpleAction,
pub ident: Rc<Ident>,
pub load: Rc<Load>,
pub reload: SimpleAction,
}

impl Default for Action {
Expand All @@ -33,6 +36,7 @@ impl Action {
let ident = Rc::new(Ident::new());
let load = Rc::new(Load::new());
let home = SimpleAction::home();
let reload = SimpleAction::reload();

let history = Rc::new(History::build({
let load = load.clone();
Expand All @@ -41,6 +45,7 @@ impl Action {

Self {
history,
reload,
home,
ident,
load,
Expand Down
13 changes: 13 additions & 0 deletions src/app/browser/window/tab/item/action/reload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use gtk::{gio::SimpleAction, glib::uuid_string_random};

pub trait Reload {
fn reload() -> Self;
}

impl Reload for SimpleAction {
fn reload() -> Self {
let reload = SimpleAction::new(&uuid_string_random(), None);
reload.set_enabled(false);
reload
}
}
13 changes: 4 additions & 9 deletions src/app/browser/window/tab/item/page/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod widget;

use super::{BrowserAction, ItemAction, Profile, TabAction, WindowAction};
use bookmark::Bookmark;
use gtk::{prelude::WidgetExt, Box, Button};
use gtk::{Box, Button};
use history::History;
use home::Home;
use reload::Reload;
Expand Down Expand Up @@ -39,8 +39,8 @@ impl Navigation {
// init children components

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 reload = Button::reload((window_action, tab_action, item_action), &request);
let home = Button::home((window_action, tab_action, item_action), &request);
let bookmark = Button::bookmark(window_action);

Expand Down Expand Up @@ -73,13 +73,8 @@ impl Navigation {
// update children components
self.bookmark
.update(self.profile.bookmark.get(&request).is_ok());
self.reload.set_sensitive(!request.is_empty());
self.request.update(
self.profile
.identity
.get(&self.request.strip_prefix())
.is_some(),
);
self.request
.update(self.profile.identity.get(&request).is_some());
}

pub fn clean(
Expand Down
56 changes: 45 additions & 11 deletions src/app/browser/window/tab/item/page/navigation/reload.rs
Original file line number Diff line number Diff line change
@@ -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 Reload {
fn reload(action: &Rc<WindowAction>) -> Self;
fn reload(
action: (&Rc<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>),
request: &Rc<Request>,
) -> Self;
}

impl Reload for Button {
fn reload(action: &Rc<WindowAction>) -> Self {
Button::builder()
.action_name(format!(
"{}.{}",
action.id,
action.reload.simple_action.name()
)) // @TODO
fn reload(
(window_action, tab_action, item_action): (
&Rc<WindowAction>,
&Rc<TabAction>,
&Rc<ItemAction>,
),
request: &Rc<Request>,
) -> Self {
let button = Button::builder()
.action_name(format!("{}.{}", tab_action.id, item_action.reload.name()))
.icon_name("view-refresh-symbolic")
.tooltip_text("Reload")
.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
}
}

0 comments on commit 8b4d184

Please sign in to comment.