mirror of
https://github.com/aljazceru/notedeck.git
synced 2026-01-24 10:44:19 +01:00
reset virtual list if notes are spliced into timeline
Calling egui_virtual_list's `items_inserted_at_start` is incorrect if we insert notes inbetween other notes in the timeline. To prevent our list getting confused, let's handle this case explicitly by calling 'reset' when we splice notes in. Ideally we would update egui_virtual_list to handle spliced-in items, but we will leave that to a future update.
This commit is contained in:
62
src/app.rs
62
src/app.rs
@@ -7,7 +7,7 @@ use crate::imgcache::ImageCache;
|
||||
use crate::notecache::{CachedNote, NoteCache};
|
||||
use crate::route::Route;
|
||||
use crate::timeline;
|
||||
use crate::timeline::{NoteRef, Timeline, ViewFilter};
|
||||
use crate::timeline::{MergeKind, NoteRef, Timeline, ViewFilter};
|
||||
use crate::ui;
|
||||
use crate::ui::profile::SimpleProfilePreviewController;
|
||||
use crate::ui::DesktopSidePanel;
|
||||
@@ -381,24 +381,9 @@ fn poll_notes_for_timeline<'a>(
|
||||
|
||||
// ViewFilter::NotesAndReplies
|
||||
{
|
||||
let timeline = &mut damus.timelines[timeline_ind];
|
||||
|
||||
let prev_items = timeline.notes(ViewFilter::NotesAndReplies).len();
|
||||
|
||||
let refs: Vec<NoteRef> = new_refs.iter().map(|(_note, nr)| *nr).collect();
|
||||
timeline.view_mut(ViewFilter::NotesAndReplies).notes =
|
||||
timeline::merge_sorted_vecs(timeline.notes(ViewFilter::NotesAndReplies), &refs);
|
||||
|
||||
let new_items = timeline.notes(ViewFilter::NotesAndReplies).len() - prev_items;
|
||||
|
||||
// TODO: technically items could have been added inbetween
|
||||
if new_items > 0 {
|
||||
damus.timelines[timeline_ind]
|
||||
.view(ViewFilter::NotesAndReplies)
|
||||
.list
|
||||
.borrow_mut()
|
||||
.items_inserted_at_start(new_items);
|
||||
}
|
||||
insert_notes_into_timeline(damus, timeline_ind, ViewFilter::NotesAndReplies, &refs)
|
||||
}
|
||||
|
||||
//
|
||||
@@ -416,28 +401,37 @@ fn poll_notes_for_timeline<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
let timeline = &mut damus.timelines[timeline_ind];
|
||||
|
||||
let prev_items = timeline.notes(ViewFilter::Notes).len();
|
||||
|
||||
timeline.view_mut(ViewFilter::Notes).notes =
|
||||
timeline::merge_sorted_vecs(timeline.notes(ViewFilter::Notes), &filtered_refs);
|
||||
|
||||
let new_items = timeline.notes(ViewFilter::Notes).len() - prev_items;
|
||||
|
||||
// TODO: technically items could have been added inbetween
|
||||
if new_items > 0 {
|
||||
damus.timelines[timeline_ind]
|
||||
.view(ViewFilter::Notes)
|
||||
.list
|
||||
.borrow_mut()
|
||||
.items_inserted_at_start(new_items);
|
||||
}
|
||||
insert_notes_into_timeline(damus, timeline_ind, ViewFilter::Notes, &filtered_refs);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_notes_into_timeline(
|
||||
app: &mut Damus,
|
||||
timeline_ind: usize,
|
||||
filter: ViewFilter,
|
||||
new_refs: &[NoteRef],
|
||||
) {
|
||||
let timeline = &mut app.timelines[timeline_ind];
|
||||
let num_prev_items = timeline.notes(filter).len();
|
||||
let (notes, merge_kind) = timeline::merge_sorted_vecs(timeline.notes(filter), new_refs);
|
||||
|
||||
timeline.view_mut(filter).notes = notes;
|
||||
let new_items = timeline.notes(filter).len() - num_prev_items;
|
||||
|
||||
// TODO: technically items could have been added inbetween
|
||||
if new_items > 0 {
|
||||
let mut list = app.timelines[timeline_ind].view(filter).list.borrow_mut();
|
||||
|
||||
match merge_kind {
|
||||
// TODO: update egui_virtual_list to support spliced inserts
|
||||
MergeKind::Spliced => list.reset(),
|
||||
MergeKind::FrontInsert => list.items_inserted_at_start(new_items),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "profiling")]
|
||||
fn setup_profiling() {
|
||||
puffin::set_scopes_on(true); // tell puffin to collect data
|
||||
|
||||
@@ -35,6 +35,7 @@ impl PartialOrd for NoteRef {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub enum ViewFilter {
|
||||
Notes,
|
||||
NotesAndReplies,
|
||||
@@ -306,13 +307,24 @@ pub fn timeline_view(ui: &mut egui::Ui, app: &mut Damus, timeline: usize) {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn merge_sorted_vecs<T: Ord + Copy>(vec1: &[T], vec2: &[T]) -> Vec<T> {
|
||||
pub enum MergeKind {
|
||||
FrontInsert,
|
||||
Spliced,
|
||||
}
|
||||
|
||||
pub fn merge_sorted_vecs<T: Ord + Copy>(vec1: &[T], vec2: &[T]) -> (Vec<T>, MergeKind) {
|
||||
let mut merged = Vec::with_capacity(vec1.len() + vec2.len());
|
||||
let mut i = 0;
|
||||
let mut j = 0;
|
||||
let mut result: Option<MergeKind> = None;
|
||||
|
||||
while i < vec1.len() && j < vec2.len() {
|
||||
if vec1[i] <= vec2[j] {
|
||||
if result.is_none() && j < vec2.len() {
|
||||
// if we're pushing from our large list and still have
|
||||
// some left in vec2, then this is a splice
|
||||
result = Some(MergeKind::Spliced);
|
||||
}
|
||||
merged.push(vec1[i]);
|
||||
i += 1;
|
||||
} else {
|
||||
@@ -329,5 +341,5 @@ pub fn merge_sorted_vecs<T: Ord + Copy>(vec1: &[T], vec2: &[T]) -> Vec<T> {
|
||||
merged.extend_from_slice(&vec2[j..]);
|
||||
}
|
||||
|
||||
merged
|
||||
(merged, result.unwrap_or(MergeKind::FrontInsert))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user