pattern_grid: explicit side through PatternGrid::size
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:
@@ -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 )
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user