@@ -7,8 +7,17 @@ use winit::platform::desktop::EventLoopExtDesktop;
77#[ cfg( target_os = "windows" ) ]
88use winit:: platform:: windows:: EventLoopExtWindows ;
99
10+ #[ derive( PartialEq , Eq ) ]
1011pub enum LoopResult {
12+ /// Continue the loop. The next process is executed immediately.
1113 Continue ,
14+
15+ /// Continue the loop. The next process is executed when a window event occurs.
16+ /// (e.g., moving the mouse over a window)
17+ /// Use when the process to be implemented is high load.
18+ ContinueOnWindowEvent ,
19+
20+ /// End the loop.
1221 Exit ,
1322}
1423
@@ -18,14 +27,20 @@ pub fn run_window_app<
1827 FnMain : FnMut ( & Window , & Context ) -> LoopResult ,
1928 FnFinalize : FnOnce ( & Window , & Context ) ,
2029> (
30+ client_size : ( u32 , u32 ) ,
2131 fn_initialize : FnInit ,
2232 mut fn_main : FnMain ,
2333 fn_finalize : FnFinalize ,
2434 fn_unity_plugin_load : fn ( interfaces : & unity_native_plugin:: interface:: UnityInterfaces ) ,
2535 fn_unity_plugin_unload : fn ( ) ,
2636) {
2737 let mut event_loop = EventLoop :: < u32 > :: new_any_thread ( ) ;
28- let window = WindowBuilder :: new ( ) . build ( & event_loop) . unwrap ( ) ;
38+ let window = WindowBuilder :: new ( )
39+ . with_inner_size ( winit:: dpi:: Size :: from (
40+ winit:: dpi:: PhysicalSize :: < u32 > :: from ( client_size) ,
41+ ) )
42+ . build ( & event_loop)
43+ . unwrap ( ) ;
2944
3045 let context = std:: rc:: Rc :: new ( fn_initialize ( & window) ) ;
3146 unsafe {
@@ -35,23 +50,31 @@ pub fn run_window_app<
3550
3651 fn_unity_plugin_load ( unity_native_plugin:: interface:: UnityInterfaces :: get ( ) ) ;
3752
53+ let mut last_result = LoopResult :: Continue ;
3854 event_loop. run_return ( |event, _, control_flow| {
55+ let instant = std:: time:: Instant :: now ( ) ;
3956 match event {
40- Event :: WindowEvent { window_id, event} => {
57+ Event :: WindowEvent { window_id, event } => {
4158 if window_id == window. id ( ) {
4259 match event {
43- WindowEvent :: CloseRequested => * control_flow = ControlFlow :: Exit ,
60+ WindowEvent :: CloseRequested => last_result = LoopResult :: Exit ,
4461 _ => {
45- * control_flow = match fn_main ( & window, context. deref ( ) ) {
46- LoopResult :: Continue => ControlFlow :: Wait ,
47- _ => ControlFlow :: Exit ,
48- } ;
62+ last_result = fn_main ( & window, context. deref ( ) ) ;
4963 }
5064 }
5165 }
5266 }
53- _ => ( ) ,
67+ _ => {
68+ if last_result == LoopResult :: Continue {
69+ last_result = fn_main ( & window, context. deref ( ) ) ;
70+ }
71+ } ,
5472 }
73+ * control_flow = match last_result {
74+ LoopResult :: Continue => ControlFlow :: WaitUntil ( instant + std:: time:: Duration :: from_millis ( 50 ) ) ,
75+ LoopResult :: ContinueOnWindowEvent => ControlFlow :: Wait ,
76+ _ => ControlFlow :: Exit ,
77+ } ;
5578 } ) ;
5679
5780 fn_unity_plugin_unload ( ) ;
0 commit comments