mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
begin request entry refactory
This commit is contained in:
parent
5255708be3
commit
e7bd5bbdc6
24 changed files with 351 additions and 849 deletions
|
|
@ -1,38 +1,132 @@
|
|||
mod database;
|
||||
mod test;
|
||||
mod widget;
|
||||
mod primary_icon;
|
||||
|
||||
use widget::Widget;
|
||||
use primary_icon::PrimaryIcon;
|
||||
|
||||
use crate::app::browser::{window::tab::item::Action as ItemAction, Action as BrowserAction};
|
||||
use super::{ItemAction, Profile};
|
||||
use gtk::{
|
||||
glib::{gformat, GString, Uri, UriFlags},
|
||||
prelude::EditableExt,
|
||||
prelude::{EditableExt, EntryExt, WidgetExt},
|
||||
Entry, EntryIconPosition, StateFlags,
|
||||
};
|
||||
use sqlite::Transaction;
|
||||
use std::rc::Rc;
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
|
||||
const PLACEHOLDER_TEXT: &str = "URL or search term...";
|
||||
|
||||
// Main
|
||||
pub struct Request {
|
||||
pub widget: Rc<Widget>,
|
||||
pub entry: Entry,
|
||||
}
|
||||
|
||||
impl Request {
|
||||
// Constructors
|
||||
|
||||
/// Build new `Self`
|
||||
pub fn build((browser_action, item_action): (&Rc<BrowserAction>, &Rc<ItemAction>)) -> Self {
|
||||
Self {
|
||||
widget: Rc::new(Widget::build((browser_action, item_action))),
|
||||
}
|
||||
pub fn build(item_action: &Rc<ItemAction>, profile: &Rc<Profile>) -> Self {
|
||||
// Init main widget
|
||||
let entry = Entry::builder()
|
||||
.placeholder_text(PLACEHOLDER_TEXT)
|
||||
.secondary_icon_tooltip_text("Go to the location")
|
||||
.hexpand(true)
|
||||
.build();
|
||||
|
||||
// Connect events
|
||||
entry.connect_icon_release({
|
||||
let item_action = item_action.clone();
|
||||
move |this, position| match position {
|
||||
EntryIconPosition::Primary => item_action.ident.activate(), // @TODO PrimaryIcon impl
|
||||
EntryIconPosition::Secondary => item_action.load.activate(Some(&this.text()), true),
|
||||
_ => todo!(), // unexpected
|
||||
}
|
||||
});
|
||||
|
||||
entry.connect_has_focus_notify(|this| {
|
||||
if this.focus_child().is_some_and(|text| text.has_focus()) {
|
||||
this.set_secondary_icon_name(Some("pan-end-symbolic"));
|
||||
} else {
|
||||
this.set_secondary_icon_name(None);
|
||||
this.select_region(0, 0);
|
||||
}
|
||||
});
|
||||
|
||||
entry.connect_changed({
|
||||
let profile = profile.clone();
|
||||
let item_action = item_action.clone();
|
||||
move |this| {
|
||||
// Update actions
|
||||
item_action.reload.set_enabled(!this.text().is_empty());
|
||||
item_action
|
||||
.home
|
||||
.set_enabled(home(uri(&this.text())).is_some());
|
||||
|
||||
// Update primary icon
|
||||
this.first_child().unwrap().remove_css_class("success"); // @TODO handle
|
||||
|
||||
this.set_primary_icon_activatable(false);
|
||||
this.set_primary_icon_sensitive(false);
|
||||
|
||||
match primary_icon::from(&this.text()) {
|
||||
PrimaryIcon::Download { name, tooltip } => {
|
||||
this.set_primary_icon_name(Some(name));
|
||||
this.set_primary_icon_tooltip_text(Some(tooltip));
|
||||
}
|
||||
PrimaryIcon::Gemini { name, tooltip }
|
||||
| PrimaryIcon::Titan { name, tooltip } => {
|
||||
this.set_primary_icon_activatable(true);
|
||||
this.set_primary_icon_sensitive(true);
|
||||
this.set_primary_icon_name(Some(name));
|
||||
if profile.identity.get(&strip_prefix(this.text())).is_some() {
|
||||
this.first_child().unwrap().add_css_class("success"); // @TODO handle
|
||||
this.set_primary_icon_tooltip_text(Some(tooltip.1));
|
||||
} else {
|
||||
this.set_primary_icon_tooltip_text(Some(tooltip.0));
|
||||
}
|
||||
}
|
||||
PrimaryIcon::Search { name, tooltip } => {
|
||||
this.set_primary_icon_name(Some(name));
|
||||
this.set_primary_icon_tooltip_text(Some(tooltip));
|
||||
}
|
||||
PrimaryIcon::Source { name, tooltip } => {
|
||||
this.set_primary_icon_name(Some(name));
|
||||
this.set_primary_icon_tooltip_text(Some(tooltip));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
entry.connect_activate({
|
||||
let item_action = item_action.clone();
|
||||
move |entry| {
|
||||
item_action.load.activate(Some(&entry.text()), true);
|
||||
}
|
||||
});
|
||||
|
||||
entry.connect_state_flags_changed({
|
||||
// Define last focus state container
|
||||
let has_focus = Cell::new(false);
|
||||
move |entry, state| {
|
||||
// Select entire text on first click (release)
|
||||
// this behavior implemented in most web-browsers,
|
||||
// to simply overwrite current request with new value
|
||||
// Note:
|
||||
// * Custom GestureClick is not an option here, as GTK Entry has default controller
|
||||
// * This is experimental feature does not follow native GTK behavior @TODO make optional
|
||||
if !has_focus.take()
|
||||
&& state.contains(StateFlags::ACTIVE | StateFlags::FOCUS_WITHIN)
|
||||
&& entry.selection_bounds().is_none()
|
||||
{
|
||||
entry.select_region(0, entry.text_length().into());
|
||||
}
|
||||
// Update last focus state
|
||||
has_focus.replace(state.contains(StateFlags::FOCUS_WITHIN));
|
||||
}
|
||||
});
|
||||
|
||||
// Return activated `Self`
|
||||
Self { entry }
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
pub fn update(&self, is_identity_active: bool) {
|
||||
self.widget.update(is_identity_active);
|
||||
}
|
||||
|
||||
pub fn clean(
|
||||
&self,
|
||||
transaction: &Transaction,
|
||||
|
|
@ -44,7 +138,7 @@ impl Request {
|
|||
match database::delete(transaction, &record.id) {
|
||||
Ok(_) => {
|
||||
// Delegate clean action to the item childs
|
||||
self.widget.clean(transaction, &record.id)?;
|
||||
// nothing yet..
|
||||
}
|
||||
Err(e) => return Err(e.to_string()),
|
||||
}
|
||||
|
|
@ -64,8 +158,12 @@ impl Request {
|
|||
match database::select(transaction, app_browser_window_tab_item_page_navigation_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
if let Some(text) = record.text {
|
||||
self.entry.set_text(&text);
|
||||
}
|
||||
|
||||
// Delegate restore action to the item childs
|
||||
self.widget.restore(transaction, &record.id)?;
|
||||
// nothing yet..
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e.to_string()),
|
||||
|
|
@ -79,12 +177,22 @@ impl Request {
|
|||
transaction: &Transaction,
|
||||
app_browser_window_tab_item_page_navigation_id: &i64,
|
||||
) -> Result<(), String> {
|
||||
match database::insert(transaction, app_browser_window_tab_item_page_navigation_id) {
|
||||
// Keep value in memory until operation complete
|
||||
let text = self.entry.text();
|
||||
|
||||
match database::insert(
|
||||
transaction,
|
||||
app_browser_window_tab_item_page_navigation_id,
|
||||
match text.is_empty() {
|
||||
true => None,
|
||||
false => Some(text.as_str()),
|
||||
},
|
||||
) {
|
||||
Ok(_) => {
|
||||
let id = database::last_insert_id(transaction);
|
||||
// let id = database::last_insert_id(transaction);
|
||||
|
||||
// Delegate save action to childs
|
||||
self.widget.save(transaction, &id)?;
|
||||
// nothing yet..
|
||||
}
|
||||
Err(e) => return Err(e.to_string()),
|
||||
}
|
||||
|
|
@ -95,48 +203,19 @@ impl Request {
|
|||
// Setters
|
||||
|
||||
pub fn to_download(&self) {
|
||||
self.widget.entry.set_text(&self.download());
|
||||
self.entry.set_text(&self.download());
|
||||
}
|
||||
|
||||
pub fn to_source(&self) {
|
||||
self.widget.entry.set_text(&self.source());
|
||||
self.entry.set_text(&self.source());
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// 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> {
|
||||
match Uri::parse(&strip_prefix(self.widget.entry.text()), UriFlags::NONE) {
|
||||
Ok(uri) => Some(uri),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current request value without system prefix
|
||||
/// * the `prefix` is not `scheme`
|
||||
pub fn strip_prefix(&self) -> GString {
|
||||
strip_prefix(self.widget.entry.text())
|
||||
}
|
||||
|
||||
/// Parse home [Uri](https://docs.gtk.org/glib/struct.Uri.html) of `Self`
|
||||
pub fn home(&self) -> Option<Uri> {
|
||||
self.uri().map(|uri| {
|
||||
Uri::build(
|
||||
UriFlags::NONE,
|
||||
&if uri.scheme() == "titan" {
|
||||
GString::from("gemini")
|
||||
} else {
|
||||
uri.scheme()
|
||||
},
|
||||
uri.userinfo().as_deref(),
|
||||
uri.host().as_deref(),
|
||||
uri.port(),
|
||||
"/",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
})
|
||||
strip_prefix(self.entry.text())
|
||||
}
|
||||
|
||||
/// Get request value in `download:` format
|
||||
|
|
@ -148,13 +227,37 @@ impl Request {
|
|||
pub 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()))
|
||||
}
|
||||
|
||||
/// Try build home [Uri](https://docs.gtk.org/glib/struct.Uri.html) for `Self`
|
||||
pub fn home(&self) -> Option<Uri> {
|
||||
home(self.uri())
|
||||
}
|
||||
}
|
||||
|
||||
// Tools
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
}
|
||||
|
||||
// Delegate migration to childs
|
||||
// nothing yet..
|
||||
|
||||
// Success
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Strip system prefix from request string
|
||||
/// * the `prefix` is not `scheme`
|
||||
pub fn strip_prefix(mut request: GString) -> GString {
|
||||
fn strip_prefix(mut request: GString) -> GString {
|
||||
if let Some(postfix) = request.strip_prefix("source:") {
|
||||
request = postfix.into()
|
||||
};
|
||||
|
|
@ -166,15 +269,29 @@ pub fn strip_prefix(mut request: GString) -> GString {
|
|||
request
|
||||
} // @TODO move prefix features to page client
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
fn uri(value: &str) -> Option<Uri> {
|
||||
match Uri::parse(value, UriFlags::NONE) {
|
||||
Ok(uri) => Some(uri),
|
||||
_ => None,
|
||||
}
|
||||
|
||||
// Delegate migration to childs
|
||||
widget::migrate(tx)?;
|
||||
|
||||
// Success
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse home [Uri](https://docs.gtk.org/glib/struct.Uri.html) for `subject`
|
||||
fn home(subject: Option<Uri>) -> Option<Uri> {
|
||||
subject.map(|uri| {
|
||||
Uri::build(
|
||||
UriFlags::NONE,
|
||||
&if uri.scheme() == "titan" {
|
||||
GString::from("gemini")
|
||||
} else {
|
||||
uri.scheme()
|
||||
},
|
||||
uri.userinfo().as_deref(),
|
||||
uri.host().as_deref(),
|
||||
uri.port(),
|
||||
"/",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue