summary refs log tree commit diff
path: root/src/subtitles/state.rs
diff options
context:
space:
mode:
authorMalte Voos <git@mal.tc>2025-12-05 15:35:38 +0100
committerMalte Voos <git@mal.tc>2025-12-05 15:43:58 +0100
commitc347b6133365dcf1b7da4e77890b20d04d6cfba4 (patch)
treec83aac6f7d1e6edc57e607f01e5d3eeee8da4a0e /src/subtitles/state.rs
parent652b1c2a0ce7db4885ebc51f7f09133a43401442 (diff)
downloadlleap-c347b6133365dcf1b7da4e77890b20d04d6cfba4.tar.gz
lleap-c347b6133365dcf1b7da4e77890b20d04d6cfba4.zip
implement machine translation; various fixes and refactorings HEAD main
Diffstat (limited to 'src/subtitles/state.rs')
-rw-r--r--src/subtitles/state.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/subtitles/state.rs b/src/subtitles/state.rs
new file mode 100644
index 0000000..6b1ebda
--- /dev/null
+++ b/src/subtitles/state.rs
@@ -0,0 +1,63 @@
+use crate::{
+    subtitles::{SUBTITLE_TRACKS, StreamIndex},
+    translation::TRANSLATIONS,
+    util::Tracker,
+};
+
+#[derive(Default)]
+pub struct SubtitleState {
+    pub stream_ix: Option<StreamIndex>,
+    pub last_started_cue_ix: Tracker<Option<usize>>,
+    pub last_ended_cue_ix: Tracker<Option<usize>>,
+}
+
+#[derive(Clone, Copy, Debug)]
+pub struct CueAddress(pub StreamIndex, pub usize);
+
+impl SubtitleState {
+    pub fn active_cue(&self) -> Option<CueAddress> {
+        if let Some(stream_ix) = self.stream_ix {
+            match (*self.last_started_cue_ix, *self.last_ended_cue_ix) {
+                (None, _) => None,
+                (Some(started_ix), None) => Some(CueAddress(stream_ix, started_ix)),
+                (Some(started_ix), Some(ended_ix)) => {
+                    if started_ix > ended_ix {
+                        Some(CueAddress(stream_ix, started_ix))
+                    } else {
+                        None
+                    }
+                }
+            }
+        } else {
+            None
+        }
+    }
+
+    pub fn is_dirty(&self) -> bool {
+        self.last_started_cue_ix.is_dirty() || self.last_ended_cue_ix.is_dirty()
+    }
+
+    pub fn reset(&mut self) {
+        self.last_started_cue_ix.reset();
+        self.last_ended_cue_ix.reset();
+    }
+
+    pub fn set_stream_ix(&mut self, stream_ix: Option<StreamIndex>) {
+        self.stream_ix = stream_ix;
+        self.last_started_cue_ix.set(None);
+        self.last_ended_cue_ix.set(None);
+    }
+}
+
+impl CueAddress {
+    pub fn resolve_text(&self) -> String {
+        SUBTITLE_TRACKS.read().get(&self.0).unwrap().texts[self.1].clone()
+    }
+
+    pub fn resolve_translation(&self) -> Option<String> {
+        TRANSLATIONS
+            .read()
+            .get(&self.0)
+            .and_then(|tln| tln.get(self.1).cloned())
+    }
+}