summary refs log tree commit diff
path: root/src/subtitle_view.rs
blob: dc485618d26a8393f1cf32f80aa9eb12fff3419f (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
use crate::cue_view::{CueView, CueViewMsg, CueViewOutput};
use crate::util::OptionTracker;
use gtk::glib;
use gtk::prelude::*;
use relm4::prelude::*;

pub struct SubtitleView {
    primary_cue: Controller<CueView>,
    secondary_cue: OptionTracker<String>,
}

#[derive(Debug)]
pub enum SubtitleViewMsg {
    SetPrimaryCue(Option<String>),
    SetSecondaryCue(Option<String>),
}

#[derive(Debug)]
pub enum SubtitleViewOutput {
    SetHoveringCue(bool),
}

#[relm4::component(pub)]
impl SimpleComponent for SubtitleView {
    type Init = ();
    type Input = SubtitleViewMsg;
    type Output = SubtitleViewOutput;

    view! {
        gtk::ScrolledWindow {
            gtk::Box {
                set_orientation: gtk::Orientation::Vertical,
                set_vexpand: true,
                set_halign: gtk::Align::Center,

                gtk::Box {
                    set_vexpand: true,
                },

                model.primary_cue.widget(),

                gtk::Box {
                    set_vexpand: true,
                },

                gtk::Label {
                    #[track = "model.secondary_cue.is_dirty()"]
                    set_text: model.secondary_cue.get().as_ref().map(|val| val.as_str()).unwrap_or(""),
                    set_justify: gtk::Justification::Center,
                },

                gtk::Box {
                    set_vexpand: true,
                },
            }
        },
    }

    fn init(
        _init: Self::Init,
        root: Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let model = Self {
            primary_cue: CueView::builder()
                .launch(())
                .forward(sender.output_sender(), |output| match output {
                    CueViewOutput::MouseEnter => SubtitleViewOutput::SetHoveringCue(true),
                    CueViewOutput::MouseLeave => SubtitleViewOutput::SetHoveringCue(false),
                }),
            secondary_cue: OptionTracker::default(),
        };

        let widgets = view_output!();

        ComponentParts { model, widgets }
    }

    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
        // Reset trackers
        self.secondary_cue.reset();

        match msg {
            SubtitleViewMsg::SetPrimaryCue(value) => {
                self.primary_cue
                    .sender()
                    .send(CueViewMsg::SetText(value))
                    .unwrap();
            }
            SubtitleViewMsg::SetSecondaryCue(value) => {
                self.secondary_cue.set(value);
            }
        }
    }
}