use crate::{ subtitles::{SUBTITLE_TRACKS, StreamIndex}, translation::TRANSLATIONS, util::Tracker, }; #[derive(Default)] pub struct SubtitleState { pub stream_ix: Option, pub last_started_cue_ix: Tracker>, pub last_ended_cue_ix: Tracker>, } #[derive(Clone, Copy, Debug)] pub struct CueAddress(pub StreamIndex, pub usize); impl SubtitleState { pub fn active_cue(&self) -> Option { 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) { 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 { TRANSLATIONS .read() .get(&self.0) .and_then(|tln| tln.get(self.1).cloned()) } }