Yoda/src/app/browser.rs

301 lines
8.7 KiB
Rust
Raw Normal View History

2024-11-03 19:57:44 +02:00
mod about;
2024-10-04 21:23:38 +03:00
mod database;
2024-10-05 03:27:09 +03:00
mod widget;
2024-10-05 17:10:31 +03:00
mod window;
2024-09-18 20:33:29 +03:00
2024-11-03 19:57:44 +02:00
use about::About;
2024-10-04 21:23:38 +03:00
use database::Database;
2024-10-05 03:27:09 +03:00
use widget::Widget;
2024-10-05 17:10:31 +03:00
use window::Window;
2024-09-24 23:08:40 +03:00
2024-10-09 10:50:41 +03:00
use adw::ApplicationWindow;
2024-09-24 23:08:40 +03:00
use gtk::{
2024-10-18 10:16:23 +03:00
gio::{Cancellable, File, SimpleAction},
2024-11-04 19:35:39 +02:00
glib::Variant,
2024-11-04 04:52:33 +02:00
prelude::{ActionExt, GtkWindowExt},
2024-10-18 10:16:23 +03:00
FileLauncher,
2024-09-24 23:08:40 +03:00
};
use sqlite::Transaction;
use std::{path::PathBuf, sync::Arc};
2024-09-23 01:57:16 +03:00
2024-09-22 02:00:54 +03:00
pub struct Browser {
// Components
2024-10-03 20:03:43 +03:00
// header: Arc<Header>,
2024-10-06 18:20:36 +03:00
window: Arc<Window>,
2024-10-05 03:27:09 +03:00
widget: Arc<Widget>,
2024-09-22 02:00:54 +03:00
}
2024-09-21 20:48:12 +03:00
2024-09-22 17:34:22 +03:00
impl Browser {
2024-09-23 13:58:56 +03:00
// Construct
2024-09-22 17:34:22 +03:00
pub fn new(
2024-10-02 02:14:00 +03:00
// Extras
2024-10-04 17:54:25 +03:00
profile_path: PathBuf,
2024-09-30 13:43:46 +03:00
// Actions
2024-11-03 19:57:44 +02:00
action_about: SimpleAction,
2024-11-01 03:09:09 +02:00
action_debug: SimpleAction,
action_profile: SimpleAction,
2024-10-27 12:50:36 +02:00
action_quit: SimpleAction,
action_update: SimpleAction,
2024-11-01 03:09:09 +02:00
action_page_new: SimpleAction,
action_page_close: SimpleAction,
action_page_close_all: SimpleAction,
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,
action_page_pin: SimpleAction,
2024-09-23 18:51:48 +03:00
) -> Browser {
2024-11-01 03:26:18 +02:00
// Init components
2024-10-09 10:50:41 +03:00
let window = Arc::new(Window::new(
2024-11-03 19:57:44 +02:00
action_about.clone(),
2024-11-01 03:09:09 +02:00
action_debug.clone(),
action_profile.clone(),
2024-09-28 03:10:07 +03:00
action_quit.clone(),
2024-10-09 10:50:41 +03:00
action_update.clone(),
2024-11-01 03:09:09 +02:00
action_page_new.clone(),
action_page_close.clone(),
action_page_close_all.clone(),
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(),
action_page_pin.clone(),
));
2024-09-28 01:29:05 +03:00
// Init widget
2024-11-01 03:23:49 +02:00
let widget = Arc::new(Widget::new(
window.gobject(),
&[
2024-11-03 19:57:44 +02:00
action_about.clone(),
2024-11-01 03:23:49 +02:00
action_debug.clone(),
action_profile.clone(),
action_quit.clone(),
action_update.clone(),
action_page_new.clone(),
action_page_close.clone(),
action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(),
action_page_history_forward.clone(),
action_page_reload.clone(),
action_page_pin.clone(),
],
));
2024-10-03 20:03:43 +03:00
// Init events
2024-11-03 19:57:44 +02:00
action_about.connect_activate({
let window = window.clone();
move |_, _| {
About::new().present(Some(window.gobject()));
}
});
2024-11-01 03:09:09 +02:00
action_debug.connect_activate({
2024-10-03 20:03:43 +03:00
let widget = widget.clone();
2024-09-28 01:29:05 +03:00
move |_, _| {
widget.gobject().emit_enable_debugging(true);
2024-09-28 01:29:05 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_profile.connect_activate({
move |_, _| {
2024-10-18 10:16:23 +03:00
FileLauncher::new(Some(&File::for_path(&profile_path))).launch(
None::<&gtk::Window>,
None::<&Cancellable>,
|result| {
if let Err(error) = result {
// @TODO
println!("Could not delegate launch action: {error}")
}
},
2024-10-04 17:54:25 +03:00
);
}
});
2024-10-03 20:03:43 +03:00
action_quit.connect_activate({
let widget = widget.clone();
2024-09-28 01:29:05 +03:00
move |_, _| {
widget.gobject().close();
2024-09-28 01:29:05 +03:00
}
});
2024-10-03 20:03:43 +03:00
action_update.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 19:35:39 +02:00
move |_, this| window.update(string_from_variant(this).as_str())
2024-09-28 01:29:05 +03:00
});
2024-11-01 03:09:09 +02:00
action_page_new.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
window.tab_append(None);
}
});
2024-11-01 03:09:09 +02:00
action_page_close.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 20:22:27 +02:00
move |this, _| {
window.tab_close(page_position_from_action_state(this));
}
});
2024-11-01 03:09:09 +02:00
action_page_close_all.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_close_all();
}
});
2024-11-01 03:14:11 +02:00
action_page_home.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 23:04:57 +02:00
move |this, _| {
window.tab_page_navigation_home(page_position_from_action_state(this));
2024-09-30 15:02:22 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_page_history_back.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-10-03 20:03:43 +03:00
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_page_navigation_history_back();
2024-10-03 20:03:43 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_page_history_forward.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-10-03 20:03:43 +03:00
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_page_navigation_history_forward();
2024-10-03 20:03:43 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_page_reload.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 19:35:39 +02:00
move |this, _| window.tab_page_navigation_reload(page_position_from_action_state(this))
});
2024-11-01 03:09:09 +02:00
action_page_pin.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 23:04:57 +02:00
move |this, _| {
window.tab_pin(page_position_from_action_state(this));
}
});
2024-11-01 03:26:18 +02:00
// Return new activated `Self`
2024-10-03 20:03:43 +03:00
Self {
widget,
// header,
2024-10-06 18:20:36 +03:00
window,
2024-10-03 20:03:43 +03:00
}
2024-09-22 17:34:22 +03:00
}
2024-09-19 18:08:09 +03:00
2024-10-04 20:20:53 +03:00
// Actions
pub fn clean(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_id) {
2024-10-04 21:23:38 +03:00
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
2024-10-04 21:23:38 +03:00
Ok(_) => {
// Delegate clean action to childs
2024-10-08 03:41:51 +03:00
self.window.clean(transaction, &record.id)?;
self.widget.clean(transaction, &record.id)?;
/* @TODO
2024-10-08 03:41:51 +03:00
self.header.clean(transaction, &record.id)?; */
2024-10-04 21:23:38 +03:00
}
Err(e) => return Err(e.to_string()),
2024-10-04 21:23:38 +03:00
}
}
}
Err(e) => return Err(e.to_string()),
2024-10-04 21:23:38 +03:00
}
Ok(())
2024-10-04 21:23:38 +03:00
}
pub fn restore(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_id) {
2024-10-05 15:32:03 +03:00
Ok(records) => {
for record in records {
// Delegate restore action to childs
2024-10-08 03:41:51 +03:00
self.widget.restore(transaction, &record.id)?;
self.window.restore(transaction, &record.id)?;
/* @TODO
2024-10-08 03:41:51 +03:00
self.header.restore(transaction, &record.id)?; */
2024-10-05 15:32:03 +03:00
}
}
Err(e) => return Err(e.to_string()),
2024-10-05 15:32:03 +03:00
}
Ok(())
2024-10-04 21:23:38 +03:00
}
pub fn save(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match Database::add(transaction, app_id) {
2024-10-04 21:23:38 +03:00
Ok(_) => {
let id = Database::last_insert_id(transaction);
2024-10-04 21:23:38 +03:00
// Delegate save action to childs
2024-10-08 03:41:51 +03:00
self.widget.save(transaction, &id)?;
self.window.save(transaction, &id)?;
/* @TODO
2024-10-08 03:41:51 +03:00
self.header.save(transaction, &id)?; */
2024-10-04 21:23:38 +03:00
}
Err(e) => return Err(e.to_string()),
2024-10-04 21:23:38 +03:00
}
Ok(())
2024-10-04 21:23:38 +03:00
}
2024-10-04 20:20:53 +03:00
pub fn init(&self) {
self.window.init();
}
2024-09-22 17:34:22 +03:00
// Getters
2024-10-06 05:16:29 +03:00
pub fn gobject(&self) -> &ApplicationWindow {
&self.widget.gobject()
2024-09-22 12:42:34 +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
/* @TODO
header::migrate(&tx)?; */
window::migrate(&tx)?;
widget::migrate(&tx)?;
// Success
Ok(())
2024-09-20 18:02:10 +03:00
}
2024-11-04 19:35:39 +02:00
2024-11-04 19:40:13 +02:00
// Private helpers @TODO move outside
2024-11-04 19:43:29 +02:00
/// Extract `Optional` page position from C-based
/// [SimpleAction state](https://docs.gtk.org/gio/property.SimpleAction.state.html)
2024-11-04 19:35:39 +02:00
fn page_position_from_action_state(action: &SimpleAction) -> Option<i32> {
let page_position = action
.state()
2024-11-04 19:37:06 +02:00
.expect("Page position required for this action")
2024-11-04 19:35:39 +02:00
.get::<i32>()
2024-11-04 19:44:19 +02:00
.expect("Parameter type does not match `i32`");
2024-11-04 19:35:39 +02:00
if page_position > -1 {
Some(page_position)
} else {
None
}
2024-11-04 19:40:13 +02:00
}
2024-11-04 19:35:39 +02:00
2024-11-04 19:43:29 +02:00
/// Extract `String` from [Variant](https://docs.gtk.org/glib/struct.Variant.html)
2024-11-04 19:35:39 +02:00
fn string_from_variant(variant: Option<&Variant>) -> String {
variant
.expect("Variant required for this action")
.get::<String>()
2024-11-04 19:44:19 +02:00
.expect("Parameter type does not match `String`")
2024-11-04 19:40:13 +02:00
}