Skip to content

Commit

Permalink
Simplify slider logic further
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jan 31, 2024
1 parent 00716a1 commit 66f8cf8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
41 changes: 21 additions & 20 deletions widget/src/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,13 @@ where
} else if cursor_position.x >= bounds.x + bounds.width {
Some(*range.end())
} else {
let step = match step_fine {
Some(step_fine) if state.keyboard_modifiers.shift() => {
step_fine.into()
}
_ => step.into(),
};
let step = if state.keyboard_modifiers.shift() {
step_fine.unwrap_or(step)
} else {
step
}
.into();

let start = (*range.start()).into();
let end = (*range.end()).into();

Expand All @@ -318,15 +319,15 @@ where
};

let increment = |value: T| -> Option<T> {
let step = match step_fine {
Some(step_fine) if state.keyboard_modifiers.shift() => {
step_fine.into()
}
_ => step.into(),
};
let step = if state.keyboard_modifiers.shift() {
step_fine.unwrap_or(step)
} else {
step
}
.into();

let steps = (value.into() / step).round();
let new_value = step * (steps + f64::from(1));
let new_value = step * (steps + 1.0);

if new_value > (*range.end()).into() {
return Some(*range.end());
Expand All @@ -336,15 +337,15 @@ where
};

let decrement = |value: T| -> Option<T> {
let step = match step_fine {
Some(step_fine) if state.keyboard_modifiers.shift() => {
step_fine.into()
}
_ => step.into(),
};
let step = if state.keyboard_modifiers.shift() {
step_fine.unwrap_or(step)
} else {
step
}
.into();

let steps = (value.into() / step).round();
let new_value = step * (steps - f64::from(1));
let new_value = step * (steps - 1.0);

if new_value < (*range.start()).into() {
return Some(*range.start());
Expand Down
41 changes: 21 additions & 20 deletions widget/src/vertical_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,13 @@ where
} else if cursor_position.y <= bounds.y {
Some(*range.end())
} else {
let step = match step_fine {
Some(step_fine) if state.keyboard_modifiers.shift() => {
step_fine.into()
}
_ => step.into(),
};
let step = if state.keyboard_modifiers.shift() {
step_fine.unwrap_or(step)
} else {
step
}
.into();

let start = (*range.start()).into();
let end = (*range.end()).into();

Expand All @@ -318,15 +319,15 @@ where
};

let increment = |value: T| -> Option<T> {
let step = match step_fine {
Some(step_fine) if state.keyboard_modifiers.shift() => {
step_fine.into()
}
_ => step.into(),
};
let step = if state.keyboard_modifiers.shift() {
step_fine.unwrap_or(step)
} else {
step
}
.into();

let steps = (value.into() / step).round();
let new_value = step * (steps + f64::from(1));
let new_value = step * (steps + 1.0);

if new_value > (*range.end()).into() {
return Some(*range.end());
Expand All @@ -336,15 +337,15 @@ where
};

let decrement = |value: T| -> Option<T> {
let step = match step_fine {
Some(step_fine) if state.keyboard_modifiers.shift() => {
step_fine.into()
}
_ => step.into(),
};
let step = if state.keyboard_modifiers.shift() {
step_fine.unwrap_or(step)
} else {
step
}
.into();

let steps = (value.into() / step).round();
let new_value = step * (steps - f64::from(1));
let new_value = step * (steps - 1.0);

if new_value < (*range.start()).into() {
return Some(*range.start());
Expand Down

0 comments on commit 66f8cf8

Please sign in to comment.