2024-10-06 18:20:36 +03:00
|
|
|
mod database;
|
2024-10-08 00:56:52 +03:00
|
|
|
mod item;
|
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-10-08 00:56:52 +03:00
|
|
|
use item::Item;
|
2024-10-06 16:28:46 +03:00
|
|
|
use widget::Widget;
|
2024-09-23 15:56:09 +03:00
|
|
|
|
2024-10-11 01:38:12 +03:00
|
|
|
use adw::TabView;
|
2024-09-24 20:48:31 +03:00
|
|
|
use gtk::{
|
2024-09-28 03:10:07 +03:00
|
|
|
gio::SimpleAction,
|
2024-10-14 05:12:30 +03:00
|
|
|
glib::{uuid_string_random, GString, Propagation},
|
|
|
|
prelude::StaticVariantType,
|
2024-09-24 20:48:31 +03:00
|
|
|
};
|
2024-10-11 01:38:12 +03:00
|
|
|
use sqlite::Transaction;
|
2024-10-07 19:54:28 +03:00
|
|
|
use std::{cell::RefCell, collections::HashMap, sync::Arc};
|
2024-09-24 19:10:40 +03:00
|
|
|
|
2024-10-06 17:30:18 +03:00
|
|
|
// Main
|
2024-09-22 02:00:54 +03:00
|
|
|
pub struct Tab {
|
2024-10-14 05:57:33 +03:00
|
|
|
// Local actions
|
2024-10-27 12:50:36 +02:00
|
|
|
action_tab_open: SimpleAction,
|
2024-10-14 05:57:33 +03:00
|
|
|
// Global actions
|
2024-11-01 03:14:11 +02:00
|
|
|
action_page_home: SimpleAction,
|
2024-11-01 03:09:09 +02:00
|
|
|
action_page_history_back: SimpleAction,
|
|
|
|
action_page_history_forward: SimpleAction,
|
|
|
|
action_page_reload: SimpleAction,
|
2024-10-27 12:50:36 +02:00
|
|
|
action_update: SimpleAction,
|
2024-09-24 19:10:40 +03:00
|
|
|
// Dynamically allocated reference index
|
2024-10-15 08:45:44 +03:00
|
|
|
index: Arc<RefCell<HashMap<GString, Arc<Item>>>>,
|
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-10-10 23:34:06 +03:00
|
|
|
pub fn new_arc(
|
2024-10-06 18:20:36 +03:00
|
|
|
// Actions
|
2024-11-01 03:14:11 +02:00
|
|
|
action_page_home: SimpleAction,
|
2024-11-01 03:09:09 +02:00
|
|
|
action_page_history_back: SimpleAction,
|
|
|
|
action_page_history_forward: SimpleAction,
|
|
|
|
action_page_reload: SimpleAction,
|
2024-10-27 12:50:36 +02:00
|
|
|
action_update: SimpleAction,
|
2024-10-10 23:34:06 +03:00
|
|
|
) -> Arc<Self> {
|
2024-10-14 05:12:30 +03:00
|
|
|
// Init local actions
|
2024-10-27 12:50:36 +02:00
|
|
|
let action_tab_open =
|
|
|
|
SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type()));
|
2024-10-14 05:12:30 +03:00
|
|
|
|
2024-10-06 20:47:08 +03:00
|
|
|
// Init empty HashMap index as no tabs appended yet
|
2024-10-11 05:01:42 +03:00
|
|
|
let index = Arc::new(RefCell::new(HashMap::new()));
|
2024-10-06 20:47:08 +03:00
|
|
|
|
|
|
|
// Init widget
|
2024-10-14 05:12:30 +03:00
|
|
|
let widget = Arc::new(Widget::new(action_tab_open.clone()));
|
2024-10-06 20:47:08 +03:00
|
|
|
|
2024-10-11 01:44:44 +03:00
|
|
|
// Init events
|
2024-10-11 05:01:42 +03:00
|
|
|
widget.gobject().connect_close_page({
|
|
|
|
let index = index.clone();
|
|
|
|
move |_, item| {
|
|
|
|
// Get index ID by keyword saved
|
|
|
|
match item.keyword() {
|
|
|
|
Some(id) => {
|
|
|
|
if id.is_empty() {
|
|
|
|
panic!("Tab index can not be empty!")
|
|
|
|
}
|
|
|
|
// Cleanup HashMap index
|
|
|
|
index.borrow_mut().remove(&id);
|
|
|
|
}
|
|
|
|
None => panic!("Undefined tab index!"),
|
|
|
|
}
|
|
|
|
|
|
|
|
Propagation::Proceed
|
2024-10-11 01:44:44 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-14 05:57:33 +03:00
|
|
|
action_tab_open.connect_activate({
|
|
|
|
let index = index.clone();
|
|
|
|
let gobject = widget.gobject().clone();
|
|
|
|
// Actions
|
|
|
|
let action_tab_open = action_tab_open.clone();
|
2024-11-01 03:14:11 +02:00
|
|
|
let action_page_home = action_page_home.clone();
|
2024-11-01 03:09:09 +02:00
|
|
|
let action_page_history_back = action_page_history_back.clone();
|
|
|
|
let action_page_history_forward = action_page_history_forward.clone();
|
|
|
|
let action_page_reload = action_page_reload.clone();
|
2024-10-14 05:57:33 +03:00
|
|
|
let action_update = action_update.clone();
|
2024-10-14 06:42:06 +03:00
|
|
|
move |_, request| {
|
2024-10-14 05:57:33 +03:00
|
|
|
// Init new tab item
|
|
|
|
let item = Item::new_arc(
|
|
|
|
&gobject,
|
|
|
|
// Local actions
|
|
|
|
action_tab_open.clone(),
|
|
|
|
// Global actions
|
2024-11-01 03:14:11 +02:00
|
|
|
action_page_home.clone(),
|
2024-11-01 03:09:09 +02:00
|
|
|
action_page_history_back.clone(),
|
|
|
|
action_page_history_forward.clone(),
|
|
|
|
action_page_reload.clone(),
|
2024-10-14 05:57:33 +03:00
|
|
|
action_update.clone(),
|
|
|
|
// Options
|
2024-10-15 15:55:14 +03:00
|
|
|
match gobject.selected_page() {
|
|
|
|
Some(page) => Some(gobject.page_position(&page) + 1),
|
|
|
|
None => None,
|
|
|
|
},
|
2024-10-14 05:57:33 +03:00
|
|
|
false,
|
2024-10-14 06:42:06 +03:00
|
|
|
false,
|
2024-10-14 05:57:33 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// Register dynamically created tab components in the HashMap index
|
|
|
|
index.borrow_mut().insert(item.id(), item.clone());
|
2024-10-14 06:42:06 +03:00
|
|
|
|
2024-10-14 06:45:05 +03:00
|
|
|
// Apply request
|
2024-10-14 06:42:06 +03:00
|
|
|
if let Some(variant) = request {
|
|
|
|
if let Some(value) = variant.get::<String>() {
|
2024-10-14 06:45:05 +03:00
|
|
|
item.set_page_navigation_request_text(value.as_str());
|
|
|
|
item.page_navigation_reload();
|
2024-10-14 06:42:06 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-14 05:57:33 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-11 01:44:44 +03:00
|
|
|
// Return activated struct
|
2024-10-10 23:34:06 +03:00
|
|
|
Arc::new(Self {
|
2024-10-14 05:57:33 +03:00
|
|
|
// Local actions
|
|
|
|
action_tab_open,
|
|
|
|
// Global actions
|
2024-11-01 03:14:11 +02:00
|
|
|
action_page_home,
|
2024-11-01 03:09:09 +02:00
|
|
|
action_page_history_back,
|
|
|
|
action_page_history_forward,
|
|
|
|
action_page_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-10-10 23:34:06 +03:00
|
|
|
})
|
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
|
2024-10-15 15:55:14 +03:00
|
|
|
pub fn append(&self, position: Option<i32>) -> Arc<Item> {
|
2024-10-08 00:56:52 +03:00
|
|
|
// Init new tab item
|
2024-10-08 19:42:51 +03:00
|
|
|
let item = Item::new_arc(
|
2024-10-11 03:06:48 +03:00
|
|
|
self.gobject(),
|
2024-10-14 05:57:33 +03:00
|
|
|
// Local actions
|
|
|
|
self.action_tab_open.clone(),
|
|
|
|
// Global actions
|
2024-11-01 03:14:11 +02:00
|
|
|
self.action_page_home.clone(),
|
2024-11-01 03:09:09 +02:00
|
|
|
self.action_page_history_back.clone(),
|
|
|
|
self.action_page_history_forward.clone(),
|
|
|
|
self.action_page_reload.clone(),
|
2024-09-28 03:10:07 +03:00
|
|
|
self.action_update.clone(),
|
2024-10-11 03:46:51 +03:00
|
|
|
// Options
|
2024-10-15 15:55:14 +03:00
|
|
|
position,
|
2024-10-11 06:29:50 +03:00
|
|
|
false,
|
2024-10-11 03:46:51 +03:00
|
|
|
true,
|
2024-10-08 06:20:46 +03:00
|
|
|
);
|
2024-09-24 20:48:31 +03:00
|
|
|
|
|
|
|
// Register dynamically created tab components in the HashMap index
|
2024-10-08 00:56:52 +03:00
|
|
|
self.index.borrow_mut().insert(item.id(), item.clone());
|
2024-09-23 21:51:32 +03:00
|
|
|
|
2024-10-11 02:40:08 +03:00
|
|
|
item.page_navigation_request_grab_focus(); // @TODO
|
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
|
|
|
|
2024-09-24 19:10:40 +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
|
|
|
}
|
|
|
|
|
2024-09-24 19:10:40 +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
|
|
|
|
2024-09-24 19:10:40 +03:00
|
|
|
// Toggle pin status for active tab
|
|
|
|
pub fn pin(&self) {
|
2024-10-11 06:24:06 +03:00
|
|
|
if let Some(page) = self.widget.gobject().selected_page() {
|
|
|
|
self.widget
|
|
|
|
.gobject()
|
|
|
|
.set_page_pinned(&page, !page.is_pinned()); // toggle
|
2024-09-24 19:10:40 +03:00
|
|
|
}
|
2024-09-23 15:44:33 +03:00
|
|
|
}
|
|
|
|
|
2024-11-01 03:14:11 +02:00
|
|
|
pub fn page_navigation_home(&self) {
|
2024-10-11 05:01:42 +03:00
|
|
|
if let Some(id) = self.widget.current_page_keyword() {
|
2024-10-06 17:28:17 +03:00
|
|
|
if let Some(item) = self.index.borrow().get(&id) {
|
2024-11-01 03:14:11 +02:00
|
|
|
item.page_navigation_home();
|
2024-09-30 16:34:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 23:23:29 +03:00
|
|
|
pub fn page_navigation_history_back(&self) {
|
2024-10-11 05:01:42 +03:00
|
|
|
if let Some(id) = self.widget.current_page_keyword() {
|
2024-10-06 17:28:17 +03:00
|
|
|
if let Some(item) = self.index.borrow().get(&id) {
|
2024-10-08 00:56:52 +03:00
|
|
|
item.page_navigation_history_back();
|
2024-09-30 23:23:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn page_navigation_history_forward(&self) {
|
2024-10-11 05:01:42 +03:00
|
|
|
if let Some(id) = self.widget.current_page_keyword() {
|
2024-10-06 17:28:17 +03:00
|
|
|
if let Some(item) = self.index.borrow().get(&id) {
|
2024-10-08 00:56:52 +03:00
|
|
|
item.page_navigation_history_forward();
|
2024-09-30 23:23:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 16:34:58 +03:00
|
|
|
pub fn page_navigation_reload(&self) {
|
2024-10-11 05:01:42 +03:00
|
|
|
if let Some(id) = self.widget.current_page_keyword() {
|
2024-10-06 17:28:17 +03:00
|
|
|
if let Some(item) = self.index.borrow().get(&id) {
|
2024-10-08 00:56:52 +03:00
|
|
|
item.page_navigation_reload();
|
2024-09-25 01:14:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:45:44 +03:00
|
|
|
pub fn update(&self, id: &str) {
|
2024-10-15 08:57:49 +03:00
|
|
|
match self.index.borrow().get(id) {
|
|
|
|
Some(item) => {
|
|
|
|
// Update item components
|
|
|
|
item.update();
|
|
|
|
|
|
|
|
// Update tab title on loading indicator inactive
|
|
|
|
if !item.page_is_loading() {
|
2024-11-02 04:44:07 +02:00
|
|
|
item.gobject().set_title(item.page_meta_title().as_str())
|
2024-10-15 08:57:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update all tabs on ID not found @TODO change initial update method
|
|
|
|
None => {
|
|
|
|
for (_, item) in self.index.borrow().iter() {
|
|
|
|
// Update item components
|
|
|
|
item.update();
|
|
|
|
|
|
|
|
// Update tab title on loading indicator inactive
|
|
|
|
if !item.page_is_loading() {
|
2024-11-02 04:44:07 +02:00
|
|
|
item.gobject().set_title(item.page_meta_title().as_str())
|
2024-10-15 08:57:49 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-24 23:08:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 21:10:12 +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 {
|
2024-10-07 21:10:12 +03:00
|
|
|
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() {
|
2024-10-08 03:41:51 +03:00
|
|
|
item.clean(transaction, &record.id)?
|
2024-10-07 04:38:22 +03:00
|
|
|
}
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 21:10:12 +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-08 00:56:52 +03:00
|
|
|
match Item::restore(
|
2024-10-11 03:06:48 +03:00
|
|
|
self.gobject(),
|
2024-10-08 00:56:52 +03:00
|
|
|
transaction,
|
|
|
|
&record.id,
|
2024-10-14 05:57:33 +03:00
|
|
|
self.action_tab_open.clone(),
|
2024-11-01 03:14:11 +02:00
|
|
|
self.action_page_home.clone(),
|
2024-11-01 03:09:09 +02:00
|
|
|
self.action_page_history_back.clone(),
|
|
|
|
self.action_page_history_forward.clone(),
|
|
|
|
self.action_page_reload.clone(),
|
2024-10-08 00:56:52 +03:00
|
|
|
self.action_update.clone(),
|
|
|
|
) {
|
|
|
|
Ok(items) => {
|
|
|
|
for item in items {
|
|
|
|
// Register dynamically created tab item in the HashMap index
|
|
|
|
self.index.borrow_mut().insert(item.id(), item.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-07 21:10:12 +03:00
|
|
|
}
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 21:10:12 +03:00
|
|
|
pub fn save(
|
|
|
|
&self,
|
|
|
|
transaction: &Transaction,
|
|
|
|
app_browser_window_id: &i64,
|
|
|
|
) -> Result<(), String> {
|
2024-10-08 00:56:52 +03:00
|
|
|
match Database::add(transaction, app_browser_window_id) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Delegate save action to childs
|
|
|
|
let id = Database::last_insert_id(transaction);
|
|
|
|
|
2024-10-11 04:37:06 +03:00
|
|
|
// Read collected HashMap index
|
|
|
|
for (_, item) in self.index.borrow().iter() {
|
|
|
|
item.save(
|
|
|
|
transaction,
|
|
|
|
&id,
|
|
|
|
&self.widget.gobject().page_position(item.gobject()),
|
2024-10-11 06:29:50 +03:00
|
|
|
&item.gobject().is_pinned(),
|
2024-10-11 04:37:06 +03:00
|
|
|
&item.gobject().is_selected(),
|
|
|
|
)?;
|
|
|
|
}
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
2024-10-08 00:56:52 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-10-06 18:20:36 +03:00
|
|
|
}
|
|
|
|
|
2024-10-12 01:08:16 +03:00
|
|
|
pub fn init(&self) {
|
|
|
|
// Append just one blank page if no tabs available after last session restore
|
|
|
|
if self.index.borrow().is_empty() {
|
2024-10-15 15:55:14 +03:00
|
|
|
self.append(None);
|
2024-10-12 01:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO other/child features..
|
|
|
|
}
|
|
|
|
|
2024-09-22 22:23:44 +03:00
|
|
|
// Getters
|
2024-10-11 01:38:12 +03:00
|
|
|
pub fn gobject(&self) -> &TabView {
|
2024-10-06 16:28:46 +03:00
|
|
|
self.widget.gobject()
|
2024-09-22 22:23:44 +03:00
|
|
|
}
|
2024-10-07 19:54:28 +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
|
2024-10-08 03:41:51 +03:00
|
|
|
Item::migrate(&tx)?;
|
2024-10-07 19:54:28 +03:00
|
|
|
|
|
|
|
/* @TODO
|
2024-10-08 03:41:51 +03:00
|
|
|
Page::migrate(&tx)?; */
|
2024-10-07 19:54:28 +03:00
|
|
|
|
|
|
|
// Success
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-09-20 18:02:10 +03:00
|
|
|
}
|