Skip to content

Commit

Permalink
Slider upgrade
Browse files Browse the repository at this point in the history
Makes color, axis, and knob style configuration, and adds annotations.
  • Loading branch information
Christoph authored and jneem committed Jan 5, 2022
1 parent c132c81 commit 6430bf8
Show file tree
Hide file tree
Showing 5 changed files with 882 additions and 127 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ You can find its changes [documented below](#070---2021-01-01).
- `scroll_to_view` and `scroll_area_to_view` methods on `UpdateCtx`, `LifecycleCtx` and `EventCtx` ([#1976] by [@xarvic])
- `Notification::route` ([#1978] by [@xarvic])
- Build on OpenBSD ([#1993] by [@klemensn])
- `RangeSlider` and `Annotated` ([#1979] by [@xarvic])

### Changed

Expand Down Expand Up @@ -804,6 +805,7 @@ Last release without a changelog :(
[#1978]: https://github.com/linebender/druid/pull/1978
[#1993]: https://github.com/linebender/druid/pull/1993
[#1996]: https://github.com/linebender/druid/pull/1996
[#1979]: https://github.com/linebender/druid/pull/1979

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
99 changes: 99 additions & 0 deletions druid/examples/slider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2019 The Druid 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.

//! This is a demo of the settings of Slider, RangeSlider and Annotated.
//! It contains a `Slider` and `RangeSlider`.
//! Every time the `RangeSlider` is moved the range of the `Slider` is updated.

// On Windows platform, don't show a console when opening the app.
#![windows_subsystem = "windows"]

use druid::widget::prelude::*;
use druid::widget::{
Axis, CrossAxisAlignment, Flex, KnobStyle, Label, RangeSlider, Slider, ViewSwitcher,
};
use druid::{AppLauncher, Color, Data, KeyOrValue, Lens, UnitPoint, WidgetExt, WindowDesc};

const VERTICAL_WIDGET_SPACING: f64 = 20.0;

#[derive(Clone, Data, Lens)]
struct AppState {
range: (f64, f64),
value: f64,
}

pub fn main() {
// describe the main window
let main_window = WindowDesc::new(build_root_widget())
.title("Slider Demo!")
.window_size((400.0, 400.0));

// create the initial app state
let initial_state: AppState = AppState {
range: (2.0, 8.0),
value: 5.0,
};

// start the application. Here we pass in the application state.
AppLauncher::with_window(main_window)
.log_to_console()
.launch(initial_state)
.expect("Failed to launch application");
}

fn build_root_widget() -> impl Widget<AppState> {
let range = Flex::row()
.with_child(Label::dynamic(|value: &(f64, f64), _| {
format!("Value Range: {:?}", value)
}))
.with_default_spacer()
.with_child(
RangeSlider::new()
.with_range(0.0, 20.0)
.with_step(1.0)
.track_color(KeyOrValue::Concrete(Color::RED))
.fix_width(250.0),
)
.lens(AppState::range);

let value = Flex::row()
.with_child(Label::dynamic(|value: &AppState, _| {
format!("Value: {:?}", value.value)
}))
.with_default_spacer()
.with_child(ViewSwitcher::new(
|data: &AppState, _| data.range,
|range, _, _| {
Slider::new()
.with_range(range.0, range.1)
.track_color(KeyOrValue::Concrete(Color::RED))
.knob_style(KnobStyle::Wedge)
.axis(Axis::Vertical)
.with_step(0.25)
.annotated(0.0, 0.0000000025)
.fix_height(250.0)
.lens(AppState::value)
.boxed()
},
));

// arrange the two widgets vertically, with some padding
Flex::column()
.with_child(range)
.with_spacer(VERTICAL_WIDGET_SPACING)
.with_child(value)
.cross_axis_alignment(CrossAxisAlignment::End)
.align_vertical(UnitPoint::RIGHT)
.padding(20.0)
}
1 change: 1 addition & 0 deletions druid/examples/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl_example!(open_save);
impl_example!(panels.unwrap());
impl_example!(scroll_colors);
impl_example!(scroll);
impl_example!(slider);
impl_example!(split_demo);
impl_example!(styled_text.unwrap());
impl_example!(switches);
Expand Down
2 changes: 1 addition & 1 deletion druid/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub use radio::{Radio, RadioGroup};
pub use scope::{DefaultScopePolicy, LensScopeTransfer, Scope, ScopePolicy, ScopeTransfer};
pub use scroll::Scroll;
pub use sized_box::SizedBox;
pub use slider::Slider;
pub use slider::{KnobStyle, RangeSlider, Slider};
pub use spinner::Spinner;
pub use split::Split;
pub use stepper::Stepper;
Expand Down
Loading

0 comments on commit 6430bf8

Please sign in to comment.