iced_audio 0.13.2

An extension to the Iced GUI library with useful widgets for audio applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Various styles for the [`VSlider`] widget
//!
//! [`VSlider`]: ../native/v_slider/struct.VSlider.html

use crate::{
    Offset,
    style::{default_colors, text_marks, tick_marks},
};
use iced_core::{Color, Rectangle, image::Handle};

/// The appearance of a [`VSlider`].
///
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone)]
pub enum Appearance {
    /// Uses an image texture for the handle
    ///
    /// The `image` feature in Iced must be enabled for this to show up
    Texture(TextureAppearance),
    /// modeled after hardware sliders
    Classic(ClassicAppearance),
    /// a modern style with a line inside a filled rectangle
    Rect(RectAppearance),
    /// same as `Rect` but can have different colors for left,
    /// right, and center positions
    RectBipolar(RectBipolarAppearance),
}

/// A classic line rail style
#[derive(Debug, Clone)]
pub struct ClassicRail {
    /// Colors of the left and right of the rail
    pub rail_colors: (Color, Color),
    /// Width (thickness) of the left and right of the rail
    pub rail_widths: (f32, f32),
    /// The padding from the rail to the top and bottom edges of the widget
    pub rail_padding: f32,
}

/// A [`Appearance`] for a [`VSlider`] that uses an image texture for the handle
///
/// [`Appearance`]: enum.Appearance.html
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
/// [`Handle`]: https://docs.rs/iced/latest/iced/pure/widget/image/struct.Handle.html
#[derive(Debug, Clone)]
pub struct TextureAppearance {
    /// The rail style
    pub rail: ClassicRail,
    /// The [`Handle`] to the image texture
    ///
    /// [`Handle`]: https://docs.rs/iced/latest/iced/pure/widget/image/struct.Handle.html
    pub image_handle: Handle,
    /// The effective height of the handle (not including any padding on the texture)
    pub handle_height: u16,
    /// The bounds of the image texture, where the origin is in the
    /// center of the handle.
    pub image_bounds: Rectangle,
}

/// A classic [`Appearance`] for a [`VSlider`], modeled after hardware sliders
///
/// [`Appearance`]: enum.Appearance.html
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
/// [`ClassicHandle`]: struct.ClassicHandle.html
#[derive(Debug, Clone)]
pub struct ClassicAppearance {
    /// The rail style
    pub rail: ClassicRail,
    /// a `ClassicHandle` defining the style of the handle
    pub handle: ClassicHandle,
}

impl Default for ClassicAppearance {
    fn default() -> Self {
        ClassicAppearance {
            rail: ClassicRail {
                rail_colors: default_colors::SLIDER_RAIL,
                rail_widths: (1.0, 1.0),
                rail_padding: 12.0,
            },
            handle: ClassicHandle::default(),
        }
    }
}

/// The [`ClassicStyle`] appearance of the handle of a [`VSlider`]
///
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
/// [`ClassicStyle`]: struct.ClassicStyle.html
#[derive(Debug, Clone)]
pub struct ClassicHandle {
    /// background color
    pub color: Color,
    /// height of the handle
    pub height: u16,
    /// the width (thickness) of the middle notch
    pub notch_width: f32,
    /// color of the middle notch
    pub notch_color: Color,
    /// radius of the background rectangle
    pub border_radius: f32,
    /// width of the background rectangle
    pub border_width: f32,
    /// color of the background rectangle border
    pub border_color: Color,
}

impl Default for ClassicHandle {
    fn default() -> Self {
        ClassicHandle {
            color: default_colors::LIGHT_BACK,
            height: 34,
            notch_width: 4.0,
            notch_color: default_colors::BORDER,
            border_radius: 2.0,
            border_color: default_colors::BORDER,
            border_width: 1.0,
        }
    }
}

/// A modern [`Appearance`] for a [`VSlider`]. It is composed of a background
/// rectangle and a rectangular handle.
///
/// [`Appearance`]: enum.Appearance.html
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone, Copy)]
pub struct RectAppearance {
    /// color of the background rectangle
    pub back_color: Color,
    /// width of the background rectangle border
    pub back_border_width: f32,
    /// radius of the background rectangle
    pub back_border_radius: f32,
    /// color of the background rectangle border
    pub back_border_color: Color,
    /// color of a filled portion in the background rectangle
    pub filled_color: Color,
    /// color of the handle rectangle
    pub handle_color: Color,
    /// height of the handle rectangle
    pub handle_height: u16,
    /// height of the gap between the handle and the filled
    /// portion of the background rectangle
    pub handle_filled_gap: f32,
}

