|
| 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 | +} |
0 commit comments