From 7d70fcf418a4a835ae03e84fa917ec2b01b09bf5 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 22 Apr 2024 11:45:29 +0200 Subject: [PATCH] lib: Support frontend creation with optional root widget --- src/ui.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index 75558a4f..1038ddd0 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -13,7 +13,7 @@ use std::time::Duration; use termion::raw::RawTerminal; use tokio::sync::broadcast; -use tokio::sync::mpsc::{self, UnboundedReceiver}; +use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; use super::event::Event; use super::store::State; @@ -32,14 +32,21 @@ pub struct Frontend { } impl Frontend { - pub fn new() -> (Self, UnboundedReceiver) { + pub fn new() -> (Self, UnboundedSender, UnboundedReceiver) { let (action_tx, action_rx) = mpsc::unbounded_channel(); - (Self { action_tx }, action_rx) + ( + Self { + action_tx: action_tx.clone(), + }, + action_tx, + action_rx, + ) } pub async fn main_loop( self, + root: Option, mut state_rx: UnboundedReceiver, mut interrupt_rx: broadcast::Receiver>, ) -> anyhow::Result> @@ -56,7 +63,13 @@ impl Frontend { let mut root = { let state = state_rx.recv().await.unwrap(); - W::new(&state, self.action_tx.clone()) + match root { + Some(mut root) => { + root.update(&state); + root + } + None => W::new(&state, self.action_tx.clone()), + } }; let result: anyhow::Result> = loop {