Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "maolan-widgets"
version = "0.1.1"
version = "0.1.2"
edition = "2024"
description = "Widgets used for Maolan DAW"
license = "BSD-2-Clause"
Expand Down
64 changes: 50 additions & 14 deletions src/adsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
use iced::{
Color, Element, Event, Length, Point, Rectangle, Renderer, Theme, mouse,
widget::{
button, canvas,
canvas,
canvas::{Action, Frame, Geometry, Path, Program, Stroke, Text},
column, row, text,
column, container, mouse_area, row, text, text_input,
},
};

/// Edits emitted by [`adsr_envelope`]. Stage indices follow attack, decay,
/// sustain, release order.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum AdsrMessage {
Change(usize, f32),
Release(usize),
Reset(usize),
Assign(usize),
Edit(usize),
EditChanged(usize, String),
EditSubmit(usize),
}

/// Draw an interactive ADSR envelope.
Expand All @@ -28,6 +31,27 @@ pub fn adsr_envelope<'a>(
ranges: [(f32, f32); 4],
steps: [f32; 4],
assigned: [bool; 4],
) -> Element<'a, AdsrMessage> {
adsr_envelope_with_edit(
values,
ranges,
steps,
assigned,
None,
"",
iced::widget::Id::unique(),
)
}

/// Draw an envelope with an optional editable stage value.
pub fn adsr_envelope_with_edit<'a>(
values: [f32; 4],
ranges: [(f32, f32); 4],
steps: [f32; 4],
assigned: [bool; 4],
editing: Option<usize>,
edit_value: &'a str,
input_id: iced::widget::Id,
) -> Element<'a, AdsrMessage> {
let graph = Envelope {
values,
Expand All @@ -41,18 +65,30 @@ pub fn adsr_envelope<'a>(
} else {
format!("{:.2}s", values[i])
};
button(column![
text(["Attack", "Decay", "Sustain", "Release"][i]).size(10),
text(value).size(10)
])
.padding(2)
.width(Length::Fill)
.style(button::text)
.on_press(AdsrMessage::Assign(i))
.into()
let label: Element<'a, AdsrMessage> = if editing == Some(i) {
text_input::<AdsrMessage, iced::Theme, iced::Renderer>("", edit_value)
.id(input_id.clone())
.size(10)
.on_input(move |value| AdsrMessage::EditChanged(i, value))
.on_submit(AdsrMessage::EditSubmit(i))
.width(Length::Fill)
.into()
} else {
let content = container(column![
text(["Attack", "Decay", "Sustain", "Release"][i]).size(10),
text(value).size(10)
])
.padding(2)
.width(Length::Fill);
mouse_area(content)
.on_press(AdsrMessage::Assign(i))
.on_double_click(AdsrMessage::Edit(i))
.into()
};
label
}))
.width(210);
column![canvas(graph).width(210).height(115), labels]
column![canvas(graph).width(210).height(96), labels]
.spacing(3)
.into()
}
Expand Down Expand Up @@ -209,7 +245,7 @@ impl Program<AdsrMessage> for Envelope {
}
for (label, position) in [
("level", Point::new(0.0, 0.0)),
("time", Point::new(183.0, 102.0)),
("time", Point::new(183.0, 78.0)),
] {
frame.fill_text(Text {
content: label.into(),
Expand Down
149 changes: 149 additions & 0 deletions src/knob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use iced::{
Alignment, Background, Border, Color, Element, Length, Theme,
widget::{column, container, text},
};

use crate::arch_range_slider::arch_range_slider;
use crate::arch_slider::arch_slider;

/// Data used to render a labeled knob and its value readout.
pub struct Knob {
pub label: String,
pub value: f32,
pub range: std::ops::RangeInclusive<f32>,
pub default: f32,
pub step: f32,
pub value_text: String,
pub assigned: bool,
}

/// A compact labeled knob with an optional amber modulation-assignment state.
pub fn knob<Message: Clone + 'static>(
knob: Knob,
on_change: impl Fn(f32) -> Message + 'static,
on_release: Message,
) -> Element<'static, Message> {
let min = *knob.range.start();
let max = *knob.range.end();
let mut slider = arch_slider(knob.range, knob.value.clamp(min, max), on_change)
.step(knob.step)
.double_click_reset(knob.default)
.on_release(on_release)
.fill_from_start()
.width(Length::Fixed(41.0))
.height(Length::Fixed(41.0));

if knob.assigned {
slider = slider
.filled_color(Color::from_rgb(0.82, 0.58, 0.08))
.handle_color(Color::from_rgb(1.0, 0.83, 0.10));
}

let assigned = knob.assigned;
container(
column![
text(knob.label)
.size(11)
.width(Length::Fill)
.align_x(iced::alignment::Horizontal::Center),
slider,
text(knob.value_text)
.size(10)
.width(Length::Fill)
.align_x(iced::alignment::Horizontal::Center)
]
.spacing(2)
.align_x(Alignment::Center),
)
.width(Length::Fixed(50.0))
.padding(2)
.style(move |_theme: &Theme| {
let border_color = if assigned {
Color::from_rgb(1.0, 0.83, 0.10)
} else {
Color::TRANSPARENT
};
container::Style {
background: assigned
.then(|| Background::Color(Color::from_rgba(1.0, 0.83, 0.10, 0.10))),
border: Border {
color: border_color,
width: if assigned { 1.0 } else { 0.0 },
radius: 3.0.into(),
},
..container::Style::default()
}
})
.into()
}

/// Data used to render a knob with two independently adjustable range handles.
pub struct RangeKnob {
pub label: String,
pub low: f32,
pub high: f32,
pub range: std::ops::RangeInclusive<f32>,
pub step: f32,
pub value_text: String,
pub assigned: bool,
}

/// A compact labeled range knob, styled like [`knob`].
pub fn range_knob<Message: Clone + 'static>(
knob: RangeKnob,
on_change: impl Fn(f32, f32) -> Message + 'static,
on_release: Message,
) -> Element<'static, Message> {
let min = *knob.range.start();
let max = *knob.range.end();
let low = knob.low.clamp(min, max);
let high = knob.high.clamp(min, max);
let mut slider = arch_range_slider(knob.range, low.min(high), low.max(high), on_change)
.step(knob.step)
.on_release(on_release)
.width(Length::Fixed(41.0))
.height(Length::Fixed(41.0));

if knob.assigned {
slider = slider
.filled_color(Color::from_rgb(0.82, 0.58, 0.08))
.handle_color(Color::from_rgb(1.0, 0.83, 0.10));
}

let assigned = knob.assigned;
container(
column![
text(knob.label)
.size(11)
.width(Length::Fill)
.align_x(iced::alignment::Horizontal::Center),
slider,
text(knob.value_text)
.size(10)
.width(Length::Fill)
.align_x(iced::alignment::Horizontal::Center)
]
.spacing(2)
.align_x(Alignment::Center),
)
.width(Length::Fixed(50.0))
.padding(2)
.style(move |_theme: &Theme| {
let border_color = if assigned {
Color::from_rgb(1.0, 0.83, 0.10)
} else {
Color::TRANSPARENT
};
container::Style {
background: assigned
.then(|| Background::Color(Color::from_rgba(1.0, 0.83, 0.10, 0.10))),
border: Border {
color: border_color,
width: if assigned { 1.0 } else { 0.0 },
radius: 3.0.into(),
},
..container::Style::default()
}
})
.into()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod drum;
pub mod horizontal_meter;
pub mod horizontal_scrollbar;
pub mod horizontal_slider;
pub mod knob;
pub mod menu;
pub mod meters;
pub mod midi;
Expand Down
Loading