summaryrefslogtreecommitdiffstats
path: root/widget/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-31 21:38:16 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-31 21:38:16 +0100
commit00716a159a371d070c5fb7d9ed2c660348343a33 (patch)
treee4b395e94bc779d035253ca67d72771bf0e02093 /widget/src
parent5e2b3d4a51be97c77124dcd60e5ee3bed1b19826 (diff)
downloadiced-00716a159a371d070c5fb7d9ed2c660348343a33.tar.gz
iced-00716a159a371d070c5fb7d9ed2c660348343a33.tar.bz2
iced-00716a159a371d070c5fb7d9ed2c660348343a33.zip
Simplify `Change` enum in `slider` logic and remove double-click behavior
Diffstat (limited to 'widget/src')
-rw-r--r--widget/src/slider.rs76
-rw-r--r--widget/src/vertical_slider.rs77
2 files changed, 35 insertions, 118 deletions
diff --git a/widget/src/slider.rs b/widget/src/slider.rs
index 3b7de8b9..3d1d08ba 100644
--- a/widget/src/slider.rs
+++ b/widget/src/slider.rs
@@ -5,7 +5,7 @@ use crate::core::event::{self, Event};
use crate::core::keyboard;
use crate::core::keyboard::key::{self, Key};
use crate::core::layout;
-use crate::core::mouse::{self, click};
+use crate::core::mouse;
use crate::core::renderer;
use crate::core::touch;
use crate::core::widget::tree::{self, Tree};
@@ -287,8 +287,9 @@ where
Message: Clone,
{
let is_dragging = state.is_dragging;
+ let current_value = *value;
- let change_cursor_position = |cursor_position: Point| -> Option<T> {
+ let locate = |cursor_position: Point| -> Option<T> {
let bounds = layout.bounds();
let new_value = if cursor_position.x <= bounds.x {
Some(*range.start())
@@ -352,25 +353,11 @@ where
T::from_f64(new_value)
};
- enum Change {
- Default,
- CursorPosition(Point),
- Increment,
- Decrement,
- }
+ let change = |new_value: T| {
+ if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
+ shell.publish((on_change)(new_value));
- let mut change = |change: Change| {
- if let Some(new_value) = match change {
- Change::Default => default,
- Change::CursorPosition(point) => change_cursor_position(point),
- Change::Increment => increment(*value),
- Change::Decrement => decrement(*value),
- } {
- if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
- shell.publish((on_change)(new_value));
-
- *value = new_value;
- }
+ *value = new_value;
}
};
@@ -379,32 +366,14 @@ where
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = cursor.position_over(layout.bounds())
{
- let click =
- mouse::Click::new(cursor_position, state.last_click);
-
- match click.kind() {
- click::Kind::Single => {
- if state.keyboard_modifiers.control()
- || state.keyboard_modifiers.command()
- {
- change(Change::Default);
- state.is_dragging = false;
- } else {
- change(Change::CursorPosition(cursor_position));
- state.is_dragging = true;
- }
- }
- click::Kind::Double => {
- change(Change::Default);
- state.is_dragging = false;
- }
- mouse::click::Kind::Triple => {
- state.is_dragging = false;
- }
+ if state.keyboard_modifiers.command() {
+ let _ = default.map(change);
+ state.is_dragging = false;
+ } else {
+ let _ = locate(cursor_position).map(change);
+ state.is_dragging = true;
}
- state.last_click = Some(click);
-
return event::Status::Captured;
}
}
@@ -423,9 +392,7 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
if is_dragging {
- let _ = cursor
- .position()
- .map(|point| change(Change::CursorPosition(point)));
+ let _ = cursor.position().and_then(locate).map(change);
return event::Status::Captured;
}
@@ -434,10 +401,10 @@ where
if cursor.position_over(layout.bounds()).is_some() {
match key {
Key::Named(key::Named::ArrowUp) => {
- change(Change::Increment);
+ let _ = increment(current_value).map(change);
}
Key::Named(key::Named::ArrowDown) => {
- change(Change::Decrement);
+ let _ = decrement(current_value).map(change);
}
_ => (),
}
@@ -573,10 +540,9 @@ pub fn mouse_interaction(
}
/// The local state of a [`Slider`].
-#[derive(Debug, Clone, Copy, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State {
is_dragging: bool,
- last_click: Option<mouse::Click>,
keyboard_modifiers: keyboard::Modifiers,
}
@@ -586,11 +552,3 @@ impl State {
State::default()
}
}
-
-impl PartialEq for State {
- fn eq(&self, other: &Self) -> bool {
- self.is_dragging == other.is_dragging
- }
-}
-
-impl Eq for State {}
diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs
index c16727b6..168d7848 100644
--- a/widget/src/vertical_slider.rs
+++ b/widget/src/vertical_slider.rs
@@ -11,7 +11,6 @@ use crate::core::keyboard;
use crate::core::keyboard::key::{self, Key};
use crate::core::layout::{self, Layout};
use crate::core::mouse;
-use crate::core::mouse::click;
use crate::core::renderer;
use crate::core::touch;
use crate::core::widget::tree::{self, Tree};
@@ -286,8 +285,9 @@ where
Message: Clone,
{
let is_dragging = state.is_dragging;
+ let current_value = *value;
- let change_cursor_position = |cursor_position: Point| -> Option<T> {
+ let locate = |cursor_position: Point| -> Option<T> {
let bounds = layout.bounds();
let new_value = if cursor_position.y >= bounds.y + bounds.height {
@@ -353,25 +353,11 @@ where
T::from_f64(new_value)
};
- enum Change {
- Default,
- CursorPosition(Point),
- Increment,
- Decrement,
- }
+ let change = |new_value: T| {
+ if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
+ shell.publish((on_change)(new_value));
- let mut change = |change: Change| {
- if let Some(new_value) = match change {
- Change::Default => default,
- Change::CursorPosition(point) => change_cursor_position(point),
- Change::Increment => increment(*value),
- Change::Decrement => decrement(*value),
- } {
- if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
- shell.publish((on_change)(new_value));
-
- *value = new_value;
- }
+ *value = new_value;
}
};
@@ -380,32 +366,16 @@ where
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = cursor.position_over(layout.bounds())
{
- let click =
- mouse::Click::new(cursor_position, state.last_click);
-
- match click.kind() {
- click::Kind::Single => {
- if state.keyboard_modifiers.control()
- || state.keyboard_modifiers.command()
- {
- change(Change::Default);
- state.is_dragging = false;
- } else {
- change(Change::CursorPosition(cursor_position));
- state.is_dragging = true;
- }
- }
- click::Kind::Double => {
- change(Change::Default);
- state.is_dragging = false;
- }
- mouse::click::Kind::Triple => {
- state.is_dragging = false;
- }
+ if state.keyboard_modifiers.control()
+ || state.keyboard_modifiers.command()
+ {
+ let _ = default.map(change);
+ state.is_dragging = false;
+ } else {
+ let _ = locate(cursor_position).map(change);
+ state.is_dragging = true;
}
- state.last_click = Some(click);
-
return event::Status::Captured;
}
}
@@ -424,9 +394,7 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
if is_dragging {
- let _ = cursor
- .position()
- .map(|point| change(Change::CursorPosition(point)));
+ let _ = cursor.position().and_then(locate).map(change);
return event::Status::Captured;
}
@@ -435,10 +403,10 @@ where
if cursor.position_over(layout.bounds()).is_some() {
match key {
Key::Named(key::Named::ArrowUp) => {
- change(Change::Increment);
+ let _ = increment(current_value).map(change);
}
Key::Named(key::Named::ArrowDown) => {
- change(Change::Decrement);
+ let _ = decrement(current_value).map(change);
}
_ => (),
}
@@ -574,10 +542,9 @@ pub fn mouse_interaction(
}
/// The local state of a [`VerticalSlider`].
-#[derive(Debug, Clone, Copy, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State {
is_dragging: bool,
- last_click: Option<mouse::Click>,
keyboard_modifiers: keyboard::Modifiers,
}
@@ -587,11 +554,3 @@ impl State {
State::default()
}
}
-
-impl PartialEq for State {
- fn eq(&self, other: &Self) -> bool {
- self.is_dragging == other.is_dragging
- }
-}
-
-impl Eq for State {}