update home button status detection

This commit is contained in:
yggverse 2025-01-23 14:34:46 +02:00
parent c5fad47a20
commit 5943ea622a
3 changed files with 27 additions and 23 deletions

View File

@ -42,7 +42,7 @@ impl Navigation {
let reload = Rc::new(Reload::build(window_action)); let reload = Rc::new(Reload::build(window_action));
let request = Rc::new(Request::build((browser_action, tab_action))); let request = Rc::new(Request::build((browser_action, tab_action)));
let bookmark = Rc::new(Bookmark::build(window_action)); let bookmark = Rc::new(Bookmark::build(window_action));
let home = Rc::new(Home::build(window_action, &request)); let home = Rc::new(Home::build(window_action));
// init main widget // init main widget
let widget = Rc::new(Widget::build( let widget = Rc::new(Widget::build(
@ -76,9 +76,17 @@ impl Navigation {
.update(self.profile.bookmark.get(&request).is_ok()); .update(self.profile.bookmark.get(&request).is_ok());
self.history.update(); self.history.update();
self.reload.update(!request.is_empty()); self.reload.update(!request.is_empty());
self.request self.request.update(
.update(self.profile.identity.get(&request).is_some()); self.profile
self.home.update(); .identity
.get(&self.request.strip_prefix())
.is_some(),
);
self.home.update(
self.request
.home()
.is_some_and(|home| home.to_string() != request),
);
} }
pub fn clean( pub fn clean(

View File

@ -1,4 +1,4 @@
use super::{Request, WindowAction}; use super::WindowAction;
use gtk::{ use gtk::{
prelude::{ButtonExt, WidgetExt}, prelude::{ButtonExt, WidgetExt},
Button, Button,
@ -7,13 +7,12 @@ use std::rc::Rc;
pub struct Home { pub struct Home {
action: Rc<WindowAction>, action: Rc<WindowAction>,
request: Rc<Request>,
pub button: Button, pub button: Button,
} }
impl Home { impl Home {
// Construct // Construct
pub fn build(action: &Rc<WindowAction>, request: &Rc<Request>) -> Self { pub fn build(action: &Rc<WindowAction>) -> Self {
// Init gobject // Init gobject
let button = Button::builder() let button = Button::builder()
.icon_name("go-home-symbolic") .icon_name("go-home-symbolic")
@ -30,14 +29,12 @@ impl Home {
// Return activated `Self` // Return activated `Self`
Self { Self {
action: action.clone(), action: action.clone(),
request: request.clone(),
button, button,
} }
} }
// Actions // Actions
pub fn update(&self) { pub fn update(&self, has_home: bool) {
let has_home = self.request.home().is_some();
self.action.home.simple_action.set_enabled(has_home); self.action.home.simple_action.set_enabled(has_home);
self.button.set_sensitive(has_home); self.button.set_sensitive(has_home);
} }

View File

@ -119,20 +119,19 @@ impl Request {
strip_prefix(self.widget.entry.text()) strip_prefix(self.widget.entry.text())
} }
/// Parse home [Uri](https://docs.gtk.org/glib/struct.Uri.html) for self /// Parse home [Uri](https://docs.gtk.org/glib/struct.Uri.html) of `Self`
pub fn home(&self) -> Option<GString> { pub fn home(&self) -> Option<Uri> {
match self.uri() { match self.uri() {
Some(uri) => { Some(uri) => Some(Uri::build(
let scheme = uri.scheme().replace("titan", "gemini"); UriFlags::NONE,
let port = uri.port(); &uri.scheme(),
uri.host().map(|host| { uri.userinfo().as_deref(),
if port.is_positive() { uri.host().as_deref(),
gformat!("{scheme}://{host}:{port}/") uri.port(),
} else { "/",
gformat!("{scheme}://{host}/") None,
} None,
}) )),
}
None => None, None => None,
} }
} }