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

use super::ContentFormat;
use crate::gettext_f;

mod imp {
    use std::cell::{Cell, RefCell};

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/content/room_history/message_row/file.ui"
    )]
    #[properties(wrapper_type = super::MessageFile)]
    pub struct MessageFile {
        /// The filename of the file.
        #[property(get, set = Self::set_filename, explicit_notify, nullable)]
        pub filename: RefCell<Option<String>>,
        /// Whether this file should be displayed in a compact format.
        #[property(get, set = Self::set_compact, explicit_notify)]
        pub compact: Cell<bool>,
    }

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

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

            klass.set_accessible_role(gtk::AccessibleRole::Group);
        }

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

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

    impl WidgetImpl for MessageFile {}
    impl BinImpl for MessageFile {}

    impl MessageFile {
        /// Set the filename of the file.
        fn set_filename(&self, filename: Option<String>) {
            let filename = filename.filter(|s| !s.is_empty());

            if filename == *self.filename.borrow() {
                return;
            }

            let obj = self.obj();
            let accessible_label = if let Some(filename) = &filename {
                gettext_f("File: {filename}", &[("filename", filename)])
            } else {
                gettext("File")
            };
            obj.update_property(&[gtk::accessible::Property::Label(&accessible_label)]);

            self.filename.replace(filename);
            obj.notify_filename();
        }

        /// Set whether this file should be displayed in a compact format.
        fn set_compact(&self, compact: bool) {
            if self.compact.get() == compact {
                return;
            }

            self.compact.set(compact);
            self.obj().notify_compact();
        }
    }
}

glib::wrapper! {
    /// A widget displaying an interface to download the content of a file message.
    pub struct MessageFile(ObjectSubclass<imp::MessageFile>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

impl MessageFile {
    pub fn new() -> Self {
        glib::Object::new()
    }

    pub fn set_format(&self, format: ContentFormat) {
        self.set_compact(matches!(
            format,
            ContentFormat::Compact | ContentFormat::Ellipsized
        ));
    }
}

impl Default for MessageFile {
    fn default() -> Self {
        Self::new()
    }
}