Skip to content

Commit cd445f7

Browse files
committed
Display grid placeholder when loading gallery example
1 parent ae35992 commit cd445f7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

examples/gallery/src/civitai.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub struct Image {
1313
}
1414

1515
impl Image {
16+
pub const LIMIT: usize = 99;
17+
1618
pub async fn list() -> Result<Vec<Self>, Error> {
1719
let client = reqwest::Client::new();
1820

@@ -27,7 +29,7 @@ impl Image {
2729
("sort", "Most Reactions"),
2830
("period", "Week"),
2931
("nsfw", "None"),
30-
("limit", "99"),
32+
("limit", &Image::LIMIT.to_string()),
3133
])
3234
.send()
3335
.await?

examples/gallery/src/main.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,13 @@ impl Gallery {
153153
}
154154

155155
pub fn view(&self) -> Element<'_, Message> {
156-
let gallery = row(self.images.iter().map(|image| {
157-
card(image, self.thumbnails.get(&image.id), self.now)
158-
}))
156+
let gallery = if self.images.is_empty() {
157+
row((0..=Image::LIMIT).map(|_| placeholder()))
158+
} else {
159+
row(self.images.iter().map(|image| {
160+
card(image, self.thumbnails.get(&image.id), self.now)
161+
}))
162+
}
159163
.spacing(10)
160164
.wrap();
161165

@@ -187,8 +191,8 @@ fn card<'a>(
187191

188192
let card = mouse_area(
189193
container(image)
190-
.width(320)
191-
.height(410)
194+
.width(Thumbnail::WIDTH)
195+
.height(Thumbnail::HEIGHT)
192196
.style(container::dark),
193197
)
194198
.on_enter(Message::ThumbnailHovered(metadata.id, true))
@@ -207,13 +211,24 @@ fn card<'a>(
207211
}
208212
}
209213

214+
fn placeholder<'a>() -> Element<'a, Message> {
215+
container(horizontal_space())
216+
.width(Thumbnail::WIDTH)
217+
.height(Thumbnail::HEIGHT)
218+
.style(container::dark)
219+
.into()
220+
}
221+
210222
struct Thumbnail {
211223
handle: image::Handle,
212224
fade_in: Animation<bool>,
213225
zoom: Animation<bool>,
214226
}
215227

216228
impl Thumbnail {
229+
const WIDTH: u16 = 320;
230+
const HEIGHT: u16 = 410;
231+
217232
fn new(rgba: Rgba) -> Self {
218233
Self {
219234
handle: image::Handle::from_rgba(

0 commit comments

Comments
 (0)