72 lines
1.3 KiB
Rust
72 lines
1.3 KiB
Rust
|
use crate::app::Message;
|
||
|
use crate::app::Element;
|
||
|
|
||
|
use iced::widget::text_input::{self, TextInput};
|
||
|
use iced::widget::scrollable::{self, Scrollable};
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct Lyrics {
|
||
|
lines: Vec<Lyric>,
|
||
|
scroll_state: scrollable::State,
|
||
|
}
|
||
|
|
||
|
impl Lyrics {
|
||
|
pub fn new() -> Self {
|
||
|
Lyrics {
|
||
|
lines: vec![Lyric::new(0)],
|
||
|
scroll_state: scrollable::State::new()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn update(&mut self, message: &Message) {
|
||
|
self.lines.iter_mut()
|
||
|
.for_each(|l| l.update(message));
|
||
|
}
|
||
|
|
||
|
pub fn view(&mut self) -> Element<Message> {
|
||
|
self.lines.iter_mut()
|
||
|
.map(Lyric::view)
|
||
|
.fold(Scrollable::new(&mut self.scroll_state), |s, l| s.push(l))
|
||
|
.into()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct Lyric {
|
||
|
state: text_input::State,
|
||
|
value: String,
|
||
|
line_no: usize,
|
||
|
}
|
||
|
|
||
|
impl Lyric {
|
||
|
pub fn new(line_no: usize) -> Self {
|
||
|
Lyric {
|
||
|
state: text_input::State::new(),
|
||
|
value: String::with_capacity(70),
|
||
|
line_no,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn view(&mut self) -> Element<Message> {
|
||
|
let line_no = self.line_no;
|
||
|
TextInput::new(
|
||
|
&mut self.state,
|
||
|
"",
|
||
|
&self.value,
|
||
|
move|new_value| Message::LyricChanged { line_no, new_value },
|
||
|
)
|
||
|
.into()
|
||
|
}
|
||
|
|
||
|
pub fn update(&mut self, message: &Message) {
|
||
|
match message {
|
||
|
Message::LyricChanged {
|
||
|
line_no, new_value,
|
||
|
} if *line_no == self.line_no => {
|
||
|
self.value = new_value.clone();
|
||
|
},
|
||
|
_ => {},
|
||
|
}
|
||
|
}
|
||
|
}
|