Support viewport-relative widget sizing
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user