-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4074f5
commit 092e9b1
Showing
6 changed files
with
177 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use egui_inbox::{AsRequestRepaint, RequestRepaintContext, UiInbox}; | ||
|
||
pub struct MyApplicationState { | ||
state: Option<String>, | ||
inbox: UiInbox<String>, | ||
repaint_rx: std::sync::mpsc::Receiver<()>, | ||
repaint_tx: std::sync::mpsc::Sender<()>, | ||
} | ||
|
||
impl AsRequestRepaint for MyApplicationState { | ||
fn as_request_repaint(&self) -> RequestRepaintContext { | ||
let repaint_tx = self.repaint_tx.clone(); | ||
RequestRepaintContext::from_callback(move || { | ||
repaint_tx.send(()).unwrap(); | ||
}) | ||
} | ||
} | ||
|
||
impl Default for MyApplicationState { | ||
fn default() -> Self { | ||
let (repaint_tx, repaint_rx) = std::sync::mpsc::channel(); | ||
Self { | ||
state: None, | ||
inbox: UiInbox::new(), | ||
repaint_rx, | ||
repaint_tx, | ||
} | ||
} | ||
} | ||
|
||
impl MyApplicationState { | ||
pub fn run(mut self) { | ||
let sender = self.inbox.sender(); | ||
std::thread::spawn(move || { | ||
let mut count = 0; | ||
loop { | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
count += 1; | ||
sender.send(format!("Count: {}", count)).ok(); | ||
} | ||
}); | ||
|
||
loop { | ||
self.inbox.read(&self).for_each(|msg| { | ||
self.state = Some(msg); | ||
}); | ||
|
||
println!("State: {:?}", self.state); | ||
|
||
self.repaint_rx.recv().unwrap(); | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
MyApplicationState::default().run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters