Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/home/link_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use url::Url;

use crate::{
home::room_screen::TimelineUpdate,
media_cache::MediaCache,
shared::{
styles::{COLOR_BG_PREVIEW, COLOR_BG_PREVIEW_HOVER},
text_or_image::{TextOrImageRef, TextOrImageWidgetRefExt},
Expand Down Expand Up @@ -315,11 +314,11 @@ impl LinkPreviewRef {
cx: &mut Cx,
link_preview_cache_entry: LinkPreviewCacheEntry,
link: &Url,
media_cache: &mut MediaCache,
update_sender: Option<crossbeam_channel::Sender<TimelineUpdate>>,
image_populate_fn: F,
) -> (ViewRef, bool)
where
F: FnOnce(&mut Cx, &TextOrImageRef, Option<Box<ImageInfo>>, MediaSource, &str, &mut MediaCache) -> bool,
F: FnOnce(&mut Cx, &TextOrImageRef, Option<Box<ImageInfo>>, MediaSource, &str, Option<crossbeam_channel::Sender<TimelineUpdate>>) -> bool,
{
let view_ref = WidgetRef::new_from_ptr(cx, self.item_template()).as_view();
let mut fully_drawn = true;
Expand Down Expand Up @@ -384,7 +383,7 @@ impl LinkPreviewRef {
image_info_source,
original_source,
"",
media_cache,
update_sender,
);
}

Expand All @@ -402,12 +401,12 @@ impl LinkPreviewRef {
&mut self,
cx: &mut Cx,
links: &Vec<url::Url>,
media_cache: &mut MediaCache,
update_sender: Option<crossbeam_channel::Sender<TimelineUpdate>>,
link_preview_cache: &mut LinkPreviewCache,
populate_image_fn: &F,
) -> bool
where
F: Fn(&mut Cx, &TextOrImageRef, Option<Box<ImageInfo>>, MediaSource, &str, &mut MediaCache) -> bool,
F: Fn(&mut Cx, &TextOrImageRef, Option<Box<ImageInfo>>, MediaSource, &str, Option<crossbeam_channel::Sender<TimelineUpdate>>) -> bool,
{
const SKIPPED_DOMAINS: &[&str] = &["matrix.to", "matrix.io"];
const MAX_LINK_PREVIEWS_BY_EXPAND: usize = 2;
Expand Down Expand Up @@ -437,9 +436,9 @@ impl LinkPreviewRef {
cx,
link_preview_cache.get_or_fetch_link_preview(url_string),
link,
media_cache,
|cx, text_or_image_ref, image_info_source, original_source, body, media_cache| {
populate_image_fn(cx, text_or_image_ref, image_info_source, original_source, body, media_cache)
update_sender.clone(),
|cx, text_or_image_ref, image_info_source, original_source, body, update_sender| {
populate_image_fn(cx, text_or_image_ref, image_info_source, original_source, body, update_sender)
},
);
fully_drawn_count += was_image_drawn as usize;
Expand Down
8 changes: 4 additions & 4 deletions src/home/room_image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use matrix_sdk::{
};
use reqwest::StatusCode;

use crate::{media_cache::{MediaCache, MediaCacheEntry}, shared::image_viewer::{ImageViewerAction, ImageViewerError, LoadState}};
use crate::{home::room_screen::TimelineUpdate, media_cache::{self, MediaCacheEntry}, shared::image_viewer::{ImageViewerAction, ImageViewerError, LoadState}};

/// Populates the image viewer modal with the given media content.
///
Expand All @@ -16,13 +16,13 @@ use crate::{media_cache::{MediaCache, MediaCacheEntry}, shared::image_viewer::{I
pub fn populate_matrix_image_modal(
cx: &mut Cx,
media_source: MediaSource,
media_cache: &mut MediaCache,
update_sender: Option<crossbeam_channel::Sender<TimelineUpdate>>,
) {
let MediaSource::Plain(mxc_uri) = media_source else {
return;
};
// Try to get media from cache or trigger fetch
let media_entry = media_cache.try_get_media_or_fetch(&mxc_uri, MediaFormat::File);
let media_entry = media_cache::get_or_fetch_media(cx, &mxc_uri, MediaFormat::File, update_sender);

// Handle the different media states
match media_entry {
Expand All @@ -39,7 +39,7 @@ pub fn populate_matrix_image_modal(
};
cx.action(ImageViewerAction::Show(LoadState::Error(error)));
// Remove failed media entry from cache for MediaFormat::File so as to start all over again from loading Thumbnail.
media_cache.remove_cache_entry(&mxc_uri, Some(MediaFormat::File));
media_cache::remove_media(cx, &mxc_uri, Some(MediaFormat::File));
}
_ => {}
}
Expand Down
48 changes: 23 additions & 25 deletions src/home/room_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use matrix_sdk_ui::timeline::{
use ruma::{OwnedUserId, events::{AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncMessageLikeEvent}};

use crate::{
app::{AppStateAction, ConfirmDeleteAction}, avatar_cache, event_preview::{plaintext_body_of_timeline_item, text_preview_of_encrypted_message, text_preview_of_member_profile_change, text_preview_of_other_message_like, text_preview_of_other_state, text_preview_of_room_membership_change, text_preview_of_timeline_item}, home::{edited_indicator::EditedIndicatorWidgetRefExt, link_preview::{LinkPreviewCache, LinkPreviewRef, LinkPreviewWidgetRefExt}, loading_pane::{LoadingPaneState, LoadingPaneWidgetExt}, room_image_viewer::{get_image_name_and_filesize, populate_matrix_image_modal}, rooms_list::RoomsListRef, tombstone_footer::SuccessorRoomDetails}, media_cache::{MediaCache, MediaCacheEntry}, profile::{
app::{AppStateAction, ConfirmDeleteAction}, avatar_cache, event_preview::{plaintext_body_of_timeline_item, text_preview_of_encrypted_message, text_preview_of_member_profile_change, text_preview_of_other_message_like, text_preview_of_other_state, text_preview_of_room_membership_change, text_preview_of_timeline_item}, home::{edited_indicator::EditedIndicatorWidgetRefExt, link_preview::{LinkPreviewCache, LinkPreviewRef, LinkPreviewWidgetRefExt}, loading_pane::{LoadingPaneState, LoadingPaneWidgetExt}, room_image_viewer::{get_image_name_and_filesize, populate_matrix_image_modal}, rooms_list::RoomsListRef, tombstone_footer::SuccessorRoomDetails}, media_cache::{self, MediaCacheEntry}, profile::{
user_profile::{ShowUserProfileAction, UserProfile, UserProfileAndRoomId, UserProfilePaneInfo, UserProfileSlidingPaneRef, UserProfileSlidingPaneWidgetExt},
user_profile_cache,
},
Expand Down Expand Up @@ -1067,7 +1067,7 @@ impl Widget for RoomScreen {
event_tl_item,
msg_like_content,
prev_event,
&mut tl_state.media_cache,
Some(tl_state.update_sender.clone()),
&mut tl_state.link_preview_cache,
&tl_state.user_power,
&self.pinned_events,
Expand Down Expand Up @@ -1434,7 +1434,7 @@ impl RoomScreen {
log!("process_timeline_updates(): media fetched for room {}", tl.room_id);
// Set Image to image viewer modal if the media is not a thumbnail.
if let (MediaFormat::File, media_source) = (request.format, request.source) {
populate_matrix_image_modal(cx, media_source, &mut tl.media_cache);
populate_matrix_image_modal(cx, media_source, Some(tl.update_sender.clone()));
}
// Here, to be most efficient, we could redraw only the media items in the timeline,
// but for now we just fall through and let the final `redraw()` call re-draw the whole timeline view.
Expand Down Expand Up @@ -1683,7 +1683,7 @@ impl RoomScreen {
}),
)));

populate_matrix_image_modal(cx, media_source, &mut tl_state.media_cache);
populate_matrix_image_modal(cx, media_source, Some(tl_state.update_sender.clone()));
}

/// Looks up the event specified by the given message details in the given timeline.
Expand Down Expand Up @@ -2132,8 +2132,8 @@ impl RoomScreen {
content_drawn_since_last_update: RangeSet::new(),
profile_drawn_since_last_update: RangeSet::new(),
update_receiver,
update_sender: update_sender.clone(),
request_sender,
media_cache: MediaCache::new(Some(update_sender.clone())),
link_preview_cache: LinkPreviewCache::new(Some(update_sender)),
saved_state: SavedState::default(),
message_highlight_animation_state: MessageHighlightAnimationState::default(),
Expand Down Expand Up @@ -2643,15 +2643,13 @@ struct TimelineUiState {
/// which is okay because a sender on an unbounded channel never needs to block.
update_receiver: crossbeam_channel::Receiver<TimelineUpdate>,

/// The channel sender for timeline updates for this room.
update_sender: crossbeam_channel::Sender<TimelineUpdate>,

/// The sender for timeline requests from a RoomScreen showing this room
/// to the background async task that handles this room's timeline updates.
request_sender: TimelineRequestSender,

/// The cache of media items (images, videos, etc.) that appear in this timeline.
///
/// Currently this excludes avatars, as those are shared across multiple rooms.
media_cache: MediaCache,

/// Cache for link preview data indexed by URL to avoid redundant network requests.
link_preview_cache: LinkPreviewCache,

Expand Down Expand Up @@ -2814,7 +2812,7 @@ fn populate_message_view(
event_tl_item: &EventTimelineItem,
msg_like_content: &MsgLikeContent,
prev_event: Option<&Arc<TimelineItem>>,
media_cache: &mut MediaCache,
update_sender: Option<crossbeam_channel::Sender<TimelineUpdate>>,
link_preview_cache: &mut LinkPreviewCache,
user_power_levels: &UserPowerLevels,
pinned_events: &[OwnedEventId],
Expand Down Expand Up @@ -2868,7 +2866,7 @@ fn populate_message_view(
body,
formatted.as_ref(),
Some(&mut item.link_preview(ids!(content.link_preview_view))),
Some(media_cache),
update_sender.clone(),
Some(link_preview_cache),
);
(item, false)
Expand Down Expand Up @@ -2906,7 +2904,7 @@ fn populate_message_view(
body,
formatted.as_ref(),
Some(&mut item.link_preview(ids!(content.link_preview_view))),
Some(media_cache),
update_sender.clone(),
Some(link_preview_cache),
);
(item, false)
Expand Down Expand Up @@ -2951,7 +2949,7 @@ fn populate_message_view(
body: formatted,
}),
Some(&mut item.link_preview(ids!(content.link_preview_view))),
Some(media_cache),
update_sender.clone(),
Some(link_preview_cache),
);
(item, false)
Expand Down Expand Up @@ -2998,7 +2996,7 @@ fn populate_message_view(
&body,
formatted.as_ref(),
Some(&mut item.link_preview(ids!(content.link_preview_view))),
Some(media_cache),
update_sender.clone(),
Some(link_preview_cache),
);
set_username_and_get_avatar_retval = Some((username, profile_drawn));
Expand All @@ -3025,7 +3023,7 @@ fn populate_message_view(
image_info,
image.source.clone(),
msg.body(),
media_cache,
update_sender.clone(),
);
new_drawn_status.content_drawn = is_image_fully_drawn;
(item, false)
Expand Down Expand Up @@ -3135,7 +3133,7 @@ fn populate_message_view(
&verification.body,
Some(&formatted),
Some(&mut item.link_preview(ids!(content.link_preview_view))),
Some(media_cache),
update_sender.clone(),
Some(link_preview_cache),
);
(item, false)
Expand Down Expand Up @@ -3180,7 +3178,7 @@ fn populate_message_view(
Some(Box::new(image_info.clone())),
MediaSource::Plain(owned_mxc_url.clone()),
body,
media_cache,
update_sender.clone(),
);
new_drawn_status.content_drawn = is_image_fully_drawn;
(item, false)
Expand Down Expand Up @@ -3385,7 +3383,7 @@ fn populate_text_message_content(
body: &str,
formatted_body: Option<&FormattedBody>,
link_preview_ref: Option<&mut LinkPreviewRef>,
media_cache: Option<&mut MediaCache>,
update_sender: Option<crossbeam_channel::Sender<TimelineUpdate>>,
link_preview_cache: Option<&mut LinkPreviewCache>,
) -> bool {
// The message was HTML-formatted rich text.
Expand All @@ -3410,13 +3408,13 @@ fn populate_text_message_content(
};

// Populate link previews if all required parameters are provided
if let (Some(link_preview_ref), Some(media_cache), Some(link_preview_cache)) =
(link_preview_ref, media_cache, link_preview_cache)
if let (Some(link_preview_ref), Some(link_preview_cache)) =
(link_preview_ref, link_preview_cache)
{
link_preview_ref.populate_below_message(
cx,
&links,
media_cache,
update_sender,
link_preview_cache,
&populate_image_message_content,
)
Expand All @@ -3434,7 +3432,7 @@ fn populate_image_message_content(
image_info_source: Option<Box<ImageInfo>>,
original_source: MediaSource,
body: &str,
media_cache: &mut MediaCache,
update_sender: Option<crossbeam_channel::Sender<TimelineUpdate>>,
) -> bool {
// We don't use thumbnails, as their resolution is too low to be visually useful.
// We also don't trust the provided mimetype, as it can be incorrect.
Expand All @@ -3459,7 +3457,7 @@ fn populate_image_message_content(
// A closure that fetches and shows the image from the given `mxc_uri`,
// marking it as fully drawn if the image was available.
let mut fetch_and_show_image_uri = |cx: &mut Cx, mxc_uri: OwnedMxcUri, image_info: Box<ImageInfo>| {
match media_cache.try_get_media_or_fetch(&mxc_uri, MEDIA_THUMBNAIL_FORMAT.into()) {
match media_cache::get_or_fetch_media(cx, &mxc_uri, MEDIA_THUMBNAIL_FORMAT.into(), update_sender.clone()) {
(MediaCacheEntry::Loaded(data), _media_format) => {
let show_image_result = text_or_image_ref.show_image(cx, Some(MediaSource::Plain(mxc_uri)),|cx, img| {
utils::load_png_or_jpg(&img, cx, &data)
Expand All @@ -3474,7 +3472,7 @@ fn populate_image_message_content(
// We're done drawing the image, so mark it as fully drawn.
fully_drawn = true;
}
(MediaCacheEntry::Requested, _media_format) => {
(MediaCacheEntry::Requested(_), _media_format) => {
// If the image is being fetched, we try to show its blurhash.
if let (Some(ref blurhash), Some(width), Some(height)) = (image_info.blurhash.clone(), image_info.width, image_info.height) {
let show_image_result = text_or_image_ref.show_image(cx, Some(MediaSource::Plain(mxc_uri)), |cx, img| {
Expand Down
Loading
Loading