use crate::compat::{vec, Any, Vec};
use crate::core::{ObjectId, Point, Rect, Size};
use crate::layout::hints::ChildInfo;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SizePolicy {
Fixed,
Preferred,
Expanding,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LayoutConstraints {
pub min: u32,
pub max: Option<u32>,
}
impl LayoutConstraints {
pub fn new(min: u32, max: Option<u32>) -> Self {
Self { min, max }
}
}
#[derive(Debug, Clone, Copy)]
pub struct LayoutContext {
pub layout_scale: f32,
pub font_scale: f32,
pub min_touch_size: Size,
}
pub fn grow_to_min_touch_size(child: Rect, min_touch_size: Size) -> Rect {
if child.width >= min_touch_size.width && child.height >= min_touch_size.height {
return child;
}
let width = child.width.max(min_touch_size.width);
let height = child.height.max(min_touch_size.height);
Rect::new(
child.x - (width - child.width) as i32 / 2,
child.y - (height - child.height) as i32 / 2,
width,
height,
)
}
impl Default for LayoutContext {
fn default() -> Self {
Self {
layout_scale: 1.0,
font_scale: crate::platform::profile::text_scale(),
min_touch_size: crate::platform::profile::recommended_touch_target().dimensions(),
}
}
}
pub trait Layout {
fn add_widget(&mut self, widget_id: ObjectId, stretch: u32);
fn remove_widget(&mut self, widget_id: ObjectId);
fn update(&self, rect: Rect, widgets: &mut dyn FnMut(ObjectId, Rect));
fn arrange(&self, rect: Rect, children: &[ChildInfo], out: &mut dyn FnMut(ObjectId, Rect)) {
let _ = children;
self.update(rect, out);
}
fn update_from_position_size(
&self,
position: Point,
size: Size,
widgets: &mut dyn FnMut(ObjectId, Rect),
) {
self.update(Rect::from_position_size(position, size), widgets);
}
fn child_ids(&self) -> Vec<ObjectId> {
vec![]
}
fn has_child(&self, _id: ObjectId) -> bool {
false
}
fn clear(&mut self) {
}
fn update_with_context(
&self,
rect: Rect,
context: &LayoutContext,
widgets: &mut dyn FnMut(ObjectId, Rect),
) {
let _ = context;
self.update(rect, widgets);
}
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}