audio_processor_iced_design_system/dropdown_with_label/
mod.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23use iced::Command;
24use iced::{
25    alignment, widget::pick_list, widget::Container, widget::Row, widget::Text, Alignment, Element,
26    Length,
27};
28
29use crate::spacing::Spacing;
30
31type Message = String;
32
33pub struct DropdownWithLabel {
34    label: String,
35    options: Vec<String>,
36    selected_option: Option<String>,
37}
38
39impl DropdownWithLabel {
40    pub fn new(
41        label: impl Into<String>,
42        options: Vec<String>,
43        selected_option: Option<impl Into<String>>,
44    ) -> Self {
45        DropdownWithLabel {
46            label: label.into(),
47            options,
48            selected_option: selected_option.map(|s| s.into()),
49        }
50    }
51
52    pub fn update(&mut self, message: Message) -> Command<Message> {
53        self.selected_option = Some(message);
54        Command::none()
55    }
56
57    pub fn view(&mut self) -> Element<Message> {
58        Row::with_children(vec![
59            Container::new(Text::new(&self.label))
60                .width(Length::FillPortion(2))
61                .align_x(alignment::Horizontal::Right)
62                .center_y()
63                .padding([0, Spacing::base_spacing()])
64                .into(),
65            Container::new(
66                pick_list::PickList::new(
67                    self.options.clone(),
68                    self.selected_option.clone(),
69                    |option| option,
70                )
71                .style(crate::style::PickList)
72                .padding(Spacing::base_spacing())
73                .width(Length::Fill),
74            )
75            .width(Length::FillPortion(8))
76            .into(),
77        ])
78        .width(Length::Fill)
79        .align_items(Alignment::Center)
80        .into()
81    }
82}
83
84#[cfg(feature = "story")]
85pub mod story {
86    use audio_processor_iced_storybook::StoryView;
87
88    use crate::string_vec;
89
90    use super::*;
91
92    pub fn default() -> Story {
93        Story::default()
94    }
95
96    pub struct Story {
97        dropdown: DropdownWithLabel,
98    }
99
100    impl Default for Story {
101        fn default() -> Self {
102            let dropdown = DropdownWithLabel::new(
103                "Dropdown label",
104                string_vec!["Option 1", "Option 2", "Option 3"],
105                Some("Option 2"),
106            );
107            Self { dropdown }
108        }
109    }
110
111    impl StoryView<Message> for Story {
112        fn update(&mut self, message: Message) -> Command<Message> {
113            self.dropdown.update(message)
114        }
115
116        fn view(&mut self) -> Element<Message> {
117            self.dropdown.view()
118        }
119    }
120}