audio_processor_iced_design_system/dropdown_with_label/
mod.rs1use 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}