move is_needs_attention state to page, reorganize TabView access levels

This commit is contained in:
yggverse 2025-01-30 22:40:47 +02:00
parent 8356c432c1
commit 25db274d2e
5 changed files with 158 additions and 191 deletions

View file

@ -3,12 +3,15 @@ mod client;
mod database;
mod page;
use super::{Action as TabAction, BrowserAction, Position, WindowAction};
use super::{Action as TabAction, BrowserAction, WindowAction};
use crate::Profile;
use action::Action;
use adw::TabView;
use adw::TabPage;
use client::Client;
use gtk::prelude::ActionMapExt;
use gtk::{
prelude::{ActionMapExt, BoxExt},
Box,
};
use page::Page;
use sqlite::Transaction;
use std::rc::Rc;
@ -19,6 +22,7 @@ pub struct Item {
// Components
pub page: Rc<Page>,
pub action: Rc<Action>,
pub tab_page: TabPage,
}
impl Item {
@ -26,21 +30,15 @@ impl Item {
/// Build new `Self`
pub fn build(
tab_view: &TabView,
(tab_page, target_child): (&TabPage, &Box),
profile: &Rc<Profile>,
(browser_action, window_action, tab_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<TabAction>,
),
(position, request, is_pinned, is_selected, is_needs_attention, is_load): (
Position,
Option<&str>,
bool,
bool,
bool,
bool,
),
request: Option<&str>,
is_load: bool,
) -> Self {
// Init components
let action = Rc::new(Action::new());
@ -57,13 +55,18 @@ impl Item {
.simple_action_group
.add_action(&action.history.forward);
// Create new `Page` implementation for `TabPage`
let page = Rc::new(Page::build(
profile,
(browser_action, window_action, tab_action, &action),
tab_view,
(position, is_pinned, is_selected, is_needs_attention),
tab_page,
));
target_child.append(&page.navigation.g_box);
target_child.append(&page.content.g_box);
target_child.append(&page.search.g_box);
target_child.append(&page.input.clamp);
// Update tab loading indicator
let client = Rc::new(Client::init(profile, &page));
@ -112,11 +115,12 @@ impl Item {
client.handle(text, true);
}
}
// Done
Self {
client,
page,
action,
tab_page: tab_page.clone(),
}
}
@ -143,76 +147,22 @@ impl Item {
Ok(())
}
// This method does not contain Self context,
// because child items creating in the runtime (by parent component)
pub fn restore(
tab_view: &TabView,
transaction: &Transaction,
app_browser_window_tab_id: i64,
profile: &Rc<Profile>,
// Actions
(browser_action, window_action, item_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<super::Action>,
),
) -> Result<Vec<Rc<Item>>, String> {
let mut items = Vec::new();
match database::select(transaction, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
// Construct new item object
let item = Rc::new(Item::build(
tab_view,
profile,
// Actions
(browser_action, window_action, item_action),
// Options tuple
(
Position::End,
None,
record.is_pinned,
record.is_selected,
record.is_needs_attention,
false,
),
));
// Delegate restore action to the item childs
item.page.restore(transaction, record.id)?;
// Result
items.push(item);
}
}
Err(e) => return Err(e.to_string()),
}
Ok(items)
}
pub fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_id: i64,
page_position: i32,
is_pinned: bool,
is_selected: bool,
is_needs_attention: bool,
) -> Result<(), String> {
match database::insert(
transaction,
app_browser_window_tab_id,
page_position,
is_pinned,
is_selected,
is_needs_attention,
self.tab_page.is_pinned(),
self.tab_page.is_selected(),
) {
Ok(_) => {
let id = database::last_insert_id(transaction);
// Delegate save action to childs
let id = database::last_insert_id(transaction);
self.page.save(transaction, id)?;
}
Err(e) => return Err(e.to_string()),
@ -235,3 +185,15 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Success
Ok(())
}
// This feature restore require parental implementation
// * see `super::Tab::restore()`
pub fn restore(
transaction: &Transaction,
app_browser_window_tab_id: i64,
) -> Result<Vec<database::Table>, String> {
match database::select(transaction, app_browser_window_tab_id) {
Ok(records) => Ok(records),
Err(e) => Err(e.to_string()),
}
}