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
use adw::subclass::prelude::*;
use gtk::{gio, glib, prelude::*, CompositeTemplate};

use crate::utils::BoundObject;

mod imp {
    use std::cell::Cell;

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(resource = "/org/gnome/Fractal/ui/components/media/audio_player.ui")]
    #[properties(wrapper_type = super::AudioPlayer)]
    pub struct AudioPlayer {
        /// The media file to play.
        #[property(get, set = Self::set_media_file, explicit_notify, nullable)]
        pub media_file: BoundObject<gtk::MediaFile>,
        /// Whether to play the media automatically.
        #[property(get, set = Self::set_autoplay, explicit_notify)]
        pub autoplay: Cell<bool>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for AudioPlayer {
        const NAME: &'static str = "AudioPlayer";
        type Type = super::AudioPlayer;
        type ParentType = adw::Bin;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

    #[glib::derived_properties]
    impl ObjectImpl for AudioPlayer {}

    impl WidgetImpl for AudioPlayer {}
    impl BinImpl for AudioPlayer {}

    impl AudioPlayer {
        /// Set the media_file to play.
        fn set_media_file(&self, media_file: Option<gtk::MediaFile>) {
            if self.media_file.obj() == media_file {
                return;
            }

            self.media_file.disconnect_signals();

            if let Some(media_file) = media_file {
                let mut handlers = Vec::new();

                if self.autoplay.get() {
                    let prepared_handler = media_file.connect_prepared_notify(|media_file| {
                        if media_file.is_prepared() {
                            media_file.play()
                        }
                    });
                    handlers.push(prepared_handler);
                }

                self.media_file.set(media_file, handlers);
            }

            self.obj().notify_media_file();
        }

        /// Set whether to play the media automatically.
        fn set_autoplay(&self, autoplay: bool) {
            if self.autoplay.get() == autoplay {
                return;
            }

            self.autoplay.set(autoplay);
            self.obj().notify_autoplay();
        }
    }
}

glib::wrapper! {
    /// A widget displaying a video media file.
    pub struct AudioPlayer(ObjectSubclass<imp::AudioPlayer>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

impl AudioPlayer {
    /// Create a new audio player.
    pub fn new() -> Self {
        glib::Object::new()
    }

    /// Set the file to play.
    ///
    /// This is a convenience method that calls
    /// [`AudioPlayer::set_media_file()`].
    pub fn set_file(&self, file: Option<&gio::File>) {
        self.set_media_file(file.map(gtk::MediaFile::for_file));
    }
}