Yoda/src/app/browser/window.rs

179 lines
4.8 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-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-14 08:51:14 +02:00
use crate::Profile;
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 {
2024-11-28 01:35:48 +02:00
pub action: Rc<Action>,
pub tab: Rc<Tab>,
pub widget: Rc<Widget>,
2024-09-22 02:00:54 +03:00
}
2024-10-05 17:10:31 +03:00
impl Window {
// Construct
2024-11-14 08:51:14 +02:00
pub fn new(profile: Rc<Profile>, browser_action: Rc<BrowserAction>) -> Self {
// Init local actions
let action = Rc::new(Action::new());
// Init components
2024-11-14 08:51:14 +02:00
let tab = Rc::new(Tab::new(profile, (browser_action.clone(), action.clone())));
2024-11-28 01:35:48 +02:00
let header = Header::new(browser_action, action.clone(), &tab.widget.gobject);
let widget = Rc::new(Widget::new(&header.widget.gobject, &tab.widget.gobject));
2024-09-23 15:56:09 +03:00
// Init events
2024-11-28 01:35:48 +02:00
action.append.connect_activate({
let tab = tab.clone();
move |position, request, is_pinned, is_selected, is_attention, is_load| {
tab.append(
position,
request,
is_pinned,
is_selected,
is_attention,
is_load,
);
}
});
2024-11-28 01:35:48 +02:00
action.bookmark.connect_activate({
2024-11-12 18:02:35 +02:00
let tab = tab.clone();
2024-11-14 12:48:58 +02:00
move |position| {
if tab.bookmark(position).is_err() {
todo!()
}
}
});
2024-11-12 18:02:35 +02:00
2024-11-28 01:35:48 +02:00
action.pin.connect_activate({
let tab = tab.clone();
move |position| tab.pin(position)
});
2024-11-28 01:35:48 +02:00
action.reload.connect_activate({
let tab = tab.clone();
move |position| tab.page_reload(position)
});
2024-11-28 01:35:48 +02:00
action.home.connect_activate({
let tab = tab.clone();
move |position| tab.page_home(position)
});
2024-11-28 01:35:48 +02:00
action.close.connect_activate({
let tab = tab.clone();
2024-11-12 18:05:44 +02:00
move |position| tab.close(position)
});
2024-11-28 01:35:48 +02:00
action.close_all.connect_activate({
let tab = tab.clone();
2024-11-12 18:05:44 +02:00
move |_| tab.close_all()
});
2024-11-28 01:35:48 +02:00
action.history_back.connect_activate({
let tab = tab.clone();
move |position| {
tab.page_history_back(position);
} // @TODO rename destination method
});
2024-11-28 01:35:48 +02:00
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> {
2024-11-13 11:45:33 +02:00
match database::select(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> {
2024-11-13 11:45:33 +02:00
match database::select(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> {
2024-11-13 11:45:33 +02:00
match database::insert(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();
}
}
// 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-11-08 05:03:02 +02:00
tab::migrate(tx)?;
// Success
Ok(())
2024-09-20 18:02:10 +03:00
}