214 lines
5.8 KiB
Rust
Raw Normal View History

2024-10-08 00:56:52 +03:00
mod database;
mod page;
2024-10-11 03:06:48 +03:00
mod widget;
2024-10-08 00:56:52 +03:00
use database::Database;
use page::Page;
2024-10-11 03:06:48 +03:00
use widget::Widget;
2024-10-08 00:56:52 +03:00
2024-10-11 04:37:06 +03:00
use adw::{TabPage, TabView};
2024-10-08 00:56:52 +03:00
use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, GString},
};
2024-10-11 03:06:48 +03:00
use sqlite::Transaction;
2024-10-08 00:56:52 +03:00
use std::sync::Arc;
pub struct Item {
// Auto-generated unique item ID
// useful as widget name in GTK actions callback
id: GString,
// Components
page: Arc<Page>,
2024-10-11 03:06:48 +03:00
widget: Arc<Widget>,
2024-10-08 00:56:52 +03:00
}
impl Item {
// Construct
2024-10-08 19:42:51 +03:00
pub fn new_arc(
2024-10-11 03:06:48 +03:00
tab_view: &TabView,
// Actions
2024-10-08 00:56:52 +03:00
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
2024-10-11 03:46:51 +03:00
// Options
2024-10-11 04:37:06 +03:00
is_selected: bool,
2024-10-08 06:20:46 +03:00
) -> Arc<Self> {
2024-10-08 00:56:52 +03:00
// Generate unique ID for new page components
let id = uuid_string_random();
// Init components
2024-10-08 06:38:37 +03:00
let page = Page::new_arc(
2024-10-08 00:56:52 +03:00
id.clone(),
action_tab_page_navigation_base.clone(),
action_tab_page_navigation_history_back.clone(),
action_tab_page_navigation_history_forward.clone(),
action_tab_page_navigation_reload.clone(),
action_update.clone(),
2024-10-08 06:20:46 +03:00
);
2024-10-08 00:56:52 +03:00
2024-10-11 05:01:42 +03:00
let widget = Widget::new_arc(id.as_str(), tab_view, page.gobject(), None, is_selected); // @TODO
2024-10-11 03:06:48 +03:00
2024-10-08 00:56:52 +03:00
// Return struct
2024-10-11 03:06:48 +03:00
Arc::new(Self { id, page, widget })
2024-10-08 00:56:52 +03:00
}
// Actions
pub fn pin(&self) {
2024-10-11 01:16:23 +03:00
//self.label.pin(!self.label.is_pinned()) // toggle
2024-10-08 00:56:52 +03:00
}
pub fn page_navigation_base(&self) {
self.page.navigation_base()
}
pub fn page_navigation_history_back(&self) {
self.page.navigation_history_back()
}
pub fn page_navigation_history_forward(&self) {
self.page.navigation_history_forward()
}
pub fn page_navigation_reload(&self) {
self.page.navigation_reload()
}
pub fn page_navigation_request_grab_focus(&self) {
self.page.navigation_request_grab_focus()
}
pub fn update(&self) {
self.page.update();
}
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
Ok(_) => {
// Delegate clean action to the item childs
/* @TODO
2024-10-08 03:41:51 +03:00
self.page.clean(transaction, &record.id)?;*/
2024-10-08 00:56:52 +03:00
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
// This method does not contain Self context,
// because child items creating in the runtime (by parent component)
pub fn restore(
2024-10-11 03:06:48 +03:00
tab_view: &TabView,
2024-10-08 00:56:52 +03:00
transaction: &Transaction,
app_browser_window_tab_id: &i64,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
) -> Result<Vec<Arc<Item>>, String> {
let mut items = Vec::new();
match Database::records(transaction, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
// Construct new item object
2024-10-08 19:42:51 +03:00
let item = Item::new_arc(
2024-10-11 03:06:48 +03:00
tab_view,
2024-10-11 03:46:51 +03:00
// Actions
2024-10-08 00:56:52 +03:00
action_tab_page_navigation_base.clone(),
action_tab_page_navigation_history_back.clone(),
action_tab_page_navigation_history_forward.clone(),
action_tab_page_navigation_reload.clone(),
action_update.clone(),
2024-10-11 03:46:51 +03:00
// Options
2024-10-11 04:37:06 +03:00
record.is_selected,
2024-10-08 06:20:46 +03:00
);
2024-10-08 00:56:52 +03:00
// Delegate restore action to the item childs
2024-10-11 01:16:23 +03:00
/* @TODO
self.page.restore(transaction, &id)?; */
2024-10-08 00:56:52 +03:00
// 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,
2024-10-11 04:37:06 +03:00
page_position: &i32,
is_selected: &bool,
2024-10-08 00:56:52 +03:00
) -> Result<(), String> {
2024-10-11 04:37:06 +03:00
match Database::add(
transaction,
app_browser_window_tab_id,
page_position,
is_selected,
) {
2024-10-08 00:56:52 +03:00
Ok(_) => {
let id = Database::last_insert_id(transaction);
// Delegate save action to childs
/* @TODO
2024-10-08 03:41:51 +03:00
self.page.save(transaction, &id)?; */
2024-10-08 00:56:52 +03:00
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
// Getters
pub fn id(&self) -> GString {
self.id.clone()
}
2024-10-11 05:21:04 +03:00
pub fn page_meta_title(&self) -> Option<GString> {
self.page.meta_title()
}
2024-10-11 04:37:06 +03:00
pub fn gobject(&self) -> &TabPage {
&self.widget.gobject()
2024-10-08 00:56:52 +03:00
}
// 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
/* @TODO
2024-10-08 03:41:51 +03:00
Page::migrate(&tx)? */
2024-10-08 00:56:52 +03:00
// Success
Ok(())
}
}