From d86d10428b04b160dac08f839d96732e3be0ef30 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 20 Mar 2023 09:43:56 -0400 Subject: [PATCH] Try setting up the GTK loop --- kifu/kifu-gtk/src/main.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/kifu/kifu-gtk/src/main.rs b/kifu/kifu-gtk/src/main.rs index 6e9ba34..1970ce3 100644 --- a/kifu/kifu-gtk/src/main.rs +++ b/kifu/kifu-gtk/src/main.rs @@ -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::(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, mut response_rx: Receiver) { + loop { + let msg = response_rx.recv().await; + match msg { + Some(msg) => gtk_tx.send(msg).unwrap(), + None => (), + } + } +}