monorepo/otg/gtk/src/main.rs

138 lines
4.4 KiB
Rust
Raw Normal View History

2023-08-21 02:12:00 +00:00
use adw::prelude::*;
2024-03-21 21:01:40 +00:00
use async_std::channel::Receiver;
use async_std::task::spawn;
use gio::ActionEntry;
2024-03-22 03:48:48 +00:00
use otg_core::{Config, ConfigOption, Core, CoreNotification, LibraryPath, Observable};
use otg_gtk::{
AppWindow,
2023-05-26 04:16:40 +00:00
CoreApi,
};
2024-03-26 12:53:24 +00:00
2024-03-22 03:48:48 +00:00
const APP_ID_DEV: &str = "com.luminescent-dreams.otg.dev";
const APP_ID_PROD: &str = "com.luminescent-dreams.otg";
2024-02-27 00:06:41 +00:00
2024-03-26 12:53:24 +00:00
// const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/otg/";
2024-02-27 00:06:41 +00:00
2024-03-21 21:01:40 +00:00
async fn handler(notifications: Receiver<CoreNotification>, app_id: String) {
loop {
let msg = notifications.recv().await;
match msg {
Ok(CoreNotification::ConfigurationUpdated(cfg)) => {
println!("commiting configuration");
let settings = gio::Settings::new(&app_id);
if let Some(LibraryPath(library_path)) = cfg.get() {
let _ = settings.set_string(
"library-path",
&library_path.into_os_string().into_string().unwrap(),
);
}
}
Ok(_) => println!("discarding message"),
Err(err) => {
println!("shutting down handler with error: {:?}", err);
return;
}
}
}
}
/*
fn handle_response(api: CoreApi, app_window: &AppWindow, message: CoreResponse) {
2023-05-26 04:16:40 +00:00
let playing_field = Arc::new(RwLock::new(None));
match message {
2023-08-25 00:24:41 +00:00
CoreResponse::ConfigurationView(view) => perftrace("ConfigurationView", || {
let config_page = ConfigurationPage::new(api, view);
2023-08-25 00:24:41 +00:00
let window = adw::PreferencesWindow::new();
window.add(&config_page);
window.set_visible_page(&config_page);
window.present();
}),
2023-07-24 23:43:22 +00:00
CoreResponse::HomeView(view) => perftrace("HomeView", || {
2023-05-26 04:16:40 +00:00
let api = api.clone();
let home = Home::new(api, view);
app_window.set_content(&home);
}),
CoreResponse::PlayingFieldView(view) => perftrace("PlayingFieldView", || {
2023-05-26 04:16:40 +00:00
let api = api.clone();
let mut playing_field = playing_field.write().unwrap();
if playing_field.is_none() {
perftrace("creating a new playing field", || {
let field = PlayingField::new(api, view);
app_window.set_content(&field);
*playing_field = Some(field);
})
2023-10-05 16:19:57 +00:00
} else if let Some(field) = playing_field.as_ref() {
field.update_view(view)
2023-05-26 04:16:40 +00:00
}
}),
CoreResponse::UpdatedConfigurationView(view) => perftrace("UpdatedConfiguration", || {
println!("updated configuration: {:?}", view);
}),
2023-05-26 04:16:40 +00:00
}
}
*/
fn load_config(app_id: &str) -> Config {
let settings = gio::Settings::new(app_id);
2024-03-15 18:07:55 +00:00
let lib_path: String = settings.string("library-path").into();
let mut config = Config::new();
2024-03-15 18:07:55 +00:00
config.set(ConfigOption::LibraryPath(lib_path.into()));
config
}
2024-03-15 18:07:55 +00:00
fn setup_app_configuration_action(app: &adw::Application, app_window: AppWindow) {
println!("setup_app_configuration_action");
2024-03-15 18:07:55 +00:00
let action = ActionEntry::builder("show_settings")
.activate(move |_app: &adw::Application, _, _| {
let app_window = app_window.clone();
app_window.open_settings()
})
.build();
app.add_action_entries([action]);
println!("setup_app_configuration_action complete");
}
2023-05-26 04:16:40 +00:00
fn main() {
2024-03-22 03:48:48 +00:00
gio::resources_register_include!("com.luminescent-dreams.otg-gtk.gresource")
2023-03-31 02:25:11 +00:00
.expect("Failed to register resources");
2024-02-27 00:06:41 +00:00
let app_id = if std::env::var_os("ENV") == Some("dev".into()) {
APP_ID_DEV
} else {
APP_ID_PROD
};
2024-03-26 12:53:24 +00:00
let config = load_config(app_id);
2024-02-27 00:06:41 +00:00
let core = Core::new(config.clone());
2024-03-21 21:01:40 +00:00
spawn({
let notifier = core.subscribe();
let app_id = app_id.to_owned();
handler(notifier, app_id)
});
2023-08-21 01:37:40 +00:00
let app = adw::Application::builder()
2024-03-22 03:48:48 +00:00
.application_id("com.luminescent-dreams.otg-gtk")
.resource_base_path("/com/luminescent-dreams/otg-gtk")
.build();
app.connect_activate({
move |app| {
let core_api = CoreApi { core: core.clone() };
let app_window = AppWindow::new(app, core_api);
2024-03-15 18:07:55 +00:00
setup_app_configuration_action(app, app_window.clone());
app_window.window.present();
}
});
println!("running the gtk loop");
app.run();
}