1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
use crate::core::algebra::Vector2; use crate::message::MessageData; use crate::{ brush::Brush, core::{color::Color, math::Rect, pool::Handle, scope_profile}, draw::{CommandKind, CommandTexture, DrawingContext}, message::UiMessage, widget::{Widget, WidgetBuilder}, BuildContext, Control, Thickness, UINode, UserInterface, }; use std::ops::{Deref, DerefMut}; #[derive(Clone)] pub struct Border<M: MessageData, C: Control<M, C>> { widget: Widget<M, C>, stroke_thickness: Thickness, } crate::define_widget_deref!(Border<M, C>); impl<M: MessageData, C: Control<M, C>> Control<M, C> for Border<M, C> { fn measure_override( &self, ui: &UserInterface<M, C>, available_size: Vector2<f32>, ) -> Vector2<f32> { scope_profile!(); let margin_x = self.stroke_thickness.left + self.stroke_thickness.right; let margin_y = self.stroke_thickness.top + self.stroke_thickness.bottom; let size_for_child = Vector2::new(available_size.x - margin_x, available_size.y - margin_y); let mut desired_size = Vector2::default(); for child_handle in self.widget.children() { ui.node(*child_handle).measure(ui, size_for_child); let child = ui.nodes.borrow(*child_handle); let child_desired_size = child.desired_size(); if child_desired_size.x > desired_size.x { desired_size.x = child_desired_size.x; } if child_desired_size.y > desired_size.y { desired_size.y = child_desired_size.y; } } desired_size.x += margin_x; desired_size.y += margin_y; desired_size } fn arrange_override(&self, ui: &UserInterface<M, C>, final_size: Vector2<f32>) -> Vector2<f32> { scope_profile!(); let rect_for_child = Rect::new( self.stroke_thickness.left, self.stroke_thickness.top, final_size.x - (self.stroke_thickness.right + self.stroke_thickness.left), final_size.y - (self.stroke_thickness.bottom + self.stroke_thickness.top), ); for child_handle in self.widget.children() { ui.node(*child_handle).arrange(ui, &rect_for_child); } final_size } fn draw(&self, drawing_context: &mut DrawingContext) { let bounds = self.widget.screen_bounds(); drawing_context.push_rect_filled(&bounds, None); drawing_context.commit( CommandKind::Geometry, self.widget.background(), CommandTexture::None, ); drawing_context.push_rect_vary(&bounds, self.stroke_thickness); drawing_context.commit( CommandKind::Geometry, self.widget.foreground(), CommandTexture::None, ); } fn handle_routed_message( &mut self, ui: &mut UserInterface<M, C>, message: &mut UiMessage<M, C>, ) { self.widget.handle_routed_message(ui, message); } } impl<M: MessageData, C: Control<M, C>> Border<M, C> { pub fn new(widget: Widget<M, C>) -> Self { Self { widget, stroke_thickness: Thickness::uniform(1.0), } } pub fn set_stroke_thickness(&mut self, thickness: Thickness) -> &mut Self { if self.stroke_thickness != thickness { self.stroke_thickness = thickness; self.widget.invalidate_layout(); } self } } pub struct BorderBuilder<M: MessageData, C: Control<M, C>> { pub widget_builder: WidgetBuilder<M, C>, pub stroke_thickness: Option<Thickness>, } impl<M: MessageData, C: Control<M, C>> BorderBuilder<M, C> { pub fn new(widget_builder: WidgetBuilder<M, C>) -> Self { Self { widget_builder, stroke_thickness: None, } } pub fn with_stroke_thickness(mut self, stroke_thickness: Thickness) -> Self { self.stroke_thickness = Some(stroke_thickness); self } pub fn build_border(mut self) -> Border<M, C> { if self.widget_builder.foreground.is_none() { self.widget_builder.foreground = Some(Brush::Solid(Color::opaque(100, 100, 100))); } Border { widget: self.widget_builder.build(), stroke_thickness: self .stroke_thickness .unwrap_or_else(|| Thickness::uniform(1.0)), } } pub fn build(self, ctx: &mut BuildContext<'_, M, C>) -> Handle<UINode<M, C>> { ctx.add_node(UINode::Border(self.build_border())) } }