Skip to content

Commit

Permalink
Xilem example for http cats API, requiring workers and image comp…
Browse files Browse the repository at this point in the history
…onent (#571)

This example is inspired by:
https://troz.net/post/2024/swiftui-mac-2024/

Current status:
- Lists status code
- Can select status code
- Downloads image from status code
- Shows image from status code

This adds two new features to Xilem:
- The worker view, which is the obvious extension to `task` for multiple
operations
- The `image` view, which just uses Masonry `Image`.

It also fixes a the Masonry Image view's layout to use the already
extant but unused method.
  • Loading branch information
DJMcNab authored Sep 3, 2024
1 parent 5944aa5 commit 7be4dd2
Show file tree
Hide file tree
Showing 13 changed files with 1,189 additions and 23 deletions.
516 changes: 511 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ cargo update -p package_name --precise 0.1.1
Licensed under the Apache License, Version 2.0
([LICENSE](LICENSE) or <http://www.apache.org/licenses/LICENSE-2.0>)

The font file (`RobotoFlex-Subset.ttf`) in `xilem/resources/fonts/roboto_flex/` is licensed solely as documented in that folder,
(and is not licensed under the Apache License, Version 2.0).
Some files used for examples are under different licenses:

- The font file (`RobotoFlex-Subset.ttf`) in `xilem/resources/fonts/roboto_flex/` is licensed solely as documented in that folder (and is not licensed under the Apache License, Version 2.0).
- The data file (`status.csv`) in `xilem/resources/data/http_cats_status/` is licensed solely as documented in that folder (and is not licensed under the Apache License, Version 2.0).

## Contribution

Expand Down
24 changes: 13 additions & 11 deletions masonry/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ 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 its layout size (e.g. it is in a [sized box](super::SizedBox) smaller
/// than the image size).
pub struct Image {
image_data: ImageBuf,
fill: FillStrat,
Expand All @@ -32,7 +36,6 @@ impl Image {
/// Create an image drawing widget from an image buffer.
///
/// By default, the Image will scale to fit its box constraints ([`FillStrat::Fill`]).
#[inline]
pub fn new(image_data: ImageBuf) -> Self {
Image {
Expand Down Expand Up @@ -82,17 +85,16 @@ impl Widget for Image {
// If either the width or height is constrained calculate a value so that the image fits
// in the size exactly. If it is unconstrained by both width and height take the size of
// the image.
let max = bc.max();
let image_size = Size::new(self.image_data.width as f64, self.image_data.height as f64);
let size = if bc.is_width_bounded() && !bc.is_height_bounded() {
let ratio = max.width / image_size.width;
Size::new(max.width, ratio * image_size.height)
} else if bc.is_height_bounded() && !bc.is_width_bounded() {
let ratio = max.height / image_size.height;
Size::new(ratio * image_size.width, max.height)
} else {
bc.constrain(image_size)
};
if image_size.is_empty() {
let size = bc.min();
trace!("Computed size: {}", size);
return size;
}
// 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);
trace!("Computed size: {}", size);
size
}
Expand Down
32 changes: 30 additions & 2 deletions xilem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license.workspace = true
repository.workspace = true
homepage.workspace = true
rust-version.workspace = true
exclude = ["/resources/fonts/roboto_flex/"]
exclude = ["/resources/fonts/roboto_flex/", "/resources/data/http_cats_status/"]

[package.metadata.docs.rs]
all-features = true
Expand All @@ -36,6 +36,16 @@ path = "examples/calc.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]

[[example]]
name = "http_cats"

[[example]]
name = "http_cats_android"
path = "examples/http_cats.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]


[[example]]
name = "stopwatch"

Expand Down Expand Up @@ -65,14 +75,28 @@ tracing.workspace = true
vello.workspace = true
smallvec.workspace = true
accesskit.workspace = true
tokio = { version = "1.39.1", features = ["rt", "rt-multi-thread", "time"] }
tokio = { version = "1.39.1", features = [
"rt",
"rt-multi-thread",
"time",
"sync",
] }

[dev-dependencies]
# Used for `variable_clock`
time = { workspace = true, features = ["local-offset"] }

# Used for http_cats
reqwest = { version = "0.12.7", default-features = false, features = [
# We use rustls as Android doesn't ship with openssl
# and this is likely to be easiest to get working.
"rustls-tls",
] }
image = { workspace = true, features = ["jpeg"] }

# Make wgpu use tracing for its spans.
profiling = { version = "1.0.15", features = ["profile-with-tracing"] }
anyhow = "1.0.86"

[target.'cfg(target_os = "android")'.dev-dependencies]
winit = { features = ["android-native-activity"], workspace = true }
Expand All @@ -81,3 +105,7 @@ winit = { features = ["android-native-activity"], workspace = true }
# Do not use when releasing a production app.
[package.metadata.android.application]
debuggable = true

[[package.metadata.android.uses_permission]]
# Needed for http_cats
name = "android.permission.INTERNET"
9 changes: 6 additions & 3 deletions xilem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ Unless you explicitly state otherwise, any contribution intentionally submitted

Licensed under the Apache License, Version 2.0 ([LICENSE](LICENSE) or <http://www.apache.org/licenses/LICENSE-2.0>)

The font file (`RobotoFlex-Subset.ttf`) in `resources/fonts/roboto_flex/` is licensed solely as documented in that folder,
(and is not licensed under the Apache License, Version 2.0).
Note that this file is *not* distributed with the.
Some files used for examples are under different licenses:

* The font file (`RobotoFlex-Subset.ttf`) in `resources/fonts/roboto_flex/` is licensed solely as documented in that folder (and is not licensed under the Apache License, Version 2.0).
* The data file (`status.csv`) in `resources/data/http_cats_status/` is licensed solely as documented in that folder (and is not licensed under the Apache License, Version 2.0).

Note that these files are *not* distributed with the released crate.

[Masonry]: https://crates.io/crates/masonry
[Druid]: https://crates.io/crates/druid
Expand Down
Loading

0 comments on commit 7be4dd2

Please sign in to comment.