-
Notifications
You must be signed in to change notification settings - Fork 569
Stepper widget #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Stepper widget #308
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
96eb67c
Draw stepper widget
scholtzan 1185145
Increase/decrease value with stepper widget
scholtzan 2594034
Cleanup stepper widget
scholtzan 3b64ecf
Add example for connecting stepper widget with textbox
scholtzan 8797365
Incroporate review feedback into stepper widget
scholtzan 4f76e71
Adress stepper review feedback
scholtzan 48759af
Single padding widget in stepper example
scholtzan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
// Copyright 2019 The xi-editor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! A stepper widget. | ||
|
||
use crate::{ | ||
BoxConstraints, Env, Event, EventCtx, LayoutCtx, PaintCtx, Size, TimerToken, UpdateCtx, Widget, | ||
}; | ||
use std::f64::EPSILON; | ||
use std::time::{Duration, Instant}; | ||
|
||
use crate::kurbo::{BezPath, Rect, RoundedRect}; | ||
use crate::piet::{LinearGradient, RenderContext, UnitPoint}; | ||
|
||
use crate::theme; | ||
use crate::Point; | ||
|
||
// Delay until stepper starts automatically changing valued when one of the button is held down. | ||
const STEPPER_REPEAT_DELAY: Duration = Duration::from_millis(500); | ||
// Delay between value changes when one of the button is held down. | ||
const STEPPER_REPEAT: Duration = Duration::from_millis(200); | ||
|
||
/// A stepper widget for step-wise increasing and decreasing a value. | ||
pub struct Stepper { | ||
max: f64, | ||
min: f64, | ||
step: f64, | ||
wrap: bool, | ||
/// A closure that will be invoked when the value changed. | ||
value_changed: Box<dyn Fn(&mut EventCtx, &mut f64, &Env)>, | ||
/// Keeps track of which button is currently triggered. | ||
increase_active: bool, | ||
decrease_active: bool, | ||
timer_id: TimerToken, | ||
} | ||
|
||
impl Stepper { | ||
pub fn new(value_changed: impl Fn(&mut EventCtx, &mut f64, &Env) + 'static) -> Self { | ||
scholtzan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Stepper { | ||
max: std::f64::MAX, | ||
min: std::f64::MIN, | ||
step: 1.0, | ||
wrap: false, | ||
value_changed: Box::new(value_changed), | ||
increase_active: false, | ||
decrease_active: false, | ||
timer_id: TimerToken::INVALID, | ||
} | ||
} | ||
|
||
pub fn max(mut self, max: f64) -> Self { | ||
scholtzan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.max = max; | ||
self | ||
} | ||
|
||
pub fn min(mut self, min: f64) -> Self { | ||
self.min = min; | ||
self | ||
} | ||
|
||
pub fn step(mut self, step: f64) -> Self { | ||
self.step = step; | ||
self | ||
} | ||
|
||
pub fn wrap(mut self, wrap: bool) -> Self { | ||
self.wrap = wrap; | ||
self | ||
} | ||
|
||
fn change_value(&mut self, ctx: &mut EventCtx, data: &mut f64, env: &Env) { | ||
// increase/decrease value depending on which button is currently active | ||
let delta = if self.increase_active { | ||
self.step | ||
} else if self.decrease_active { | ||
-1. * self.step | ||
} else { | ||
0.0 | ||
}; | ||
|
||
let old_data = *data; | ||
*data = (*data + delta).max(self.min).min(self.max); | ||
|
||
if (*data - old_data).abs() > EPSILON { | ||
// callback | ||
(self.value_changed)(ctx, data, env); | ||
} else if self.wrap { | ||
if (*data - self.min).abs() < EPSILON { | ||
*data = self.max | ||
} else { | ||
*data = self.min | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Widget<f64> for Stepper { | ||
fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &f64, env: &Env) { | ||
let rounded_rect = | ||
RoundedRect::from_origin_size(Point::ORIGIN, paint_ctx.size().to_vec2(), 4.); | ||
|
||
let height = paint_ctx.size().height; | ||
let width = env.get(theme::BASIC_WIDGET_HEIGHT); | ||
let button_size = Size::new(width, height / 2.); | ||
|
||
paint_ctx.stroke(rounded_rect, &env.get(theme::BORDER), 2.0); | ||
paint_ctx.clip(rounded_rect); | ||
|
||
// draw buttons for increase/decrease | ||
let increase_button_origin = Point::ORIGIN; | ||
let mut decrease_button_origin = Point::ORIGIN; | ||
decrease_button_origin.y += height / 2.; | ||
|
||
let increase_button_rect = Rect::from_origin_size(increase_button_origin, button_size); | ||
let decrease_button_rect = Rect::from_origin_size(decrease_button_origin, button_size); | ||
|
||
let active_gradient = LinearGradient::new( | ||
UnitPoint::TOP, | ||
UnitPoint::BOTTOM, | ||
(env.get(theme::PRIMARY_LIGHT), env.get(theme::PRIMARY_DARK)), | ||
); | ||
|
||
let inactive_gradient = LinearGradient::new( | ||
UnitPoint::TOP, | ||
UnitPoint::BOTTOM, | ||
(env.get(theme::BUTTON_DARK), env.get(theme::BUTTON_LIGHT)), | ||
); | ||
|
||
// draw buttons that are currently triggered as active | ||
if self.increase_active { | ||
paint_ctx.fill(increase_button_rect, &active_gradient); | ||
} else { | ||
paint_ctx.fill(increase_button_rect, &inactive_gradient); | ||
}; | ||
|
||
if self.decrease_active { | ||
paint_ctx.fill(decrease_button_rect, &active_gradient); | ||
} else { | ||
paint_ctx.fill(decrease_button_rect, &inactive_gradient); | ||
}; | ||
|
||
// draw up and down triangles | ||
let mut arrows = BezPath::new(); | ||
arrows.move_to(Point::new(4., height / 2. - 4.)); | ||
arrows.line_to(Point::new(width - 4., height / 2. - 4.)); | ||
arrows.line_to(Point::new(width / 2., 4.)); | ||
arrows.close_path(); | ||
|
||
arrows.move_to(Point::new(4., height / 2. + 4.)); | ||
arrows.line_to(Point::new(width - 4., height / 2. + 4.)); | ||
arrows.line_to(Point::new(width / 2., height - 4.)); | ||
arrows.close_path(); | ||
|
||
paint_ctx.fill(arrows, &env.get(theme::LABEL_COLOR)); | ||
} | ||
|
||
fn layout( | ||
&mut self, | ||
_layout_ctx: &mut LayoutCtx, | ||
bc: &BoxConstraints, | ||
_data: &f64, | ||
env: &Env, | ||
) -> Size { | ||
bc.constrain(Size::new( | ||
env.get(theme::BASIC_WIDGET_HEIGHT), | ||
env.get(theme::BORDERED_WIDGET_HEIGHT), | ||
)) | ||
} | ||
|
||
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut f64, env: &Env) { | ||
let height = env.get(theme::BORDERED_WIDGET_HEIGHT); | ||
|
||
match event { | ||
Event::MouseDown(mouse) => { | ||
ctx.set_active(true); | ||
|
||
if mouse.pos.y > height / 2. { | ||
self.decrease_active = true; | ||
} else { | ||
self.increase_active = true; | ||
} | ||
|
||
self.change_value(ctx, data, env); | ||
|
||
let delay = Instant::now() + STEPPER_REPEAT_DELAY; | ||
self.timer_id = ctx.request_timer(delay); | ||
|
||
ctx.invalidate(); | ||
} | ||
Event::MouseUp(_) => { | ||
ctx.set_active(false); | ||
|
||
self.decrease_active = false; | ||
self.increase_active = false; | ||
self.timer_id = TimerToken::INVALID; | ||
|
||
ctx.invalidate(); | ||
} | ||
Event::Timer(id) if *id == self.timer_id => { | ||
self.change_value(ctx, data, env); | ||
let delay = Instant::now() + STEPPER_REPEAT; | ||
self.timer_id = ctx.request_timer(delay); | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
fn update(&mut self, ctx: &mut UpdateCtx, old_data: Option<&f64>, data: &f64, _env: &Env) { | ||
if old_data | ||
.map(|old_data| (*data - old_data).abs() > EPSILON) | ||
.unwrap_or(true) | ||
{ | ||
ctx.invalidate(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.