use std::ops::Range; use crate::{ geometry::{rect::RectF, vector::Vector2F}, json, Drawable, Element, SceneBuilder, SizeConstraint, View, ViewContext, }; use serde_json::json; pub struct Expanded { child: Element, full_width: bool, full_height: bool, } impl Expanded { pub fn new(child: impl Drawable) -> Self { Self { child: child.into_element(), full_width: true, full_height: true, } } pub fn full_width(mut self) -> Self { self.full_width = true; self.full_height = false; self } pub fn full_height(mut self) -> Self { self.full_width = false; self.full_height = true; self } } impl Drawable for Expanded { type LayoutState = (); type PaintState = (); fn layout( &mut self, mut constraint: SizeConstraint, view: &mut V, cx: &mut ViewContext, ) -> (Vector2F, Self::LayoutState) { if self.full_width { constraint.min.set_x(constraint.max.x()); } if self.full_height { constraint.min.set_y(constraint.max.y()); } let size = self.child.layout(constraint, view, cx); (size, ()) } fn paint( &mut self, scene: &mut SceneBuilder, bounds: RectF, visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, cx: &mut ViewContext, ) -> Self::PaintState { self.child .paint(scene, bounds.origin(), visible_bounds, view, cx); } fn rect_for_text_range( &self, range_utf16: Range, _: RectF, _: RectF, _: &Self::LayoutState, _: &Self::PaintState, view: &V, cx: &ViewContext, ) -> Option { self.child.rect_for_text_range(range_utf16, view, cx) } fn debug( &self, _: RectF, _: &Self::LayoutState, _: &Self::PaintState, view: &V, cx: &ViewContext, ) -> json::Value { json!({ "type": "Expanded", "full_width": self.full_width, "full_height": self.full_height, "child": self.child.debug(view, cx) }) } }