Skip to content

Commit 6430bf8

Browse files
Christophjneem
Christoph
authored andcommitted
Slider upgrade
Makes color, axis, and knob style configuration, and adds annotations.
1 parent c132c81 commit 6430bf8

File tree

5 files changed

+882
-127
lines changed

5 files changed

+882
-127
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ You can find its changes [documented below](#070---2021-01-01).
6161
- `scroll_to_view` and `scroll_area_to_view` methods on `UpdateCtx`, `LifecycleCtx` and `EventCtx` ([#1976] by [@xarvic])
6262
- `Notification::route` ([#1978] by [@xarvic])
6363
- Build on OpenBSD ([#1993] by [@klemensn])
64+
- `RangeSlider` and `Annotated` ([#1979] by [@xarvic])
6465

6566
### Changed
6667

@@ -804,6 +805,7 @@ Last release without a changelog :(
804805
[#1978]: https://github.com/linebender/druid/pull/1978
805806
[#1993]: https://github.com/linebender/druid/pull/1993
806807
[#1996]: https://github.com/linebender/druid/pull/1996
808+
[#1979]: https://github.com/linebender/druid/pull/1979
807809

808810
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
809811
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0

druid/examples/slider.rs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2019 The Druid Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! This is a demo of the settings of Slider, RangeSlider and Annotated.
16+
//! It contains a `Slider` and `RangeSlider`.
17+
//! Every time the `RangeSlider` is moved the range of the `Slider` is updated.
18+
19+
// On Windows platform, don't show a console when opening the app.
20+
#![windows_subsystem = "windows"]
21+
22+
use druid::widget::prelude::*;
23+
use druid::widget::{
24+
Axis, CrossAxisAlignment, Flex, KnobStyle, Label, RangeSlider, Slider, ViewSwitcher,
25+
};
26+
use druid::{AppLauncher, Color, Data, KeyOrValue, Lens, UnitPoint, WidgetExt, WindowDesc};
27+
28+
const VERTICAL_WIDGET_SPACING: f64 = 20.0;
29+
30+
#[derive(Clone, Data, Lens)]
31+
struct AppState {
32+
range: (f64, f64),
33+
value: f64,
34+
}
35+
36+
pub fn main() {
37+
// describe the main window
38+
let main_window = WindowDesc::new(build_root_widget())
39+
.title("Slider Demo!")
40+
.window_size((400.0, 400.0));
41+
42+
// create the initial app state
43+
let initial_state: AppState = AppState {
44+
range: (2.0, 8.0),
45+
value: 5.0,
46+
};
47+
48+
// start the application. Here we pass in the application state.
49+
AppLauncher::with_window(main_window)
50+
.log_to_console()
51+
.launch(initial_state)
52+
.expect("Failed to launch application");
53+
}
54+
55+
fn build_root_widget() -> impl Widget<AppState> {
56+
let range = Flex::row()
57+
.with_child(Label::dynamic(|value: &(f64, f64), _| {
58+
format!("Value Range: {:?}", value)
59+
}))
60+
.with_default_spacer()
61+
.with_child(
62+
RangeSlider::new()
63+
.with_range(0.0, 20.0)
64+
.with_step(1.0)
65+
.track_color(KeyOrValue::Concrete(Color::RED))
66+
.fix_width(250.0),
67+
)
68+
.lens(AppState::range);
69+
70+
let value = Flex::row()
71+
.with_child(Label::dynamic(|value: &AppState, _| {
72+
format!("Value: {:?}", value.value)
73+
}))
74+
.with_default_spacer()
75+
.with_child(ViewSwitcher::new(
76+
|data: &AppState, _| data.range,
77+
|range, _, _| {
78+
Slider::new()
79+
.with_range(range.0, range.1)
80+
.track_color(KeyOrValue::Concrete(Color::RED))
81+
.knob_style(KnobStyle::Wedge)
82+
.axis(Axis::Vertical)
83+
.with_step(0.25)
84+
.annotated(0.0, 0.0000000025)
85+
.fix_height(250.0)
86+
.lens(AppState::value)
87+
.boxed()
88+
},
89+
));
90+
91+
// arrange the two widgets vertically, with some padding
92+
Flex::column()
93+
.with_child(range)
94+
.with_spacer(VERTICAL_WIDGET_SPACING)
95+
.with_child(value)
96+
.cross_axis_alignment(CrossAxisAlignment::End)
97+
.align_vertical(UnitPoint::RIGHT)
98+
.padding(20.0)
99+
}

druid/examples/web/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl_example!(open_save);
7575
impl_example!(panels.unwrap());
7676
impl_example!(scroll_colors);
7777
impl_example!(scroll);
78+
impl_example!(slider);
7879
impl_example!(split_demo);
7980
impl_example!(styled_text.unwrap());
8081
impl_example!(switches);

druid/src/widget/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub use radio::{Radio, RadioGroup};
9191
pub use scope::{DefaultScopePolicy, LensScopeTransfer, Scope, ScopePolicy, ScopeTransfer};
9292
pub use scroll::Scroll;
9393
pub use sized_box::SizedBox;
94-
pub use slider::Slider;
94+
pub use slider::{KnobStyle, RangeSlider, Slider};
9595
pub use spinner::Spinner;
9696
pub use split::Split;
9797
pub use stepper::Stepper;

0 commit comments

Comments
 (0)