@@ -151,7 +151,7 @@ impl InputState {
151
151
152
152
fn mouse_input ( & mut self , button : & MouseButton , state : & ElementState ) -> ClickCount {
153
153
self . mouse_state . update ( button, state) ;
154
- self . mouse_click_tracker . input ( button, state, & self . mouse_position )
154
+ self . mouse_click_tracker . input ( button, state, self . mouse_position )
155
155
}
156
156
157
157
fn cef_modifiers ( & self , location : & winit:: keyboard:: KeyLocation , is_repeat : bool ) -> CefModifiers {
@@ -187,7 +187,7 @@ impl From<&mut InputState> for MouseEvent {
187
187
}
188
188
}
189
189
190
- #[ derive( Default , Clone ) ]
190
+ #[ derive( Default , Clone , Copy ) ]
191
191
pub ( crate ) struct MousePosition {
192
192
x : usize ,
193
193
y : usize ,
@@ -233,61 +233,64 @@ struct ClickTracker {
233
233
right : Option < ClickRecord > ,
234
234
}
235
235
impl ClickTracker {
236
- fn input ( & mut self , button : & MouseButton , state : & ElementState , position : & MousePosition ) -> ClickCount {
236
+ fn input ( & mut self , button : & MouseButton , state : & ElementState , position : MousePosition ) -> ClickCount {
237
237
let record = match button {
238
238
MouseButton :: Left => & mut self . left ,
239
239
MouseButton :: Right => & mut self . right ,
240
240
MouseButton :: Middle => & mut self . middle ,
241
241
_ => return ClickCount :: Single ,
242
242
} ;
243
243
244
- let now = Instant :: now ( ) ;
245
-
246
244
let Some ( record) = record else {
247
- * record = Some ( ClickRecord {
248
- time : now,
249
- position : position. clone ( ) ,
250
- down_count : ClickCount :: Single ,
251
- up_count : ClickCount :: Single ,
252
- } ) ;
245
+ * record = Some ( ClickRecord { position, ..Default :: default ( ) } ) ;
253
246
return ClickCount :: Single ;
254
247
} ;
255
248
249
+ let previous = record. time ;
250
+
251
+ let now = Instant :: now ( ) ;
252
+ record. time = now;
253
+ record. position = position;
254
+
255
+ match state {
256
+ ElementState :: Pressed if record. down_count == ClickCount :: Double => {
257
+ * record = ClickRecord {
258
+ down_count : ClickCount :: Single ,
259
+ ..* record
260
+ } ;
261
+ return ClickCount :: Single ;
262
+ }
263
+ ElementState :: Released if record. up_count == ClickCount :: Double => {
264
+ * record = ClickRecord {
265
+ up_count : ClickCount :: Single ,
266
+ ..* record
267
+ } ;
268
+ return ClickCount :: Single ;
269
+ }
270
+ _ => { }
271
+ }
272
+
256
273
let dx = position. x . abs_diff ( record. position . x ) ;
257
274
let dy = position. y . abs_diff ( record. position . y ) ;
258
275
let within_dist = dx <= MULTICLICK_ALLOWED_TRAVEL && dy <= MULTICLICK_ALLOWED_TRAVEL ;
259
- let within_time = now. saturating_duration_since ( record . time ) <= MULTICLICK_TIMEOUT ;
276
+ let within_time = now. saturating_duration_since ( previous ) <= MULTICLICK_TIMEOUT ;
260
277
261
278
let count = if within_time && within_dist { ClickCount :: Double } else { ClickCount :: Single } ;
262
279
263
280
* record = match state {
264
- ElementState :: Pressed => ClickRecord {
265
- time : now,
266
- position : position. clone ( ) ,
267
- down_count : count. clone ( ) ,
268
- up_count : record. up_count . clone ( ) ,
269
- } ,
270
- ElementState :: Released => ClickRecord {
271
- time : now,
272
- position : position. clone ( ) ,
273
- down_count : record. down_count . clone ( ) ,
274
- up_count : count. clone ( ) ,
275
- } ,
281
+ ElementState :: Pressed => ClickRecord { down_count : count, ..* record } ,
282
+ ElementState :: Released => ClickRecord { up_count : count, ..* record } ,
276
283
} ;
277
284
count
278
285
}
279
286
}
280
287
281
- #[ derive( Clone ) ]
288
+ #[ derive( Clone , Copy , PartialEq , Default ) ]
282
289
enum ClickCount {
290
+ #[ default]
283
291
Single ,
284
292
Double ,
285
293
}
286
- impl Default for ClickCount {
287
- fn default ( ) -> Self {
288
- Self :: Single
289
- }
290
- }
291
294
impl From < ClickCount > for i32 {
292
295
fn from ( count : ClickCount ) -> i32 {
293
296
match count {
@@ -297,13 +300,25 @@ impl From<ClickCount> for i32 {
297
300
}
298
301
}
299
302
303
+ #[ derive( Clone , Copy ) ]
300
304
struct ClickRecord {
301
305
time : Instant ,
302
306
position : MousePosition ,
303
307
down_count : ClickCount ,
304
308
up_count : ClickCount ,
305
309
}
306
310
311
+ impl Default for ClickRecord {
312
+ fn default ( ) -> Self {
313
+ Self {
314
+ time : Instant :: now ( ) ,
315
+ position : Default :: default ( ) ,
316
+ down_count : Default :: default ( ) ,
317
+ up_count : Default :: default ( ) ,
318
+ }
319
+ }
320
+ }
321
+
307
322
struct CefModifiers ( u32 ) ;
308
323
impl CefModifiers {
309
324
fn new ( input_state : & InputState , location : & winit:: keyboard:: KeyLocation , is_repeat : bool ) -> Self {
0 commit comments