Skip to content

Commit

Permalink
remove Subject struct, use public Page API for Client driver, r…
Browse files Browse the repository at this point in the history
…emove `widget` mod
  • Loading branch information
yggverse committed Jan 28, 2025
1 parent 6945aae commit ed1dbd4
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 428 deletions.
12 changes: 6 additions & 6 deletions src/app/browser/window/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Tab {
// Register dynamically created tab components in the HashMap index
self.index
.borrow_mut()
.insert(item.widget.tab_page.clone(), item.clone());
.insert(item.page.tab_page.clone(), item.clone());

update_actions(
&self.tab_view,
Expand Down Expand Up @@ -305,7 +305,7 @@ impl Tab {
// Register dynamically created tab item in the HashMap index
self.index
.borrow_mut()
.insert(item.widget.tab_page.clone(), item.clone());
.insert(item.page.tab_page.clone(), item.clone());
}
}
Err(e) => return Err(e.to_string()),
Expand Down Expand Up @@ -333,10 +333,10 @@ impl Tab {
item.save(
transaction,
id,
self.tab_view.page_position(&item.widget.tab_page),
item.widget.tab_page.is_pinned(),
item.widget.tab_page.is_selected(),
item.widget.tab_page.needs_attention(),
self.tab_view.page_position(&item.page.tab_page),
item.page.tab_page.is_pinned(),
item.page.tab_page.is_selected(),
item.page.tab_page.needs_attention(),
)?;
}
}
Expand Down
26 changes: 6 additions & 20 deletions src/app/browser/window/tab/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod client;
mod database;
mod identity;
mod page;
mod widget;

use super::{Action as TabAction, BrowserAction, Position, WindowAction};
use crate::Profile;
Expand All @@ -14,14 +13,12 @@ use gtk::prelude::{ActionMapExt, Cast};
use page::Page;
use sqlite::Transaction;
use std::rc::Rc;
use widget::Widget;

pub struct Item {
// Multi-protocol handler
pub client: Rc<Client>,
// Components
pub page: Rc<Page>,
pub widget: Rc<Widget>,
pub action: Rc<Action>,
}

