background thread sends ts as mvp
parent
14df9125ef
commit
8182f90783
61
src/gui.rs
61
src/gui.rs
|
|
@ -7,6 +7,7 @@ use iced_futures::backend::native::async_std::time::every;
|
|||
use handlebars::Handlebars;
|
||||
use serde_json::json;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::thread;
|
||||
|
||||
use crate::stream::OutputStream;
|
||||
use crate::config::GUI;
|
||||
|
|
@ -31,7 +32,9 @@ pub fn main(cfg: GUI, output_stream: Box<dyn OutputStream>) -> iced::Result {
|
|||
}
|
||||
|
||||
struct Main {
|
||||
ntfy: String,
|
||||
c: std::sync::mpsc::Receiver<String>,
|
||||
ntfy1: String,
|
||||
ntfy2: String,
|
||||
configuring: Option<Message>,
|
||||
inputs: Inputs,
|
||||
keys_newly_down: Vec<iced::keyboard::KeyCode>,
|
||||
|
|
@ -100,6 +103,18 @@ impl Main {
|
|||
}
|
||||
|
||||
fn exchange(&mut self) {
|
||||
self.exchange_send();
|
||||
self.exchange_recv();
|
||||
}
|
||||
|
||||
fn exchange_recv(&mut self) {
|
||||
match self.c.try_recv() {
|
||||
Ok(msg) => self.ntfy2 = msg,
|
||||
_ => {},
|
||||
};
|
||||
}
|
||||
|
||||
fn exchange_send(&mut self) {
|
||||
let mut s = vec![];
|
||||
for key_code in self.keys_newly_down.iter() {
|
||||
match self.key_code_to_string(key_code) {
|
||||
|
|
@ -170,8 +185,16 @@ impl Application for Main {
|
|||
type Executor = executor::Default;
|
||||
|
||||
fn new(flags: Self::Flags) -> (Self, Command<Message>) {
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
thread::spawn(move || {
|
||||
Feedback{
|
||||
c: sender,
|
||||
}.listen()
|
||||
});
|
||||
return (Self {
|
||||
ntfy: String::from(":wave:"),
|
||||
c: receiver,
|
||||
ntfy1: String::from(":wave:"),
|
||||
ntfy2: String::from(""),
|
||||
configuring: None,
|
||||
inputs: Inputs{
|
||||
stick: Stick {
|
||||
|
|
@ -223,7 +246,7 @@ impl Application for Main {
|
|||
Message::R => { self.inputs.r = key_code },
|
||||
_ => {},
|
||||
};
|
||||
self.ntfy = format!("{:?} => {:?}", key_code.clone(), self.configuring.as_ref().unwrap());
|
||||
self.ntfy1 = format!("{:?} => {:?}", key_code.clone(), self.configuring.as_ref().unwrap());
|
||||
self.configuring = None;
|
||||
},
|
||||
_ => {},
|
||||
|
|
@ -268,7 +291,7 @@ impl Application for Main {
|
|||
},
|
||||
_ => {
|
||||
self.configuring = Some(msg.clone());
|
||||
self.ntfy = format!("push a key to bind to {:?}", msg.clone());
|
||||
self.ntfy1 = format!("push a key to bind to {:?}", msg.clone());
|
||||
},
|
||||
}
|
||||
return Command::none();
|
||||
|
|
@ -296,10 +319,38 @@ impl Application for Main {
|
|||
button(text(controller_button_to_string(Message::Y, self.inputs.y))).on_press(Message::Y),
|
||||
button(text(controller_button_to_string(Message::L, self.inputs.l))).on_press(Message::L),
|
||||
button(text(controller_button_to_string(Message::R, self.inputs.r))).on_press(Message::R),
|
||||
text(self.ntfy.clone()).size(50),
|
||||
text(self.ntfy1.clone()).size(50),
|
||||
text(self.ntfy2.clone()).size(50),
|
||||
]
|
||||
.padding(20)
|
||||
.align_items(Alignment::Center)
|
||||
.into();
|
||||
}
|
||||
}
|
||||
|
||||
struct Feedback {
|
||||
c: std::sync::mpsc::Sender<String>,
|
||||
}
|
||||
|
||||
impl Feedback {
|
||||
fn listen(&mut self) {
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||
match self.read() {
|
||||
Some(msg) if msg.len() > 0 => self.write(msg),
|
||||
_ => {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn read(&mut self) -> Option<String> {
|
||||
return Some(String::from(format!("feedback.read: {:?}", SystemTime::now())));
|
||||
}
|
||||
|
||||
fn write(&mut self, msg: String) {
|
||||
match self.c.send(msg.clone()) {
|
||||
Ok(_) => {},
|
||||
Err(err) => eprintln!("feedback.listen() failed to display {}: {}", msg, err),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue