Yoda/src/app/browser/window.rs

167 lines
4.5 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 tab;
mod widget;
2024-09-26 01:11:07 +03:00
2024-10-06 18:20:36 +03:00
use database::Database;
use sqlite::{Connection, Transaction};
2024-09-23 18:51:48 +03:00
use tab::Tab;
use widget::Widget;
2024-09-23 01:57:16 +03:00
2024-10-06 18:20:36 +03:00
use std::sync::{Arc, RwLock};
use gtk::{gio::SimpleAction, glib::GString, Box};
2024-09-23 15:56:09 +03:00
2024-10-05 17:10:31 +03:00
pub struct Window {
2024-10-06 18:20:36 +03:00
database: Arc<Database>,
2024-09-26 01:11:07 +03:00
tab: Arc<Tab>,
widget: Arc<Widget>,
2024-09-22 02:00:54 +03:00
}
2024-10-05 17:10:31 +03:00
impl Window {
// Construct
2024-09-28 03:10:07 +03:00
pub fn new(
2024-10-06 18:20:36 +03:00
// Extras
profile_database_connection: Arc<RwLock<Connection>>,
// 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 18:20:36 +03:00
// Init database
let database = {
// Init writable database connection
let mut connection = match profile_database_connection.write() {
Ok(connection) => connection,
Err(e) => todo!("{e}"),
};
// Init new transaction
let transaction = match connection.transaction() {
Ok(transaction) => transaction,
Err(e) => todo!("{e}"),
};
// Init database structure
match Database::init(&transaction) {
Ok(database) => match transaction.commit() {
Ok(_) => Arc::new(database),
Err(e) => todo!("{e}"),
},
Err(e) => todo!("{e}"),
}
};
// Init components
let tab = Arc::new(Tab::new(
2024-10-06 18:20:36 +03:00
profile_database_connection,
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,
action_tab_page_navigation_reload,
action_update,
));
2024-09-26 01:11:07 +03:00
tab.activate(tab.clone());
2024-09-28 02:27:34 +03:00
// GTK
2024-10-06 16:28:46 +03:00
let widget = Arc::new(Widget::new(tab.gobject()));
2024-09-23 15:56:09 +03:00
// Init struct
2024-10-06 18:20:36 +03:00
Self {
database,
tab,
widget,
}
}
// Actions
pub fn tab_append(&self, tab_page_navigation_request_text: Option<GString>) {
self.tab.append(tab_page_navigation_request_text, true);
2024-09-22 02:00:54 +03:00
}
pub fn tab_page_navigation_base(&self) {
self.tab.page_navigation_base();
}
2024-09-30 23:23:29 +03:00
pub fn tab_page_navigation_history_back(&self) {
self.tab.page_navigation_history_back();
}
pub fn tab_page_navigation_history_forward(&self) {
self.tab.page_navigation_history_forward();
}
pub fn tab_page_navigation_reload(&self) {
self.tab.page_navigation_reload();
2024-09-25 01:14:45 +03:00
}
2024-09-23 16:03:39 +03:00
pub fn tab_close(&self) {
self.tab.close();
}
pub fn tab_close_all(&self) {
2024-09-24 00:56:46 +03:00
self.tab.close_all();
2024-09-23 16:03:39 +03:00
}
2024-09-23 15:44:33 +03:00
pub fn tab_pin(&self) {
self.tab.pin();
}
2024-09-24 23:08:40 +03:00
pub fn update(&self) {
self.tab.update();
}
2024-10-06 18:20:36 +03:00
pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) {
match self.database.records(tx, app_browser_id) {
Ok(records) => {
for record in records {
match self.database.delete(tx, &record.id) {
Ok(_) => {
// Delegate clean action to childs
self.tab.clean(tx, &record.id);
}
Err(e) => todo!("{e}"),
}
}
}
Err(e) => todo!("{e}"),
}
}
pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) {
match self.database.records(tx, app_browser_id) {
Ok(records) => {
for record in records {
// Delegate restore action to childs
2024-10-06 18:24:55 +03:00
self.tab.restore(tx, &record.id);
2024-10-06 18:20:36 +03:00
}
}
Err(e) => todo!("{e}"),
}
}
pub fn save(&self, tx: &Transaction, app_browser_id: &i64) {
match self.database.add(tx, app_browser_id) {
Ok(_) => {
// Delegate save action to childs
2024-10-06 18:24:55 +03:00
self.tab.save(tx, &self.database.last_insert_id(tx));
2024-10-06 18:20:36 +03:00
}
Err(e) => todo!("{e}"),
}
}
// Getters
2024-09-27 16:23:59 +03:00
pub fn tab_page_title(&self) -> Option<GString> {
2024-09-25 22:19:48 +03:00
self.tab.page_title()
}
2024-09-27 16:23:59 +03:00
pub fn tab_page_description(&self) -> Option<GString> {
2024-09-25 22:19:48 +03:00
self.tab.page_description()
}
2024-10-06 05:16:29 +03:00
pub fn gobject(&self) -> &Box {
&self.widget.gobject()
}
2024-09-20 18:02:10 +03:00
}