From 2c5b209571809c38a2079781ec24cfdbb29cde1f Mon Sep 17 00:00:00 2001 From: Maor Malka Date: Tue, 13 Jan 2026 05:57:30 -0500 Subject: [PATCH] added support for using scroll wheel to modify knob value --- examples/example_knob.rs | 3 ++- src/config.rs | 2 ++ src/widget.rs | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/example_knob.rs b/examples/example_knob.rs index 72e8f4d..439f108 100644 --- a/examples/example_knob.rs +++ b/examples/example_knob.rs @@ -88,7 +88,8 @@ impl eframe::App for KnobDemo { .with_show_filled_segments(self.show_filled) .with_colors(self.knob_color, self.line_color, self.text_color) .with_step(self.use_step.then_some(0.02)) - .with_double_click_reset(0.5); + .with_double_click_reset(0.5) + .with_middle_scroll(); if *label == "Wiper, Sweep" { knob = knob.with_sweep_range(0.25, 0.75).with_size(50.0); diff --git a/src/config.rs b/src/config.rs index ad27bc9..05b93e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,6 +17,7 @@ pub struct KnobConfig { pub(crate) min_angle: f32, pub(crate) max_angle: f32, pub(crate) reset_value: Option, + pub(crate) allow_scroll: bool, } impl KnobConfig { @@ -38,6 +39,7 @@ impl KnobConfig { show_background_arc: true, show_filled_segments: true, reset_value: None, + allow_scroll:false } } } diff --git a/src/widget.rs b/src/widget.rs index cb6410a..575895d 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -161,6 +161,13 @@ impl<'a> Knob<'a> { self.config.reset_value = Some(reset_value); self } + + /// Allows user to use scroll wheel to change knob value + /// Uses config.step for the increment value + pub fn with_middle_scroll(mut self) -> Self { + self.config.allow_scroll = true; + self + } } impl Widget for Knob<'_> { @@ -198,6 +205,17 @@ impl Widget for Knob<'_> { if let Some(reset_value) = self.config.reset_value { *self.value = reset_value } + } else if response.hovered() & self.config.allow_scroll { + if let Some(scoll) = ui.input(|input| { + input.events.iter().find_map(|e| match e { + egui::Event::MouseWheel { delta, .. } => Some(*delta), + _ => None, + }) + }) { + *self.value = (*self.value + + scoll.y * self.config.step.unwrap_or(self.config.drag_sensitivity)) + .clamp(self.min, self.max); + } } let knob_rect = renderer.calculate_knob_rect(rect);