Simplify Change enum in slider logic and remove double-click behavior

This commit is contained in:
Héctor Ramón Jiménez 2024-01-31 21:38:16 +01:00
parent 5e2b3d4a51
commit 00716a159a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 35 additions and 118 deletions

View file

@ -5,7 +5,7 @@ use crate::core::event::{self, Event};
use crate::core::keyboard; use crate::core::keyboard;
use crate::core::keyboard::key::{self, Key}; use crate::core::keyboard::key::{self, Key};
use crate::core::layout; use crate::core::layout;
use crate::core::mouse::{self, click}; use crate::core::mouse;
use crate::core::renderer; use crate::core::renderer;
use crate::core::touch; use crate::core::touch;
use crate::core::widget::tree::{self, Tree}; use crate::core::widget::tree::{self, Tree};
@ -287,8 +287,9 @@ where
Message: Clone, Message: Clone,
{ {
let is_dragging = state.is_dragging; 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 bounds = layout.bounds();
let new_value = if cursor_position.x <= bounds.x { let new_value = if cursor_position.x <= bounds.x {
Some(*range.start()) Some(*range.start())
@ -352,25 +353,11 @@ where
T::from_f64(new_value) T::from_f64(new_value)
}; };
enum Change { let change = |new_value: T| {
Default, if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
CursorPosition(Point), shell.publish((on_change)(new_value));
Increment,
Decrement,
}
let mut change = |change: Change| { *value = new_value;
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;
}
} }
}; };
@ -379,32 +366,14 @@ where
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = cursor.position_over(layout.bounds()) if let Some(cursor_position) = cursor.position_over(layout.bounds())
{ {
let click = if state.keyboard_modifiers.command() {
mouse::Click::new(cursor_position, state.last_click); let _ = default.map(change);
state.is_dragging = false;
match click.kind() { } else {
click::Kind::Single => { let _ = locate(cursor_position).map(change);
if state.keyboard_modifiers.control() state.is_dragging = true;
|| 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;
}
} }
state.last_click = Some(click);
return event::Status::Captured; return event::Status::Captured;
} }
} }
@ -423,9 +392,7 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. }) Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => { | Event::Touch(touch::Event::FingerMoved { .. }) => {
if is_dragging { if is_dragging {
let _ = cursor let _ = cursor.position().and_then(locate).map(change);
.position()
.map(|point| change(Change::CursorPosition(point)));
return event::Status::Captured; return event::Status::Captured;
} }
@ -434,10 +401,10 @@ where
if cursor.position_over(layout.bounds()).is_some() { if cursor.position_over(layout.bounds()).is_some() {
match key { match key {
Key::Named(key::Named::ArrowUp) => { Key::Named(key::Named::ArrowUp) => {
change(Change::Increment); let _ = increment(current_value).map(change);
} }
Key::Named(key::Named::ArrowDown) => { 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`]. /// The local state of a [`Slider`].
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State { pub struct State {
is_dragging: bool, is_dragging: bool,
last_click: Option<mouse::Click>,
keyboard_modifiers: keyboard::Modifiers, keyboard_modifiers: keyboard::Modifiers,
} }
@ -586,11 +552,3 @@ impl State {
State::default() State::default()
} }
} }
impl PartialEq for State {
fn eq(&self, other: &Self) -> bool {
self.is_dragging == other.is_dragging
}
}
impl Eq for State {}

View file

@ -11,7 +11,6 @@ use crate::core::keyboard;
use crate::core::keyboard::key::{self, Key}; use crate::core::keyboard::key::{self, Key};
use crate::core::layout::{self, Layout}; use crate::core::layout::{self, Layout};
use crate::core::mouse; use crate::core::mouse;
use crate::core::mouse::click;
use crate::core::renderer; use crate::core::renderer;
use crate::core::touch; use crate::core::touch;
use crate::core::widget::tree::{self, Tree}; use crate::core::widget::tree::{self, Tree};
@ -286,8 +285,9 @@ where
Message: Clone, Message: Clone,
{ {
let is_dragging = state.is_dragging; 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 bounds = layout.bounds();
let new_value = if cursor_position.y >= bounds.y + bounds.height { let new_value = if cursor_position.y >= bounds.y + bounds.height {
@ -353,25 +353,11 @@ where
T::from_f64(new_value) T::from_f64(new_value)
}; };
enum Change { let change = |new_value: T| {
Default, if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
CursorPosition(Point), shell.publish((on_change)(new_value));
Increment,
Decrement,
}
let mut change = |change: Change| { *value = new_value;
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;
}
} }
}; };
@ -380,32 +366,16 @@ where
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = cursor.position_over(layout.bounds()) if let Some(cursor_position) = cursor.position_over(layout.bounds())
{ {
let click = if state.keyboard_modifiers.control()
mouse::Click::new(cursor_position, state.last_click); || state.keyboard_modifiers.command()
{
match click.kind() { let _ = default.map(change);
click::Kind::Single => { state.is_dragging = false;
if state.keyboard_modifiers.control() } else {
|| state.keyboard_modifiers.command() let _ = locate(cursor_position).map(change);
{ state.is_dragging = true;
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;
}
} }
state.last_click = Some(click);
return event::Status::Captured; return event::Status::Captured;
} }
} }
@ -424,9 +394,7 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. }) Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => { | Event::Touch(touch::Event::FingerMoved { .. }) => {
if is_dragging { if is_dragging {
let _ = cursor let _ = cursor.position().and_then(locate).map(change);
.position()
.map(|point| change(Change::CursorPosition(point)));
return event::Status::Captured; return event::Status::Captured;
} }
@ -435,10 +403,10 @@ where
if cursor.position_over(layout.bounds()).is_some() { if cursor.position_over(layout.bounds()).is_some() {
match key { match key {
Key::Named(key::Named::ArrowUp) => { Key::Named(key::Named::ArrowUp) => {
change(Change::Increment); let _ = increment(current_value).map(change);
} }
Key::Named(key::Named::ArrowDown) => { 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`]. /// The local state of a [`VerticalSlider`].
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State { pub struct State {
is_dragging: bool, is_dragging: bool,
last_click: Option<mouse::Click>,
keyboard_modifiers: keyboard::Modifiers, keyboard_modifiers: keyboard::Modifiers,
} }
@ -587,11 +554,3 @@ impl State {
State::default() State::default()
} }
} }
impl PartialEq for State {
fn eq(&self, other: &Self) -> bool {
self.is_dragging == other.is_dragging
}
}
impl Eq for State {}