Skip to content

Commit

Permalink
Add Options::line_scroll_speed and scroll_zoom_speed (emilk#4532)
Browse files Browse the repository at this point in the history
This lets integrations and user change how sensitive egui is to scroll
events
  • Loading branch information
emilk authored and hacknus committed Oct 30, 2024
1 parent 4c8e6f0 commit 6095196
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 4 additions & 12 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();

Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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();
}
}

Expand Down
44 changes: 44 additions & 0 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}
}
Expand All @@ -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 _;
Expand Down Expand Up @@ -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"));
}
}
Expand Down

0 comments on commit 6095196

Please sign in to comment.