summary refs log tree commit diff
path: root/src/subtitles/state.rs
diff options
context:
space:
mode:
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())
+    }
+}