Skip to content

Commit 77d7c32

Browse files
Review improvements
1 parent b9ae757 commit 77d7c32

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

desktop/src/cef/consts.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1+
use std::time::Duration;
2+
13
pub(crate) const RESOURCE_SCHEME: &str = "resources";
24
pub(crate) const RESOURCE_DOMAIN: &str = "resources";
5+
6+
pub(crate) const SCROLL_LINE_HEIGHT: usize = 40;
7+
pub(crate) const SCROLL_LINE_WIDTH: usize = 40;
8+
pub(crate) const SCROLL_SPEED_X: f32 = 3.0;
9+
pub(crate) const SCROLL_SPEED_Y: f32 = 3.0;
10+
11+
pub(crate) const MULTICLICK_TIMEOUT: Duration = Duration::from_millis(500);
12+
pub(crate) const MULTICLICK_ALLOWED_TRAVEL: usize = 4;

desktop/src/cef/input.rs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use cef::sys::{cef_event_flags_t, cef_key_event_type_t, cef_mouse_button_type_t};
22
use cef::{Browser, ImplBrowser, ImplBrowserHost, KeyEvent, KeyEventType, MouseEvent};
3-
use std::time::{Duration, Instant};
3+
use std::time::Instant;
44
use winit::dpi::PhysicalPosition;
55
use winit::event::{ElementState, MouseButton, MouseScrollDelta, WindowEvent};
66

77
mod keymap;
88
use keymap::{ToNativeKeycode, ToVKBits};
99

10+
use super::consts::{MULTICLICK_ALLOWED_TRAVEL, MULTICLICK_TIMEOUT, SCROLL_LINE_HEIGHT, SCROLL_LINE_WIDTH, SCROLL_SPEED_X, SCROLL_SPEED_Y};
11+
1012
pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputState, event: &WindowEvent) {
1113
match event {
1214
WindowEvent::CursorMoved { position, .. } => {
@@ -15,8 +17,6 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
1517
let Some(host) = browser.host() else {
1618
return;
1719
};
18-
19-
host.set_focus(1);
2020
host.send_mouse_move_event(Some(&input_state.into()), 0);
2121
}
2222
WindowEvent::MouseInput { state, button, .. } => {
@@ -35,36 +35,26 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
3535
let Some(host) = browser.host() else {
3636
return;
3737
};
38-
39-
host.set_focus(1);
4038
host.send_mouse_click_event(Some(&input_state.into()), cef_button, cef_mouse_up, cef_click_count);
4139
}
4240
WindowEvent::MouseWheel { delta, phase: _, device_id: _, .. } => {
43-
let Some(host) = browser.host() else {
44-
return;
45-
};
46-
47-
host.set_focus(1);
48-
4941
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),
5444
MouseScrollDelta::PixelDelta(physical_position) => (physical_position.x as f32, physical_position.y as f32),
5545
};
46+
delta_x *= SCROLL_SPEED_X;
47+
delta_y *= SCROLL_SPEED_Y;
48+
49+
let Some(host) = browser.host() else {
50+
return;
51+
};
5652
host.send_mouse_wheel_event(Some(&mouse_event), delta_x as i32, delta_y as i32);
5753
}
5854
WindowEvent::ModifiersChanged(modifiers) => {
5955
input_state.modifiers_changed(&modifiers.state());
6056
}
6157
WindowEvent::KeyboardInput { device_id: _, event, is_synthetic: _ } => {
62-
let Some(host) = browser.host() else {
63-
return;
64-
};
65-
66-
host.set_focus(1);
67-
6858
let (named_key, character) = match &event.logical_key {
6959
winit::keyboard::Key::Named(named_key) => (
7060
Some(named_key),
@@ -99,6 +89,10 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
9989

10090
key_event.native_key_code = native_key_code;
10191

92+
let Some(host) = browser.host() else {
93+
return;
94+
};
95+
10296
match event.state {
10397
ElementState::Pressed => {
10498
key_event.type_ = KeyEventType::from(cef_key_event_type_t::KEYEVENT_RAWKEYDOWN);
@@ -234,9 +228,6 @@ struct ClickTracker {
234228
}
235229
impl ClickTracker {
236230
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-
240231
let record = match button {
241232
MouseButton::Left => &mut self.left,
242233
MouseButton::Right => &mut self.right,
@@ -258,8 +249,8 @@ impl ClickTracker {
258249

259250
let dx = position.x.abs_diff(record.position.x);
260251
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;
263254

264255
let count = if within_time && within_dist { ClickCount::Double } else { ClickCount::Single };
265256

0 commit comments

Comments
 (0)