pattern_grid: explicit side through PatternGrid::size
CI / build + test (push) Has been cancelled
CI / cargo audit (push) Has been cancelled

The grid's preferred size was the theme's fluid constant (264 px at the reference surface) capped by the width the parent allocates, so a caller could only shrink it: a lock screen that wants the grid to span most of a phone's width had no way to ask for it, and a container's max_width did nothing past the default. PatternGrid gains a size field with a size( impl Into<Length> ) builder; preferred_size resolves it through Canvas::resolve_geom like the other geometry overrides and still caps it at max_width, and map_msg carries it across. Without the builder the fluid default applies as before.
docs/widgets.md describes the builder next to hide_stroke and error; CHANGELOG lists it under Unreleased.
This commit is contained in:
2026-09-10 21:33:55 +02:00
parent 7c1ef86afe
commit c9ae5438f8
3 changed files with 20 additions and 3 deletions
+17 -3
View File
@@ -8,7 +8,7 @@
//! `on_release`.
use std::sync::Arc;
use crate::types::{ Color, Point, Rect };
use crate::types::{ Color, Length, Point, Rect };
use crate::render::Canvas;
use super::Element;
@@ -25,13 +25,21 @@ pub struct PatternGrid<Msg: Clone>
pub( crate ) on_release: Option<Msg>,
pub( crate ) hide_stroke: bool,
pub( crate ) error: bool,
pub( crate ) size: Option<Length>,
}
impl<Msg: Clone> PatternGrid<Msg>
{
pub fn new( path: Vec<u8> ) -> Self
{
Self { path, on_node: None, on_release: None, hide_stroke: false, error: false }
Self { path, on_node: None, on_release: None, hide_stroke: false, error: false, size: None }
}
/// Side of the square grid; without it the theme's fluid default applies.
pub fn size( mut self, s: impl Into<Length> ) -> Self
{
self.size = Some( s.into() );
self
}
pub fn on_node( mut self, f: impl Fn( u8 ) -> Msg + 'static ) -> Self
@@ -77,12 +85,18 @@ impl<Msg: Clone> PatternGrid<Msg>
on_release: self.on_release.map( |m| ( *f )( m ) ),
hide_stroke: self.hide_stroke,
error: self.error,
size: self.size,
}
}
pub fn preferred_size( &self, max_width: f32, canvas: &Canvas ) -> ( f32, f32 )
{
let size = canvas.geom_px( theme::SIZE ).min( max_width );
let size = match self.size
{
Some( l ) => canvas.resolve_geom( l ),
None => canvas.geom_px( theme::SIZE ),
};
let size = size.min( max_width );
( size, size )
}