2021-12-30 22:32:52 +00:00
|
|
|
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 {
|
2021-12-31 04:01:08 +00:00
|
|
|
lines: vec![Lyric::new()],
|
2021-12-30 22:32:52 +00:00
|
|
|
scroll_state: scrollable::State::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 04:01:08 +00:00
|
|
|
pub fn update_line(&mut self, new_content: String, line_no: usize) {
|
|
|
|
self.lines[line_no].set_content(new_content);
|
2021-12-30 22:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn view(&mut self) -> Element<Message> {
|
|
|
|
self.lines.iter_mut()
|
2021-12-31 04:01:08 +00:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, l)| l.view(i))
|
2021-12-30 22:32:52 +00:00
|
|
|
.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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lyric {
|
2021-12-31 04:01:08 +00:00
|
|
|
pub fn new() -> Self {
|
2021-12-30 22:32:52 +00:00
|
|
|
Lyric {
|
|
|
|
state: text_input::State::new(),
|
|
|
|
value: String::with_capacity(70),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 04:01:08 +00:00
|
|
|
pub fn view(&mut self, line_no: usize) -> Element<Message> {
|
2021-12-30 22:32:52 +00:00
|
|
|
TextInput::new(
|
|
|
|
&mut self.state,
|
|
|
|
"",
|
|
|
|
&self.value,
|
|
|
|
move|new_value| Message::LyricChanged { line_no, new_value },
|
|
|
|
)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
2021-12-31 04:01:08 +00:00
|
|
|
pub fn set_content(&mut self, content: String) {
|
|
|
|
self.value = content;
|
2021-12-30 22:32:52 +00:00
|
|
|
}
|
|
|
|
}
|