@@ -151,7 +151,7 @@ impl InputState {
151151
152152 fn mouse_input ( & mut self , button : & MouseButton , state : & ElementState ) -> ClickCount {
153153 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 )
155155 }
156156
157157 fn cef_modifiers ( & self , location : & winit:: keyboard:: KeyLocation , is_repeat : bool ) -> CefModifiers {
@@ -187,7 +187,7 @@ impl From<&mut InputState> for MouseEvent {
187187 }
188188}
189189
190- #[ derive( Default , Clone ) ]
190+ #[ derive( Default , Clone , Copy ) ]
191191pub ( crate ) struct MousePosition {
192192 x : usize ,
193193 y : usize ,
@@ -233,61 +233,64 @@ struct ClickTracker {
233233 right : Option < ClickRecord > ,
234234}
235235impl 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 {
237237 let record = match button {
238238 MouseButton :: Left => & mut self . left ,
239239 MouseButton :: Right => & mut self . right ,
240240 MouseButton :: Middle => & mut self . middle ,
241241 _ => return ClickCount :: Single ,
242242 } ;
243243
244- let now = Instant :: now ( ) ;
245-
246244 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 ( ) } ) ;
253246 return ClickCount :: Single ;
254247 } ;
255248
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+
256273 let dx = position. x . abs_diff ( record. position . x ) ;
257274 let dy = position. y . abs_diff ( record. position . y ) ;
258275 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 ;
260277
261278 let count = if within_time && within_dist { ClickCount :: Double } else { ClickCount :: Single } ;
262279
263280 * 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 } ,
276283 } ;
277284 count
278285 }
279286}
280287
281- #[ derive( Clone ) ]
288+ #[ derive( Clone , Copy , PartialEq , Default ) ]
282289enum ClickCount {
290+ #[ default]
283291 Single ,
284292 Double ,
285293}
286- impl Default for ClickCount {
287- fn default ( ) -> Self {
288- Self :: Single
289- }
290- }
291294impl From < ClickCount > for i32 {
292295 fn from ( count : ClickCount ) -> i32 {
293296 match count {
@@ -297,13 +300,25 @@ impl From<ClickCount> for i32 {
297300 }
298301}
299302
303+ #[ derive( Clone , Copy ) ]
300304struct ClickRecord {
301305 time : Instant ,
302306 position : MousePosition ,
303307 down_count : ClickCount ,
304308 up_count : ClickCount ,
305309}
306310
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+
307322struct CefModifiers ( u32 ) ;
308323impl CefModifiers {
309324 fn new ( input_state : & InputState , location : & winit:: keyboard:: KeyLocation , is_repeat : bool ) -> Self {
0 commit comments