Skip to main content

egui_pop_platform/
wheel_lines.rs

1//! The lines a notch of the wheel moves on the system, read in the process, to
2//! seed egui-pop's wheel speed: winit gives notches, and leaves the system's lines
3//! out.
4//!
5//! <div class="warning">Experimental: may change in any release
6//! (docs/stability.md).</div>
7//!
8//! | | Read from |
9//! |---|---|
10//! | Windows | `HKCU\Control Panel\Desktop` `WheelScrollLines`, where the system keeps what `SPI_GETWHEELSCROLLLINES` answers, through the registry API (winreg). "One screen at a time" (`-1`) is no number of lines: 3 |
11//! | Other systems | nothing: 3 |
12//!
13//! Give it to `AppBuilder::system_wheel_lines`, which makes it the default of the
14//! wheel speed until the user chooses one:
15//!
16//! ```no_run
17//! let app = egui_pop_api::App::builder()
18//!     .system_wheel_lines(egui_pop_platform::wheel_lines::read());
19//! ```
20
21use egui_pop_api::pointer::SYSTEM_WHEEL_LINES;
22
23/// The system's lines a notch, now; [`SYSTEM_WHEEL_LINES`] when it says none.
24pub fn read() -> u32 {
25    read_registry()
26        .as_deref()
27        .and_then(parse)
28        .unwrap_or(SYSTEM_WHEEL_LINES)
29}
30
31/// The registry's value as lines: `None` for what is no number of lines, as `-1`,
32/// one screen at a time.
33pub fn parse(value: &str) -> Option<u32> {
34    value.trim().parse().ok()
35}
36
37#[cfg(windows)]
38fn read_registry() -> Option<String> {
39    use winreg::RegKey;
40    use winreg::enums::HKEY_CURRENT_USER;
41    RegKey::predef(HKEY_CURRENT_USER)
42        .open_subkey("Control Panel\\Desktop")
43        .ok()?
44        .get_value::<String, _>("WheelScrollLines")
45        .ok()
46}
47
48#[cfg(not(windows))]
49fn read_registry() -> Option<String> {
50    None
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn lines_are_read_and_a_screen_is_none() {
59        assert_eq!(parse("3"), Some(3));
60        assert_eq!(parse(" 12 "), Some(12));
61        assert_eq!(parse("-1"), None);
62        assert_eq!(parse(""), None);
63    }
64}