mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-16 01:29:57 +00:00
193 lines
7.1 KiB
Rust
193 lines
7.1 KiB
Rust
mod action;
|
|
mod browser;
|
|
mod database;
|
|
|
|
use action::Action;
|
|
use browser::Browser;
|
|
use database::Database;
|
|
|
|
use gtk::{
|
|
gio::SimpleAction,
|
|
glib::ExitCode,
|
|
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt},
|
|
Application,
|
|
};
|
|
use sqlite::Connection;
|
|
|
|
use std::sync::Arc;
|
|
|
|
const APPLICATION_ID: &str = "io.github.yggverse.Yoda";
|
|
|
|
pub struct App {
|
|
// GTK
|
|
app: Application,
|
|
// Components
|
|
//browser: Arc<Browser>,
|
|
database: Arc<Database>,
|
|
// Actions
|
|
action_debug: Arc<SimpleAction>,
|
|
action_quit: Arc<SimpleAction>,
|
|
action_update: Arc<SimpleAction>,
|
|
action_tab_append: Arc<SimpleAction>,
|
|
action_tab_close: Arc<SimpleAction>,
|
|
action_tab_close_all: Arc<SimpleAction>,
|
|
action_tab_page_navigation_base: Arc<SimpleAction>,
|
|
action_tab_page_navigation_history_back: Arc<SimpleAction>,
|
|
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
|
|
action_tab_page_navigation_reload: Arc<SimpleAction>,
|
|
action_tab_pin: Arc<SimpleAction>,
|
|
}
|
|
|
|
impl App {
|
|
// Construct
|
|
pub fn new(profile_database_connection: Arc<Connection>) -> Self {
|
|
// Init app database model
|
|
let database = Arc::new(Database::init(profile_database_connection));
|
|
|
|
// Init actions
|
|
let action_debug = Action::new("win", true);
|
|
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
|
|
app.set_accels_for_action(&action_debug.detailed_name(), &["<Primary>i"]);
|
|
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"],
|
|
);
|
|
|
|
// Return app struct
|
|
Self {
|
|
// Actions (SimpleAction)
|
|
action_debug: action_debug.simple(),
|
|
action_quit: action_quit.simple(),
|
|
action_update: action_update.simple(),
|
|
action_tab_append: action_tab_append.simple(),
|
|
action_tab_close: action_tab_close.simple(),
|
|
action_tab_close_all: action_tab_close_all.simple(),
|
|
action_tab_page_navigation_base: action_tab_page_navigation_base.simple(),
|
|
action_tab_page_navigation_history_back: action_tab_page_navigation_history_back
|
|
.simple(),
|
|
action_tab_page_navigation_history_forward: action_tab_page_navigation_history_forward
|
|
.simple(),
|
|
action_tab_page_navigation_reload: action_tab_page_navigation_reload.simple(),
|
|
action_tab_pin: action_tab_pin.simple(),
|
|
// Extras
|
|
database,
|
|
// GTK
|
|
app,
|
|
}
|
|
}
|
|
|
|
// Actions
|
|
pub fn activate(&self) -> &Self {
|
|
// Init events
|
|
self.app.connect_activate({
|
|
// let database = database.clone();
|
|
let action_debug = self.action_debug.clone();
|
|
let action_quit = self.action_quit.clone();
|
|
let action_update = self.action_update.clone();
|
|
let action_tab_append = self.action_tab_append.clone();
|
|
let action_tab_close = self.action_tab_close.clone();
|
|
let action_tab_close_all = self.action_tab_close_all.clone();
|
|
let action_tab_page_navigation_base = self.action_tab_page_navigation_base.clone();
|
|
let action_tab_page_navigation_history_back =
|
|
self.action_tab_page_navigation_history_back.clone();
|
|
let action_tab_page_navigation_history_forward =
|
|
self.action_tab_page_navigation_history_forward.clone();
|
|
let action_tab_page_navigation_reload = self.action_tab_page_navigation_reload.clone();
|
|
let action_tab_pin = self.action_tab_pin.clone();
|
|
move |application| {
|
|
// Restore previous session
|
|
// @TODO
|
|
|
|
// Init components
|
|
let browser = Arc::new(Browser::new(
|
|
&application,
|
|
/*db.clone(),*/
|
|
action_debug.clone(),
|
|
action_quit.clone(),
|
|
action_update.clone(),
|
|
action_tab_append.clone(),
|
|
action_tab_close.clone(),
|
|
action_tab_close_all.clone(),
|
|
action_tab_page_navigation_base.clone(),
|
|
action_tab_page_navigation_history_back.clone(),
|
|
action_tab_page_navigation_history_forward.clone(),
|
|
action_tab_page_navigation_reload.clone(),
|
|
action_tab_pin.clone(),
|
|
));
|
|
|
|
// Activate events
|
|
browser.activate();
|
|
|
|
// Show main widget
|
|
browser.widget().present();
|
|
|
|
// Make initial update
|
|
action_update.activate(None);
|
|
}
|
|
});
|
|
|
|
&self
|
|
}
|
|
|
|
pub fn run(&self) -> ExitCode {
|
|
self.app.run()
|
|
}
|
|
|
|
pub fn save(&self) {
|
|
// Cleanup previous record
|
|
match self.database.clean() {
|
|
Ok(_) => {
|
|
// Delegate clean action to children components
|
|
// self.browser.clean(app_id) @TODO
|
|
// ..
|
|
|
|
// Create new record
|
|
match self.database.add() {
|
|
Ok(_) => {
|
|
// let app_id = self.database.last_insert_id();
|
|
|
|
// Delegate save action to children components
|
|
// self.browser.save(app_id) @TODO
|
|
// ..
|
|
}
|
|
Err(error) => panic!("{error}"), // @TODO
|
|
}
|
|
}
|
|
Err(error) => panic!("{error}"), // @TODO
|
|
}
|
|
}
|
|
}
|