Select Git revision
dial.rs 2.97 KiB
// 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.
//! An example of the `dial` widget.
use druid::widget::{Flex, Label, WidgetExt};
use druid::{AppLauncher, Color, Data, Lens, PlatformError, Size, Widget, WindowDesc};
use druid_widgets::Dial;
#[derive(Clone, Data, Lens)]
struct DialLabel {
value: f64,
}
impl DialLabel {
fn new(text: &str) -> impl Widget<DialLabel> {
let text = text.to_string();
let label =
Label::new(move |data: &DialLabel, _env: &_| format!("{} {:.3}", text, data.value));
let solid = Color::rgb8(0x3a, 0x3a, 0x3a);
let mut col = Flex::column();
col.add_child(
Dial::new(Size::new(100.0, 100.0))
.lens(DialLabel::value)
.background(solid.clone())
.padding(5.0),
0.0,
);
col.add_child(label.padding(5.0).background(solid.clone()), 0.0);
col
}
}
#[derive(Clone, Data, Lens)]
struct Envelope {
attack: DialLabel,
decay: DialLabel,
sustain: DialLabel,
release: DialLabel,
}
impl Envelope {
fn new() -> impl Widget<Envelope> {
let solid = Color::rgb8(0x3a, 0x3a, 0x3a);
Flex::row()
.with_child(
DialLabel::new("Attack")
.lens(Envelope::attack)
.background(solid.clone())
.padding(5.0),
1.0,
)
.with_child(
DialLabel::new("Decay")
.lens(Envelope::decay)
.background(solid.clone())
.padding(5.0),
1.0,
)
.with_child(
DialLabel::new("Sustain")
.lens(Envelope::sustain)
.background(solid.clone())
.padding(5.0),
1.0,
)
.with_child(
DialLabel::new("Release")
.lens(Envelope::release)
.background(solid.clone())
.padding(5.0),
1.0,
)
}
}
fn main() -> Result<(), PlatformError> {
AppLauncher::with_window(WindowDesc::new(Envelope::new))
.use_simple_logger()
.launch(Envelope {
attack: DialLabel { value: 0.1 },
decay: DialLabel { value: 0.2 },
sustain: DialLabel { value: 0.3 },
release: DialLabel { value: 0.4 },
})?;
Ok(())
}