2023-08-21 02:12:00 +00:00
|
|
|
use adw::prelude::*;
|
2024-03-15 14:49:25 +00:00
|
|
|
use gio::ActionEntry;
|
|
|
|
use kifu_core::{Config, ConfigOption, Core};
|
2023-05-26 04:16:40 +00:00
|
|
|
use kifu_gtk::{
|
2023-06-15 03:47:12 +00:00
|
|
|
perftrace,
|
2024-03-15 14:49:25 +00:00
|
|
|
// ui::{ConfigurationPage, Home, PlayingField},
|
|
|
|
AppWindow,
|
2023-05-26 04:16:40 +00:00
|
|
|
CoreApi,
|
|
|
|
};
|
2023-04-07 02:29:15 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2024-02-27 00:06:41 +00:00
|
|
|
const APP_ID_DEV: &str = "com.luminescent-dreams.kifu-gtk.dev";
|
|
|
|
const APP_ID_PROD: &str = "com.luminescent-dreams.kifu-gtk";
|
|
|
|
|
|
|
|
const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/kifu-gtk/";
|
|
|
|
|
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);
|
|
|
|
let db_path: String = settings.string("database-path").into();
|
|
|
|
let mut config = Config::new();
|
|
|
|
config.set(ConfigOption::LibraryPath(db_path.into()));
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_app_configuration_action(app: &adw::Application) {
|
|
|
|
println!("setup_app_configuration_action");
|
|
|
|
let action = ActionEntry::builder("show_config")
|
|
|
|
.activate(|_app: &adw::Application, _, _| {
|
|
|
|
println!("show configuration window");
|
|
|
|
})
|
|
|
|
.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() {
|
2023-03-31 02:25:11 +00:00
|
|
|
gio::resources_register_include!("com.luminescent-dreams.kifu-gtk.gresource")
|
|
|
|
.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-15 14:49:25 +00:00
|
|
|
let config = load_config(&app_id);
|
2024-02-27 00:06:41 +00:00
|
|
|
|
2023-03-20 12:16:20 +00:00
|
|
|
let runtime = Arc::new(
|
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
2024-02-27 00:06:41 +00:00
|
|
|
/*
|
2023-07-26 02:49:43 +00:00
|
|
|
let config_path = std::env::var("CONFIG")
|
2023-10-05 16:19:57 +00:00
|
|
|
.map(std::path::PathBuf::from)
|
2023-07-26 02:49:43 +00:00
|
|
|
.or({
|
2023-10-05 16:19:57 +00:00
|
|
|
std::env::var("HOME").map(|base| {
|
2023-07-26 02:49:43 +00:00
|
|
|
let mut config_path = std::path::PathBuf::from(base);
|
|
|
|
config_path.push(".config");
|
|
|
|
config_path.push("kifu");
|
2023-10-05 16:19:57 +00:00
|
|
|
config_path
|
2023-07-26 02:49:43 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.expect("no config path could be found");
|
2024-02-27 00:06:41 +00:00
|
|
|
*/
|
2023-07-20 03:57:19 +00:00
|
|
|
|
2024-02-27 13:34:17 +00:00
|
|
|
let core = Core::new(config);
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2024-02-27 12:49:10 +00:00
|
|
|
/*
|
2023-03-20 12:16:20 +00:00
|
|
|
let core_handle = runtime.spawn({
|
|
|
|
let core = core.clone();
|
|
|
|
async move {
|
|
|
|
core.run().await;
|
|
|
|
}
|
|
|
|
});
|
2024-02-27 12:49:10 +00:00
|
|
|
*/
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2023-08-21 01:37:40 +00:00
|
|
|
let app = adw::Application::builder()
|
2023-03-20 12:16:20 +00:00
|
|
|
.application_id("com.luminescent-dreams.kifu-gtk")
|
2023-08-21 01:37:40 +00:00
|
|
|
.resource_base_path("/com/luminescent-dreams/kifu-gtk")
|
2023-03-20 12:16:20 +00:00
|
|
|
.build();
|
|
|
|
|
2024-03-15 14:49:25 +00:00
|
|
|
|
2023-03-20 12:16:20 +00:00
|
|
|
app.connect_activate({
|
|
|
|
let runtime = runtime.clone();
|
|
|
|
move |app| {
|
2024-03-15 14:49:25 +00:00
|
|
|
let app_window = AppWindow::new(app, core.clone());
|
|
|
|
|
|
|
|
setup_app_configuration_action(app);
|
2023-08-25 01:56:03 +00:00
|
|
|
|
2023-03-20 12:16:20 +00:00
|
|
|
let api = CoreApi {
|
|
|
|
rt: runtime.clone(),
|
|
|
|
core: core.clone(),
|
|
|
|
};
|
|
|
|
|
2024-03-15 14:49:25 +00:00
|
|
|
/*
|
2023-08-25 00:24:41 +00:00
|
|
|
let action_config = gio::SimpleAction::new("show-config", None);
|
|
|
|
action_config.connect_activate({
|
|
|
|
let api = api.clone();
|
|
|
|
move |_, _| {
|
|
|
|
api.dispatch(CoreRequest::OpenConfiguration);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.add_action(&action_config);
|
2024-03-15 14:49:25 +00:00
|
|
|
*/
|
2023-08-25 00:24:41 +00:00
|
|
|
|
2023-08-25 01:56:03 +00:00
|
|
|
app_window.window.present();
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2024-02-27 12:49:10 +00:00
|
|
|
/*
|
2023-04-07 01:52:39 +00:00
|
|
|
gtk_rx.attach(None, {
|
|
|
|
let api = api.clone();
|
|
|
|
move |message| {
|
2023-05-26 04:16:40 +00:00
|
|
|
perftrace("handle_response", || {
|
2023-08-25 01:56:03 +00:00
|
|
|
handle_response(api.clone(), &app_window, message)
|
2023-05-26 04:16:40 +00:00
|
|
|
});
|
2023-11-14 15:05:56 +00:00
|
|
|
glib::ControlFlow::Continue
|
2023-03-25 13:24:36 +00:00
|
|
|
}
|
2023-03-20 12:16:20 +00:00
|
|
|
});
|
2024-02-27 12:49:10 +00:00
|
|
|
*/
|
2023-03-20 12:16:20 +00:00
|
|
|
|
2024-03-15 14:49:25 +00:00
|
|
|
// api.dispatch(CoreRequest::Home);
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("running the gtk loop");
|
|
|
|
app.run();
|
|
|
|
|
2024-02-27 12:49:10 +00:00
|
|
|
/* let _ = runtime.block_on(core_handle); */
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|