Yoda/src/app/browser/window.rs

180 lines
4.7 KiB
Rust
Raw Normal View History

mod action;
2024-10-06 18:20:36 +03:00
mod database;
2024-10-09 10:50:41 +03:00
mod header;
2024-09-20 18:02:10 +03:00
mod tab;
mod widget;
2024-09-26 01:11:07 +03:00
use action::Action;
2024-10-06 18:20:36 +03:00
use database::Database;
2024-10-09 10:50:41 +03:00
use header::Header;
use sqlite::Transaction;
2024-09-23 18:51:48 +03:00
use tab::Tab;
use widget::Widget;
2024-09-23 01:57:16 +03:00
use crate::app::browser::action::Action as BrowserAction;
2024-11-11 05:11:48 +02:00
use gtk::glib::GString;
use std::rc::Rc;
2024-09-23 15:56:09 +03:00
2024-10-05 17:10:31 +03:00
pub struct Window {
action: Rc<Action>,
2024-11-11 05:11:48 +02:00
tab: Rc<Tab>,
2024-11-08 05:21:08 +02:00
widget: Rc<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
// Actions
browser_action: Rc<BrowserAction>,
2024-09-28 03:10:07 +03:00
) -> Self {
// Init local actions
let action = Rc::new(Action::new());
// Init components
2024-11-11 01:14:09 +02:00
let tab = Rc::new(Tab::new(browser_action.clone(), action.clone()));
2024-11-11 05:11:48 +02:00
let header = Header::new(browser_action, action.clone(), tab.widget().gobject());
let widget = Rc::new(Widget::new(header.gobject(), tab.widget().gobject()));
2024-09-23 15:56:09 +03:00
// Init events
action.append().connect_activate({
let tab = tab.clone();
2024-11-11 13:03:55 +02:00
move |position, request, is_pinned, is_selected, is_load| {
tab.append(position, request, is_pinned, is_selected, is_load);
}
});
action.pin().connect_activate({
let tab = tab.clone();
move |position| tab.pin(position)
});
action.reload().connect_activate({
let tab = tab.clone();
move |position| tab.page_reload(position)
});
action.home().connect_activate({
let tab = tab.clone();
move |position| tab.page_home(position)
});
action.close().connect_activate({
let tab = tab.clone();
move |position| {
tab.close(position);
}
});
action.close_all().connect_activate({
let tab = tab.clone();
move |_| {
tab.close_all();
} // @TODO position not in use
});
action.history_back().connect_activate({
let tab = tab.clone();
move |position| {
tab.page_history_back(position);
} // @TODO rename destination method
});
action.history_forward().connect_activate({
let tab = tab.clone();
move |position| {
tab.page_history_forward(position);
} // @TODO rename destination method
});
// Init struct
2024-10-09 10:50:41 +03:00
Self {
action,
2024-11-11 05:11:48 +02:00
tab,
2024-10-09 10:50:41 +03:00
widget,
}
}
// Actions
2024-11-10 07:09:55 +02:00
pub fn update(&self, tab_item_id: Option<GString>) {
self.tab.update(tab_item_id);
2024-09-24 23:08:40 +03:00
}
pub fn clean(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_browser_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-08 03:41:51 +03:00
self.tab.clean(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
}
}
}
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_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_browser_id) {
2024-10-06 18:20:36 +03:00
Ok(records) => {
for record in records {
// Delegate restore action to childs
2024-10-08 03:41:51 +03:00
self.tab.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_id: &i64) -> Result<(), String> {
match Database::add(transaction, app_browser_id) {
2024-10-06 18:20:36 +03:00
Ok(_) => {
// Delegate save action to childs
if let Err(e) = self
.tab
.save(transaction, &Database::last_insert_id(transaction))
{
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 init(&self) {
self.tab.init();
}
// Getters
2024-11-10 08:07:44 +02:00
pub fn action(&self) -> &Rc<Action> {
&self.action
}
2024-11-11 05:11:48 +02:00
pub fn widget(&self) -> &Rc<Widget> {
&self.widget
}
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
2024-11-08 05:03:02 +02:00
if let Err(e) = Database::init(tx) {
return Err(e.to_string());
}
// Delegate migration to childs
2024-11-08 05:03:02 +02:00
tab::migrate(tx)?;
// Success
Ok(())
2024-09-20 18:02:10 +03:00
}