1
1
use cef:: sys:: { cef_event_flags_t, cef_key_event_type_t, cef_mouse_button_type_t} ;
2
2
use cef:: { Browser , ImplBrowser , ImplBrowserHost , KeyEvent , KeyEventType , MouseEvent } ;
3
- use std:: time:: { Duration , Instant } ;
3
+ use std:: time:: Instant ;
4
4
use winit:: dpi:: PhysicalPosition ;
5
5
use winit:: event:: { ElementState , MouseButton , MouseScrollDelta , WindowEvent } ;
6
6
7
7
mod keymap;
8
8
use keymap:: { ToNativeKeycode , ToVKBits } ;
9
9
10
+ use super :: consts:: { MULTICLICK_ALLOWED_TRAVEL , MULTICLICK_TIMEOUT , SCROLL_LINE_HEIGHT , SCROLL_LINE_WIDTH , SCROLL_SPEED_X , SCROLL_SPEED_Y } ;
11
+
10
12
pub ( crate ) fn handle_window_event ( browser : & Browser , input_state : & mut InputState , event : & WindowEvent ) {
11
13
match event {
12
14
WindowEvent :: CursorMoved { position, .. } => {
@@ -15,8 +17,6 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
15
17
let Some ( host) = browser. host ( ) else {
16
18
return ;
17
19
} ;
18
-
19
- host. set_focus ( 1 ) ;
20
20
host. send_mouse_move_event ( Some ( & input_state. into ( ) ) , 0 ) ;
21
21
}
22
22
WindowEvent :: MouseInput { state, button, .. } => {
@@ -35,36 +35,26 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
35
35
let Some ( host) = browser. host ( ) else {
36
36
return ;
37
37
} ;
38
-
39
- host. set_focus ( 1 ) ;
40
38
host. send_mouse_click_event ( Some ( & input_state. into ( ) ) , cef_button, cef_mouse_up, cef_click_count) ;
41
39
}
42
40
WindowEvent :: MouseWheel { delta, phase : _, device_id : _, .. } => {
43
- let Some ( host) = browser. host ( ) else {
44
- return ;
45
- } ;
46
-
47
- host. set_focus ( 1 ) ;
48
-
49
41
let mouse_event = input_state. into ( ) ;
50
- let line_width = 40 ; //feels about right, TODO: replace with correct value
51
- let line_height = 30 ; //feels about right, TODO: replace with correct value
52
- let ( delta_x, delta_y) = match delta {
53
- MouseScrollDelta :: LineDelta ( x, y) => ( x * line_width as f32 , y * line_height as f32 ) ,
42
+ let ( mut delta_x, mut delta_y) = match delta {
43
+ MouseScrollDelta :: LineDelta ( x, y) => ( x * SCROLL_LINE_WIDTH as f32 , y * SCROLL_LINE_HEIGHT as f32 ) ,
54
44
MouseScrollDelta :: PixelDelta ( physical_position) => ( physical_position. x as f32 , physical_position. y as f32 ) ,
55
45
} ;
46
+ delta_x *= SCROLL_SPEED_X ;
47
+ delta_y *= SCROLL_SPEED_Y ;
48
+
49
+ let Some ( host) = browser. host ( ) else {
50
+ return ;
51
+ } ;
56
52
host. send_mouse_wheel_event ( Some ( & mouse_event) , delta_x as i32 , delta_y as i32 ) ;
57
53
}
58
54
WindowEvent :: ModifiersChanged ( modifiers) => {
59
55
input_state. modifiers_changed ( & modifiers. state ( ) ) ;
60
56
}
61
57
WindowEvent :: KeyboardInput { device_id : _, event, is_synthetic : _ } => {
62
- let Some ( host) = browser. host ( ) else {
63
- return ;
64
- } ;
65
-
66
- host. set_focus ( 1 ) ;
67
-
68
58
let ( named_key, character) = match & event. logical_key {
69
59
winit:: keyboard:: Key :: Named ( named_key) => (
70
60
Some ( named_key) ,
@@ -99,6 +89,10 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
99
89
100
90
key_event. native_key_code = native_key_code;
101
91
92
+ let Some ( host) = browser. host ( ) else {
93
+ return ;
94
+ } ;
95
+
102
96
match event. state {
103
97
ElementState :: Pressed => {
104
98
key_event. type_ = KeyEventType :: from ( cef_key_event_type_t:: KEYEVENT_RAWKEYDOWN ) ;
@@ -234,9 +228,6 @@ struct ClickTracker {
234
228
}
235
229
impl ClickTracker {
236
230
fn input ( & mut self , button : & MouseButton , state : & ElementState , position : & MousePosition ) -> ClickCount {
237
- const ALLOWABLE_MULTICLICK_SLOP : usize = 4 ;
238
- const ALLOWABLE_MULTICLICK_TIME : Duration = Duration :: from_millis ( 500 ) ;
239
-
240
231
let record = match button {
241
232
MouseButton :: Left => & mut self . left ,
242
233
MouseButton :: Right => & mut self . right ,
@@ -258,8 +249,8 @@ impl ClickTracker {
258
249
259
250
let dx = position. x . abs_diff ( record. position . x ) ;
260
251
let dy = position. y . abs_diff ( record. position . y ) ;
261
- let within_dist = dx <= ALLOWABLE_MULTICLICK_SLOP && dy <= ALLOWABLE_MULTICLICK_SLOP ;
262
- let within_time = now. saturating_duration_since ( record. time ) <= ALLOWABLE_MULTICLICK_TIME ;
252
+ let within_dist = dx <= MULTICLICK_ALLOWED_TRAVEL && dy <= MULTICLICK_ALLOWED_TRAVEL ;
253
+ let within_time = now. saturating_duration_since ( record. time ) <= MULTICLICK_TIMEOUT ;
263
254
264
255
let count = if within_time && within_dist { ClickCount :: Double } else { ClickCount :: Single } ;
265
256
0 commit comments