Browse Source

Add `Options::line_scroll_speed` and `scroll_zoom_speed` (#4532)

This lets integrations and user change how sensitive egui is to scroll
events
pull/4537/head
Emil Ernerfeldt 6 months ago
committed by GitHub
parent
commit
a98c42e317
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      crates/egui/src/context.rs
  2. 16
      crates/egui/src/input_state.rs
  3. 44
      crates/egui/src/memory.rs

1
crates/egui/src/context.rs

@ -442,6 +442,7 @@ impl ContextImpl {
new_raw_input,
viewport.repaint.requested_immediate_repaint_prev_frame(),
pixels_per_point,
&self.memory.options,
);
let screen_rect = viewport.input.screen_rect;

16
crates/egui/src/input_state.rs

@ -183,6 +183,7 @@ impl InputState {
mut new: RawInput,
requested_immediate_repaint_prev_frame: bool,
pixels_per_point: f32,
options: &crate::Options,
) -> Self {
crate::profile_function!();
@ -235,17 +236,7 @@ impl InputState {
} => {
let mut delta = match unit {
MouseWheelUnit::Point => *delta,
MouseWheelUnit::Line => {
// TODO(emilk): figure out why these constants need to be different on web and on native (winit).
let is_web = cfg!(target_arch = "wasm32");
let points_per_scroll_line = if is_web {
8.0
} else {
50.0 // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461
};
points_per_scroll_line * *delta
}
MouseWheelUnit::Line => options.line_scroll_speed * *delta,
MouseWheelUnit::Page => screen_rect.height() * *delta,
};
@ -319,7 +310,8 @@ impl InputState {
unprocessed_scroll_delta_for_zoom -= applied;
}
zoom_factor_delta *= (smooth_scroll_delta_for_zoom / 200.0).exp();
zoom_factor_delta *=
(options.scroll_zoom_speed * smooth_scroll_delta_for_zoom).exp();
}
}

44
crates/egui/src/memory.rs

@ -227,10 +227,26 @@ pub struct Options {
///
/// By default this is `true` in debug builds.
pub warn_on_id_clash: bool,
// ------------------------------
// Input:
/// Multiplier for the scroll speed when reported in [`crate::MouseWheelUnit::Line`]s.
pub line_scroll_speed: f32,
/// Controls the speed at which we zoom in when doing ctrl/cmd + scroll.
pub scroll_zoom_speed: f32,
}
impl Default for Options {
fn default() -> Self {
// TODO(emilk): figure out why these constants need to be different on web and on native (winit).
let is_web = cfg!(target_arch = "wasm32");
let line_scroll_speed = if is_web {
8.0
} else {
40.0 // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461
};
Self {
style: Default::default(),
zoom_factor: 1.0,
@ -240,6 +256,10 @@ impl Default for Options {
screen_reader: false,
preload_font_glyphs: true,
warn_on_id_clash: cfg!(debug_assertions),
// Input:
line_scroll_speed,
scroll_zoom_speed: 1.0 / 200.0,
}
}
}
@ -256,6 +276,9 @@ impl Options {
screen_reader: _, // needs to come from the integration
preload_font_glyphs: _,
warn_on_id_clash,
line_scroll_speed,
scroll_zoom_speed,
} = self;
use crate::Widget as _;
@ -292,6 +315,27 @@ impl Options {
});
});
CollapsingHeader::new("🖱 Input")
.default_open(false)
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Line scroll speed");
ui.add(
crate::DragValue::new(line_scroll_speed).clamp_range(0.0..=f32::INFINITY),
)
.on_hover_text("How many lines to scroll with each tick of the mouse wheel");
});
ui.horizontal(|ui| {
ui.label("Scroll zoom speed");
ui.add(
crate::DragValue::new(scroll_zoom_speed)
.clamp_range(0.0..=f32::INFINITY)
.speed(0.001),
)
.on_hover_text("How fast to zoom with ctrl/cmd + scroll");
});
});
ui.vertical_centered(|ui| crate::reset_button(ui, self, "Reset all"));
}
}

Loading…
Cancel
Save