Skip to content

Commit 106033d

Browse files
authored
Merge pull request #108 from kevinaboos/update_to_include_portallist_changes
Update to include some new PortalList changes
2 parents dccbedb + 00604ff commit 106033d

File tree

3 files changed

+100
-37
lines changed

3 files changed

+100
-37
lines changed

Cargo.lock

+33-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/home/room_screen.rs

+67-3
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,49 @@ struct TimelineUiState {
804804
/// Currently this excludes avatars, as those are shared across multiple rooms.
805805
media_cache: MediaCache,
806806

807+
/// The index and scroll position of the first three events that have been drawn
808+
/// in the most recent draw pass of this timeline's PortalList.
809+
///
810+
/// We save three events because one of 3 adjacent timeline items is (practically)
811+
/// guaranteed to be a standard real event that has a true unique ID.
812+
/// (For example, not day dividers, not read markers, etc.)
813+
///
814+
/// If any of the `event_ids` are `Some`, this indicates that the timeline was
815+
/// fully cleared and is in the process of being restored via pagination,
816+
/// but it has not yet been paginated enough to the point where one of events
817+
/// in this list are visible.
818+
/// Once the timeline has been sufficiently paginated to display
819+
/// one of the events in this list, all `event_ids` should be set to `None`.`
820+
first_three_events: FirstDrawnEvents<3>,
821+
807822
/// The states relevant to the UI display of this timeline that are saved upon
808823
/// a `Hide` action and restored upon a `Show` action.
809824
saved_state: SavedState,
810825
}
811826

827+
/// The item index, scroll position, and optional unique IDs of the first `N` events
828+
/// that have been drawn in the most recent draw pass of a timeline's PortalList.
829+
#[derive(Debug)]
830+
struct FirstDrawnEvents<const N: usize> {
831+
index_and_scroll: [ItemIndexScroll; N],
832+
event_ids: [Option<OwnedEventId>; N],
833+
}
834+
impl<const N: usize> Default for FirstDrawnEvents<N> {
835+
fn default() -> Self {
836+
Self {
837+
index_and_scroll: std::array::from_fn(|_| ItemIndexScroll::default()),
838+
event_ids: std::array::from_fn(|_| None),
839+
}
840+
}
841+
}
842+
843+
///
844+
#[derive(Clone, Copy, Debug, Default)]
845+
struct ItemIndexScroll {
846+
index: usize,
847+
scroll: f64,
848+
}
849+
812850
/// States that are necessary to save in order to maintain a consistent UI display for a timeline.
813851
///
814852
/// These are saved when navigating away from a timeline (upon `Hide`)
@@ -854,6 +892,7 @@ impl Timeline {
854892
content_drawn_since_last_update: RangeSet::new(),
855893
profile_drawn_since_last_update: RangeSet::new(),
856894
update_receiver,
895+
first_three_events: Default::default(),
857896
media_cache: MediaCache::new(MediaFormatConst::File, Some(update_sender)),
858897
saved_state: SavedState::default(),
859898
};
@@ -1026,11 +1065,26 @@ impl Widget for Timeline {
10261065
if items.is_empty() {
10271066
log!("Timeline::handle_event(): timeline was cleared for room {}", tl.room_id);
10281067

1029-
// TODO: Save the current first event ID before it gets removed
1068+
// If the bottom of the timeline (the last event) is visible, then we should
1069+
// set the timeline to live mode.
1070+
// If the bottom of the timelien is *not* visible, then we should
1071+
// set the timeline to Focused mode.
1072+
1073+
// TODO: Save the event IDs of the top 3 items before we apply this update,
1074+
// which indicates this timeline is in the process of being restored,
10301075
// such that we can jump back to that position later after applying this update.
10311076

10321077
// TODO: here we need to re-build the timeline via TimelineBuilder
10331078
// and set the TimelineFocus to one of the above-saved event IDs.
1079+
1080+
// TODO: the docs for `TimelineBuilder::with_focus()` claim that the timeline's focus mode
1081+
// can be changed after creation, but I do not see any methods to actually do that.
1082+
// <https://matrix-org.github.io/matrix-rust-sdk/matrix_sdk_ui/timeline/struct.TimelineBuilder.html#method.with_focus>
1083+
//
1084+
// As such, we probably need to create a new async request enum variant
1085+
// that tells the background async task to build a new timeline
1086+
// (either in live mode or focused mode around one or more events)
1087+
// and then replaces the existing timeline in ALL_ROOMS_INFO with the new one.
10341088
}
10351089

10361090
// Maybe todo?: we can often avoid the following loops that iterate over the `items` list
@@ -1126,8 +1180,10 @@ impl Widget for Timeline {
11261180

11271181
list.set_item_range(cx, 0, last_item_id);
11281182

1129-
while let Some(item_id) = list.next_visible_item(cx) {
1130-
// log!("Drawing item {}", item_id);
1183+
let mut item_index_and_scroll_iter = tl_state.first_three_events.index_and_scroll.iter_mut();
1184+
1185+
while let Some((item_id, scroll)) = list.next_visible_item_with_scroll(cx) {
1186+
// log!("Drawing item {} at scroll: {}", item_id, scroll_offset);
11311187
let item = if item_id == 0 {
11321188
list.item(cx, item_id, live_id!(TopSpace)).unwrap()
11331189
} else {
@@ -1139,6 +1195,14 @@ impl Widget for Timeline {
11391195
continue;
11401196
};
11411197

1198+
if let Some(index_and_scroll) = item_index_and_scroll_iter.next() {
1199+
// log!("########### Saving item ID {} and scroll {} for room {}, at_end? {}",
1200+
// item_id, scroll, room_id,
1201+
// if list.is_at_end() { "Y" } else { "N" },
1202+
// );
1203+
*index_and_scroll = ItemIndexScroll { index: item_id, scroll };
1204+
}
1205+
11421206
// Determine whether this item's content and profile have been drawn since the last update.
11431207
// Pass this state to each of the `populate_*` functions so they can attempt to re-use
11441208
// an item in the timeline's portallist that was previously populated, if one exists.

src/sliding_sync.rs

-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ async fn async_worker(mut receiver: UnboundedReceiver<MatrixRequest>) -> Result<
454454
log!("BUG: room info not found for send message request {room_id}");
455455
continue;
456456
};
457-
458457
room_info.timeline.clone()
459458
};
460459

0 commit comments

Comments
 (0)