2021-12-31 05:09:01 +00:00
|
|
|
use crate::app::Theme;
|
2021-12-30 22:32:52 +00:00
|
|
|
use crate::app::Message;
|
|
|
|
use crate::app::Element;
|
|
|
|
|
2021-12-31 05:09:01 +00:00
|
|
|
use iced::Align;
|
|
|
|
use iced::Length;
|
2021-12-30 22:32:52 +00:00
|
|
|
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 {
|
2021-12-31 05:09:01 +00:00
|
|
|
let mut lyric = Lyric::new();
|
|
|
|
lyric.select();
|
2021-12-30 22:32:52 +00:00
|
|
|
Lyrics {
|
2021-12-31 05:09:01 +00:00
|
|
|
lines: vec![lyric],
|
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
|
|
|
}
|
|
|
|
|
2021-12-31 04:22:59 +00:00
|
|
|
pub fn advance_line(&mut self, current_line: usize) {
|
|
|
|
let new_line = current_line + 1;
|
|
|
|
|
|
|
|
if new_line == self.lines.len() {
|
|
|
|
self.lines.push(Lyric::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
self.lines.get_mut(new_line)
|
|
|
|
.expect("Unexpected .advance_line with index beyond # of lines")
|
|
|
|
.select();
|
|
|
|
self.lines.get_mut(current_line)
|
|
|
|
.unwrap()
|
|
|
|
.deselect();
|
|
|
|
}
|
|
|
|
|
2021-12-31 05:09:01 +00:00
|
|
|
pub fn view(&mut self, theme: Theme) -> Element<Message> {
|
|
|
|
let is_sole_line = self.lines.len() == 1;
|
2021-12-30 22:32:52 +00:00
|
|
|
self.lines.iter_mut()
|
2021-12-31 04:01:08 +00:00
|
|
|
.enumerate()
|
2021-12-31 05:09:01 +00:00
|
|
|
.map(|(i, l)| l.view(is_sole_line, i, theme))
|
2021-12-30 22:32:52 +00:00
|
|
|
.fold(Scrollable::new(&mut self.scroll_state), |s, l| s.push(l))
|
2021-12-31 05:09:01 +00:00
|
|
|
.width(Length::Fill)
|
|
|
|
.align_items(Align::Center)
|
2021-12-30 22:32:52 +00:00
|
|
|
.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 05:09:01 +00:00
|
|
|
pub fn view(&mut self, show_placeholder: bool, line_no: usize, theme: Theme) -> Element<Message> {
|
|
|
|
|
|
|
|
let placeholder = if show_placeholder {
|
|
|
|
"Paste some lyrics to get started"
|
|
|
|
} else if self.state.is_focused() {
|
|
|
|
"..."
|
|
|
|
} else { "" };
|
|
|
|
|
|
|
|
let size = if self.state.is_focused() { 30 } else { 20 };
|
|
|
|
|
2021-12-30 22:32:52 +00:00
|
|
|
TextInput::new(
|
|
|
|
&mut self.state,
|
2021-12-31 05:09:01 +00:00
|
|
|
placeholder,
|
2021-12-30 22:32:52 +00:00
|
|
|
&self.value,
|
|
|
|
move|new_value| Message::LyricChanged { line_no, new_value },
|
|
|
|
)
|
2021-12-31 05:09:01 +00:00
|
|
|
.style(theme)
|
|
|
|
.size(size)
|
|
|
|
.width(Length::Units(300))
|
2021-12-31 04:22:59 +00:00
|
|
|
.on_submit(Message::LineAdvanced(line_no))
|
2021-12-30 22:32:52 +00:00
|
|
|
.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
|
|
|
}
|
2021-12-31 04:22:59 +00:00
|
|
|
|
|
|
|
pub fn select(&mut self) {
|
|
|
|
self.state.focus();
|
2021-12-31 05:09:01 +00:00
|
|
|
self.state.move_cursor_to_end();
|
2021-12-31 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deselect(&mut self) {
|
|
|
|
self.state.unfocus();
|
|
|
|
}
|
2021-12-30 22:32:52 +00:00
|
|
|
}
|