347 lines
10 KiB
Rust
Raw Normal View History

2024-10-06 18:20:36 +03:00
mod database;
2024-09-20 18:02:10 +03:00
mod label;
mod page;
2024-10-06 16:28:46 +03:00
mod widget;
2024-09-18 22:38:18 +03:00
2024-10-06 18:20:36 +03:00
use database::Database;
2024-09-23 18:51:48 +03:00
use label::Label;
use page::Page;
use sqlite::Transaction;
2024-10-06 16:28:46 +03:00
use widget::Widget;
2024-09-23 15:56:09 +03:00
use gtk::{
2024-09-28 03:10:07 +03:00
gio::SimpleAction,
glib::{uuid_string_random, GString},
2024-09-28 15:53:13 +03:00
prelude::{ActionExt, WidgetExt},
2024-09-28 02:31:32 +03:00
GestureClick, Notebook,
};
2024-09-24 21:29:05 +03:00
use std::{cell::RefCell, collections::HashMap, sync::Arc};
2024-10-06 17:30:00 +03:00
// Common struct for HashMap index
2024-10-07 04:38:22 +03:00
pub struct TabItem {
label: Arc<Label>,
page: Arc<Page>,
}
2024-10-06 17:30:18 +03:00
// Main
2024-09-22 02:00:54 +03:00
pub struct Tab {
2024-10-06 18:20:36 +03:00
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
2024-09-30 17:45:44 +03:00
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
2024-09-30 14:49:37 +03:00
action_tab_page_navigation_reload: Arc<SimpleAction>,
2024-09-28 03:10:07 +03:00
action_update: Arc<SimpleAction>,
// Dynamically allocated reference index
2024-10-07 04:38:22 +03:00
index: RefCell<HashMap<GString, Arc<TabItem>>>,
2024-10-06 16:28:46 +03:00
// GTK
widget: Arc<Widget>,
2024-09-18 22:38:18 +03:00
}
2024-09-22 02:00:54 +03:00
impl Tab {
2024-09-22 22:23:44 +03:00
// Construct
2024-09-28 03:10:07 +03:00
pub fn new(
2024-10-06 18:20:36 +03:00
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
2024-09-30 17:45:44 +03:00
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
2024-09-30 14:49:37 +03:00
action_tab_page_navigation_reload: Arc<SimpleAction>,
2024-09-28 03:10:07 +03:00
action_update: Arc<SimpleAction>,
) -> Self {
2024-10-06 20:47:08 +03:00
// Init empty HashMap index as no tabs appended yet
let index = RefCell::new(HashMap::new());
// Init widget
let widget = Arc::new(Widget::new());
2024-09-28 03:10:07 +03:00
// Return non activated struct
2024-09-26 01:11:07 +03:00
Self {
2024-09-28 03:10:07 +03:00
// Define action links
action_tab_page_navigation_base,
2024-09-30 17:45:44 +03:00
action_tab_page_navigation_history_back,
action_tab_page_navigation_history_forward,
2024-09-30 14:49:37 +03:00
action_tab_page_navigation_reload,
2024-09-28 03:10:07 +03:00
action_update,
2024-09-24 21:29:05 +03:00
// Init empty HashMap index as no tabs appended yet
2024-10-06 20:47:08 +03:00
index,
2024-10-06 16:28:46 +03:00
// GTK
2024-10-06 20:47:08 +03:00
widget,
2024-09-26 01:11:07 +03:00
}
}
2024-09-24 21:29:05 +03:00
2024-09-26 01:11:07 +03:00
// Actions
pub fn activate(&self, tab: Arc<Self>) {
2024-10-06 16:28:46 +03:00
self.widget
.gobject()
.connect_page_removed(move |_, widget, _| {
// Cleanup HashMap index
let id = widget.widget_name();
// Check for required value as raw access to gobject @TODO
if id.is_empty() {
panic!("Undefined tab index!")
}
tab.index.borrow_mut().remove(&id);
2024-10-06 16:28:46 +03:00
});
2024-09-28 15:53:13 +03:00
2024-09-28 18:51:08 +03:00
// Switch page post-event (`connect_switch_page` activates before `page_number` get updated)
2024-10-06 16:28:46 +03:00
self.widget.gobject().connect_page_notify({
2024-09-28 15:53:13 +03:00
let action_update = self.action_update.clone();
// Update window header with current page title
2024-09-28 18:51:08 +03:00
move |_| action_update.activate(None)
2024-09-26 01:11:07 +03:00
});
2024-09-18 22:47:53 +03:00
}
2024-09-22 02:00:54 +03:00
2024-10-07 04:38:22 +03:00
pub fn append(
&self,
page_navigation_request_text: Option<GString>,
is_current_page: bool,
) -> Arc<TabItem> {
// Generate unique ID for new page components
let id = uuid_string_random();
2024-09-23 21:51:32 +03:00
// Init new tab components
let label = Arc::new(Label::new(id.clone(), false));
2024-10-07 04:38:22 +03:00
2024-09-28 03:10:07 +03:00
let page = Arc::new(Page::new(
id.clone(),
page_navigation_request_text.clone(),
self.action_tab_page_navigation_base.clone(),
2024-09-30 17:45:44 +03:00
self.action_tab_page_navigation_history_back.clone(),
self.action_tab_page_navigation_history_forward.clone(),
2024-09-30 14:49:37 +03:00
self.action_tab_page_navigation_reload.clone(),
2024-09-28 03:10:07 +03:00
self.action_update.clone(),
));
2024-10-07 04:38:22 +03:00
// Init new tab item
let item = Arc::new(TabItem {
label: label.clone(),
page: page.clone(),
});
// Register dynamically created tab components in the HashMap index
2024-10-07 04:38:22 +03:00
self.index.borrow_mut().insert(id.clone(), item.clone());
2024-09-23 18:51:48 +03:00
2024-09-23 21:51:32 +03:00
// Init additional label actions
let controller = GestureClick::new();
controller.connect_pressed({
let label = label.clone();
2024-10-06 16:30:41 +03:00
move |_, count, _, _| {
2024-09-23 21:51:32 +03:00
// double click
2024-10-06 16:30:41 +03:00
if count == 2 {
2024-09-23 22:01:48 +03:00
label.pin(!label.is_pinned()); // toggle
2024-09-23 21:51:32 +03:00
}
}
});
2024-10-06 16:28:46 +03:00
label.gobject().add_controller(controller);
2024-09-23 21:51:32 +03:00
// Append new Notebook page
2024-10-06 16:28:46 +03:00
self.widget
.append(label.gobject(), page.widget(), is_current_page, true);
2024-09-23 18:51:48 +03:00
if page_navigation_request_text.is_none() {
2024-09-30 23:29:08 +03:00
page.navigation_request_grab_focus();
}
2024-10-07 04:38:22 +03:00
item
2024-09-22 22:23:44 +03:00
}
2024-09-18 22:47:53 +03:00
// Close active tab
2024-09-23 16:03:39 +03:00
pub fn close(&self) {
2024-10-06 17:12:08 +03:00
self.widget.close();
2024-09-23 16:03:39 +03:00
}
// Close all tabs
2024-09-23 16:03:39 +03:00
pub fn close_all(&self) {
2024-10-06 17:12:08 +03:00
self.widget.close_all();
2024-09-24 00:56:46 +03:00
}
2024-09-23 16:03:39 +03:00
// Toggle pin status for active tab
pub fn pin(&self) {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
item.label.pin(!item.label.is_pinned()); // toggle
}
}
2024-09-23 15:44:33 +03:00
}
pub fn page_navigation_base(&self) {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_base();
}
}
}
2024-09-30 23:23:29 +03:00
pub fn page_navigation_history_back(&self) {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_history_back();
2024-09-30 23:23:29 +03:00
}
}
}
pub fn page_navigation_history_forward(&self) {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_history_forward();
2024-09-30 23:23:29 +03:00
}
}
}
pub fn page_navigation_reload(&self) {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_reload();
2024-09-25 01:14:45 +03:00
}
}
}
2024-09-24 23:08:40 +03:00
pub fn update(&self) {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
item.page.update();
if let Some(title) = item.page.title() {
item.label.update(Some(&title));
} else {
item.label.update(None);
2024-09-24 23:08:40 +03:00
}
}
}
}
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_id) {
2024-10-06 18:20:36 +03:00
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
2024-10-06 18:20:36 +03:00
Ok(_) => {
// Delegate clean action to childs
2024-10-07 04:38:22 +03:00
for (_, item) in self.index.borrow().iter() {
if let Err(e) = item.label.clean(transaction, &record.id) {
return Err(e.to_string());
}
// @TODO item.page.clean(transaction, &record.id);
2024-10-07 04:38:22 +03:00
}
2024-10-06 18:20:36 +03:00
}
Err(e) => return Err(e.to_string()),
2024-10-06 18:20:36 +03:00
}
}
}
Err(e) => return Err(e.to_string()),
2024-10-06 18:20:36 +03:00
}
Ok(())
2024-10-06 18:20:36 +03:00
}
pub fn restore(
&self,
transaction: &Transaction,
app_browser_window_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_id) {
2024-10-06 18:20:36 +03:00
Ok(records) => {
2024-10-06 19:08:30 +03:00
for record in records {
2024-10-07 04:38:22 +03:00
let item = self.append(None, record.is_current);
2024-10-06 18:20:36 +03:00
// Delegate restore action to childs
if let Err(e) = item.label.restore(transaction, &record.id) {
return Err(e.to_string());
}
// item.page.restore(transaction, record.id);
2024-10-06 18:20:36 +03:00
}
}
Err(e) => return Err(e.to_string()),
2024-10-06 18:20:36 +03:00
}
Ok(())
2024-10-06 18:20:36 +03:00
}
pub fn save(
&self,
transaction: &Transaction,
app_browser_window_id: &i64,
) -> Result<(), String> {
2024-10-06 20:47:08 +03:00
let mut page_number = 0;
2024-10-07 04:38:22 +03:00
for (_, item) in self.index.borrow().iter() {
2024-10-07 20:34:48 +03:00
match Database::add(
transaction,
2024-10-06 20:47:08 +03:00
app_browser_window_id,
&match self.widget.gobject().current_page() {
Some(number) => number == page_number,
None => false,
},
) {
2024-10-06 19:08:30 +03:00
Ok(_) => {
// Delegate save action to childs
let id = Database::last_insert_id(transaction);
2024-10-07 04:38:22 +03:00
if let Err(e) = item.label.save(transaction, &id) {
return Err(e.to_string());
}
2024-10-06 20:47:08 +03:00
// @TODO
2024-10-06 19:08:30 +03:00
// item.page.save()
}
Err(e) => return Err(e.to_string()),
2024-10-06 18:20:36 +03:00
}
2024-10-06 20:47:08 +03:00
page_number += 1;
2024-10-06 18:20:36 +03:00
}
Ok(())
2024-10-06 18:20:36 +03:00
}
2024-09-22 22:23:44 +03:00
// Getters
2024-09-27 16:23:59 +03:00
pub fn page_title(&self) -> Option<GString> {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
if let Some(item) = self.index.borrow().get(&id) {
return item.page.title();
2024-09-25 22:19:48 +03:00
}
}
2024-09-27 16:23:59 +03:00
None
2024-09-25 22:19:48 +03:00
}
2024-09-27 16:23:59 +03:00
pub fn page_description(&self) -> Option<GString> {
2024-10-06 17:12:08 +03:00
if let Some(id) = self.widget.current_name() {
// Get page by widget ID
if let Some(item) = self.index.borrow().get(&id) {
return item.page.description();
2024-09-25 22:19:48 +03:00
}
}
2024-09-27 16:23:59 +03:00
None
2024-09-25 22:19:48 +03:00
}
2024-10-06 16:28:46 +03:00
pub fn gobject(&self) -> &Notebook {
self.widget.gobject()
2024-09-22 22:23:44 +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
if let Err(e) = Label::migrate(&tx) {
return Err(e.to_string());
}
/* @TODO
if let Err(e) = Page::migrate(&tx) {
return Err(e.to_string());
} */
// Success
Ok(())
}
2024-09-20 18:02:10 +03:00
}