@@ -4,6 +4,49 @@ use crate::{app, color::*, containers::*, demos::*, paint::*, widgets::*, *};
4
4
5
5
// ----------------------------------------------------------------------------
6
6
7
+ /// How often we repaint the demo app by default
8
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
9
+ enum RunMode {
10
+ /// This is the default for the demo.
11
+ ///
12
+ /// If this is selected, Egui is only updated if are input events
13
+ /// (like mouse movements) or there are some animations in the GUI.
14
+ ///
15
+ /// Reactive mode saves CPU.
16
+ ///
17
+ /// The downside is that the UI can become out-of-date if something it is supposed to monitor changes.
18
+ /// For instance, a GUI for a thermostat need to repaint each time the temperature changes.
19
+ /// To ensure the UI is up to date you need to call `egui::Context::request_repaint()` each
20
+ /// time such an event happens. You can also chose to call `request_repaint()` once every second
21
+ /// or after every single frame - this is called `Continuous` mode,
22
+ /// and for games and interactive tools that need repainting every frame anyway, this should be the default.
23
+ Reactive ,
24
+
25
+ /// This will call `egui::Context::request_repaint()` at the end of each frame
26
+ /// to request the backend to repaint as soon as possible.
27
+ ///
28
+ /// On most platforms this will mean that Egui will run at the display refresh rate of e.g. 60 Hz.
29
+ ///
30
+ /// For this demo it is not any reason to do so except to
31
+ /// demonstrate how quickly Egui runs.
32
+ ///
33
+ /// For games or other interactive apps, this is probably what you want to do.
34
+ /// It will guarantee that Egui is always up-to-date.
35
+ Continuous ,
36
+ }
37
+
38
+ /// Default for demo is Reactive since
39
+ /// 1) We want to use minimal CPU
40
+ /// 2) There are no external events that could invalidate the UI
41
+ /// so there are no events to miss.
42
+ impl Default for RunMode {
43
+ fn default ( ) -> Self {
44
+ RunMode :: Reactive
45
+ }
46
+ }
47
+
48
+ // ----------------------------------------------------------------------------
49
+
7
50
/// Demonstrates how to make an app using Egui.
8
51
///
9
52
/// Implements `egui::app::App` so it can be used with
@@ -12,6 +55,8 @@ use crate::{app, color::*, containers::*, demos::*, paint::*, widgets::*, *};
12
55
#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
13
56
#[ cfg_attr( feature = "serde" , serde( default ) ) ]
14
57
pub struct DemoApp {
58
+ #[ serde( skip) ] // go back to `Continuous` mode each time we start
59
+ run_mode : RunMode ,
15
60
previous_web_location_hash : String ,
16
61
open_windows : OpenWindows ,
17
62
demo_window : DemoWindow ,
@@ -172,16 +217,15 @@ impl DemoApp {
172
217
ui. separator ( ) ;
173
218
174
219
ui. horizontal ( |ui| {
175
- let mut run_mode = backend . run_mode ( ) ;
220
+ let run_mode = & mut self . run_mode ;
176
221
ui. label ( "Run mode:" ) ;
177
- ui. radio_value ( "Continuous" , & mut run_mode, app :: RunMode :: Continuous )
222
+ ui. radio_value ( "Continuous" , run_mode, RunMode :: Continuous )
178
223
. tooltip_text ( "Repaint everything each frame" ) ;
179
- ui. radio_value ( "Reactive" , & mut run_mode, app :: RunMode :: Reactive )
224
+ ui. radio_value ( "Reactive" , run_mode, RunMode :: Reactive )
180
225
. tooltip_text ( "Repaint when there are animations or input (e.g. mouse movement)" ) ;
181
- backend. set_run_mode ( run_mode) ;
182
226
} ) ;
183
227
184
- if backend . run_mode ( ) == app :: RunMode :: Continuous {
228
+ if self . run_mode == RunMode :: Continuous {
185
229
ui. add (
186
230
label ! ( "Repainting the UI each frame. FPS: {:.1}" , backend. fps( ) )
187
231
. text_style ( TextStyle :: Monospace ) ,
@@ -232,6 +276,11 @@ impl app::App for DemoApp {
232
276
. map ( |info| info. web_location_hash . as_str ( ) )
233
277
. unwrap_or_default ( ) ;
234
278
self . ui ( ui, web_location_hash) ;
279
+
280
+ if self . run_mode == RunMode :: Continuous {
281
+ // Tell the backend to repaint as soon as possible
282
+ ui. ctx ( ) . request_repaint ( ) ;
283
+ }
235
284
}
236
285
237
286
#[ cfg( feature = "serde_json" ) ]
0 commit comments