Dealing with async and things that should only run once #4415
Replies: 2 comments
-
Usually you would model this like a state machine. You have to add fields to your app's struct that keep track of the current state, and then draw the UI accordingly. For example, this button from the demo app disables the UI for 2 seconds and then enables it again, by setting a field to the time at which the button was clicked, and then disabling the UI if it has been less than 2 seconds since that time. egui/crates/egui_demo_lib/src/demo/window_options.rs Lines 146 to 148 in 0bc59f5 egui/crates/egui_demo_lib/src/demo/window_options.rs Lines 58 to 61 in 0bc59f5 In your case, you would either spawn a thread or something like a tokio task to do the operation in the background without blocking the UI, and give it some way to communicate progress information and results back to the main thread (like a mutex or an mpsc channel), then store the handle in a field of your struct so you know something is in progress while you're drawing the UI. I have this app that downloads images asynchronously, if you want to take a look: https://gitlab.com/ygor.souza/xkcd-viewer. The image part is a bit outdated since it targets an old version of egui, but the basic state management logic is mostly independent of egui. This is one way to do it, but there are others. You may also want to take a look at the http example from the demo app: https://www.egui.rs/#Http. |
Beta Was this translation helpful? Give feedback.
-
This is great, thank you!
…On Sat, Apr 27, 2024, 22:13 YgorSouza ***@***.***> wrote:
Usually you would model this like a state machine. You have to add fields
to your app's struct that keep track of the current state, and then draw
the UI accordingly. For example, this button from the demo app disables the
UI for 2 seconds and then enables it again, by setting a field to the time
at which the button was clicked, and then disabling the UI if it has been
less than 2 seconds since that time.
https://github.com/emilk/egui/blob/0bc59f578b907e357f118dba362d89d5e36f4c31/crates/egui_demo_lib/src/demo/window_options.rs#L146-L148
https://github.com/emilk/egui/blob/0bc59f578b907e357f118dba362d89d5e36f4c31/crates/egui_demo_lib/src/demo/window_options.rs#L58-L61
In your case, you would either spawn a thread or something like a tokio
task to do the operation in the background without blocking the UI, and
give it some way to communicate progress information and results back to
the main thread (like a mutex or an mpsc channel), then store the handle in
a field of your struct so you know something is in progress while you're
drawing the UI. I have this app that downloads images asynchronously, if
you want to take a look: https://gitlab.com/ygor.souza/xkcd-viewer. The
image part is a bit outdated since it targets an old version of egui, but
the basic state management logic is mostly independent of egui. This is one
way to do it, but there are others. You may also want to take a look at the
http example from the demo app: https://www.egui.rs/#Http.
—
Reply to this email directly, view it on GitHub
<#4415 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKHUEXW64CKVZI66G3PRBVLY7QBIBAVCNFSM6AAAAABGZWNXNWVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TENBXG44TG>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hello, I am trying to make a simple updater for another app using egui with eframe, but I have absolutely no clue how to approach this.
It'll automatically do an update step-by-step (while showing the user what step it's at with a progress bar), so for example, it would do something like:
The thing I am confused about the most is how to deal with things that should only run once, like a function to download the latest version of the app I want to update. If I were to call that in the same place where the UI gets drawn, it would be called every single frame, which is definitely not good!
Another issue is that I'll also have to use async functions and I don't know how to make these work with the eframe/egui app, which doesn't use async.
How could I go about doing this?
Beta Was this translation helpful? Give feedback.
All reactions