2024-10-02 02:14:00 +03:00
|
|
|
mod action;
|
|
|
|
mod browser;
|
2024-10-02 15:22:50 +03:00
|
|
|
mod database;
|
2024-10-02 02:14:00 +03:00
|
|
|
|
|
|
|
use action::Action;
|
|
|
|
use browser::Browser;
|
2024-10-02 15:22:50 +03:00
|
|
|
use database::Database;
|
2024-10-02 02:14:00 +03:00
|
|
|
|
|
|
|
use gtk::{
|
2024-10-02 18:46:08 +03:00
|
|
|
glib::ExitCode,
|
2024-10-02 02:14:00 +03:00
|
|
|
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt},
|
|
|
|
Application,
|
|
|
|
};
|
2024-10-02 18:46:08 +03:00
|
|
|
use sqlite::Connection;
|
2024-10-02 02:14:00 +03:00
|
|
|
|
2024-10-04 17:54:25 +03:00
|
|
|
use std::{path::PathBuf, sync::Arc};
|
2024-10-02 02:14:00 +03:00
|
|
|
|
|
|
|
const APPLICATION_ID: &str = "io.github.yggverse.Yoda";
|
|
|
|
|
|
|
|
pub struct App {
|
2024-10-03 01:30:13 +03:00
|
|
|
// Actions
|
2024-10-03 20:03:43 +03:00
|
|
|
// action_update: Arc<SimpleAction>,
|
2024-10-03 15:38:34 +03:00
|
|
|
// Components
|
2024-10-03 20:03:43 +03:00
|
|
|
// browser: Arc<Browser>,
|
2024-10-03 15:38:34 +03:00
|
|
|
// Extras
|
2024-10-04 02:23:54 +03:00
|
|
|
// database: Arc<Database>,
|
2024-10-03 15:38:34 +03:00
|
|
|
// GTK
|
|
|
|
app: Application,
|
2024-10-02 02:14:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
// Construct
|
2024-10-04 17:54:25 +03:00
|
|
|
pub fn new(profile_database_connection: Arc<Connection>, profile_path: PathBuf) -> Self {
|
2024-10-04 02:57:50 +03:00
|
|
|
// Init database model
|
|
|
|
let database = match Database::init(profile_database_connection) {
|
|
|
|
Ok(database) => Arc::new(database),
|
2024-10-04 16:19:26 +03:00
|
|
|
Err(error) => panic!("{error}"), // @TODO
|
2024-10-04 02:57:50 +03:00
|
|
|
};
|
2024-10-02 02:14:00 +03:00
|
|
|
|
|
|
|
// Init actions
|
2024-10-04 16:19:26 +03:00
|
|
|
let action_tool_debug = Action::new("win", true);
|
|
|
|
let action_tool_profile_directory = Action::new("win", true);
|
2024-10-02 02:14:00 +03:00
|
|
|
let action_quit = Action::new("win", true);
|
|
|
|
let action_update = Action::new("win", true);
|
|
|
|
let action_tab_append = Action::new("win", true);
|
|
|
|
let action_tab_close = Action::new("win", true);
|
|
|
|
let action_tab_close_all = Action::new("win", true);
|
|
|
|
let action_tab_page_navigation_base = Action::new("win", false);
|
|
|
|
let action_tab_page_navigation_history_back = Action::new("win", false);
|
|
|
|
let action_tab_page_navigation_history_forward = Action::new("win", false);
|
|
|
|
let action_tab_page_navigation_reload = Action::new("win", true);
|
|
|
|
let action_tab_pin = Action::new("win", true);
|
|
|
|
|
|
|
|
// Init GTK
|
|
|
|
let app = Application::builder()
|
|
|
|
.application_id(APPLICATION_ID)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Init accels
|
2024-10-04 16:19:26 +03:00
|
|
|
app.set_accels_for_action(&action_tool_debug.detailed_name(), &["<Primary>i"]);
|
2024-10-02 02:14:00 +03:00
|
|
|
app.set_accels_for_action(&action_update.detailed_name(), &["<Primary>u"]);
|
|
|
|
app.set_accels_for_action(&action_quit.detailed_name(), &["<Primary>Escape"]);
|
|
|
|
app.set_accels_for_action(&action_tab_append.detailed_name(), &["<Primary>t"]);
|
|
|
|
app.set_accels_for_action(&action_tab_pin.detailed_name(), &["<Primary>p"]);
|
|
|
|
app.set_accels_for_action(&action_tab_close.detailed_name(), &["<Primary>q"]);
|
|
|
|
app.set_accels_for_action(
|
|
|
|
&action_tab_page_navigation_base.detailed_name(),
|
|
|
|
&["<Primary>h"],
|
|
|
|
);
|
|
|
|
app.set_accels_for_action(
|
|
|
|
&action_tab_page_navigation_history_back.detailed_name(),
|
|
|
|
&["<Primary>Left"],
|
|
|
|
);
|
|
|
|
app.set_accels_for_action(
|
|
|
|
&action_tab_page_navigation_history_forward.detailed_name(),
|
|
|
|
&["<Primary>Right"],
|
|
|
|
);
|
|
|
|
app.set_accels_for_action(
|
|
|
|
&action_tab_page_navigation_reload.detailed_name(),
|
|
|
|
&["<Primary>r"],
|
|
|
|
);
|
|
|
|
|
2024-10-03 15:38:34 +03:00
|
|
|
// Init components
|
|
|
|
let browser = Arc::new(Browser::new(
|
|
|
|
/*db.clone(),*/
|
2024-10-04 17:54:25 +03:00
|
|
|
profile_path,
|
2024-10-04 16:19:26 +03:00
|
|
|
action_tool_debug.simple(),
|
|
|
|
action_tool_profile_directory.simple(),
|
2024-10-03 15:38:34 +03:00
|
|
|
action_quit.simple(),
|
|
|
|
action_update.simple(),
|
|
|
|
action_tab_append.simple(),
|
|
|
|
action_tab_close.simple(),
|
|
|
|
action_tab_close_all.simple(),
|
|
|
|
action_tab_page_navigation_base.simple(),
|
|
|
|
action_tab_page_navigation_history_back.simple(),
|
|
|
|
action_tab_page_navigation_history_forward.simple(),
|
|
|
|
action_tab_page_navigation_reload.simple(),
|
|
|
|
action_tab_pin.simple(),
|
|
|
|
));
|
|
|
|
|
2024-10-03 20:03:43 +03:00
|
|
|
// Init events
|
|
|
|
app.connect_activate({
|
|
|
|
let action_update = action_update.simple();
|
|
|
|
let browser = browser.clone();
|
2024-10-04 16:19:26 +03:00
|
|
|
let database = database.clone();
|
2024-10-03 15:38:34 +03:00
|
|
|
move |this| {
|
2024-10-04 19:21:37 +03:00
|
|
|
// Restore previous session from DB
|
2024-10-04 02:23:54 +03:00
|
|
|
match database.records() {
|
|
|
|
Ok(records) => {
|
|
|
|
for record in records {
|
|
|
|
println!("{:?}", record.id) // @TODO
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 16:19:26 +03:00
|
|
|
Err(error) => panic!("{error}"), // @TODO
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
2024-10-02 02:14:00 +03:00
|
|
|
|
2024-10-03 01:30:13 +03:00
|
|
|
// Activate events
|
2024-10-03 20:03:43 +03:00
|
|
|
browser.widget().set_application(Some(this));
|
2024-10-03 01:30:13 +03:00
|
|
|
|
2024-10-02 02:14:00 +03:00
|
|
|
// Show main widget
|
|
|
|
browser.widget().present();
|
|
|
|
|
|
|
|
// Make initial update
|
2024-10-03 01:30:13 +03:00
|
|
|
action_update.activate(None);
|
2024-10-02 02:14:00 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-04 02:23:54 +03:00
|
|
|
// Save current session to DB
|
|
|
|
app.connect_window_removed({
|
2024-10-04 19:21:37 +03:00
|
|
|
// let browser = browser.clone();
|
2024-10-04 02:23:54 +03:00
|
|
|
let database = database.clone();
|
|
|
|
move |_, _| {
|
|
|
|
match database.records() {
|
2024-10-04 16:19:26 +03:00
|
|
|
Ok(records) => {
|
2024-10-04 19:21:37 +03:00
|
|
|
// Cleanup previous records
|
|
|
|
for record in records {
|
|
|
|
match database.delete(record.id) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Delegate clean action to children components
|
|
|
|
// browser.clean(app_id); @TODO
|
|
|
|
}
|
|
|
|
Err(error) => panic!("{error}"), // @TODO
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 02:23:54 +03:00
|
|
|
|
|
|
|
// Create new record
|
|
|
|
match database.add() {
|
|
|
|
Ok(_) => {
|
2024-10-04 16:19:26 +03:00
|
|
|
// let app_id = database.last_insert_id();
|
2024-10-04 02:23:54 +03:00
|
|
|
|
|
|
|
// Delegate save action to children components
|
|
|
|
// self.browser.save(app_id) @TODO
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
Err(error) => panic!("{error}"), // @TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => panic!("{error}"), // @TODO
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 16:19:26 +03:00
|
|
|
});
|
2024-10-03 15:38:34 +03:00
|
|
|
|
2024-10-03 20:03:43 +03:00
|
|
|
// Return activated App struct
|
|
|
|
Self {
|
|
|
|
// Actions (SimpleAction)
|
|
|
|
// action_update: action_update.simple(),
|
|
|
|
// Components
|
|
|
|
// browser,
|
|
|
|
// Extras
|
2024-10-04 02:23:54 +03:00
|
|
|
// database,
|
2024-10-03 20:03:43 +03:00
|
|
|
// GTK
|
|
|
|
app,
|
|
|
|
}
|
2024-10-02 02:14:00 +03:00
|
|
|
}
|
|
|
|
|
2024-10-03 20:03:43 +03:00
|
|
|
// Actions
|
2024-10-02 02:14:00 +03:00
|
|
|
pub fn run(&self) -> ExitCode {
|
|
|
|
self.app.run()
|
|
|
|
}
|
|
|
|
}
|