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;
|
2024-03-15 14:49:25 +00:00
|
|
|
use gio::ActionEntry;
|
2024-03-22 03:48:48 +00:00
|
|
|
use otg_core::{Config, ConfigOption, Core, CoreNotification, LibraryPath, Observable};
|
|
|
|
use otg_gtk::{
|
2024-03-15 14:49:25 +00:00
|
|
|
AppWindow,
|
2023-05-26 04:16:40 +00:00
|
|
|
CoreApi,
|
|
|
|
};
|
2024-03-26 12:53:24 +00:00
|
|
|
|
2023-03-20 12:16:20 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 14:49:25 +00:00
|
|
|
/*
|
2023-08-25 01:56:03 +00:00
|
|
|
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", || {
|
2023-08-25 00:52:27 +00:00
|
|
|
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();
|
|
|
|
|
2023-08-20 03:45:19 +00:00
|
|
|
let home = Home::new(api, view);
|
2023-08-25 01:56:03 +00:00
|
|
|
app_window.set_content(&home);
|
2023-06-15 03:47:12 +00:00
|
|
|
}),
|
|
|
|
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() {
|
2023-06-15 03:47:12 +00:00
|
|
|
perftrace("creating a new playing field", || {
|
|
|
|
let field = PlayingField::new(api, view);
|
2023-08-25 01:56:03 +00:00
|
|
|
app_window.set_content(&field);
|
2023-06-15 03:47:12 +00:00
|
|
|
*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
|
|
|
}
|
2023-06-15 03:47:12 +00:00
|
|
|
}),
|
2023-08-25 00:52:27 +00:00
|
|
|
CoreResponse::UpdatedConfigurationView(view) => perftrace("UpdatedConfiguration", || {
|
|
|
|
println!("updated configuration: {:?}", view);
|
|
|
|
}),
|
2023-05-26 04:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-15 14:49:25 +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();
|
2024-03-15 14:49:25 +00:00
|
|
|
let mut config = Config::new();
|
2024-03-15 18:07:55 +00:00
|
|
|
config.set(ConfigOption::LibraryPath(lib_path.into()));
|
2024-03-15 14:49:25 +00:00
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2024-03-15 18:07:55 +00:00
|
|
|
fn setup_app_configuration_action(app: &adw::Application, app_window: AppWindow) {
|
2024-03-15 14:49:25 +00:00
|
|
|
println!("setup_app_configuration_action");
|
2024-03-15 18:07:55 +00:00
|
|
|
let action = ActionEntry::builder("show_settings")
|
|
|
|
.activate(move |_app: &adw::Application, _, _| {
|
2024-03-21 23:20:09 +00:00
|
|
|
let app_window = app_window.clone();
|
|
|
|
app_window.open_settings()
|
2024-03-15 14:49:25 +00:00
|
|
|
})
|
|
|
|
.build();
|
|
|
|
app.add_action_entries([action]);
|
|
|
|
println!("setup_app_configuration_action complete");
|
|
|
|
}
|
2023-05-26 04:16:40 +00:00
|
|
|
|
2023-03-20 12:16:20 +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
|
|
|
|
2024-03-21 23:20:09 +00:00
|
|
|
let core = Core::new(config.clone());
|
2023-03-20 12:16:20 +00:00
|
|
|
|
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")
|
2023-03-20 12:16:20 +00:00
|
|
|
.build();
|
|
|
|
|
|
|
|
app.connect_activate({
|
|
|
|
move |app| {
|
2024-03-21 23:20:09 +00:00
|
|
|
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());
|
2023-08-25 01:56:03 +00:00
|
|
|
|
|
|
|
app_window.window.present();
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("running the gtk loop");
|
|
|
|
app.run();
|
|
|
|
}
|