ソースを参照

wallet: use pubsub to subscribe to events in main async draw() fn

darkfi 2 年 前
コミット
08b134d802
3 ファイル変更39 行追加7 行削除
  1. 22 2
      bin/darkwallet/src/gfx2.rs
  2. 15 3
      bin/darkwallet/src/main.rs
  3. 2 2
      bin/darkwallet/src/pubsub.rs

+ 22 - 2
bin/darkwallet/src/gfx2.rs

@@ -24,6 +24,7 @@ use crate::{
     gfx::Rectangle,
     keysym::{KeyCodeAsStr, MouseButtonAsU8},
     prop::{Property, PropertySubType, PropertyType},
+    pubsub::Publisher,
     res::{ResourceId, ResourceManager},
     scene::{
         MethodResponseFn, SceneGraph, SceneGraphPtr, SceneNode, SceneNodeId, SceneNodeInfo,
@@ -188,6 +189,12 @@ pub enum GraphicsMethod {
     ReplaceDrawCall((Vec<usize>, DrawCall)),
 }
 
+#[derive(Debug, Clone)]
+pub enum GraphicsEvent {
+    KeyDown((KeyCode, KeyMods, bool)),
+    Resize((f32, f32)),
+}
+
 struct Stage {
     ctx: Box<dyn RenderingBackend>,
     pipeline: Pipeline,
@@ -196,11 +203,13 @@ struct Stage {
     last_draw_time: Option<Instant>,
 
     method_recvr: mpsc::Receiver<GraphicsMethod>,
+    event_pub: Arc<Publisher<GraphicsEvent>>,
 }
 
 impl Stage {
     pub fn new(
     method_recvr: mpsc::Receiver<GraphicsMethod>,
+    event_pub: Arc<Publisher<GraphicsEvent>>,
         ) -> Self {
         let mut ctx: Box<dyn RenderingBackend> = window::new_rendering_backend();
 
@@ -252,7 +261,8 @@ impl Stage {
                 dcs: vec![]
             },
             last_draw_time: None,
-            method_recvr
+            method_recvr,
+            event_pub,
         }
     }
 
@@ -362,10 +372,20 @@ impl EventHandler for Stage {
 
         self.ctx.commit_frame();
     }
+
+    fn key_down_event(&mut self, keycode: KeyCode, mods: KeyMods, repeat: bool) {
+        let event = GraphicsEvent::KeyDown((keycode, mods, repeat));
+        self.event_pub.notify_sync(event);
+    }
+    fn resize_event(&mut self, width: f32, height: f32) {
+        let event = GraphicsEvent::Resize((width, height));
+        self.event_pub.notify_sync(event);
+    }
 }
 
 pub fn run_gui(
     method_recvr: mpsc::Receiver<GraphicsMethod>,
+    event_pub: Arc<Publisher<GraphicsEvent>>,
     ) {
     #[cfg(target_os = "android")]
     {
@@ -399,6 +419,6 @@ pub fn run_gui(
     conf.platform.apple_gfx_api =
         if metal { conf::AppleGfxApi::Metal } else { conf::AppleGfxApi::OpenGl };
 
-    miniquad::start(conf, || Box::new(Stage::new(method_recvr)));
+    miniquad::start(conf, || Box::new(Stage::new(method_recvr, event_pub)));
 }
 

+ 15 - 3
bin/darkwallet/src/main.rs

@@ -67,7 +67,16 @@ fn start_sentinel(scene_graph: SceneGraphPtr) {
     });
 }
 
-async fn amain(ex: Arc<smol::Executor<'static>>, render_api: Arc<gfx2::RenderApi>) {
+async fn amain(ex: Arc<smol::Executor<'static>>, render_api: Arc<gfx2::RenderApi>,
+    event_sub: pubsub::Subscription<gfx2::GraphicsEvent>
+    ) {
+    let task = ex.spawn(async move {
+        loop {
+            let ev = event_sub.receive().await;
+            debug!("ev: {:?}", ev);
+        }
+    });
+
     let x1 = 0.1;
     let x2 = 0.6;
     let y1 = 0.1;
@@ -159,8 +168,11 @@ fn main() {
     let (method_sender, method_recvr) = mpsc::channel();
     let render_api = gfx2::RenderApi::new(method_sender);
 
+    let event_pub = pubsub::Publisher::new();
+    let event_sub = event_pub.clone().subscribe();
+
     let gfx_handle = thread::spawn(move || {
-        //gfx2::run_gui(method_recvr);
+        gfx2::run_gui(method_recvr, event_pub);
     });
 
     let n_threads = std::thread::available_parallelism().unwrap().get();
@@ -172,7 +184,7 @@ fn main() {
         // Run the main future on this thread
         .finish(|| {
             smol::future::block_on(async {
-                amain(ex.clone(), render_api).await;
+                amain(ex.clone(), render_api, event_sub).await;
                 drop(signal);
                 Ok::<(), Error>(())
             });

+ 2 - 2
bin/darkwallet/src/pubsub.rs

@@ -45,7 +45,7 @@ impl<T: Clone + Send + 'static> Publisher<T> {
         Arc::new(Self { subs: SkipMap::new() })
     }
 
-    pub async fn subscribe(self: Arc<Self>) -> Subscription<T> {
+    pub fn subscribe(self: Arc<Self>) -> Subscription<T> {
         let (sendr, recvr) = smol::channel::unbounded();
         let sub_id = OsRng.gen();
         // Optional to check whether this ID already exists.
@@ -78,7 +78,7 @@ impl<T: Clone + Send + 'static> Publisher<T> {
             if let Err(e) = sub.try_send(message_result.clone()) {
                 warn!(
                     target: "system::publisher",
-                    "[system::publisher] Error returned sending message in notify_with_exclude() call! {}", e,
+                    "[system::publisher] Error returned sending message in notify_with_exclude_sync() call! {}", e,
                 );
             }
         }