Compare commits

...

2 Commits

Author SHA1 Message Date
Savanni D'Gerinel d86d10428b Try setting up the GTK loop 2023-03-20 09:43:56 -04:00
Savanni D'Gerinel 9b58770070 Set up the core run loop 2023-03-20 09:43:48 -04:00
2 changed files with 42 additions and 8 deletions

View File

@ -11,11 +11,12 @@ pub enum Request {
PlayingField,
}
#[derive(Debug)]
pub enum Response {
PlayingFieldView(PlayingFieldView),
}
fn disptach(state: Arc<RwLock<AppState>>, request: Request) -> Response {
fn dispatch(state: Arc<RwLock<AppState>>, request: Request) -> Response {
match request {
Request::PlayingField => Response::PlayingFieldView(playing_field()),
}
@ -45,8 +46,11 @@ impl CoreApp {
)
}
pub async fn run(&self) {
thread::sleep(Duration::from_secs(5));
unimplemented!()
pub async fn run(&mut self) {
loop {
let msg = self.request_rx.recv().await.unwrap();
let resp = dispatch(self.state.clone(), msg);
self.response_tx.send(resp).await.unwrap();
}
}
}

View File

@ -1,10 +1,14 @@
use gtk::prelude::*;
use kifu_core::CoreApp;
use tokio::runtime::Runtime;
use kifu_core::{CoreApp, Request, Response};
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use tokio::sync::mpsc::{Receiver, Sender};
#[tokio::main]
async fn main() {
let (core, _request_tx, _response_rx) = CoreApp::new();
let (mut core, request_tx, response_rx) = CoreApp::new();
let core_handle = tokio::spawn(async move {
core.run().await;
});
@ -14,9 +18,25 @@ async fn main() {
.application_id("com.luminescent-dreams.kifu-gtk")
.build();
app.connect_activate(|app| {
let _ = tokio::spawn({
let request_tx = request_tx.clone();
async move {
let _ = request_tx.send(Request::PlayingField).await;
}
});
app.connect_activate(move |app| {
let (gtk_tx, gtk_rx) =
gtk::glib::MainContext::channel::<Response>(gtk::glib::PRIORITY_DEFAULT);
let _ = tokio::task::spawn(connector(gtk_tx, response_rx));
let window = gtk::ApplicationWindow::new(app);
window.present();
gtk_rx.attach(None, |message| {
println!("message: {:?}", message);
Continue(true)
});
});
println!("running the gtk loop");
@ -26,3 +46,13 @@ async fn main() {
core_handle.await;
gtk_handle.await;
}
async fn connector(gtk_tx: gtk::glib::Sender<Response>, mut response_rx: Receiver<Response>) {
loop {
let msg = response_rx.recv().await;
match msg {
Some(msg) => gtk_tx.send(msg).unwrap(),
None => (),
}
}
}