/// A modern [`Appearance`] for a [`VSlider`]. It is composed of a background
/// rectangle and a rectangular handle. It has different colors for left, right,
/// and center values.
///
/// [`Appearance`]: enum.Appearance.html
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone, Copy)]
pub struct RectBipolarAppearance {
    /// color of the background rectangle
    pub back_color: Color,
    /// width of the background rectangle border
    pub back_border_width: f32,
    /// radius of the background rectangle
    pub back_border_radius: f32,
    /// color of the background rectangle border
    pub back_border_color: Color,
    /// color of a filled portion in the background
    /// rectangle on the top side of the center
    pub top_filled_color: Color,
    /// color of a filled portion in the background
    /// rectangle on the bottom side of the center
    pub bottom_filled_color: Color,
    /// color of the handle rectangle when it is on the
    /// top side of the center
    pub handle_top_color: Color,
    /// color of the handle rectangle when it is on the
    /// bottom side of the center
    pub handle_bottom_color: Color,
    /// color of the handle rectangle when it is in the center
    pub handle_center_color: Color,
    /// height of the handle rectangle
    pub handle_height: u16,
    /// height of the gap between the handle and the filled
    /// portion of the background rectangle
    pub handle_filled_gap: f32,
}

/// The position of a [`ModRangeStyle`] ring for a [`VSlider`]
///
/// [`ModRangeStyle`]: struct.ModRangeStyle.html
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone)]
pub enum ModRangePlacement {
    /// In the center of the widget
    Center {
        /// The width of the mod range.
        width: f32,
        /// The offset from the center of the widget.
        offset: f32,
    },
    /// In the center of the widget while filling the width
    /// of the widget.
    CenterFilled {
        /// The padding from the left and right edges of the widget.
        edge_padding: f32,
    },
    /// To the left of the widget
    Left {
        /// The width of the mod range.
        width: f32,
        /// The offset from the left edge of the widget.
        offset: f32,
    },
    /// To the right of the widget
    Right {
        /// The width of the mod range.
        width: f32,
        /// The offset from the right edge of the widget.
        offset: f32,
    },
}

/// A style for a [`ModulationRange`] line for a [`VSlider`]
///
/// [`ModulationRange`]: ../../core/struct.ModulationRange.html
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone)]
pub struct ModRangeAppearance {
    /// The placement of the line relative to the widget
    pub placement: ModRangePlacement,
    /// The width of the background border.
    pub back_border_width: f32,
    /// The radius of the background border.
    pub back_border_radius: f32,
    /// The color of the background border.
    pub back_border_color: Color,
    /// The color of the background.
    /// Set to `None` for no background.
    pub back_color: Option<Color>,
    /// The color of a filled portion of the line.
    pub filled_color: Color,
    /// The color of a filled portion of the line when `end` is less than
    /// `start`.
    pub filled_inverse_color: Color,
}

/// Style of tick marks for a [`VSlider`].
///
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone)]
pub struct TickMarksAppearance {
    /// The style of the tick marks
    pub style: tick_marks::Appearance,
    /// The placement of the tick marks
    pub placement: tick_marks::Placement,
}

/// Style of text marks for a [`VSlider`].
///
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
#[derive(Debug, Clone)]
pub struct TextMarksAppearance {
    /// The style of the text marks
    pub style: text_marks::Appearance,
    /// The placement of the text marks
    pub placement: text_marks::Placement,
}

/// A set of rules that dictate the style of a [`VSlider`].
///
/// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
pub trait StyleSheet {
    /// The supported style of the [`StyleSheet`].
    type Style;

    /// Produces the style of an active [`VSlider`].
    ///
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn active(&self, style: &Self::Style) -> Appearance;

    /// Produces the style of a hovered [`VSlider`].
    ///
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn hovered(&self, style: &Self::Style) -> Appearance;

    /// Produces the style of a [`VSlider`] that is being dragged.
    ///
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn dragging(&self, style: &Self::Style) -> Appearance;

    /// The style of tick marks for a [`VSlider`]
    ///
    /// For no tick marks, don't override this or set this to return `None`.
    ///
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn tick_marks_appearance(&self, _style: &Self::Style) -> Option<TickMarksAppearance> {
        None
    }

