summary refs log tree commit diff
path: root/src/subtitle_selection_dialog.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/subtitle_selection_dialog.rs')
-rw-r--r--src/subtitle_selection_dialog.rs148
1 files changed, 86 insertions, 62 deletions
diff --git a/src/subtitle_selection_dialog.rs b/src/subtitle_selection_dialog.rs
index 6136d56..8e5d283 100644
--- a/src/subtitle_selection_dialog.rs
+++ b/src/subtitle_selection_dialog.rs
@@ -2,37 +2,47 @@ use adw::prelude::*;
 use gtk::gio;
 use relm4::prelude::*;
 
+use crate::subtitles::{MetadataCollection, StreamIndex};
 use crate::track_selector::{
     TrackInfo, TrackSelector, TrackSelectorInit, TrackSelectorMsg, TrackSelectorOutput,
 };
-use crate::tracks::{SUBTITLE_TRACKS, StreamIndex};
+
+#[derive(Clone, Copy, Default, Debug)]
+pub struct SubtitleSettings {
+    pub primary_track_ix: Option<StreamIndex>,
+    pub secondary_track_ix: Option<StreamIndex>,
+    pub show_secondary: bool,
+    pub show_machine_translation: bool,
+}
 
 pub struct SubtitleSelectionDialog {
     parent_window: adw::ApplicationWindow,
     dialog: adw::PreferencesDialog,
-    track_list_model: gio::ListStore,
     primary_selector: Controller<TrackSelector>,
     secondary_selector: Controller<TrackSelector>,
-    primary_track_ix: Option<StreamIndex>,
-    secondary_track_ix: Option<StreamIndex>,
+
+    settings: SubtitleSettings,
 }
 
 #[derive(Debug)]
 pub enum SubtitleSelectionDialogMsg {
     Show,
-    PrimaryTrackChanged(Option<StreamIndex>),
-    SecondaryTrackChanged(Option<StreamIndex>),
+    Close,
+    // ui messages
+    PrimaryTrackChanged(StreamIndex),
+    SecondaryTrackChanged(StreamIndex),
+    ShowSecondaryChanged(bool),
+    ShowMachineTranslationChanged(bool),
 }
 
 #[derive(Debug)]
 pub enum SubtitleSelectionDialogOutput {
-    PrimaryTrackSelected(Option<StreamIndex>),
-    SecondaryTrackSelected(Option<StreamIndex>),
+    ApplySubtitleSettings(SubtitleSettings),
 }
 
 #[relm4::component(pub)]
 impl SimpleComponent for SubtitleSelectionDialog {
-    type Init = adw::ApplicationWindow;
+    type Init = (adw::ApplicationWindow, MetadataCollection);
     type Input = SubtitleSelectionDialogMsg;
     type Output = SubtitleSelectionDialogOutput;
 
@@ -41,22 +51,50 @@ impl SimpleComponent for SubtitleSelectionDialog {
         adw::PreferencesDialog {
             set_title: "Subtitle Settings",
             add: &page,
+            connect_closed => SubtitleSelectionDialogMsg::Close,
         },
 
         #[name(page)]
         adw::PreferencesPage {
             adw::PreferencesGroup {
                 model.primary_selector.widget(),
-                model.secondary_selector.widget(),
+
+                adw::ExpanderRow {
+                    set_title: "Show secondary subtitles",
+                    set_subtitle: "Enable this if there exist subtitles a language you already know",
+                    set_show_enable_switch: true,
+                    #[watch]
+                    set_enable_expansion: model.settings.show_secondary,
+                    connect_enable_expansion_notify[sender] => move |expander_row| {
+                        sender.input(SubtitleSelectionDialogMsg::ShowSecondaryChanged(expander_row.enables_expansion()))
+                    },
+
+                    add_row: model.secondary_selector.widget(),
+                },
+
+                adw::ExpanderRow {
+                    set_title: "Show machine translations",
+                    set_subtitle: "This is useful in case there are no subtitles in your native language or you prefer a more direct translation of the primary subtitles",
+                    set_show_enable_switch: true,
+                    #[watch]
+                    set_enable_expansion: model.settings.show_machine_translation,
+                    connect_enable_expansion_notify[sender] => move |expander_row| {
+                        sender.input(SubtitleSelectionDialogMsg::ShowMachineTranslationChanged(expander_row.enables_expansion()))
+                    },
+
+                    // TODO add row for language selection
+                },
             }
         },
     }
 
     fn init(
-        parent_window: Self::Init,
+        init: Self::Init,
         root: Self::Root,
         sender: ComponentSender<Self>,
     ) -> ComponentParts<Self> {
+        let (parent_window, metadata) = init;
+
         let primary_selector = TrackSelector::builder()
             .launch(TrackSelectorInit {
                 title: "Primary subtitle track",
@@ -81,73 +119,59 @@ impl SimpleComponent for SubtitleSelectionDialog {
         let model = Self {
             parent_window,
             dialog: root.clone(),
-            track_list_model: gio::ListStore::new::<TrackInfo>(),
             primary_selector,
             secondary_selector,
-            primary_track_ix: None,
-            secondary_track_ix: None,
+            settings: Default::default(),
         };
 
         let widgets = view_output!();
 
+        let track_list_model = gio::ListStore::new::<TrackInfo>();
+        for (&stream_index, track_metadata) in metadata.subtitles.iter() {
+            let track_info = TrackInfo::new(
+                stream_index,
+                track_metadata.language.map(|lang| lang.to_name()),
+                track_metadata.title.clone(),
+            );
+            track_list_model.append(&track_info);
+        }
+
+        model
+            .primary_selector
+            .sender()
+            .send(TrackSelectorMsg::SetListModel(track_list_model.clone()))
+            .unwrap();
+        model
+            .secondary_selector
+            .sender()
+            .send(TrackSelectorMsg::SetListModel(track_list_model.clone()))
+            .unwrap();
+
         ComponentParts { model, widgets }
     }
 
     fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
         match msg {
             SubtitleSelectionDialogMsg::Show => {
-                self.update_track_list_model();
-
-                self.primary_selector
-                    .sender()
-                    .send(TrackSelectorMsg::SetListModel(
-                        self.track_list_model.clone(),
-                    ))
-                    .unwrap();
-                self.secondary_selector
-                    .sender()
-                    .send(TrackSelectorMsg::SetListModel(
-                        self.track_list_model.clone(),
-                    ))
-                    .unwrap();
-
                 self.dialog.present(Some(&self.parent_window));
             }
-            SubtitleSelectionDialogMsg::PrimaryTrackChanged(stream_index) => {
-                self.primary_track_ix = stream_index;
-                sender
-                    .output(SubtitleSelectionDialogOutput::PrimaryTrackSelected(
-                        stream_index,
-                    ))
-                    .unwrap();
+            SubtitleSelectionDialogMsg::Close => sender
+                .output(SubtitleSelectionDialogOutput::ApplySubtitleSettings(
+                    self.settings,
+                ))
+                .unwrap(),
+            SubtitleSelectionDialogMsg::PrimaryTrackChanged(stream_ix) => {
+                self.settings.primary_track_ix = Some(stream_ix);
             }
-            SubtitleSelectionDialogMsg::SecondaryTrackChanged(stream_index) => {
-                self.secondary_track_ix = stream_index;
-                sender
-                    .output(SubtitleSelectionDialogOutput::SecondaryTrackSelected(
-                        stream_index,
-                    ))
-                    .unwrap();
+            SubtitleSelectionDialogMsg::SecondaryTrackChanged(stream_ix) => {
+                self.settings.secondary_track_ix = Some(stream_ix);
+            }
+            SubtitleSelectionDialogMsg::ShowSecondaryChanged(val) => {
+                self.settings.show_secondary = val;
+            }
+            SubtitleSelectionDialogMsg::ShowMachineTranslationChanged(val) => {
+                self.settings.show_machine_translation = val;
             }
-        }
-    }
-}
-
-impl SubtitleSelectionDialog {
-    fn update_track_list_model(&mut self) {
-        let tracks = SUBTITLE_TRACKS.read();
-
-        // Clear previous entries
-        self.track_list_model.remove_all();
-
-        // Add all available tracks
-        for (&stream_index, track) in tracks.iter() {
-            let track_info = TrackInfo::new(
-                stream_index,
-                track.metadata.language.map(|lang| lang.to_name()),
-                track.metadata.title.clone(),
-            );
-            self.track_list_model.append(&track_info);
         }
     }
 }