mxl_player_components/ui/codec_ranking/
widget.rs

1use mxl_relm4_components::relm4::{self, adw::prelude::*, gtk::glib::clone, prelude::*};
2
3use super::{
4    messages::{CodecRankingComponentInput, CodecRankingComponentOutput},
5    model::CodecRankingComponentModel,
6};
7
8#[relm4::component(pub)]
9impl Component for CodecRankingComponentModel {
10    type Init = super::CodecRankingComponentInit;
11    type Input = CodecRankingComponentInput;
12    type Output = CodecRankingComponentOutput;
13    type CommandOutput = ();
14
15    view! {
16        #[name(pref_group)]
17        adw::PreferencesGroup {
18        }
19    }
20
21    // Initialize the component.
22    fn init(init: Self::Init, root: Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
23        let model = CodecRankingComponentModel {};
24
25        let widgets = view_output!();
26
27        widgets.pref_group.set_title(&init.title);
28
29        for ci in init.codec_info_list {
30            let switch = gtk::Switch::builder()
31                .valign(gtk::Align::Center)
32                .state(ci.enabled)
33                .active(ci.enabled)
34                .build();
35            switch.connect_state_notify(clone!(
36                #[strong]
37                sender,
38                #[strong(rename_to = codec_info)]
39                ci.clone(),
40                move |switch| {
41                    if switch.state() {
42                        sender.input(Self::Input::SetRank(codec_info.name.clone(), codec_info.default_rank));
43                    } else {
44                        sender.input(Self::Input::SetRank(codec_info.name.clone(), gst::Rank::NONE));
45                    }
46                }
47            ));
48            let row = adw::ActionRow::builder()
49                .title(ci.name.clone())
50                .subtitle(ci.long_name.clone())
51                .activatable(true)
52                .activatable_widget(&switch)
53                .build();
54            row.add_suffix(&switch);
55            widgets.pref_group.add(&row);
56        }
57
58        ComponentParts { model, widgets }
59    }
60
61    fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>, _: &Self::Root) {
62        match msg {
63            CodecRankingComponentInput::SetRank(name, rank) => {
64                sender
65                    .output_sender()
66                    .emit(CodecRankingComponentOutput::SetRank(name, rank));
67            }
68        }
69    }
70}