    /// The style of an [`ModulationRange`] line for a [`VSlider`]
    ///
    /// For no modulation range line, don't override this or set this to return `None`.
    ///
    /// [`ModulationRange`]: ../../core/struct.ModulationRange.html
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn mod_range_appearance(&self, _style: &Self::Style) -> Option<ModRangeAppearance> {
        None
    }

    /// The style of a second [`ModulationRange`] line for a [`VSlider`]
    ///
    /// For no second modulation range line, don't override this or set this to return `None`.
    ///
    /// [`ModulationRange`]: ../../core/struct.ModulationRange.html
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn mod_range_appearance_2(&self, _style: &Self::Style) -> Option<ModRangeAppearance> {
        None
    }

    /// The style of text marks for a [`VSlider`]
    ///
    /// For no text marks, don't override this or set this to return `None`.
    ///
    /// [`VSlider`]: ../../native/v_slider/struct.VSlider.html
    fn text_marks_appearance(&self, _style: &Self::Style) -> Option<TextMarksAppearance> {
        None
    }
}

/// The style of a VSlider.
#[derive(Default)]
pub enum VSlider {
    /// The default style.
    #[default]
    Default,
    /// A custom style.
    Custom(Box<dyn StyleSheet<Style = iced_core::Theme>>),
}

impl<S> From<S> for VSlider
where
    S: 'static + StyleSheet<Style = iced_core::Theme>,
{
    fn from(val: S) -> Self {
        VSlider::Custom(Box::new(val))
    }
}

impl StyleSheet for iced_core::Theme {
    type Style = VSlider;

    fn active(&self, style: &Self::Style) -> Appearance {
        match style {
            VSlider::Default => Appearance::Classic(Default::default()),
            VSlider::Custom(custom) => custom.active(self),
        }
    }

    fn hovered(&self, style: &Self::Style) -> Appearance {
        match style {
            VSlider::Default => Appearance::Classic(ClassicAppearance {
                handle: ClassicHandle {
                    color: default_colors::LIGHT_BACK_HOVER,
                    ..Default::default()
                },
                ..Default::default()
            }),
            VSlider::Custom(custom) => custom.hovered(self),
        }
    }

    fn dragging(&self, style: &Self::Style) -> Appearance {
        match style {
            VSlider::Default => Appearance::Classic(ClassicAppearance {
                handle: ClassicHandle {
                    color: default_colors::LIGHT_BACK_DRAG,
                    ..Default::default()
                },
                ..Default::default()
            }),
            VSlider::Custom(custom) => custom.dragging(self),
        }
    }

    fn tick_marks_appearance(&self, style: &Self::Style) -> Option<TickMarksAppearance> {
        match style {
            VSlider::Default => Some(TickMarksAppearance {
                style: tick_marks::Appearance {
                    tier_1: tick_marks::Shape::Line {
                        length: 24.0,
                        width: 2.0,
                        color: default_colors::TICK_TIER_1,
                    },
                    tier_2: tick_marks::Shape::Line {
                        length: 22.0,
                        width: 1.0,
                        color: default_colors::TICK_TIER_2,
                    },
                    tier_3: tick_marks::Shape::Line {
                        length: 18.0,
                        width: 1.0,
                        color: default_colors::TICK_TIER_3,
                    },
                },
                placement: tick_marks::Placement::Center {
                    offset: Offset::ZERO,
                    fill_length: false,
                },
            }),
            VSlider::Custom(custom) => custom.tick_marks_appearance(self),
        }
    }

    fn mod_range_appearance(&self, style: &Self::Style) -> Option<ModRangeAppearance> {
        match style {
            VSlider::Default => None,
            VSlider::Custom(custom) => custom.mod_range_appearance(self),
        }
    }

    fn mod_range_appearance_2(&self, style: &Self::Style) -> Option<ModRangeAppearance> {
        match style {
            VSlider::Default => None,
            VSlider::Custom(custom) => custom.mod_range_appearance_2(self),
        }
    }

    fn text_marks_appearance(&self, style: &Self::Style) -> Option<TextMarksAppearance> {
        match style {
            VSlider::Default => Some(TextMarksAppearance {
                style: Default::default(),
                placement: text_marks::Placement::LeftOrTop {
                    inside: false,
                    offset: Offset { x: -7.0, y: 0.0 },
                },
            }),
            VSlider::Custom(custom) => custom.text_marks_appearance(self),
        }
    }
}