Skip to content

Commit 06bbbf6

Browse files
committed
Added loading_view
1 parent 8787d68 commit 06bbbf6

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/shared/image_viewer_modal.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,24 @@ live_design! {
6565
source: (IMG_DEFAULT_AVATAR)
6666
}
6767
}
68-
loading_label_view = <View> {
68+
loading_view = <View> {
6969
width: Fill,
70-
height: Fit
70+
height: Fit,
71+
flow: Down,
72+
align: {x: 0.5, y: 0.5},
73+
spacing: 10
74+
75+
loading_spinner = <LoadingSpinner> {
76+
width: 40,
77+
height: 40,
78+
draw_bg: {
79+
color: (COLOR_ACTIVE_PRIMARY)
80+
border_size: 3.0,
81+
}
82+
}
83+
7184
<Label> {
72-
width: Fill,
85+
width: Fit,
7386
height: 30,
7487
text: "Loading image...",
7588
draw_text: {
@@ -118,6 +131,11 @@ impl Widget for ImageViewerModal {
118131
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
119132
self.view.handle_event(cx, event, scope);
120133

134+
if !self.image_loaded && self.mxc_uri.is_some() {
135+
if let Event::Signal = event {
136+
self.populate_image_viewer(cx, self.mxc_uri.clone().unwrap());
137+
}
138+
}
121139
if let Event::Actions(actions) = event {
122140
if self.view.button(id!(close_button)).clicked(actions) {
123141
self.close(cx);
@@ -133,32 +151,33 @@ impl Widget for ImageViewerModal {
133151
impl ImageViewerModal {
134152
/// Set the image to display in the modal
135153
pub fn set_image(&mut self, cx: &mut Cx, mxc_uri: OwnedMxcUri) {
154+
self.mxc_uri = Some(mxc_uri.clone());
136155
self.image_loaded = false;
137156
self.show_loading_state(cx);
138-
self.try_load_image(cx, mxc_uri.clone());
157+
self.populate_image_viewer(cx, mxc_uri);
139158
}
140159

141160
/// Try to load the image from the media cache
142-
fn try_load_image(&mut self, cx: &mut Cx, mxc_uri: OwnedMxcUri) {
161+
fn populate_image_viewer(&mut self, cx: &mut Cx, mxc_uri: OwnedMxcUri) {
143162
let media_cache = get_media_cache();
144163
let mut cache = media_cache.lock().unwrap();
145164

146165
// Try to get the full-size image first, fallback to thumbnail
147-
let (media_entry, _format) = cache.try_get_media_or_fetch(
166+
let (media_entry, format) = cache.try_get_media_or_fetch(
148167
mxc_uri.clone(),
149168
MediaFormat::File,
150169
);
151170
drop(cache);
152-
match media_entry {
153-
MediaCacheEntry::Loaded(data) => {
171+
match (media_entry, format) {
172+
(MediaCacheEntry::Loaded(data), MediaFormat::File) => {
154173
self.load_image_data(cx, &data);
155174
self.image_loaded = true;
156175
}
157-
MediaCacheEntry::Requested => {
176+
(MediaCacheEntry::Requested, _) | (MediaCacheEntry::Loaded(_), MediaFormat::Thumbnail(_))=> {
158177
// Image is being fetched, keep showing loading state
159178
self.show_loading_state(cx);
160179
}
161-
MediaCacheEntry::Failed => {
180+
(MediaCacheEntry::Failed, _) => {
162181
self.show_error_state(cx);
163182
self.image_loaded = true;
164183
}
@@ -170,8 +189,8 @@ impl ImageViewerModal {
170189
let image = self.view.image(id!(image));
171190
match load_png_or_jpg(&image, cx, data) {
172191
Ok(()) => {
173-
self.view.view(id!(loading_label)).set_visible(cx, false);
174-
self.view.view(id!(error_label)).set_visible(cx, false);
192+
self.view.view(id!(loading_view)).set_visible(cx, false);
193+
self.view.view(id!(error_label_view)).set_visible(cx, false);
175194
self.view.image(id!(image)).set_visible(cx, true);
176195
}
177196
Err(e) => {
@@ -183,15 +202,15 @@ impl ImageViewerModal {
183202

184203
/// Show loading state
185204
fn show_loading_state(&mut self, cx: &mut Cx) {
186-
self.view.view(id!(loading_label)).set_visible(cx, true);
187-
self.view.view(id!(error_label)).set_visible(cx, false);
205+
self.view.view(id!(loading_view)).set_visible(cx, true);
206+
self.view.view(id!(error_label_view)).set_visible(cx, false);
188207
self.view.image(id!(image)).set_visible(cx, false);
189208
}
190209

191210
/// Show error state
192211
fn show_error_state(&mut self, cx: &mut Cx) {
193-
self.view.view(id!(loading_label)).set_visible(cx, false);
194-
self.view.view(id!(error_label)).set_visible(cx, true);
212+
self.view.view(id!(loading_view)).set_visible(cx, false);
213+
self.view.view(id!(error_label_view)).set_visible(cx, true);
195214
self.view.image(id!(image)).set_visible(cx, false);
196215
}
197216
fn close(&mut self, cx: &mut Cx) {
@@ -214,7 +233,6 @@ impl ImageViewerModalRef {
214233

215234
/// Close the modal
216235
pub fn close(&self, cx: &mut Cx) {
217-
// Reset the state
218236
if let Some(mut inner) = self.borrow_mut() {
219237
inner.close(cx);
220238
}

0 commit comments

Comments
 (0)