monorepo/kifu/kifu-gtk/src/main.rs

90 lines
2.2 KiB
Rust
Raw Normal View History

2023-03-31 02:25:11 +00:00
use gio::resources_lookup_data;
use gtk::prelude::*;
use kifu_core::{CoreApp, Request, Response};
2023-03-25 13:24:36 +00:00
use kifu_gtk::ui::PlayingField;
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use tokio::{
runtime::Runtime,
sync::mpsc::{Receiver, Sender},
};
#[derive(Clone)]
pub struct CoreApi {
gtk_tx: gtk::glib::Sender<Response>,
rt: Arc<Runtime>,
core: CoreApp,
}
impl CoreApi {
pub fn dispatch(&self, request: Request) {
self.rt.spawn({
let gtk_tx = self.gtk_tx.clone();
let core = self.core.clone();
async move { gtk_tx.send(core.dispatch(request).await) }
});
}
}
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");
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-03-25 13:24:36 +00:00
gtk_rx.attach(None, move |message| {
println!("message: {:?}", message);
2023-03-25 13:24:36 +00:00
match message {
Response::PlayingFieldView(view) => {
let playing_field = PlayingField::new(view);
window.set_child(Some(&playing_field));
}
}
Continue(true)
});
api.dispatch(Request::PlayingField);
}
});
println!("running the gtk loop");
app.run();
let _ = runtime.block_on(async { core_handle.await });
}