From 66daa5bd0efd328b19e4ff80ecdf1fd4c779302a Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Thu, 30 Dec 2021 23:22:59 -0500 Subject: [PATCH] Add ability to advance lines --- src/app/lyrics.rs | 24 ++++++++++++++++++++++++ src/app/mod.rs | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/src/app/lyrics.rs b/src/app/lyrics.rs index 3be9d16..7fc8e2d 100644 --- a/src/app/lyrics.rs +++ b/src/app/lyrics.rs @@ -22,6 +22,21 @@ impl Lyrics { self.lines[line_no].set_content(new_content); } + 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(); + } + pub fn view(&mut self) -> Element { self.lines.iter_mut() .enumerate() @@ -52,10 +67,19 @@ impl Lyric { &self.value, move|new_value| Message::LyricChanged { line_no, new_value }, ) + .on_submit(Message::LineAdvanced(line_no)) .into() } pub fn set_content(&mut self, content: String) { self.value = content; } + + pub fn select(&mut self) { + self.state.focus(); + } + + pub fn deselect(&mut self) { + self.state.unfocus(); + } } diff --git a/src/app/mod.rs b/src/app/mod.rs index efd5765..a4a135b 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -21,6 +21,7 @@ pub enum Message { line_no: usize, new_value: String, }, + LineAdvanced(usize), } impl Sandbox for DelyriumApp { @@ -42,6 +43,9 @@ impl Sandbox for DelyriumApp { Message::LyricChanged { line_no, new_value } => { self.lyrics_component.update_line(new_value, line_no); }, + Message::LineAdvanced(current_line) => { + self.lyrics_component.advance_line(current_line); + } } }