fractal/session/model/room/timeline/
timeline_item.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use gtk::{glib, prelude::*, subclass::prelude::*};
use matrix_sdk_ui::timeline::{TimelineItem as SdkTimelineItem, TimelineItemKind};
use ruma::OwnedUserId;
use tracing::error;

use super::{Event, Timeline, VirtualItem};
use crate::session::model::Room;

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

    use super::*;

    #[repr(C)]
    pub struct TimelineItemClass {
        parent_class: glib::object::ObjectClass,
        pub(super) selectable: fn(&super::TimelineItem) -> bool,
        pub(super) can_hide_header: fn(&super::TimelineItem) -> bool,
        pub(super) event_sender_id: fn(&super::TimelineItem) -> Option<OwnedUserId>,
    }

    unsafe impl ClassStruct for TimelineItemClass {
        type Type = TimelineItem;
    }

    pub(super) fn timeline_item_selectable(this: &super::TimelineItem) -> bool {
        let klass = this.class();
        (klass.as_ref().selectable)(this)
    }

    pub(super) fn timeline_item_can_hide_header(this: &super::TimelineItem) -> bool {
        let klass = this.class();
        (klass.as_ref().can_hide_header)(this)
    }

    pub(super) fn timeline_item_event_sender_id(this: &super::TimelineItem) -> Option<OwnedUserId> {
        let klass = this.class();
        (klass.as_ref().event_sender_id)(this)
    }

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::TimelineItem)]
    pub struct TimelineItem {
        /// The timeline containing this `TimelineItem`.
        #[property(get, construct_only)]
        timeline: OnceCell<Timeline>,
        /// A unique ID for this `TimelineItem` in the local timeline.
        #[property(get, construct_only)]
        timeline_id: RefCell<String>,
        /// Whether this `TimelineItem` is selectable.
        ///
        /// Defaults to `false`.
        #[property(get = Self::selectable)]
        selectable: PhantomData<bool>,
        /// Whether this `TimelineItem` should show its header.
        ///
        /// Defaults to `false`.
        #[property(get, set = Self::set_show_header, explicit_notify)]
        show_header: Cell<bool>,
        /// Whether this `TimelineItem` is allowed to hide its header.
        ///
        /// Defaults to `false`.
        #[property(get = Self::can_hide_header)]
        can_hide_header: PhantomData<bool>,
        /// If this is a Matrix event, the sender of the event.
        ///
        /// Defaults to `None`.
        #[property(get = Self::event_sender_id)]
        event_sender_id: PhantomData<Option<String>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for TimelineItem {
        const NAME: &'static str = "TimelineItem";
        const ABSTRACT: bool = true;
        type Type = super::TimelineItem;
        type Class = TimelineItemClass;
    }

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

    impl TimelineItem {
        /// Whether this `TimelineItem` is selectable.
        ///
        /// Defaults to `false`.
        fn selectable(&self) -> bool {
            imp::timeline_item_selectable(&self.obj())
        }

        /// Set whether this `TimelineItem` should show its header.
        fn set_show_header(&self, show: bool) {
            if self.show_header.get() == show {
                return;
            }

            self.show_header.set(show);
            self.obj().notify_show_header();
        }

        /// Whether this `TimelineItem` is allowed to hide its header.
        ///
        /// Defaults to `false`.
        fn can_hide_header(&self) -> bool {
            imp::timeline_item_can_hide_header(&self.obj())
        }

        /// If this is a Matrix event, the sender of the event.
        ///
        /// Defaults to `None`.
        fn event_sender_id(&self) -> Option<String> {
            imp::timeline_item_event_sender_id(&self.obj()).map(Into::into)
        }
    }
}

glib::wrapper! {
    /// Interface implemented by items inside the `Timeline`.
    pub struct TimelineItem(ObjectSubclass<imp::TimelineItem>);
}

impl TimelineItem {
    /// Create a new `TimelineItem` with the given SDK timeline item.
    ///
    /// Constructs the proper child type.
    pub fn new(item: &SdkTimelineItem, timeline: &Timeline) -> Self {
        let timeline_id = &item.unique_id().0;

        match item.kind() {
            TimelineItemKind::Event(event_item) => {
                Event::new(timeline, event_item.clone(), timeline_id).upcast()
            }
            TimelineItemKind::Virtual(virtual_item) => {
                VirtualItem::with_item(timeline, virtual_item, timeline_id).upcast()
            }
        }
    }

