reorganize home action, add middle click handler

This commit is contained in:
yggverse 2025-01-26 13:07:20 +02:00
parent 418b04c86c
commit 3ee6a03c30
5 changed files with 93 additions and 30 deletions

View file

@ -56,9 +56,10 @@ impl Item {
let id = Rc::new(uuid_string_random()); let id = Rc::new(uuid_string_random());
// Init components // Init components
let action = Rc::new(Action::new()); let action = Rc::new(Action::new());
tab_action.simple_action_group.add_action(&action.home);
tab_action tab_action
.simple_action_group .simple_action_group
.add_action(&action.history.back); .add_action(&action.history.back);
@ -85,16 +86,20 @@ impl Item {
// Update tab loading indicator // Update tab loading indicator
let client = Rc::new(Client::init(&page, &widget.tab_page)); let client = Rc::new(Client::init(&page, &widget.tab_page));
// Init events // Connect events
action.home.connect_activate({
if let Some(text) = request { let client = client.clone();
page.navigation.request.widget.entry.set_text(text); let page = page.clone();
if is_load { move |this, _| {
client.handle(text, true); 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({ action.ident.connect_activate({
let browser_action = browser_action.clone(); let browser_action = browser_action.clone();
let page = page.clone(); let page = page.clone();
@ -102,9 +107,7 @@ impl Item {
let profile = profile.clone(); let profile = profile.clone();
let window_action = window_action.clone(); let window_action = window_action.clone();
move || { move || {
// Request should match valid URI for all drivers supported
if let Some(uri) = page.navigation.request.uri() { if let Some(uri) = page.navigation.request.uri() {
// Route by scheme
let scheme = uri.scheme(); let scheme = uri.scheme();
if scheme == "gemini" || scheme == "titan" { if scheme == "gemini" || scheme == "titan" {
return identity::default( return identity::default(
@ -115,12 +118,10 @@ impl Item {
.present(Some(&parent)); .present(Some(&parent));
} }
} }
// Show dialog with unsupported request message
identity::unsupported().present(Some(&parent)); identity::unsupported().present(Some(&parent));
} }
}); });
// Load new request for item
action.load.connect_activate({ action.load.connect_activate({
let page = page.clone(); let page = page.clone();
let client = client.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 // Done
Self { Self {
id, id,
@ -144,6 +152,13 @@ impl Item {
// Actions // Actions
pub fn update(&self) { 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 // Update child components
self.page.update(); self.page.update();
} }

View file

@ -1,8 +1,11 @@
mod history; mod history;
mod home;
mod ident; mod ident;
mod load; mod load;
use gtk::gio::SimpleAction;
use history::History; use history::History;
use home::Home;
use ident::Ident; use ident::Ident;
use load::Load; use load::Load;
@ -11,6 +14,7 @@ use std::rc::Rc;
/// [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html) wrapper for `Browser` actions /// [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html) wrapper for `Browser` actions
pub struct Action { pub struct Action {
pub history: Rc<History>, pub history: Rc<History>,
pub home: SimpleAction,
pub ident: Rc<Ident>, pub ident: Rc<Ident>,
pub load: Rc<Load>, pub load: Rc<Load>,
} }
@ -28,6 +32,7 @@ impl Action {
pub fn new() -> Self { pub fn new() -> Self {
let ident = Rc::new(Ident::new()); let ident = Rc::new(Ident::new());
let load = Rc::new(Load::new()); let load = Rc::new(Load::new());
let home = SimpleAction::home();
let history = Rc::new(History::build({ let history = Rc::new(History::build({
let load = load.clone(); let load = load.clone();
@ -36,6 +41,7 @@ impl Action {
Self { Self {
history, history,
home,
ident, ident,
load, load,
} }

View file

@ -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
}
}

View file

@ -38,10 +38,10 @@ impl Navigation {
) -> Self { ) -> Self {
// init children components // init children components
let home = Button::home(window_action);
let history = Box::history((window_action, tab_action, item_action)); let history = Box::history((window_action, tab_action, item_action));
let reload = Button::reload(window_action); let reload = Button::reload(window_action);
let request = Rc::new(Request::build((browser_action, item_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); let bookmark = Button::bookmark(window_action);
// init main widget // init main widget
@ -80,11 +80,6 @@ impl Navigation {
.get(&self.request.strip_prefix()) .get(&self.request.strip_prefix())
.is_some(), .is_some(),
); );
self.home.set_sensitive(
self.request
.home()
.is_some_and(|home| home.to_string() != request),
);
} }
pub fn clean( pub fn clean(

View file

@ -1,21 +1,55 @@
use super::WindowAction; use super::{ItemAction, Request, TabAction, WindowAction};
use gtk::{prelude::ActionExt, Button}; use crate::app::browser::window::action::Position;
use gtk::{
gdk::BUTTON_MIDDLE,
prelude::{ActionExt, WidgetExt},
Button, GestureClick,
};
use std::rc::Rc; use std::rc::Rc;
pub trait Home { pub trait Home {
fn home(action: &Rc<WindowAction>) -> Self; fn home(
action: (&Rc<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>),
request: &Rc<Request>,
) -> Self;
} }
impl Home for Button { impl Home for Button {
fn home(action: &Rc<WindowAction>) -> Self { fn home(
Button::builder() (window_action, tab_action, item_action): (
.action_name(format!( &Rc<WindowAction>,
"{}.{}", &Rc<TabAction>,
action.id, &Rc<ItemAction>,
action.home.simple_action.name() ),
)) // @TODO request: &Rc<Request>,
) -> Self {
let button = Button::builder()
.action_name(format!("{}.{}", tab_action.id, item_action.home.name()))
.icon_name("go-home-symbolic") .icon_name("go-home-symbolic")
.tooltip_text("Home") .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
} }
} }