Skip to content

Commit

Permalink
Display grid placeholder when loading gallery example
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jan 28, 2025
1 parent ae35992 commit cd445f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/gallery/src/civitai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Image {
}

impl Image {
pub const LIMIT: usize = 99;

pub async fn list() -> Result<Vec<Self>, Error> {
let client = reqwest::Client::new();

Expand All @@ -27,7 +29,7 @@ impl Image {
("sort", "Most Reactions"),
("period", "Week"),
("nsfw", "None"),
("limit", "99"),
("limit", &Image::LIMIT.to_string()),
])
.send()
.await?
Expand Down
25 changes: 20 additions & 5 deletions examples/gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ impl Gallery {
}

pub fn view(&self) -> Element<'_, Message> {
let gallery = row(self.images.iter().map(|image| {
card(image, self.thumbnails.get(&image.id), self.now)
}))
let gallery = if self.images.is_empty() {
row((0..=Image::LIMIT).map(|_| placeholder()))
} else {
row(self.images.iter().map(|image| {
card(image, self.thumbnails.get(&image.id), self.now)
}))
}
.spacing(10)
.wrap();

Expand Down Expand Up @@ -187,8 +191,8 @@ fn card<'a>(

let card = mouse_area(
container(image)
.width(320)
.height(410)
.width(Thumbnail::WIDTH)
.height(Thumbnail::HEIGHT)
.style(container::dark),
)
.on_enter(Message::ThumbnailHovered(metadata.id, true))
Expand All @@ -207,13 +211,24 @@ fn card<'a>(
}
}

fn placeholder<'a>() -> Element<'a, Message> {
container(horizontal_space())
.width(Thumbnail::WIDTH)
.height(Thumbnail::HEIGHT)
.style(container::dark)
.into()
}

struct Thumbnail {
handle: image::Handle,
fade_in: Animation<bool>,
zoom: Animation<bool>,
}

impl Thumbnail {
const WIDTH: u16 = 320;
const HEIGHT: u16 = 410;

fn new(rgba: Rgba) -> Self {
Self {
handle: image::Handle::from_rgba(
Expand Down

0 comments on commit cd445f7

Please sign in to comment.