    /// Update this `TimelineItem` with the given SDK timeline item.
    ///
    /// A `TimelineItem` should not be updated with a SDK item that has a
    /// different timeline ID.
    pub(crate) fn update_with(&self, item: &SdkTimelineItem) {
        if self.timeline_id() != item.unique_id().0 {
            error!("Should not update an item with a different timeline ID");
        }

        match item.kind() {
            TimelineItemKind::Event(new_event) => {
                if let Some(event) = self.downcast_ref::<Event>() {
                    event.update_with(new_event.clone());
                } else {
                    error!("Could not update a TimelineItem that is not an Event with an event SDK item");
                }
            }
            TimelineItemKind::Virtual(new_item) => {
                if let Some(virtual_item) = self.downcast_ref::<VirtualItem>() {
                    virtual_item.update_with_item(new_item);
                } else {
                    error!("Could not update a TimelineItem that is not a VirtualItem with a virtual SDK item");
                }
            }
        }
    }
}

/// Public trait containing implemented methods for everything that derives from
/// `TimelineItem`.
///
/// To override the behavior of these methods, override the corresponding method
/// of `TimelineItemImpl`.
#[allow(dead_code)]
pub(crate) trait TimelineItemExt: 'static {
    /// The timeline containing this `TimelineItem`.
    fn timeline(&self) -> Timeline;

    /// The room containing this `TimelineItem`.
    fn room(&self) -> Room {
        self.timeline().room()
    }

    /// A unique ID for this `TimelineItem` in the local timeline.
    fn timeline_id(&self) -> String;

    /// Whether this `TimelineItem` is selectable.
    ///
    /// Defaults to `false`.
    fn selectable(&self) -> bool;

    /// Whether this `TimelineItem` should show its header.
    ///
    /// Defaults to `false`.
    fn show_header(&self) -> bool;

    /// Set whether this `TimelineItem` should show its header.
    fn set_show_header(&self, show: bool);

    /// Whether this `TimelineItem` is allowed to hide its header.
    ///
    /// Defaults to `false`.
    fn can_hide_header(&self) -> bool;

    /// If this is a Matrix event, the sender of the event.
    ///
    /// Defaults to `None`.
    fn event_sender_id(&self) -> Option<OwnedUserId>;
}

impl<O: IsA<TimelineItem>> TimelineItemExt for O {
    fn timeline(&self) -> Timeline {
        self.upcast_ref().timeline()
    }

    fn timeline_id(&self) -> String {
        self.upcast_ref().timeline_id()
    }

    fn selectable(&self) -> bool {
        self.upcast_ref().selectable()
    }

    fn show_header(&self) -> bool {
        self.upcast_ref().show_header()
    }

    fn set_show_header(&self, show: bool) {
        self.upcast_ref().set_show_header(show);
    }

    fn can_hide_header(&self) -> bool {
        self.upcast_ref().can_hide_header()
    }

    fn event_sender_id(&self) -> Option<OwnedUserId> {
        imp::timeline_item_event_sender_id(self.upcast_ref())
    }
}

/// Public trait that must be implemented for everything that derives from
/// `TimelineItem`.
///
/// Overriding a method from this Trait overrides also its behavior in
/// `TimelineItemExt`.
pub(crate) trait TimelineItemImpl: ObjectImpl {
    fn selectable(&self) -> bool {
        false
    }

    fn can_hide_header(&self) -> bool {
        false
    }

    fn event_sender_id(&self) -> Option<OwnedUserId> {
        None
    }
}

// Make `TimelineItem` subclassable.
unsafe impl<T> IsSubclassable<T> for TimelineItem
where
    T: TimelineItemImpl,
    T::Type: IsA<TimelineItem>,
{
    fn class_init(class: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(class.upcast_ref_mut());

        let klass = class.as_mut();

        klass.selectable = selectable_trampoline::<T>;
        klass.can_hide_header = can_hide_header_trampoline::<T>;
        klass.event_sender_id = event_sender_id_trampoline::<T>;
    }
}

// Virtual method implementation trampolines.
fn selectable_trampoline<T>(this: &TimelineItem) -> bool
where
    T: ObjectSubclass + TimelineItemImpl,
    T::Type: IsA<TimelineItem>,
{
    let this = this.downcast_ref::<T::Type>().unwrap();
    this.imp().selectable()
}

fn can_hide_header_trampoline<T>(this: &TimelineItem) -> bool
where
    T: ObjectSubclass + TimelineItemImpl,
    T::Type: IsA<TimelineItem>,
{
    let this = this.downcast_ref::<T::Type>().unwrap();
    this.imp().can_hide_header()
}

fn event_sender_id_trampoline<T>(this: &TimelineItem) -> Option<OwnedUserId>
where
    T: ObjectSubclass + TimelineItemImpl,
    T::Type: IsA<TimelineItem>,
{
    let this = this.downcast_ref::<T::Type>().unwrap();
    this.imp().event_sender_id()
}