Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xilem example for http cats API, requiring workers and image component #571

Merged
merged 13 commits into from
Sep 3, 2024
4 changes: 3 additions & 1 deletion masonry/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use crate::{
/// A widget that renders a bitmap Image.
///
/// The underlying image uses `Arc` for buffer data, making it cheap to clone.
///
/// This currently uses bilinear interpolation, which falls down when the image is larger than the
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
pub struct Image {
image_data: ImageBuf,
fill: FillStrat,
Expand Down Expand Up @@ -87,7 +89,7 @@ impl Widget for Image {
trace!("Computed size: {}", size);
return size;
}
// This size logic has NOT been carefully considered
// This size logic has NOT been carefully considered, in particular with regards to self.fill.
// TODO: Carefully consider it
let size =
bc.constrain_aspect_ratio(image_size.height / image_size.width, image_size.width);
Expand Down
5 changes: 2 additions & 3 deletions xilem/examples/http_cats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Status {
}

#[derive(Debug)]
/// What operations have happened on a fetching image.
/// The state of the download of an image from a URL
enum ImageState {
NotRequested,
Pending,
Expand Down Expand Up @@ -144,7 +144,6 @@ impl HttpCats {

/// Load a [`vello::peniko::Image`] from the given url.
async fn image_from_url(url: &str) -> anyhow::Result<Image> {
// TODO: Error handling
let response = reqwest::get(url).await?;
let bytes = response.bytes().await?;
let image = image::load_from_memory(&bytes)?.into_rgba8();
Expand Down Expand Up @@ -185,7 +184,7 @@ impl Status {
),
ImageState::Pending => OneOf3::B(sized_box(spinner()).width(80.).height(80.)),
// TODO: Alt text?
ImageState::Available(image_data) => OneOf3::C(image(image_data.clone())),
ImageState::Available(image_data) => OneOf3::C(image(image_data)),
};
flex((
prose(format!("HTTP Status Code: {}", self.code)).alignment(TextAlignment::Middle),
Expand Down
18 changes: 14 additions & 4 deletions xilem/src/view/image.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

//! The bitmap image widget.

use masonry::widget::{self, FillStrat};
use xilem_core::{Mut, ViewMarker};

use crate::{MessageResult, Pod, View, ViewCtx, ViewId};

/// Displays a bitmap Image.
/// Displays the bitmap `image`.
///
/// By default, the Image will scale to fit its box constraints ([`FillStrat::Fill`]).
/// To configure this, call [`fill`](Image::fill) on the returned value.
pub fn image(image: vello::peniko::Image) -> Image {
///
/// Corresponds to the [`Image`](widget::Image) widget.
///
/// It is not currently supported to use a GPU-resident [texture](vello::wgpu::Texture) in this widget.
/// See [#gpu>vello adding wgpu texture buffers to scene](https://xi.zulipchat.com/#narrow/stream/197075-gpu/topic/vello.20adding.20wgpu.20texture.20buffers.20to.20scene)
/// for discussion.
pub fn image(image: &vello::peniko::Image) -> Image {
Image {
image,
// Image only contains a `Blob` and Copy fields, and so is cheap to clone.
// We take by reference as we expect all users of this API will need to clone, and it's
// easier than documenting that cloning is cheap.
image: image.clone(),
fill: FillStrat::default(),
}
}
Expand All @@ -39,7 +50,6 @@ impl<State, Action> View<State, Action, ViewCtx> for Image {
type ViewState = ();

fn build(&self, _: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
// Image's clone is cheap, so it's ok for this to be a view.
(Pod::new(widget::Image::new(self.image.clone())), ())
}

Expand Down