fractal/session/view/content/room_history/message_row/
caption.rs

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 gtk::{glib, prelude::*, subclass::prelude::*};
use ruma::events::room::message::FormattedBody;

use super::{text::MessageText, ContentFormat};
use crate::session::model::Room;

mod imp {
    use std::marker::PhantomData;

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::MessageCaption)]
    pub struct MessageCaption {
        /// The widget displaying the file alongside the caption.
        #[property(get = Self::child, set = Self::set_child, explicit_notify, nullable)]
        pub child: PhantomData<Option<gtk::Widget>>,
        pub caption_widget: MessageText,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MessageCaption {
        const NAME: &'static str = "ContentMessageCaption";
        type Type = super::MessageCaption;
        type ParentType = gtk::Grid;

        fn class_init(klass: &mut Self::Class) {
            klass.set_css_name("message-caption");

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

    #[glib::derived_properties]
    impl ObjectImpl for MessageCaption {
        fn constructed(&self) {
            self.parent_constructed();

            let obj = self.obj();
            obj.attach(&self.caption_widget, 0, 1, 1, 1);
            obj.set_row_spacing(6);
        }
    }

    impl WidgetImpl for MessageCaption {}
    impl GridImpl for MessageCaption {}

    impl MessageCaption {
        /// The widget displaying the file alongside the caption.
        fn child(&self) -> Option<gtk::Widget> {
            self.obj().child_at(0, 0)
        }

        /// Set the widget displaying the file alongside the caption.
        fn set_child(&self, widget: Option<gtk::Widget>) {
            let prev_widget = self.child();

            if prev_widget == widget {
                return;
            }
            let obj = self.obj();

            if let Some(widget) = prev_widget {
                obj.remove(&widget);
            }

            if let Some(widget) = widget {
                obj.attach(&widget, 0, 0, 1, 1);
            }

            obj.notify_child();
        }
    }
}

glib::wrapper! {
    /// A widget displaying a caption alongside a file message.
    pub struct MessageCaption(ObjectSubclass<imp::MessageCaption>)
        @extends gtk::Widget, gtk::Grid, @implements gtk::Accessible;
}

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

    /// Set the caption.
    pub fn set_caption(
        &self,
        caption: String,
        formatted_caption: Option<FormattedBody>,
        room: &Room,
        format: ContentFormat,
        detect_at_room: bool,
    ) {
        self.imp().caption_widget.with_markup(
            formatted_caption,
            caption,
            room,
            format,
            detect_at_room,
        );
    }
}