use super::placement::{
FlyoutPlacementMode, PointPlacementKind, calculate_placement, calculate_point_placement,
calculate_submenu_placement,
};
use waft_embedder::{Offset, Rect, Size, TextDirection};
use waft_foundation::App;
use waft_rendering::*;
use waft_widgets::*;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct FlyoutLayoutSettings {
pub target: Rect,
pub position: Option<Offset>,
pub exclusion: Rect,
pub submenu: bool,
pub point_kind: PointPlacementKind,
pub placement: FlyoutPlacementMode,
pub direction: TextDirection,
pub minimum: Size,
pub maximum: Size,
pub container: Rect,
pub allow_fallbacks: bool,
}
#[derive(Debug)]
pub(super) struct FlyoutLayout {
pub settings: FlyoutLayoutSettings,
pub child: WidgetRef,
}
impl RenderObjectWidget for FlyoutLayout {
type RenderObject = RenderFlyoutLayout;
fn create_render_object(&self, app: &mut App, _: BuildContext) -> AnyRenderObject {
RenderHandle::new_box(
app,
RenderFlyoutLayout {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
child: RenderObjectWithChildData::new(),
settings: self.settings,
},
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_: BuildContext,
render: RenderHandle<RenderFlyoutLayout>,
) {
if render.get(app).settings != self.settings {
render.get_mut(app).settings = self.settings;
render.as_object().mark_needs_layout(app);
}
}
}
impl SingleChildRenderObjectWidget for FlyoutLayout {
fn child(&self) -> Option<&WidgetRef> {
Some(&self.child)
}
}
pub(super) struct RenderFlyoutLayout {
render_object: RenderObjectData,
render_box: RenderBoxData,
child: RenderObjectWithChildData<AnyRenderBox>,
settings: FlyoutLayoutSettings,
}
impl RenderObjectWithChildMixin for RenderFlyoutLayout {
type ChildType = AnyRenderBox;
fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
&self.get(app).child
}
fn child_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut RenderObjectWithChildData<AnyRenderBox> {
&mut self.get_mut(app).child
}
}
impl RenderShiftedBox for RenderFlyoutLayout {}
impl RenderObject for RenderFlyoutLayout {
waft_rendering::render_object_accessors!();
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
let root_size = self.constraints(app).biggest();
assert!(
root_size.width().is_finite() && root_size.height().is_finite(),
"flyout layout requires a bounded root overlay"
);
if let Some(child) = self.child(app) {
let settings = self.get(app).settings;
let width = settings
.maximum
.width()
.min(settings.container.width())
.max(0.0);
let height = settings
.maximum
.height()
.min(settings.container.height())
.max(0.0);
child.layout(
app,
BoxConstraints::new()
.min_width(settings.minimum.width().min(width))
.max_width(width)
.min_height(settings.minimum.height().min(height))
.max_height(height),
true,
);
let rectangle = if settings.submenu {
calculate_submenu_placement(
settings.target,
child.size(app),
settings.direction,
settings.container,
)
} else if let Some(point) = settings.position {
calculate_point_placement(
settings.placement,
settings.direction,
point,
child.size(app),
settings.exclusion,
settings.container,
settings.point_kind,
)
} else {
calculate_placement(
settings.placement,
settings.direction,
settings.target,
child.size(app),
settings.minimum,
settings.maximum,
settings.container,
settings.allow_fallbacks,
)
};
child.layout(app, BoxConstraints::tight(rectangle.size()), true);
child.parent_data_of_mut::<BoxParentData>(app).offset = rectangle.top_left();
}
self.set_size(app, root_size);
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
RenderShiftedBox::paint(self, app, context, offset);
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
if let Some(child) = self.child(app) {
visitor(child.as_object());
}
}
}
impl RenderBox for RenderFlyoutLayout {
waft_rendering::render_box_accessors!();
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
RenderShiftedBox::hit_test_children(self, app, result, position)
}
fn compute_dry_layout(
self: RenderHandle<Self>,
_: &mut App,
constraints: BoxConstraints,
) -> Size {
constraints.biggest()
}
}