mirror of https://github.com/YGGverse/Yoda.git
yggverse
2 months ago
37 changed files with 176 additions and 111 deletions
@ -0,0 +1,125 @@ |
|||||||
|
mod action; |
||||||
|
mod browser; |
||||||
|
|
||||||
|
use action::Action; |
||||||
|
use browser::Browser; |
||||||
|
|
||||||
|
use gtk::{ |
||||||
|
glib::{user_config_dir, ExitCode}, |
||||||
|
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt}, |
||||||
|
Application, |
||||||
|
}; |
||||||
|
|
||||||
|
use std::{fs::create_dir_all, sync::Arc}; |
||||||
|
|
||||||
|
const APPLICATION_ID: &str = "io.github.yggverse.Yoda"; |
||||||
|
|
||||||
|
pub struct App { |
||||||
|
// GTK
|
||||||
|
app: Application, |
||||||
|
// Components
|
||||||
|
//browser: Arc<Browser>,
|
||||||
|
} |
||||||
|
|
||||||
|
impl App { |
||||||
|
// Construct
|
||||||
|
pub fn new() -> Self { |
||||||
|
// Init profile directory
|
||||||
|
let mut fs = user_config_dir(); |
||||||
|
|
||||||
|
fs.push(APPLICATION_ID); |
||||||
|
|
||||||
|
if let Err(e) = create_dir_all(&fs) { |
||||||
|
panic!("Failed to create profile directory: {e}") |
||||||
|
} |
||||||
|
|
||||||
|
// Init profile database
|
||||||
|
/* @TODO
|
||||||
|
let mut db = fs.clone(); |
||||||
|
|
||||||
|
db.push("database.sqlite3"); |
||||||
|
|
||||||
|
let db = match sqlite::open(db) { |
||||||
|
Ok(db) => Arc::new(db), |
||||||
|
Err(e) => panic!("Failed to connect profile database: {e}"), |
||||||
|
};*/ |
||||||
|
|
||||||
|
// 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"], |
||||||
|
); |
||||||
|
|
||||||
|
// Init events
|
||||||
|
app.connect_activate({ |
||||||
|
move |application| { |
||||||
|
// Init components
|
||||||
|
let browser = Arc::new(Browser::new( |
||||||
|
&application, |
||||||
|
/*db.clone(),*/ |
||||||
|
action_debug.simple(), |
||||||
|
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(), |
||||||
|
)); |
||||||
|
|
||||||
|
// Show main widget
|
||||||
|
browser.widget().present(); |
||||||
|
|
||||||
|
// Make initial update
|
||||||
|
action_update.simple().activate(None); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
// Return activated struct
|
||||||
|
Self { app } |
||||||
|
} |
||||||
|
|
||||||
|
// Actions
|
||||||
|
pub fn run(&self) -> ExitCode { |
||||||
|
self.app.run() |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,42 @@ |
|||||||
|
// This helper created as the attempt to drop static names usage
|
||||||
|
// and replace them with objects (to follow encapsulation for children mods)
|
||||||
|
// @TODO find alternative implementation, if exist for GTK 4
|
||||||
|
|
||||||
|
use std::sync::Arc; |
||||||
|
|
||||||
|
use gtk::{ |
||||||
|
gio::SimpleAction, |
||||||
|
glib::{gformat, uuid_string_random, GString}, |
||||||
|
prelude::ActionExt, |
||||||
|
}; |
||||||
|
|
||||||
|
pub struct Action { |
||||||
|
group: GString, |
||||||
|
simple: Arc<SimpleAction>, |
||||||
|
} |
||||||
|
|
||||||
|
impl Action { |
||||||
|
// Construct
|
||||||
|
pub fn new(group: &str, is_enabled: bool) -> Self { |
||||||
|
// Create random action name as no static values should be in use
|
||||||
|
let simple = Arc::new(SimpleAction::new(&uuid_string_random(), None)); |
||||||
|
simple.set_enabled(is_enabled); |
||||||
|
|
||||||
|
// Assign action to the group
|
||||||
|
let group = GString::from(group); |
||||||
|
|
||||||
|
// Return new Action
|
||||||
|
Self { group, simple } |
||||||
|
} |
||||||
|
|
||||||
|
// Getters
|
||||||
|
pub fn detailed_name(&self) -> GString { |
||||||
|
gformat!("{}.{}", self.group, self.simple.name()) // @TODO find the way to ident parent group
|
||||||
|
// from SimpleAction object
|
||||||
|
} |
||||||
|
|
||||||
|
// App mods work with simple and system-wide data types, let them take it
|
||||||
|
pub fn simple(&self) -> Arc<SimpleAction> { |
||||||
|
self.simple.clone() |
||||||
|
} |
||||||
|
} |
@ -1,112 +1,8 @@ |
|||||||
mod browser; |
mod app; |
||||||
|
|
||||||
use browser::Browser; |
use app::App; |
||||||
|
use gtk::glib::ExitCode; |
||||||
use gtk::{ |
|
||||||
gio::SimpleAction, |
|
||||||
glib::{user_config_dir, ExitCode}, |
|
||||||
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt}, |
|
||||||
Application, |
|
||||||
}; |
|
||||||
|
|
||||||
use std::{fs::create_dir_all, sync::Arc}; |
|
||||||
|
|
||||||
const APP_ID: &str = "io.github.yggverse.Yoda"; |
|
||||||
|
|
||||||
fn main() -> ExitCode { |
fn main() -> ExitCode { |
||||||
// Init app
|
App::new().run() |
||||||
let app = Application::builder().application_id(APP_ID).build(); |
|
||||||
|
|
||||||
// Init actions
|
|
||||||
let action_debug = Arc::new(SimpleAction::new("debug", None)); |
|
||||||
let action_quit = Arc::new(SimpleAction::new("quit", None)); |
|
||||||
let action_update = Arc::new(SimpleAction::new("update", None)); |
|
||||||
|
|
||||||
let action_tab_append = Arc::new(SimpleAction::new("tab_append", None)); |
|
||||||
let action_tab_close = Arc::new(SimpleAction::new("tab_close", None)); |
|
||||||
let action_tab_close_all = Arc::new(SimpleAction::new("tab_close_all", None)); |
|
||||||
|
|
||||||
let action_tab_page_navigation_base = |
|
||||||
Arc::new(SimpleAction::new("tab_page_navigation_base", None)); |
|
||||||
action_tab_page_navigation_base.set_enabled(false); |
|
||||||
|
|
||||||
let action_tab_page_navigation_history_back = |
|
||||||
Arc::new(SimpleAction::new("tab_page_navigation_history_back", None)); |
|
||||||
action_tab_page_navigation_history_back.set_enabled(false); |
|
||||||
|
|
||||||
let action_tab_page_navigation_history_forward = Arc::new(SimpleAction::new( |
|
||||||
"tab_page_navigation_history_forward", |
|
||||||
None, |
|
||||||
)); |
|
||||||
action_tab_page_navigation_history_forward.set_enabled(false); |
|
||||||
|
|
||||||
let action_tab_page_navigation_reload = |
|
||||||
Arc::new(SimpleAction::new("tab_page_navigation_reload", None)); |
|
||||||
let action_tab_pin = Arc::new(SimpleAction::new("tab_pin", None)); |
|
||||||
|
|
||||||
// Init accels
|
|
||||||
app.set_accels_for_action("win.debug", &["<Primary>i"]); |
|
||||||
app.set_accels_for_action("win.update", &["<Primary>u"]); |
|
||||||
app.set_accels_for_action("win.quit", &["<Primary>Escape"]); |
|
||||||
|
|
||||||
app.set_accels_for_action("win.tab_append", &["<Primary>t"]); |
|
||||||
app.set_accels_for_action("win.tab_pin", &["<Primary>p"]); |
|
||||||
app.set_accels_for_action("win.tab_close", &["<Primary>q"]); |
|
||||||
app.set_accels_for_action("win.tab_page_navigation_base", &["<Primary>h"]); |
|
||||||
app.set_accels_for_action("win.tab_page_navigation_history_back", &["<Primary>Left"]); |
|
||||||
app.set_accels_for_action( |
|
||||||
"win.tab_page_navigation_history_forward", |
|
||||||
&["<Primary>Right"], |
|
||||||
); |
|
||||||
app.set_accels_for_action("win.tab_page_navigation_reload", &["<Primary>r"]); |
|
||||||
//app.set_accels_for_action("win.tab_page_bookmark", &["<Primary>b"]);
|
|
||||||
|
|
||||||
// Create new window
|
|
||||||
app.connect_activate({ |
|
||||||
// Init profile directory
|
|
||||||
let mut fs = user_config_dir(); |
|
||||||
|
|
||||||
fs.push(APP_ID); |
|
||||||
|
|
||||||
if let Err(e) = create_dir_all(&fs) { |
|
||||||
panic!("Failed to create profile directory: {e}") |
|
||||||
} |
|
||||||
|
|
||||||
// Init profile database
|
|
||||||
/* @TODO
|
|
||||||
let mut db = fs.clone(); |
|
||||||
|
|
||||||
db.push("database.sqlite3"); |
|
||||||
|
|
||||||
let db = match sqlite::open(db) { |
|
||||||
Ok(db) => Arc::new(db), |
|
||||||
Err(e) => panic!("Failed to connect profile database: {e}"), |
|
||||||
};*/ |
|
||||||
|
|
||||||
move |this: &Application| { |
|
||||||
Browser::new( |
|
||||||
this, |
|
||||||
/*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(), |
|
||||||
) |
|
||||||
.widget() |
|
||||||
.present(); |
|
||||||
|
|
||||||
// Make initial update
|
|
||||||
action_update.activate(None); |
|
||||||
} |
|
||||||
}); |
|
||||||
|
|
||||||
// Start
|
|
||||||
app.run() |
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue