2023-03-20 12:16:20 +00:00
|
|
|
use gtk::prelude::*;
|
|
|
|
use kifu_core::{CoreApp, Request, Response};
|
2023-04-07 01:52:39 +00:00
|
|
|
use kifu_gtk::{ui::PlayingField, CoreApi};
|
2023-04-07 02:29:15 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
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(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let core = CoreApp::new();
|
|
|
|
|
|
|
|
let core_handle = runtime.spawn({
|
|
|
|
let core = core.clone();
|
|
|
|
async move {
|
|
|
|
core.run().await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let app = gtk::Application::builder()
|
|
|
|
.application_id("com.luminescent-dreams.kifu-gtk")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
app.connect_activate({
|
|
|
|
let runtime = runtime.clone();
|
|
|
|
move |app| {
|
|
|
|
let (gtk_tx, gtk_rx) =
|
|
|
|
gtk::glib::MainContext::channel::<Response>(gtk::glib::PRIORITY_DEFAULT);
|
|
|
|
|
|
|
|
let api = CoreApi {
|
|
|
|
gtk_tx,
|
|
|
|
rt: runtime.clone(),
|
|
|
|
core: core.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let window = gtk::ApplicationWindow::new(app);
|
|
|
|
window.present();
|
|
|
|
|
2023-04-07 01:52:39 +00:00
|
|
|
gtk_rx.attach(None, {
|
|
|
|
let api = api.clone();
|
2023-04-07 02:13:09 +00:00
|
|
|
let playing_field = Arc::new(RwLock::new(None));
|
2023-04-07 01:52:39 +00:00
|
|
|
move |message| {
|
|
|
|
match message {
|
|
|
|
Response::PlayingFieldView(view) => {
|
|
|
|
let api = api.clone();
|
2023-04-07 02:13:09 +00:00
|
|
|
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
let mut playing_field = playing_field.write().unwrap();
|
|
|
|
if playing_field.is_none() {
|
|
|
|
let field = PlayingField::new(api, view);
|
|
|
|
window.set_child(Some(&field));
|
|
|
|
*playing_field = Some(field);
|
|
|
|
} else {
|
|
|
|
playing_field.as_ref().map(|field| field.update_view(view));
|
|
|
|
}
|
|
|
|
let end = std::time::Instant::now();
|
|
|
|
println!("Time to render the playing field: {:?}", end - start);
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
2023-03-25 13:24:36 +00:00
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
|
|
|
api.dispatch(Request::PlayingField);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("running the gtk loop");
|
|
|
|
app.run();
|
|
|
|
|
|
|
|
let _ = runtime.block_on(async { core_handle.await });
|
|
|
|
}
|