@@ -45,8 +45,10 @@ use crate::{
45
45
/// Describes a generic event.
46
46
///
47
47
/// See the module-level docs for more information on the event loop manages each event.
48
+ ///
49
+ /// `T` is a user-defined custom event type.
48
50
#[ derive( Debug , PartialEq ) ]
49
- pub enum Event < ' a , T : ' static > {
51
+ pub enum Event < T : ' static > {
50
52
/// Emitted when new events arrive from the OS to be processed.
51
53
///
52
54
/// This event type is useful as a place to put code that should be done before you start
@@ -58,7 +60,7 @@ pub enum Event<'a, T: 'static> {
58
60
/// Emitted when the OS sends an event to a winit window.
59
61
WindowEvent {
60
62
window_id : WindowId ,
61
- event : WindowEvent < ' a > ,
63
+ event : WindowEvent ,
62
64
} ,
63
65
64
66
/// Emitted when the OS sends an event to a device.
@@ -114,8 +116,8 @@ pub enum Event<'a, T: 'static> {
114
116
LoopDestroyed ,
115
117
}
116
118
117
- impl < ' a , T > Event < ' a , T > {
118
- pub fn map_nonuser_event < U > ( self ) -> Result < Event < ' a , U > , Event < ' a , T > > {
119
+ impl < T > Event < T > {
120
+ pub fn map_nonuser_event < U > ( self ) -> Result < Event < U > , Event < T > > {
119
121
use self :: Event :: * ;
120
122
match self {
121
123
UserEvent ( _) => Err ( self ) ,
@@ -133,7 +135,8 @@ impl<'a, T> Event<'a, T> {
133
135
134
136
/// If the event doesn't contain a reference, turn it into an event with a `'static` lifetime.
135
137
/// Otherwise, return `None`.
136
- pub fn to_static ( self ) -> Option < Event < ' static , T > > {
138
+ // TODO: remove?
139
+ pub fn to_static ( self ) -> Option < Event < T > > {
137
140
use self :: Event :: * ;
138
141
match self {
139
142
WindowEvent { window_id, event } => event
@@ -180,7 +183,7 @@ pub enum StartCause {
180
183
181
184
/// Describes an event from a `Window`.
182
185
#[ derive( Debug , PartialEq ) ]
183
- pub enum WindowEvent < ' a > {
186
+ pub enum WindowEvent {
184
187
/// The size of the window has changed. Contains the client area's new dimensions.
185
188
Resized ( PhysicalSize < u32 > ) ,
186
189
@@ -307,7 +310,7 @@ pub enum WindowEvent<'a> {
307
310
/// For more information about DPI in general, see the [`dpi`](crate::dpi) module.
308
311
ScaleFactorChanged {
309
312
scale_factor : f64 ,
310
- new_inner_size : & ' a mut PhysicalSize < u32 > ,
313
+ new_inner_size : NewInnerSizeInteriorMutCoolThing ,
311
314
} ,
312
315
313
316
/// The system window theme has changed.
@@ -319,8 +322,89 @@ pub enum WindowEvent<'a> {
319
322
ThemeChanged ( Theme ) ,
320
323
}
321
324
322
- impl < ' a > WindowEvent < ' a > {
323
- pub fn to_static ( self ) -> Option < WindowEvent < ' static > > {
325
+ use std:: {
326
+ cell:: Cell ,
327
+ fmt:: Debug ,
328
+ sync:: { Arc , Mutex } ,
329
+ } ;
330
+
331
+ // TODO naming
332
+ #[ derive( Debug , Clone ) ]
333
+ pub struct NewInnerSizeInteriorMutCoolThing {
334
+ inner : Arc < Mutex < NewInnerSizeInteriorMutCoolThingInner > > ,
335
+ }
336
+
337
+ #[ derive( Debug , Clone ) ]
338
+ struct NewInnerSizeInteriorMutCoolThingInner {
339
+ /// TODO doc this uses interior mutability in `set_inner_size`!
340
+ new_inner_size : Cell < PhysicalSize < u32 > > ,
341
+
342
+ /// If the event that this originated from was handled, this struct is essential read-only. Once this field is `true`, no further calls to `set_inner_size` will succeed.
343
+ is_readonly : bool ,
344
+ }
345
+
346
+ pub struct NewInnerSizeInteriorMutCoolThingIsReadonlyNowError ;
347
+
348
+ impl Debug for NewInnerSizeInteriorMutCoolThingIsReadonlyNowError {
349
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
350
+ write ! ( f, "todo error message, but this new_inner_size cell is being mutated after winit has consumed it, so there will be no effect on the window" )
351
+ }
352
+ }
353
+
354
+ /// Manually implement `PartialEq` to treat `Self` like just a `PhysicalSize<u32>`.
355
+ ///
356
+ /// Ignores the `Mutex` (i.e. unconditionally locks).
357
+ impl PartialEq < NewInnerSizeInteriorMutCoolThing > for NewInnerSizeInteriorMutCoolThing {
358
+ /// Note: will deadlock when `other` == `self`!
359
+ fn eq ( & self , other : & NewInnerSizeInteriorMutCoolThing ) -> bool {
360
+ // Note: ignoring `is_readonly` flag!
361
+ self . inner . lock ( ) . unwrap ( ) . new_inner_size == other. inner . lock ( ) . unwrap ( ) . new_inner_size
362
+ }
363
+ }
364
+
365
+ impl From < NewInnerSizeInteriorMutCoolThing > for PhysicalSize < u32 > {
366
+ fn from ( new_inner_size : NewInnerSizeInteriorMutCoolThing ) -> Self {
367
+ new_inner_size. inner_size ( )
368
+ }
369
+ }
370
+
371
+ impl NewInnerSizeInteriorMutCoolThing {
372
+ pub fn new ( new_inner_size : PhysicalSize < u32 > ) -> Self {
373
+ Self {
374
+ inner : Arc :: new ( Mutex :: new ( NewInnerSizeInteriorMutCoolThingInner {
375
+ new_inner_size : Cell :: new ( new_inner_size) ,
376
+ is_readonly : false ,
377
+ } ) ) ,
378
+ }
379
+ }
380
+
381
+ // todo: doc that this needs to be called after the winit event loop processes the containing event and looks at the resulting `inner_size`.
382
+ pub ( crate ) fn mark_new_inner_size_consumed ( self ) {
383
+ self . inner . lock ( ) . unwrap ( ) . is_readonly = true ;
384
+ }
385
+
386
+ pub fn inner_size ( & self ) -> PhysicalSize < u32 > {
387
+ self . inner . lock ( ) . unwrap ( ) . new_inner_size . get ( )
388
+ }
389
+
390
+ pub fn set_inner_size (
391
+ & self ,
392
+ new_size : PhysicalSize < u32 > ,
393
+ ) -> Result < ( ) , NewInnerSizeInteriorMutCoolThingIsReadonlyNowError > {
394
+ let a = self . inner . lock ( ) . unwrap ( ) ;
395
+ if a. is_readonly {
396
+ Err ( NewInnerSizeInteriorMutCoolThingIsReadonlyNowError )
397
+ } else {
398
+ // Only one borrow guarenteed by `Mutex`.
399
+ a. new_inner_size . set ( new_size) ;
400
+ Ok ( ( ) )
401
+ }
402
+ }
403
+ }
404
+
405
+ impl WindowEvent {
406
+ // TODO remove?
407
+ pub fn to_static ( self ) -> Option < WindowEvent > {
324
408
use self :: WindowEvent :: * ;
325
409
match self {
326
410
Resized ( size) => Some ( Resized ( size) ) ,
0 commit comments