Support viewport-relative widget sizing
Some checks failed
CI / build + test (push) Has been cancelled
CI / cargo audit (push) Has been cancelled

This commit is contained in:
yamabush1
2026-06-04 12:38:59 +02:00
parent ae8380b1ac
commit 68c6a87bf6
8 changed files with 61 additions and 33 deletions

View File

@@ -2,7 +2,7 @@
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
use std::sync::Arc;
use crate::types::Rect;
use crate::types::{ Length, Rect };
use crate::render::Canvas;
/// A static image widget that renders RGBA pixel data.
@@ -34,8 +34,8 @@ pub struct Image
pub height: u32,
/// When `true` the image scales to fill the available width (cover mode).
pub cover: bool,
/// Optional explicit display size in logical pixels.
pub display_size: Option<( f32, f32 )>,
/// Optional explicit display size in logical units.
pub display_size: Option<( Length, Length )>,
/// Opacity multiplier in `[0.0, 1.0]`. Default: `1.0`.
pub opacity: f32,
}
@@ -66,10 +66,10 @@ impl Image
self
}
/// Set an explicit display size in logical pixels.
pub fn size( mut self, width: f32, height: f32 ) -> Self
/// Set an explicit display size in logical units.
pub fn size( mut self, width: impl Into<Length>, height: impl Into<Length> ) -> Self
{
self.display_size = Some( ( width.max( 0.0 ), height.max( 0.0 ) ) );
self.display_size = Some( ( width.into(), height.into() ) );
self
}
@@ -81,11 +81,15 @@ impl Image
}
/// Return the preferred `(width, height)` given available `max_width`.
pub fn preferred_size( &self, max_width: f32 ) -> (f32, f32)
pub fn preferred_size( &self, max_width: f32, canvas: &Canvas ) -> (f32, f32)
{
if let Some( size ) = self.display_size
if let Some( ( width, height ) ) = self.display_size
{
return size;
let viewport = canvas.viewport_logical();
return (
width.resolve( viewport, Length::EM_BASE_DEFAULT ).max( 0.0 ),
height.resolve( viewport, Length::EM_BASE_DEFAULT ).max( 0.0 ),
);
}
if self.cover
{