mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-02-04 07:24:17 +00:00
delegate home url parse to request component
This commit is contained in:
parent
eae35d5ab8
commit
137301200f
@ -250,8 +250,8 @@ impl Tab {
|
||||
|
||||
pub fn page_home(&self, page_position: Option<i32>) {
|
||||
if let Some(item) = self.item(page_position) {
|
||||
if let Some(text) = item.page.navigation.home.url() {
|
||||
item.page.navigation.request.widget.entry.set_text(&text);
|
||||
if let Some(home) = item.page.navigation.request.home() {
|
||||
item.page.navigation.request.widget.entry.set_text(&home);
|
||||
self.window_action.reload.activate();
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ impl Item {
|
||||
let window_action = window_action.clone();
|
||||
move || {
|
||||
// Request should match valid URI for all drivers supported
|
||||
if let Some(uri) = page.navigation.request.as_uri() {
|
||||
if let Some(uri) = page.navigation.request.uri() {
|
||||
// Rout by scheme
|
||||
if uri.scheme().to_lowercase() == "gemini" {
|
||||
return identity::new_gemini(
|
||||
|
@ -208,7 +208,7 @@ fn snap_history(subject: &Rc<Subject>, uri: Option<&Uri>) {
|
||||
None => {
|
||||
// this case especially useful for some routes that contain redirects
|
||||
// maybe some parental optimization wanted @TODO
|
||||
if let Some(uri) = subject.page.navigation.request.as_uri() {
|
||||
if let Some(uri) = subject.page.navigation.request.uri() {
|
||||
subject.page.profile.history.memory.request.set(uri);
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +157,7 @@ impl Page {
|
||||
self.navigation.restore(transaction, &record.id)?;
|
||||
// Make initial page history snap using `navigation` values restored
|
||||
// * just to have back/forward navigation ability
|
||||
if let Some(uri) = self.navigation.request.as_uri() {
|
||||
if let Some(uri) = self.navigation.request.uri() {
|
||||
self.profile.history.memory.request.set(uri);
|
||||
}
|
||||
}
|
||||
|
@ -37,15 +37,16 @@ impl Navigation {
|
||||
),
|
||||
) -> Self {
|
||||
// init children components
|
||||
let home = Rc::new(Home::build(window_action));
|
||||
|
||||
let history = Rc::new(History::build(window_action));
|
||||
let reload = Rc::new(Reload::build(window_action));
|
||||
let request = Rc::new(Request::build((browser_action, tab_action)));
|
||||
let bookmark = Rc::new(Bookmark::build(window_action));
|
||||
let home = Rc::new(Home::build(window_action, &request));
|
||||
|
||||
// init main widget
|
||||
let widget = Rc::new(Widget::build(
|
||||
&home.widget.button,
|
||||
&home.button,
|
||||
&history.widget.g_box,
|
||||
&reload.widget.button,
|
||||
&request.widget.entry,
|
||||
@ -74,7 +75,6 @@ impl Navigation {
|
||||
self.bookmark
|
||||
.update(self.profile.bookmark.get(&request).is_ok());
|
||||
self.history.update();
|
||||
self.home.update(self.request.as_uri().as_ref());
|
||||
self.reload.update(!request.is_empty());
|
||||
self.request.update(
|
||||
self.profile
|
||||
@ -85,6 +85,7 @@ impl Navigation {
|
||||
.match_scope(&request)
|
||||
.is_some(),
|
||||
);
|
||||
self.home.update();
|
||||
}
|
||||
|
||||
pub fn clean(
|
||||
|
@ -1,57 +1,44 @@
|
||||
mod widget;
|
||||
|
||||
use widget::Widget;
|
||||
|
||||
use super::WindowAction;
|
||||
use gtk::glib::{gformat, GString, Uri};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use super::{Request, WindowAction};
|
||||
use gtk::{
|
||||
prelude::{ButtonExt, WidgetExt},
|
||||
Button,
|
||||
};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct Home {
|
||||
action: Rc<WindowAction>,
|
||||
uri: RefCell<Option<Uri>>,
|
||||
pub widget: Rc<Widget>,
|
||||
request: Rc<Request>,
|
||||
pub button: Button,
|
||||
}
|
||||
|
||||
impl Home {
|
||||
// Construct
|
||||
pub fn build(action: &Rc<WindowAction>) -> Self {
|
||||
pub fn build(action: &Rc<WindowAction>, request: &Rc<Request>) -> Self {
|
||||
// Init gobject
|
||||
let button = Button::builder()
|
||||
.icon_name("go-home-symbolic")
|
||||
.tooltip_text("Home")
|
||||
.sensitive(false)
|
||||
.build();
|
||||
|
||||
// Init events
|
||||
button.connect_clicked({
|
||||
let action = action.clone();
|
||||
move |_| action.home.activate()
|
||||
});
|
||||
|
||||
// Return activated `Self`
|
||||
Self {
|
||||
action: action.clone(),
|
||||
uri: RefCell::new(None),
|
||||
widget: Rc::new(Widget::build(action)),
|
||||
request: request.clone(),
|
||||
button,
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
pub fn update(&self, request: Option<&Uri>) {
|
||||
let has_home = match request {
|
||||
Some(uri) => {
|
||||
self.uri.replace(Some(uri.clone()));
|
||||
uri.path().len() > 1
|
||||
}
|
||||
None => {
|
||||
self.uri.replace(None);
|
||||
false
|
||||
}
|
||||
};
|
||||
pub fn update(&self) {
|
||||
let has_home = self.request.home().is_some();
|
||||
self.action.home.simple_action.set_enabled(has_home);
|
||||
self.widget.update(has_home);
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn url(&self) -> Option<GString> {
|
||||
if let Some(uri) = &*self.uri.borrow() {
|
||||
let scheme = uri.scheme();
|
||||
let port = uri.port();
|
||||
if let Some(host) = uri.host() {
|
||||
return Some(if port.is_positive() {
|
||||
gformat!("{scheme}://{host}:{port}/")
|
||||
} else {
|
||||
gformat!("{scheme}://{host}/")
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
self.button.set_sensitive(has_home);
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
use super::WindowAction;
|
||||
use gtk::{
|
||||
prelude::{ButtonExt, WidgetExt},
|
||||
Button,
|
||||
};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct Widget {
|
||||
pub button: Button,
|
||||
}
|
||||
|
||||
impl Widget {
|
||||
// Constructors
|
||||
|
||||
/// Build new `Self`
|
||||
pub fn build(action: &Rc<WindowAction>) -> Self {
|
||||
// Init gobject
|
||||
let button = Button::builder()
|
||||
.icon_name("go-home-symbolic")
|
||||
.tooltip_text("Home")
|
||||
.sensitive(false)
|
||||
.build();
|
||||
|
||||
// Init events
|
||||
button.connect_clicked({
|
||||
let action = action.clone();
|
||||
move |_| action.home.activate()
|
||||
});
|
||||
|
||||
// Return activated `Self`
|
||||
Self { button }
|
||||
}
|
||||
|
||||
// Actions
|
||||
pub fn update(&self, is_sensitive: bool) {
|
||||
self.button.set_sensitive(is_sensitive);
|
||||
}
|
||||
}
|
@ -106,7 +106,7 @@ impl Request {
|
||||
|
||||
/// Try get current request value as [Uri](https://docs.gtk.org/glib/struct.Uri.html)
|
||||
/// * `strip_prefix` on parse
|
||||
pub fn as_uri(&self) -> Option<Uri> {
|
||||
pub fn uri(&self) -> Option<Uri> {
|
||||
match Uri::parse(&strip_prefix(self.widget.entry.text()), UriFlags::NONE) {
|
||||
Ok(uri) => Some(uri),
|
||||
_ => None,
|
||||
@ -119,6 +119,24 @@ impl Request {
|
||||
strip_prefix(self.widget.entry.text())
|
||||
}
|
||||
|
||||
/// Parse home [Uri](https://docs.gtk.org/glib/struct.Uri.html) for self
|
||||
pub fn home(&self) -> Option<GString> {
|
||||
match self.uri() {
|
||||
Some(uri) => {
|
||||
let scheme = uri.scheme().replace("titan", "gemini");
|
||||
let port = uri.port();
|
||||
uri.host().map(|host| {
|
||||
if port.is_positive() {
|
||||
gformat!("{scheme}://{host}:{port}/")
|
||||
} else {
|
||||
gformat!("{scheme}://{host}/")
|
||||
}
|
||||
})
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get request value in `download:` format
|
||||
pub fn download(&self) -> GString {
|
||||
gformat!("download:{}", self.strip_prefix())
|
||||
|
Loading…
x
Reference in New Issue
Block a user