2023-08-21 02:12:00 +00:00
|
|
|
use adw::prelude::*;
|
2023-05-13 18:02:24 +00:00
|
|
|
use kifu_core::{CoreApp, CoreRequest, CoreResponse};
|
2023-05-26 04:16:40 +00:00
|
|
|
use kifu_gtk::{
|
2023-06-15 03:47:12 +00:00
|
|
|
perftrace,
|
2023-07-24 23:43:22 +00:00
|
|
|
ui::{Home, PlayingField},
|
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
|
|
|
|
2023-08-21 01:37:40 +00:00
|
|
|
fn handle_response(api: CoreApi, window: adw::ApplicationWindow, message: CoreResponse) {
|
2023-05-26 04:16:40 +00:00
|
|
|
let playing_field = Arc::new(RwLock::new(None));
|
|
|
|
match message {
|
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-21 01:37:40 +00:00
|
|
|
window.set_content(Some(&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-21 01:37:40 +00:00
|
|
|
window.set_content(Some(&field));
|
2023-06-15 03:47:12 +00:00
|
|
|
*playing_field = Some(field);
|
|
|
|
})
|
2023-05-26 04:16:40 +00:00
|
|
|
} else {
|
|
|
|
playing_field.as_ref().map(|field| field.update_view(view));
|
|
|
|
}
|
2023-06-15 03:47:12 +00:00
|
|
|
}),
|
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");
|
|
|
|
|
2023-03-20 12:16:20 +00:00
|
|
|
let runtime = Arc::new(
|
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
2023-07-26 02:49:43 +00:00
|
|
|
let config_path = std::env::var("CONFIG")
|
|
|
|
.and_then(|config| Ok(std::path::PathBuf::from(config)))
|
|
|
|
.or({
|
|
|
|
std::env::var("HOME").and_then(|base| {
|
|
|
|
let mut config_path = std::path::PathBuf::from(base);
|
|
|
|
config_path.push(".config");
|
|
|
|
config_path.push("kifu");
|
|
|
|
Ok(config_path)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.expect("no config path could be found");
|
2023-07-20 03:57:19 +00:00
|
|
|
|
|
|
|
let core = CoreApp::new(config_path);
|
2023-03-20 12:16:20 +00:00
|
|
|
|
|
|
|
let core_handle = runtime.spawn({
|
|
|
|
let core = core.clone();
|
|
|
|
async move {
|
|
|
|
core.run().await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
|
2023-08-21 01:37:40 +00:00
|
|
|
let action_config = gio::SimpleAction::new("show-config", None);
|
|
|
|
action_config.connect_activate(|_, _| {
|
|
|
|
println!("trigger the configuration menu");
|
|
|
|
});
|
|
|
|
app.add_action(&action_config);
|
|
|
|
|
|
|
|
app.connect_startup(|app| {
|
|
|
|
println!("connect_startup");
|
|
|
|
let menu_model = gio::Menu::new();
|
|
|
|
menu_model.append(Some("Quit"), None);
|
|
|
|
menu_model.append(Some("Configuration"), Some("show-config"));
|
|
|
|
app.set_menubar(Some(&menu_model));
|
|
|
|
});
|
|
|
|
|
2023-03-20 12:16:20 +00:00
|
|
|
app.connect_activate({
|
|
|
|
let runtime = runtime.clone();
|
|
|
|
move |app| {
|
|
|
|
let (gtk_tx, gtk_rx) =
|
2023-05-13 18:02:24 +00:00
|
|
|
gtk::glib::MainContext::channel::<CoreResponse>(gtk::glib::PRIORITY_DEFAULT);
|
2023-03-20 12:16:20 +00:00
|
|
|
|
|
|
|
let api = CoreApi {
|
|
|
|
gtk_tx,
|
|
|
|
rt: runtime.clone(),
|
|
|
|
core: core.clone(),
|
|
|
|
};
|
|
|
|
|
2023-08-21 02:12:00 +00:00
|
|
|
let window = adw::ApplicationWindow::builder()
|
|
|
|
.application(app)
|
|
|
|
.width_request(800)
|
|
|
|
.height_request(500)
|
|
|
|
.build();
|
2023-03-20 12:16:20 +00:00
|
|
|
window.present();
|
|
|
|
|
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", || {
|
|
|
|
handle_response(api.clone(), window.clone(), message)
|
|
|
|
});
|
2023-04-07 01:52:39 +00:00
|
|
|
Continue(true)
|
2023-03-25 13:24:36 +00:00
|
|
|
}
|
2023-03-20 12:16:20 +00:00
|
|
|
});
|
|
|
|
|
2023-07-24 23:43:22 +00:00
|
|
|
api.dispatch(CoreRequest::Home);
|
2023-03-20 12:16:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("running the gtk loop");
|
|
|
|
app.run();
|
|
|
|
|
|
|
|
let _ = runtime.block_on(async { core_handle.await });
|
|
|
|
}
|