implement request as trait

This commit is contained in:
yggverse 2025-01-27 16:30:27 +02:00
parent 6db928afee
commit 3d1a07213c
11 changed files with 141 additions and 95 deletions

View file

@ -7,7 +7,11 @@ mod request;
use super::{ItemAction, Profile, TabAction, WindowAction};
use bookmark::Bookmark;
use gtk::{prelude::BoxExt, Box, Button, Orientation};
use gtk::{
glib::{GString, Uri},
prelude::{BoxExt, EditableExt, WidgetExt},
Box, Button, Entry, Orientation,
};
use history::History;
use home::Home;
use reload::Reload;
@ -22,7 +26,7 @@ pub struct Navigation {
pub home: Button,
pub reload: Button,
pub bookmark: Button,
pub request: Rc<Request>,
pub request: Entry,
pub g_box: Box,
}
@ -35,14 +39,14 @@ impl Navigation {
&Rc<ItemAction>,
),
) -> Self {
// init children components
// Init children components
let history = Box::history((window_action, tab_action, item_action));
let request = Rc::new(Request::build(item_action, profile));
let request = Entry::request(item_action, profile);
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, profile, &request);
// Init main widget
let g_box = Box::builder()
.orientation(Orientation::Horizontal)
.spacing(SPACING)
@ -54,7 +58,7 @@ impl Navigation {
g_box.append(&home);
g_box.append(&history);
g_box.append(&reload);
g_box.append(&request.entry); // @TODO
g_box.append(&request);
g_box.append(&bookmark);
Self {
@ -126,6 +130,32 @@ impl Navigation {
Ok(())
}
pub fn grab_focus(&self) -> bool {
self.request.grab_focus()
}
pub fn to_download(&self) {
self.request.to_download();
}
pub fn to_source(&self) {
self.request.to_source();
}
// Getters
pub fn request(&self) -> GString {
self.request.text()
}
pub fn uri(&self) -> Option<Uri> {
self.request.uri()
}
pub fn home(&self) -> Option<Uri> {
self.request.home()
}
}
// Tools

View file

@ -1,7 +1,7 @@
use super::{Profile, Request, WindowAction};
use super::{Profile, WindowAction};
use gtk::{
prelude::{ActionExt, ButtonExt, EditableExt},
Button,
Button, Entry,
};
use std::rc::Rc;
@ -9,12 +9,12 @@ const ICON_YES: &str = "starred-symbolic";
const ICON_NON: &str = "non-starred-symbolic";
pub trait Bookmark {
fn bookmark(action: &Rc<WindowAction>, profile: &Rc<Profile>, request: &Rc<Request>) -> Self;
fn bookmark(action: &Rc<WindowAction>, profile: &Rc<Profile>, request: &Entry) -> Self;
}
impl Bookmark for Button {
fn bookmark(action: &Rc<WindowAction>, profile: &Rc<Profile>, request: &Rc<Request>) -> Self {
let has_bookmark = profile.bookmark.get(&request.entry.text()).is_ok();
fn bookmark(action: &Rc<WindowAction>, profile: &Rc<Profile>, request: &Entry) -> Self {
let has_bookmark = profile.bookmark.get(&request.text()).is_ok();
let button = Button::builder()
.action_name(format!(
@ -31,9 +31,7 @@ impl Bookmark for Button {
let profile = profile.clone();
let request = request.clone();
move |_, _| {
button.set_icon_name(icon_name(
profile.bookmark.get(&request.entry.text()).is_ok(),
))
button.set_icon_name(icon_name(profile.bookmark.get(&request.text()).is_ok()))
}
}); // @TODO use local action

View file

@ -3,15 +3,12 @@ use crate::app::browser::window::action::Position;
use gtk::{
gdk::BUTTON_MIDDLE,
prelude::{ActionExt, WidgetExt},
Button, GestureClick,
Button, Entry, GestureClick,
};
use std::rc::Rc;
pub trait Home {
fn home(
action: (&Rc<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>),
request: &Rc<Request>,
) -> Self;
fn home(action: (&Rc<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>), request: &Entry) -> Self;
}
impl Home for Button {
@ -21,7 +18,7 @@ impl Home for Button {
&Rc<TabAction>,
&Rc<ItemAction>,
),
request: &Rc<Request>,
request: &Entry,
) -> Self {
let button = Button::builder()
.action_name(format!("{}.{}", tab_action.id, item_action.home.name()))

View file

@ -3,14 +3,14 @@ use crate::app::browser::window::action::Position;
use gtk::{
gdk::BUTTON_MIDDLE,
prelude::{ActionExt, WidgetExt},
Button, GestureClick,
Button, Entry, GestureClick,
};
use std::rc::Rc;
pub trait Reload {
fn reload(
action: (&Rc<WindowAction>, &Rc<TabAction>, &Rc<ItemAction>),
request: &Rc<Request>,
request: &Entry,
) -> Self;
}
@ -21,7 +21,7 @@ impl Reload for Button {
&Rc<TabAction>,
&Rc<ItemAction>,
),
request: &Rc<Request>,
request: &Entry,
) -> Self {
let button = Button::builder()
.action_name(format!("{}.{}", tab_action.id, item_action.reload.name()))

View file

@ -14,15 +14,50 @@ use std::{cell::Cell, rc::Rc};
const PLACEHOLDER_TEXT: &str = "URL or search term...";
pub struct Request {
pub entry: Entry,
pub trait Request {
// Constructors
fn request(item_action: &Rc<ItemAction>, profile: &Rc<Profile>) -> Self;
// Actions
fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String>;
fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String>;
fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String>;
// Setters
fn to_download(&self);
fn to_source(&self);
// Getters
fn strip_prefix(&self) -> GString;
fn download(&self) -> GString;
fn source(&self) -> GString;
fn uri(&self) -> Option<Uri>;
fn home(&self) -> Option<Uri>;
}
impl Request {
impl Request for Entry {
// Constructors
/// Build new `Self`
pub fn build(item_action: &Rc<ItemAction>, profile: &Rc<Profile>) -> Self {
fn request(item_action: &Rc<ItemAction>, profile: &Rc<Profile>) -> Self {
// Init main widget
let entry = Entry::builder()
.placeholder_text(PLACEHOLDER_TEXT)
@ -122,12 +157,11 @@ impl Request {
}
});
// Return activated `Self`
Self { entry }
entry
}
// Actions
pub fn clean(
fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
@ -150,7 +184,7 @@ impl Request {
Ok(())
}
pub fn restore(
fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
@ -159,7 +193,7 @@ impl Request {
Ok(records) => {
for record in records {
if let Some(text) = record.text {
self.entry.set_text(&text);
self.set_text(&text);
}
// Delegate restore action to the item childs
@ -172,13 +206,13 @@ impl Request {
Ok(())
}
pub fn save(
fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
// Keep value in memory until operation complete
let text = self.entry.text();
let text = self.text();
match database::insert(
transaction,
@ -202,40 +236,40 @@ impl Request {
// Setters
pub fn to_download(&self) {
self.entry.set_text(&self.download());
fn to_download(&self) {
self.set_text(&self.download());
}
pub fn to_source(&self) {
self.entry.set_text(&self.source());
fn to_source(&self) {
self.set_text(&self.source());
}
// Getters
/// Get current request value without system prefix
/// * the `prefix` is not `scheme`
pub fn strip_prefix(&self) -> GString {
strip_prefix(self.entry.text())
fn strip_prefix(&self) -> GString {
strip_prefix(self.text())
}
/// Get request value in `download:` format
pub fn download(&self) -> GString {
fn download(&self) -> GString {
gformat!("download:{}", self.strip_prefix())
}
/// Get request value in `source:` format
pub fn source(&self) -> GString {
fn source(&self) -> GString {
gformat!("source:{}", self.strip_prefix())
}
/// Try get current request value as [Uri](https://docs.gtk.org/glib/struct.Uri.html)
/// * `strip_prefix` on parse
pub fn uri(&self) -> Option<Uri> {
uri(&strip_prefix(self.entry.text()))
fn uri(&self) -> Option<Uri> {
uri(&strip_prefix(self.text()))
}
/// Try build home [Uri](https://docs.gtk.org/glib/struct.Uri.html) for `Self`
pub fn home(&self) -> Option<Uri> {
fn home(&self) -> Option<Uri> {
home(self.uri())
}
}