From 31d59fd0ee92fa07c34e1cbc8e5f2e298ebe5881 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sun, 9 Jan 2022 17:10:12 -0500 Subject: [PATCH] Set timestamp on newline --- src/controls.rs | 12 ++++++++++++ src/editor.rs | 2 +- src/lyrics.rs | 23 +++++++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/controls.rs b/src/controls.rs index 387937a..1233e9e 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -101,6 +101,18 @@ impl Controls { false } } + + /// Returns the current position of the playhead + /// + /// If there was an error, this will be `None`. In all other cases, this will return + /// `Some`. + pub fn position(&self) -> Option { + if let NoError { player, .. } = &self.error_state { + Some(player.position()) + } else { + None + } + } } impl Program for (&Controls, Theme) { diff --git a/src/editor.rs b/src/editor.rs index 1bff14b..cb6bed0 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -69,7 +69,7 @@ impl Editor { self.lyrics.insert_text(text); } pub fn handle_lyric_event(&mut self, line_no: usize, kind: LyricEvent) { - self.lyrics.handle_event(line_no, kind) + self.lyrics.handle_event(line_no, kind, || self.controls.position()) } pub fn current_line_mut(&mut self) -> (usize, &mut Lyric) { self.lyrics.current_line_mut() diff --git a/src/lyrics.rs b/src/lyrics.rs index dbd23b7..f1d3fc5 100644 --- a/src/lyrics.rs +++ b/src/lyrics.rs @@ -70,7 +70,7 @@ impl Lyrics { self.lines[line_no + n_pieces].select(); } - pub fn handle_event(&mut self, line_no: usize, kind: LyricEvent) { + pub fn handle_event(&mut self, line_no: usize, kind: LyricEvent, timestamp: impl Fn() -> Option) { match kind { LyricEvent::LyricChanged(newval) => { self.update_line(newval, line_no) @@ -79,7 +79,7 @@ impl Lyrics { self.lines[line_no].timestamp_update(newval) }, LyricEvent::LineAdvanced => { - self.advance_line(line_no) + self.advance_line(line_no, timestamp()) }, } } @@ -88,7 +88,7 @@ impl Lyrics { self.lines[line_no].value = new_content; } - pub fn advance_line(&mut self, current_line: usize) { + pub fn advance_line(&mut self, current_line: usize, timestamp: Option) { let new_line = current_line + 1; let line = if new_line == self.lines.len() { @@ -100,9 +100,11 @@ impl Lyrics { line.select(); - self.lines.get_mut(current_line) - .unwrap() - .deselect(); + let previous_line = self.lines.get_mut(current_line).unwrap(); + previous_line.deselect(); + if let Some(timestamp) = timestamp { + previous_line.set_timestamp(timestamp); + } } pub fn insert_line(&mut self, index: usize, content: Option) -> &mut Lyric { @@ -245,6 +247,15 @@ impl Lyric { self.timestamp_state.unfocus(); } + pub fn set_timestamp(&mut self, timestamp: Duration) { + self.timestamp = timestamp; + let seconds = timestamp.as_secs(); + let minutes = seconds / 60; + let seconds = seconds % 60; + let millis = timestamp.as_millis() % 1000; + self.timestamp_raw = format!("{}:{:02}.{:03}", minutes, seconds, millis); + } + pub (crate) fn timestamp_update(&mut self, newval: String) { if let Some((shift, validated)) = Self::clean_timestamp(newval) { self.timestamp = dbg!(Self::parse_validated_timestamp(&validated));