From 0d298b70d23663b6e230b41818442b4f1a91f14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ki=C3=ABd=20Llaentenn?= Date: Mon, 2 Sep 2024 09:03:48 -0400 Subject: [PATCH 1/3] slider: handle mouse wheel events --- widget/src/slider.rs | 16 ++++++++++++++++ widget/src/vertical_slider.rs | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/widget/src/slider.rs b/widget/src/slider.rs index e586684ab8..130c9bf3d0 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -288,6 +288,22 @@ where }; match event { + Event::Mouse(mouse::Event::WheelScrolled { delta }) => { + if let Some(_) = cursor.position_over(layout.bounds()) { + let delta = match delta { + mouse::ScrollDelta::Lines { x: _, y } => y, + mouse::ScrollDelta::Pixels { x: _, y } => y, + }; + + if delta < 0.0 { + let _ = decrement(current_value).map(change); + } else { + let _ = increment(current_value).map(change); + } + + return event::Status::Captured; + } + } Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { if let Some(cursor_position) = diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index f21b996c2d..5a3519f483 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -291,6 +291,22 @@ where }; match event { + Event::Mouse(mouse::Event::WheelScrolled { delta }) => { + if let Some(_) = cursor.position_over(layout.bounds()) { + let delta = match delta { + mouse::ScrollDelta::Lines { x: _, y } => y, + mouse::ScrollDelta::Pixels { x: _, y } => y, + }; + + if delta < 0.0 { + let _ = decrement(current_value).map(change); + } else { + let _ = increment(current_value).map(change); + } + + return event::Status::Captured; + } + } Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { if let Some(cursor_position) = From cdf02ddda95a900215de8a9fa1681130795991bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 13 Sep 2024 23:45:30 +0200 Subject: [PATCH 2/3] Enable `slider` scrolling only when `Ctrl` is pressed --- widget/src/slider.rs | 34 ++++++++++++++++++---------------- widget/src/vertical_slider.rs | 34 ++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/widget/src/slider.rs b/widget/src/slider.rs index 130c9bf3d0..5d0a363a0e 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -288,22 +288,6 @@ where }; match event { - Event::Mouse(mouse::Event::WheelScrolled { delta }) => { - if let Some(_) = cursor.position_over(layout.bounds()) { - let delta = match delta { - mouse::ScrollDelta::Lines { x: _, y } => y, - mouse::ScrollDelta::Pixels { x: _, y } => y, - }; - - if delta < 0.0 { - let _ = decrement(current_value).map(change); - } else { - let _ = increment(current_value).map(change); - } - - return event::Status::Captured; - } - } Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { if let Some(cursor_position) = @@ -340,6 +324,24 @@ where return event::Status::Captured; } } + Event::Mouse(mouse::Event::WheelScrolled { delta }) + if state.keyboard_modifiers.control() => + { + if let Some(_) = cursor.position_over(layout.bounds()) { + let delta = match delta { + mouse::ScrollDelta::Lines { x: _, y } => y, + mouse::ScrollDelta::Pixels { x: _, y } => y, + }; + + if delta < 0.0 { + let _ = decrement(current_value).map(change); + } else { + let _ = increment(current_value).map(change); + } + + return event::Status::Captured; + } + } Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => { if cursor.position_over(layout.bounds()).is_some() { match key { diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index 5a3519f483..f8f4b4a3e2 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -291,22 +291,6 @@ where }; match event { - Event::Mouse(mouse::Event::WheelScrolled { delta }) => { - if let Some(_) = cursor.position_over(layout.bounds()) { - let delta = match delta { - mouse::ScrollDelta::Lines { x: _, y } => y, - mouse::ScrollDelta::Pixels { x: _, y } => y, - }; - - if delta < 0.0 { - let _ = decrement(current_value).map(change); - } else { - let _ = increment(current_value).map(change); - } - - return event::Status::Captured; - } - } Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { if let Some(cursor_position) = @@ -345,6 +329,24 @@ where return event::Status::Captured; } } + Event::Mouse(mouse::Event::WheelScrolled { delta }) + if state.keyboard_modifiers.control() => + { + if let Some(_) = cursor.position_over(layout.bounds()) { + let delta = match delta { + mouse::ScrollDelta::Lines { x: _, y } => y, + mouse::ScrollDelta::Pixels { x: _, y } => y, + }; + + if delta < 0.0 { + let _ = decrement(current_value).map(change); + } else { + let _ = increment(current_value).map(change); + } + + return event::Status::Captured; + } + } Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => { if cursor.position_over(layout.bounds()).is_some() { match key { From 83041f6880a79fb1676fc1fd753474d189aa9b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 13 Sep 2024 23:46:51 +0200 Subject: [PATCH 3/3] Use `mouse::Cursor::is_over` in `slider` --- widget/src/slider.rs | 4 ++-- widget/src/vertical_slider.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/widget/src/slider.rs b/widget/src/slider.rs index 5d0a363a0e..302cfae7f5 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -327,7 +327,7 @@ where Event::Mouse(mouse::Event::WheelScrolled { delta }) if state.keyboard_modifiers.control() => { - if let Some(_) = cursor.position_over(layout.bounds()) { + if cursor.is_over(layout.bounds()) { let delta = match delta { mouse::ScrollDelta::Lines { x: _, y } => y, mouse::ScrollDelta::Pixels { x: _, y } => y, @@ -343,7 +343,7 @@ where } } Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => { - if cursor.position_over(layout.bounds()).is_some() { + if cursor.is_over(layout.bounds()) { match key { Key::Named(key::Named::ArrowUp) => { let _ = increment(current_value).map(change); diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index f8f4b4a3e2..f02a490a8f 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -332,7 +332,7 @@ where Event::Mouse(mouse::Event::WheelScrolled { delta }) if state.keyboard_modifiers.control() => { - if let Some(_) = cursor.position_over(layout.bounds()) { + if cursor.is_over(layout.bounds()) { let delta = match delta { mouse::ScrollDelta::Lines { x: _, y } => y, mouse::ScrollDelta::Pixels { x: _, y } => y, @@ -348,7 +348,7 @@ where } } Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => { - if cursor.position_over(layout.bounds()).is_some() { + if cursor.is_over(layout.bounds()) { match key { Key::Named(key::Named::ArrowUp) => { let _ = increment(current_value).map(change);