summary refs log tree commit diff
path: root/src/transcript.rs
blob: 2bddb72b7df527bab2ae9185146de495b55e0587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use gtk::{ListBox, pango::WrapMode, prelude::*};
use relm4::prelude::*;

use crate::subtitle_extractor::{StreamIndex, SubtitleCue, TRACKS};

#[derive(Debug)]
pub enum SubtitleCueOutput {
    SeekTo(gst::ClockTime),
}

#[relm4::factory(pub)]
impl FactoryComponent for SubtitleCue {
    type Init = Self;
    type Input = ();
    type Output = SubtitleCueOutput;
    type CommandOutput = ();
    type ParentWidget = gtk::ListBox;

    view! {
        gtk::Button {
            inline_css: "padding: 5px; border-radius: 0;",
            connect_clicked: {
                let start = self.start;
                move |_| {
                    sender.output(SubtitleCueOutput::SeekTo(start)).unwrap()
                }
            },

            gtk::Label {
                set_label: &self.text,
                set_wrap: true,
                set_wrap_mode: WrapMode::Word,
                set_xalign: 0.0,
                add_css_class: "body",
            }
        }
    }

    fn init_model(init: Self::Init, _index: &Self::Index, _sender: FactorySender<Self>) -> Self {
        init
    }
}

pub struct Transcript {
    active_stream_index: Option<StreamIndex>,
    active_cues: FactoryVecDeque<SubtitleCue>,
    pending_scroll: Option<usize>,
}

#[derive(Debug)]
pub enum TranscriptMsg {
    NewCue(StreamIndex, SubtitleCue),
    SelectTrack(StreamIndex),
    ScrollToCue(usize),
}

#[derive(Debug)]
pub enum TranscriptOutput {
    SeekTo(gst::ClockTime),
}

pub struct TranscriptWidgets {
    viewport: gtk::Viewport,
}

impl SimpleComponent for Transcript {
    type Init = ();
    type Input = TranscriptMsg;
    type Output = TranscriptOutput;
    type Widgets = TranscriptWidgets;
    type Root = gtk::ScrolledWindow;

    fn init(
        _init: Self::Init,
        root: Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let listbox = ListBox::builder()
            .selection_mode(gtk::SelectionMode::None)
            .build();

        let active_cues =
            FactoryVecDeque::builder()
                .launch(listbox)
                .forward(sender.output_sender(), |output| match output {
                    SubtitleCueOutput::SeekTo(pos) => TranscriptOutput::SeekTo(pos),
                });

        let model = Self {
            active_stream_index: None,
            active_cues,
            pending_scroll: None,
        };

        let widgets = TranscriptWidgets {
            viewport: gtk::Viewport::builder().build(),
        };

        widgets.viewport.set_child(Some(model.active_cues.widget()));
        root.set_child(Some(&widgets.viewport));

        ComponentParts { model, widgets }
    }

    fn init_root() -> Self::Root {
        gtk::ScrolledWindow::new()
    }

    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
        self.pending_scroll = None;

        match msg {
            TranscriptMsg::NewCue(stream_index, cue) => {
                if self.active_stream_index == Some(stream_index) {
                    self.active_cues.guard().push_back(cue);
                }
            }
            TranscriptMsg::SelectTrack(stream_index) => {
                self.active_stream_index = Some(stream_index);

                // Clear current widgets and populate with selected track's cues
                self.active_cues.guard().clear();
                let tracks = TRACKS.read();
                if let Some(track) = tracks.get(&stream_index) {
                    for cue in &track.cues {
                        self.active_cues.guard().push_back(cue.clone());
                    }
                }
            }
            TranscriptMsg::ScrollToCue(ix) => {
                self.pending_scroll = Some(ix);
            }
        }
    }

    fn update_view(&self, widgets: &mut Self::Widgets, _sender: ComponentSender<Self>) {
        if let Some(ix) = self.pending_scroll {
            if let Some(row) = self.active_cues.widget().row_at_index(ix as i32) {
                widgets.viewport.scroll_to(&row, None);
            }
        }
    }
}