use crate::Brush;
use waft_embedder::{Canvas, Clip, Offset, RRect, Radius, Size, TextBaseline};
use waft_foundation::App;
use waft_painting::{BorderRadius, ClipContext, EdgeInsets, EdgeInsetsGeometry};
use waft_rendering::*;
use waft_widgets::*;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BackgroundSizing {
#[default]
InnerBorderEdge,
OuterBorderEdge,
}
#[derive(Clone, Debug)]
pub struct ControlBorder {
pub child: Option<WidgetRef>,
pub background: Option<Brush>,
pub border_brush: Option<Brush>,
pub border_thickness: [f64; 4],
pub corner_radius: [f64; 4],
pub background_sizing: BackgroundSizing,
pub padding: [f64; 4],
}
impl ControlBorder {
pub fn new(background: Brush, border_brush: Brush) -> ControlBorder {
Self::new_optional(Some(background), Some(border_brush))
}
pub fn new_optional(background: Option<Brush>, border_brush: Option<Brush>) -> Self {
ControlBorder {
child: None,
background,
border_brush,
border_thickness: [1.0; 4],
corner_radius: [0.0; 4],
background_sizing: BackgroundSizing::InnerBorderEdge,
padding: [0.0; 4],
}
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ControlBorder {
self.child = Some(child.into_widget());
self
}
pub fn border_thickness(mut self, thickness: f64) -> ControlBorder {
self.border_thickness = [thickness; 4];
self
}
pub fn border_thickness_ltrb(mut self, thickness: [f64; 4]) -> ControlBorder {
self.border_thickness = thickness;
self
}
pub fn corner_radius(mut self, radius: f64) -> ControlBorder {
self.corner_radius = [radius; 4];
self
}
pub fn corner_radius_corners(mut self, radii: [f64; 4]) -> ControlBorder {
self.corner_radius = radii;
self
}
pub fn background_sizing(mut self, sizing: BackgroundSizing) -> ControlBorder {
self.background_sizing = sizing;
self
}
pub fn padding(mut self, padding: [f64; 4]) -> ControlBorder {
self.padding = padding;
self
}
}
impl StatelessWidget for ControlBorder {
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let [left, top, right, bottom] = self.padding;
let thickness = self.border_thickness;
let style = ControlBorderStyle {
background: self.background,
border_brush: self.border_brush,
border_thickness: thickness,
corner_radius: self.corner_radius,
background_sizing: self.background_sizing,
};
let inner = Padding::new(EdgeInsetsGeometry::from_ltrb(
left + thickness[0],
top + thickness[1],
right + thickness[2],
bottom + thickness[3],
));
let inner = match &self.child {
Some(child) => inner.child(child.clone()),
None => inner,
};
let [top_left, top_right, bottom_right, bottom_left] =
self.corner_radius.map(Radius::circular);
let rounded = self.corner_radius.iter().any(|radius| *radius > 0.0);
ClipRRect::new()
.border_radius(
BorderRadius::only(top_left, top_right, bottom_left, bottom_right).into(),
)
.clip_behavior(if rounded { Clip::AntiAlias } else { Clip::None })
.child(BorderPaint {
style,
child: inner.into_widget(),
})
.into_widget()
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct ControlBorderStyle {
background: Option<Brush>,
border_brush: Option<Brush>,
border_thickness: [f64; 4],
corner_radius: [f64; 4],
background_sizing: BackgroundSizing,
}
impl ControlBorderStyle {
fn outlines(self, size: Size) -> (RRect, RRect) {
let bounds = Offset::ZERO & size;
let [top_left, top_right, bottom_right, bottom_left] =
self.corner_radius.map(Radius::circular);
let outer =
RRect::from_rect_and_corners(bounds, top_left, top_right, bottom_right, bottom_left)
.scale_radii();
let [left, top, right, bottom] = self.border_thickness;
let inner = EdgeInsets::from_ltrb(left, top, right, bottom).deflate_rrect(outer);
(outer, inner)
}
fn hit_test(self, size: Size, point: Offset) -> bool {
if size.width() <= 0.0 || size.height() <= 0.0 {
return false;
}
let (outer, inner) = self.outlines(size);
let in_outer = outer.contains(point);
let in_inner = inner.contains(point);
let background_hit = self.background.is_some()
&& match self.background_sizing {
BackgroundSizing::InnerBorderEdge => in_inner,
BackgroundSizing::OuterBorderEdge => in_outer,
};
background_hit || (self.border_brush.is_some() && in_outer && !in_inner)
}
fn paint(self, canvas: &mut Canvas, size: Size) {
let bounds = Offset::ZERO & size;
let (outer, inner) = self.outlines(size);
let background_rect = match self.background_sizing {
BackgroundSizing::InnerBorderEdge => inner,
BackgroundSizing::OuterBorderEdge => outer,
};
if let Some(background) = self.background
&& (matches!(background, Brush::Acrylic(_))
|| background.representative_color().a > 0.0)
{
background.paint_rrect(canvas, background_rect, bounds);
}
if let Some(border) = self.border_brush
&& self
.border_thickness
.iter()
.any(|thickness| *thickness > 0.0)
&& (matches!(border, Brush::Acrylic(_)) || border.representative_color().a > 0.0)
{
border.paint_drrect(canvas, outer, inner, bounds);
}
}
}
#[derive(Debug)]
struct BorderPaint {
style: ControlBorderStyle,
child: WidgetRef,
}
impl RenderObjectWidget for BorderPaint {
type RenderObject = RenderBorderPaint;
fn create_render_object(&self, app: &mut App, _: BuildContext) -> AnyRenderObject {
RenderHandle::new_box(
app,
RenderBorderPaint {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
child: RenderObjectWithChildData::new(),
style: self.style,
},
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_: BuildContext,
render: RenderHandle<RenderBorderPaint>,
) {
if render.get(app).style != self.style {
render.get_mut(app).style = self.style;
render.as_object().mark_needs_paint(app);
}
}
}
impl SingleChildRenderObjectWidget for BorderPaint {
fn child(&self) -> Option<&WidgetRef> {
Some(&self.child)
}
}
struct RenderBorderPaint {
render_object: RenderObjectData,
render_box: RenderBoxData,
child: RenderObjectWithChildData<AnyRenderBox>,
style: ControlBorderStyle,
}
impl RenderObjectWithChildMixin for RenderBorderPaint {
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 RenderProxyBoxMixin for RenderBorderPaint {}
impl RenderObject for RenderBorderPaint {
waft_rendering::render_object_accessors!();
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
RenderProxyBoxMixin::perform_layout(self, app);
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
if let Some(child) = self.child(app) {
visitor(child.as_object());
}
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
let style = self.get(app).style;
let size = self.size(app);
let canvas = context.canvas();
canvas.save();
canvas.translate(offset.dx() as f32, offset.dy() as f32);
style.paint(canvas, size);
canvas.restore();
RenderProxyBoxMixin::paint(self, app, context, offset);
}
}
impl RenderBox for RenderBorderPaint {
waft_rendering::render_box_accessors!();
fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
RenderProxyBoxMixin::compute_min_intrinsic_width(self, app, height)
}
fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
RenderProxyBoxMixin::compute_max_intrinsic_width(self, app, height)
}
fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
RenderProxyBoxMixin::compute_min_intrinsic_height(self, app, width)
}
fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
RenderProxyBoxMixin::compute_max_intrinsic_height(self, app, width)
}
fn compute_dry_layout(
self: RenderHandle<Self>,
app: &mut App,
constraints: BoxConstraints,
) -> Size {
RenderProxyBoxMixin::compute_dry_layout(self, app, constraints)
}
fn compute_distance_to_actual_baseline(
self: RenderHandle<Self>,
app: &mut App,
baseline: TextBaseline,
) -> Option<f64> {
RenderProxyBoxMixin::compute_distance_to_actual_baseline(self, app, baseline)
}
fn compute_dry_baseline(
self: RenderHandle<Self>,
app: &mut App,
constraints: BoxConstraints,
baseline: TextBaseline,
) -> Option<f64> {
RenderProxyBoxMixin::compute_dry_baseline(self, app, constraints, baseline)
}
fn hit_test_self(self: RenderHandle<Self>, app: &App, position: Offset) -> bool {
self.get(app).style.hit_test(self.size(app), position)
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
let outer = self.get(app).style.outlines(self.size(app)).0;
outer.contains(position)
&& RenderProxyBoxMixin::hit_test_children(self, app, result, position)
}
}