Expand All @@ -37,7 +34,7 @@ impl Item {
&Rc<WindowAction>,
&Rc<TabAction>,
),
(position, request, is_pinned, is_selected, is_attention, is_load): (
(position, request, is_pinned, is_selected, is_needs_attention, is_load): (
Position,
Option<&str>,
bool,
Expand All @@ -63,18 +60,12 @@ impl Item {
let page = Rc::new(Page::build(
profile,
(browser_action, window_action, tab_action, &action),
));

let widget = Rc::new(Widget::build(
tab_view,
&page.g_box,
None,
position,
(is_pinned, is_selected, is_attention),
(position, is_pinned, is_selected, is_needs_attention),
));

// Update tab loading indicator
let client = Rc::new(Client::init(&page, &widget.tab_page));
let client = Rc::new(Client::init(&page));

// Connect events
action.home.connect_activate({
Expand Down Expand Up @@ -143,7 +134,6 @@ impl Item {
Self {
client,
page,
widget,
action,
}
}
Expand All @@ -160,7 +150,6 @@ impl Item {
Ok(_) => {
// Delegate clean action to the item childs
self.page.clean(transaction, record.id)?;
self.widget.clean(transaction, record.id)?;
}
Err(e) => return Err(e.to_string()),
}
Expand Down Expand Up @@ -203,14 +192,13 @@ impl Item {
None,
record.is_pinned,
record.is_selected,
record.is_attention,
record.is_needs_attention,
false,
),
));

// Delegate restore action to the item childs
item.page.restore(transaction, record.id)?;
item.widget.restore(transaction, record.id)?;

// Result
items.push(item);
Expand All @@ -229,22 +217,21 @@ impl Item {
page_position: i32,
is_pinned: bool,
is_selected: bool,
is_attention: bool,
is_needs_attention: bool,
) -> Result<(), String> {
match database::insert(
transaction,
app_browser_window_tab_id,
page_position,
is_pinned,
is_selected,
is_attention,
is_needs_attention,
) {
Ok(_) => {
let id = database::last_insert_id(transaction);

// Delegate save action to childs
self.page.save(transaction, id)?;
self.widget.save(transaction, id)?;
}
Err(e) => return Err(e.to_string()),
}
Expand All @@ -262,7 +249,6 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> {

// Delegate migration to childs
page::migrate(tx)?;
widget::migrate(tx)?;

// Success
Ok(())
Expand Down
57 changes: 22 additions & 35 deletions src/app/browser/window/tab/item/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod driver;
mod feature;
mod subject;

use super::Page;
use adw::TabPage;
use driver::Driver;
use feature::Feature;
use gtk::{
Expand All @@ -12,29 +10,23 @@ use gtk::{
prelude::{ActionExt, CancellableExt},
};
use std::{cell::Cell, rc::Rc};
use subject::Subject;

/// Multi-protocol client API for tab `Item`
pub struct Client {
cancellable: Cell<Cancellable>,
driver: Rc<Driver>,
subject: Rc<Subject>,
page: Rc<Page>,
}

impl Client {
// Constructors

/// Create new `Self`
pub fn init(page: &Rc<Page>, tab_page: &TabPage) -> Self {
let subject = Rc::new(Subject {
page: page.clone(),
tab_page: tab_page.clone(),
});

pub fn init(page: &Rc<Page>) -> Self {
Self {
cancellable: Cell::new(Cancellable::new()),
driver: Rc::new(Driver::build(&subject)),
subject,
driver: Rc::new(Driver::build(page)),
page: page.clone(),
}
}

Expand All @@ -44,51 +36,46 @@ impl Client {
/// * or `navigation` entry if the value not provided
pub fn handle(&self, request: &str, is_snap_history: bool) {
// Move focus out from navigation entry @TODO
self.subject.page.browser_action.escape.activate(None);
self.page.browser_action.escape.activate(None);

// Initially disable find action
self.subject
.page
self.page
.window_action
.find
.simple_action
.set_enabled(false);

// Reset widgets
self.subject.page.search.unset();
self.subject.page.input.unset();
self.subject.tab_page.set_title("Loading..");
self.subject.page.navigation.set_progress_fraction(0.1);

self.subject.tab_page.set_loading(true);
self.page.search.unset();
self.page.input.unset();
self.page.set_title("Loading..");
self.page.set_progress(0.1);

if is_snap_history {
snap_history(&self.subject, None);
snap_history(&self.page, None);
}

// run async resolver to detect Uri, scheme-less host, or search query
lookup(request, self.cancellable(), {
let driver = self.driver.clone();
let subject = self.subject.clone();
let page = self.page.clone();
move |feature, cancellable, result| {
match result {
// route by scheme
Ok(uri) => match uri.scheme().as_str() {
"gemini" | "titan" => driver.gemini.handle(uri, feature, cancellable),
scheme => {
// no scheme match driver, complete with failure message
let status = subject.page.content.to_status_failure();
let status = page.content.to_status_failure();
status.set_description(Some(&format!(
"Scheme `{scheme}` yet not supported"
)));
subject.tab_page.set_title(&status.title());
subject.page.navigation.set_progress_fraction(0.0);
subject.tab_page.set_loading(false);
page.set_title(&status.title());
page.set_progress(0.0);
}
},
// begin redirection to new address suggested
Err(uri) => subject
.page
Err(uri) => page
.item_action
.load
.activate(Some(&uri.to_string()), false),
Expand Down Expand Up @@ -190,22 +177,22 @@ fn search(query: &str) -> Uri {

/// Make new history record in related components
/// * optional [Uri](https://docs.gtk.org/glib/struct.Uri.html) reference wanted only for performance reasons, to not parse it twice
fn snap_history(subject: &Rc<Subject>, uri: Option<&Uri>) {
let request = subject.page.navigation.request();
fn snap_history(page: &Page, uri: Option<&Uri>) {
let request = page.navigation.request();

// Add new record into the global memory index (used in global menu)
// * if the `Uri` is `None`, try parse it from `request`
match uri {
Some(uri) => subject.page.profile.history.memory.request.set(uri.clone()),
Some(uri) => page.profile.history.memory.request.set(uri.clone()),
None => {
// this case especially useful for some routes that contain redirects
// maybe some parental optimization wanted @TODO
if let Some(uri) = subject.page.navigation.uri() {
subject.page.profile.history.memory.request.set(uri);
if let Some(uri) = page.navigation.uri() {
page.profile.history.memory.request.set(uri);
}
}
}

// Add new record into the page navigation history
subject.page.item_action.history.add(request, true)
page.item_action.history.add(request, true)
}
6 changes: 3 additions & 3 deletions src/app/browser/window/tab/item/client/driver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod gemini;

use super::{Feature, Subject};
use super::{Feature, Page};
use gemini::Gemini;
use std::rc::Rc;

Expand All @@ -13,9 +13,9 @@ impl Driver {
// Constructors

/// Build new `Self`
pub fn build(subject: &Rc<Subject>) -> Self {
pub fn build(page: &Rc<Page>) -> Self {
Driver {
gemini: Gemini::init(subject),
gemini: Gemini::init(page),
}
}
}
Loading

0 comments on commit ed1dbd4

Please sign in to comment.