`UiScalar` was `Len` without the `leftover` weight, which is the separation canonical `main` already had as `Len` beside `LayoutLen` and this branch collapsed. It is needed back for the queued clamp: a cap may not contain a share, because a cap has to read the report a rule otherwise makes moot, and a share puts the container's division into the same equation -- two self-consistent assignments, which is the multiple-fixed-point failure generated seed 13 punished for orthogonal sizing. `min(report, cap)` is not a `LayoutLen` either: it is a sum of parts, and the smaller of two of them is not one. So `UiScalar` is `Len`, what was `Len` is `LayoutLen`, and the two say in their docs which is which: a `Len` is pixels plus a fraction of a box -- a position being the length from the box's start, which is why a span is two of them -- and a `LayoutLen` is a `Len` plus a claim only a container dividing its room can answer. `From<Len> for LayoutLen` is the one-way step between them. Names only; the shader's `UiScalar` is renamed with them. Checked: fmt, clippy, 105 tests, and `tabs`, `minimal`, `view`, `text` and `random` byte-identical at 1920x1200. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
use crate::prelude::*;
|
|
use image::DynamicImage;
|
|
|
|
pub struct Image {
|
|
handle: TextureHandle,
|
|
}
|
|
|
|
impl Widget for Image {
|
|
fn draw(&mut self, painter: &mut Painter) -> Size {
|
|
painter.primitive(&self.handle);
|
|
Size::px(self.handle.size())
|
|
}
|
|
|
|
fn size_hint(&self, axis: Axis) -> Option<LayoutLen> {
|
|
Some(LayoutLen::px(self.handle.size().axis(axis)))
|
|
}
|
|
}
|
|
|
|
pub fn image<State: UiRsc>(image: impl LoadableImage) -> impl WidgetFn<State, Image> {
|
|
let image = image.get_image().expect("Failed to load image");
|
|
move |state| Image {
|
|
handle: state.ui_mut().textures.add(image),
|
|
}
|
|
}
|
|
|
|
pub trait LoadableImage {
|
|
fn get_image(self) -> Result<DynamicImage, String>;
|
|
}
|
|
|
|
impl LoadableImage for &str {
|
|
fn get_image(self) -> Result<DynamicImage, String> {
|
|
image::open(self).map_err(|e| format!("{e:?}"))
|
|
}
|
|
}
|
|
|
|
impl LoadableImage for String {
|
|
fn get_image(self) -> Result<DynamicImage, String> {
|
|
image::open(self).map_err(|e| format!("{e:?}"))
|
|
}
|
|
}
|
|
|
|
impl<const LEN: usize> LoadableImage for &[u8; LEN] {
|
|
fn get_image(self) -> Result<DynamicImage, String> {
|
|
image::load_from_memory(self).map_err(|e| format!("{e:?}"))
|
|
}
|
|
}
|
|
|
|
impl LoadableImage for DynamicImage {
|
|
fn get_image(self) -> Result<DynamicImage, String> {
|
|
Ok(self)
|
|
}
|
|
}
|