Set timestamp on newline

This commit is contained in:
Emi Simpson 2022-01-09 17:10:12 -05:00
parent eddf56ae64
commit 31d59fd0ee
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
3 changed files with 30 additions and 7 deletions

View File

@ -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<Duration> {
if let NoError { player, .. } = &self.error_state {
Some(player.position())
} else {
None
}
}
}
impl Program<Message> for (&Controls, Theme) {

View File

@ -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()

View File

@ -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<Duration>) {
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<Duration>) {
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<String>) -> &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));