1use std::any::Any;
5use std::fmt;
6
7use crate::align::{Align, Anchors, SelfAlign};
8use crate::canvas::Canvas;
9use crate::error::{Error, Result};
10use crate::event::{EventArgs, EventCtx, KeyEvent, PointerEvent, WheelEvent};
11use crate::geometry::{Bounds, PointF, RectF, SizeF};
12use crate::paint::PaintCtx;
13use crate::platform::CursorShape;
14use crate::prop::{PropInfo, PropKind, PropValue};
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
21pub struct ComponentId {
22 pub(crate) index: u32,
23 pub(crate) generation: u32,
24}
25
26impl ComponentId {
27 pub fn to_u64(self) -> u64 {
29 (self.generation as u64) << 32 | self.index as u64
30 }
31
32 pub fn from_u64(value: u64) -> Self {
34 Self { index: value as u32, generation: (value >> 32) as u32 }
35 }
36}
37
38impl fmt::Display for ComponentId {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 write!(f, "#{}v{}", self.index, self.generation)
41 }
42}
43
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
46pub struct ControlState {
47 pub hover: bool,
48 pub pressed: bool,
49 pub focused: bool,
50 pub checked: bool,
51 pub disabled: bool,
53}
54
55pub trait Component: Any {
86 fn class_name(&self) -> &'static str;
89
90 fn inner(&self) -> Option<&dyn Component> {
92 None
93 }
94
95 fn inner_mut(&mut self) -> Option<&mut dyn Component> {
96 None
97 }
98
99 fn class_chain(&self, out: &mut Vec<&'static str>) {
102 out.push(self.class_name());
103 if let Some(inner) = self.inner() {
104 inner.class_chain(out);
105 }
106 }
107
108 fn control(&self) -> Option<&ControlBase> {
110 self.inner().and_then(|i| i.control())
111 }
112
113 fn control_mut(&mut self) -> Option<&mut ControlBase> {
114 self.inner_mut().and_then(|i| i.control_mut())
115 }
116
117 fn props(&self) -> &'static [PropInfo] {
120 &[]
121 }
122
123 fn default_event(&self) -> Option<&'static str> {
127 self.inner().and_then(|c| c.default_event())
128 }
129
130 fn get_prop(&self, _name: &str) -> Option<PropValue> {
132 None
133 }
134
135 fn set_prop(&mut self, name: &str, _value: PropValue) -> Result<()> {
138 Err(Error::UnknownProperty(name.to_string()))
139 }
140
141 fn notification(&mut self, freed: ComponentId) {
144 if let Some(inner) = self.inner_mut() {
145 inner.notification(freed);
146 }
147 }
148
149 fn paint(&self, canvas: &mut dyn Canvas, ctx: &PaintCtx<'_>) {
151 if let Some(inner) = self.inner() {
152 inner.paint(canvas, ctx);
153 }
154 }
155
156 fn transparent_background(&self) -> bool {
160 self.inner().is_some_and(|i| i.transparent_background())
161 }
162
163 fn opaque(&self, style: &crate::theme::ResolvedStyle) -> bool {
168 self.inner().is_some_and(|i| i.opaque(style))
169 }
170
171 fn paint_after(&self, canvas: &mut dyn Canvas, ctx: &PaintCtx<'_>) {
173 if let Some(inner) = self.inner() {
174 inner.paint_after(canvas, ctx);
175 }
176 }
177
178 fn paint_is_pure(&self) -> bool {
185 false
186 }
187
188 fn provided(&self) -> Option<crate::providers::Provided<'_>> {
191 None
192 }
193
194 fn resized(&mut self, ctx: &mut EventCtx<'_>) {
196 if let Some(inner) = self.inner_mut() {
197 inner.resized(ctx);
198 }
199 }
200
201 fn click(&mut self, ctx: &mut EventCtx<'_>) {
203 match self.inner_mut() {
204 Some(inner) => inner.click(ctx),
205 None => ctx.fire("on_click", EventArgs::None),
206 }
207 }
208
209 fn pointer_down(&mut self, ctx: &mut EventCtx<'_>, event: &PointerEvent) {
210 if let Some(inner) = self.inner_mut() {
211 inner.pointer_down(ctx, event);
212 }
213 }
214
215 fn pointer_up(&mut self, ctx: &mut EventCtx<'_>, event: &PointerEvent) {
216 if let Some(inner) = self.inner_mut() {
217 inner.pointer_up(ctx, event);
218 }
219 }
220
221 fn pointer_move(&mut self, ctx: &mut EventCtx<'_>, event: &PointerEvent) {
222 if let Some(inner) = self.inner_mut() {
223 inner.pointer_move(ctx, event);
224 }
225 }
226
227 fn pointer_enter(&mut self, ctx: &mut EventCtx<'_>) {
228 if let Some(inner) = self.inner_mut() {
229 inner.pointer_enter(ctx);
230 }
231 }
232
233 fn pointer_leave(&mut self, ctx: &mut EventCtx<'_>) {
234 if let Some(inner) = self.inner_mut() {
235 inner.pointer_leave(ctx);
236 }
237 }
238
239 fn wheel(&mut self, ctx: &mut EventCtx<'_>, event: &WheelEvent) {
240 if let Some(inner) = self.inner_mut() {
241 inner.wheel(ctx, event);
242 }
243 }
244
245 fn gesture(&mut self, ctx: &mut EventCtx<'_>, event: &crate::event::GestureEvent) {
248 if let Some(inner) = self.inner_mut() {
249 inner.gesture(ctx, event);
250 }
251 }
252
253 fn key_down(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
254 if let Some(inner) = self.inner_mut() {
255 inner.key_down(ctx, event);
256 }
257 }
258
259 fn key_up(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
260 if let Some(inner) = self.inner_mut() {
261 inner.key_up(ctx, event);
262 }
263 }
264
265 fn shortcut(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
269 if let Some(inner) = self.inner_mut() {
270 inner.shortcut(ctx, event);
271 }
272 }
273
274 fn platform_hooks(&self) -> Option<&dyn PlatformHooks> {
277 self.inner().and_then(|i| i.platform_hooks())
278 }
279
280 fn platform_hooks_mut(&mut self) -> Option<&mut dyn PlatformHooks> {
281 self.inner_mut().and_then(|i| i.platform_hooks_mut())
282 }
283
284 fn design_hooks(&self) -> Option<&dyn DesignHooks> {
287 self.inner().and_then(|i| i.design_hooks())
288 }
289
290 fn design_hooks_mut(&mut self) -> Option<&mut dyn DesignHooks> {
291 self.inner_mut().and_then(|i| i.design_hooks_mut())
292 }
293
294 fn contains_point(&self, local: PointF, size: SizeF) -> bool {
297 self.inner().map_or(true, |i| i.contains_point(local, size))
298 }
299
300 fn hit_slop(&self) -> f32 {
304 self.inner().map_or(0.0, |i| i.hit_slop())
305 }
306
307 fn scroll_axes(&self) -> (bool, bool) {
310 self.inner().map_or((false, false), |i| i.scroll_axes())
311 }
312
313 fn drag_item(&self, _ui: &crate::Ui) -> Option<(String, String)> {
318 None
319 }
320
321 fn drag_visual(&self, _ui: &crate::Ui, _source: ComponentId) -> Option<crate::DragVisual> {
324 None
325 }
326
327 fn drop_accepts(&self, _ui: &crate::Ui, _id: ComponentId, _data: &str) -> bool {
332 false
333 }
334
335 fn drop_hover(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
338
339 fn drop_leave(&mut self, _ctx: &mut EventCtx<'_>) {}
341
342 fn drop_receive(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
344
345 fn drag_ended(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _end: crate::DragEnd) {}
347
348 fn accepts_text(&self) -> bool {
349 self.inner().is_some_and(|i| i.accepts_text())
350 }
351
352 fn captures_key(&self, event: &KeyEvent) -> bool {
355 self.inner().is_some_and(|i| i.captures_key(event))
356 }
357
358 fn mnemonic_text(&self) -> Option<&str> {
361 self.inner().and_then(|i| i.mnemonic_text())
362 }
363
364 fn mnemonic_focuses_child(&self) -> bool {
366 self.inner().is_some_and(|i| i.mnemonic_focuses_child())
367 }
368
369 fn text_input(&mut self, ctx: &mut EventCtx<'_>, text: &str) {
371 if let Some(inner) = self.inner_mut() {
372 inner.text_input(ctx, text);
373 }
374 }
375
376 fn focus_enter(&mut self, ctx: &mut EventCtx<'_>) {
377 if let Some(inner) = self.inner_mut() {
378 inner.focus_enter(ctx);
379 }
380 }
381
382 fn focus_exit(&mut self, ctx: &mut EventCtx<'_>) {
383 if let Some(inner) = self.inner_mut() {
384 inner.focus_exit(ctx);
385 }
386 }
387
388 fn property_changed(&mut self, ctx: &mut EventCtx<'_>, name: &str) {
392 if let Some(inner) = self.inner_mut() {
393 inner.property_changed(ctx, name);
394 }
395 }
396
397 fn checked_state(&self) -> Option<bool> {
400 self.inner().and_then(|i| i.checked_state())
401 }
402
403 fn change_args(&self, name: &str, value: &PropValue) -> Option<EventArgs> {
410 match self.inner() {
411 Some(inner) => inner.change_args(name, value),
412 None => Some(EventArgs::Value(value.clone())),
413 }
414 }
415
416 fn paint_only_property(&self, name: &str) -> bool {
421 self.inner().is_some_and(|inner| inner.paint_only_property(name))
422 }
423
424 fn timer_interval(&self) -> Option<f64> {
427 self.inner().and_then(|i| i.timer_interval())
428 }
429
430 fn timer_precision(&self) -> crate::TimerPrecision {
432 self.inner().map(|i| i.timer_precision()).unwrap_or_default()
433 }
434
435 fn timer_background(&self) -> bool {
438 self.inner().is_some_and(|i| i.timer_background())
439 }
440
441 fn timer_late(&mut self, late: f64) {
443 if let Some(inner) = self.inner_mut() {
444 inner.timer_late(late);
445 }
446 }
447
448 fn frame(&mut self, ctx: &mut EventCtx<'_>) -> bool {
452 self.inner_mut().is_some_and(|inner| inner.frame(ctx))
453 }
454
455 fn timer(&mut self, ctx: &mut EventCtx<'_>) {
458 match self.inner_mut() {
459 Some(inner) => inner.timer(ctx),
460 None => ctx.fire("on_timer", EventArgs::None),
461 }
462 }
463
464 fn arrange(&self, area: RectF, children: &[ArrangeItem]) -> Option<Vec<RectF>> {
469 self.inner().and_then(|i| i.arrange(area, children))
470 }
471
472 fn baseline(&self, ui: &crate::Ui, id: ComponentId, height: f32) -> Option<f32> {
476 self.inner().and_then(|i| i.baseline(ui, id, height))
477 }
478
479 fn dock_gap(&self) -> f32 {
482 self.inner().map_or(0.0, |i| i.dock_gap())
483 }
484
485 fn auto_height(&self, ui: &crate::Ui, id: ComponentId, width: f32) -> Option<f32> {
489 self.inner().and_then(|i| i.auto_height(ui, id, width))
490 }
491
492 fn auto_width(&self, ui: &crate::Ui, id: ComponentId) -> Option<f32> {
496 self.inner().and_then(|i| i.auto_width(ui, id))
497 }
498
499 fn closes_on_outside_press(&self) -> bool {
506 false
507 }
508
509 fn dismissed(&mut self, ctx: &mut EventCtx<'_>) -> bool {
515 let _ = ctx;
516 false
517 }
518
519 fn dies_with(&self) -> Option<ComponentId> {
523 self.inner().and_then(|i| i.dies_with())
524 }
525
526 fn access_items(&self, ui: &crate::Ui, id: ComponentId) -> Vec<crate::automation::AccessItem> {
529 self.inner().map_or_else(Vec::new, |i| i.access_items(ui, id))
530 }
531
532 fn access_action(&mut self, ctx: &mut EventCtx<'_>, index: usize, action: crate::automation::ItemAction) -> bool {
535 self.inner_mut().is_some_and(|i| i.access_action(ctx, index, action))
536 }
537
538 fn attach(&self) -> Option<(ComponentId, f32, f32)> {
542 self.inner().and_then(|i| i.attach())
543 }
544
545 fn event_info(&self, event: &str) -> Vec<(&'static str, String)> {
549 self.inner().map(|i| i.event_info(event)).unwrap_or_default()
550 }
551
552 fn message(&mut self, ctx: &mut EventCtx<'_>, message: &dyn Any) {
556 if let Some(inner) = self.inner_mut() {
557 inner.message(ctx, message);
558 }
559 }
560
561 fn methods(&self) -> &'static [MethodInfo] {
564 &[]
565 }
566
567 fn invoke(&mut self, ctx: &mut EventCtx<'_>, method: &str, args: &[PropValue]) -> std::result::Result<PropValue, String> {
571 let class = self.class_name();
572 match self.inner_mut() {
573 Some(inner) => inner.invoke(ctx, method, args),
574 None => Err(format!("{class} não tem o método '{method}'")),
575 }
576 }
577
578}
579
580pub trait PlatformHooks {
584 fn taskbar(&self) -> Option<crate::taskbar::TaskbarSpec> {
586 None
587 }
588
589 fn tray(&self) -> Option<crate::tray::TraySpec> {
591 None
592 }
593
594 fn native_view(&self) -> Option<NativeView> {
598 None
599 }
600
601 fn take_native_commands(&self) -> Vec<NativeCommand> {
603 Vec::new()
604 }
605
606 fn connection(&self) -> Option<Connection> {
610 None
611 }
612
613 fn files_dropped(&mut self, _ctx: &mut EventCtx<'_>, _paths: &[String]) -> bool {
616 false
617 }
618
619 fn files_hovering(&mut self, _ctx: &mut EventCtx<'_>, _paths: Option<&[String]>) -> bool {
622 false
623 }
624}
625
626pub trait DesignHooks {
630 fn design_grip(&self, _local: PointF) -> Option<usize> {
633 None
634 }
635
636 fn design_drag(&mut self, _grip: usize, _delta: PointF, _begin: bool) -> Option<(Option<ComponentId>, &'static str, PropValue)> {
640 None
641 }
642}
643
644#[derive(Clone, Copy, Debug, PartialEq)]
646pub struct ArrangeItem {
647 pub id: ComponentId,
648 pub size: SizeF,
649 pub margins: Bounds,
651 pub span: u32,
653 pub row_span: u32,
655 pub cell: Option<(u32, u32)>,
657 pub grow: f32,
659 pub baseline: Option<f32>,
661 pub spacing: f32,
664}
665
666impl ArrangeItem {
667 pub fn of(id: ComponentId, cb: &ControlBase) -> Self {
669 ArrangeItem {
670 id,
671 size: SizeF::new(cb.width(), cb.height()),
672 margins: cb.margins(),
673 span: cb.column_span(),
674 row_span: cb.row_span(),
675 cell: cb.grid_cell(),
676 grow: cb.grow(),
677 baseline: None,
678 spacing: cb.spacing_scale(),
679 }
680 }
681}
682
683#[derive(Clone, Debug, PartialEq)]
685pub enum NativeView {
686 Web { url: String, html: String, generation: u64, navigation: Vec<String>, sandbox: String, background: Option<crate::Color>, data_dir: String, browser_args: String },
695}
696
697#[derive(Clone, Debug, PartialEq)]
701pub struct WebMessage {
702 pub text: String,
703 pub url: String,
704}
705
706pub fn navigation_allowed(allowed: &[String], url: &str) -> bool {
710 allowed.is_empty()
711 || url.starts_with("about:")
712 || url.starts_with("data:")
713 || allowed.iter().any(|a| {
714 let a = a.trim_end_matches('/');
715 url == a || url.starts_with(&format!("{a}/")) || url.starts_with(&format!("{a}?")) || (a.matches('/').count() <= 2 && url_origin(url) == url_origin(a))
716 })
717}
718
719pub fn url_origin(url: &str) -> String {
721 let Some((scheme, rest)) = url.split_once("://") else { return url.to_string() };
722 let host = rest.split(['/', '?', '#']).next().unwrap_or_default();
723 format!("{}://{}", scheme.to_ascii_lowercase(), host.to_ascii_lowercase())
724}
725
726#[derive(Clone, Copy, Debug, PartialEq)]
729pub struct MethodInfo {
730 pub name: &'static str,
731 pub args: &'static [PropKind],
732 pub returns: Option<PropKind>,
733 pub params: &'static [&'static str],
736}
737
738impl MethodInfo {
739 pub const fn new(name: &'static str, args: &'static [PropKind], returns: Option<PropKind>) -> Self {
740 Self { name, args, returns, params: &[] }
741 }
742
743 pub const fn params(self, names: &'static [&'static str]) -> Self {
746 Self { params: names, ..self }
747 }
748}
749
750#[derive(Clone, Debug, PartialEq)]
752pub enum NativeCommand {
753 Eval(String),
755 Back,
756 Forward,
757 Reload,
758}
759
760#[derive(Clone, Debug, PartialEq)]
762pub struct NativeViewSlot {
763 pub id: ComponentId,
764 pub rect: crate::RectF,
765 pub clip: Option<crate::RectF>,
768 pub visible: bool,
769 pub view: NativeView,
770 pub commands: Vec<NativeCommand>,
771}
772
773pub fn prop_infos_of(component: &dyn Component) -> Vec<PropInfo> {
779 let mut out: Vec<PropInfo> = Vec::new();
780 let mut seen = std::collections::HashSet::new();
783 let mut push = |info: &PropInfo| {
784 if seen.insert(info.name) {
785 out.push(*info);
786 }
787 };
788 let mut layer = Some(component);
789 while let Some(l) = layer {
790 l.props().iter().for_each(&mut push);
791 layer = l.inner();
792 }
793 if component.control().is_some() {
794 ControlBase::PROPS.iter().for_each(&mut push);
795 }
796 out
797}
798
799pub fn prop_info_of(component: &dyn Component, name: &str) -> Option<PropInfo> {
802 let mut layer = Some(component);
803 while let Some(l) = layer {
804 if let Some(info) = l.props().iter().find(|p| p.name == name) {
805 return Some(*info);
806 }
807 layer = l.inner();
808 }
809 if component.control().is_some() {
810 return ControlBase::PROPS.iter().find(|p| p.name == name).copied();
811 }
812 None
813}
814
815pub const VALUE_PROPERTIES: [&str; 19] = [
820 "text", "checked", "value", "item_index", "selected", "date", "end_date", "time", "color", "files", "html", "index", "page", "hotkey",
821 "angle", "pressed", "position", "low", "high",
822];
823
824pub fn is_value_property(info: &PropInfo) -> bool {
828 match info.name {
829 "tab_index" | "expanded" => true,
830 name => matches!(info.category, "Data" | "Runtime") && VALUE_PROPERTIES.contains(&name),
831 }
832}
833
834pub fn read_prop(component: &dyn Component, name: &str) -> Option<PropValue> {
835 let mut layer = Some(component);
836 while let Some(l) = layer {
837 if let Some(v) = l.get_prop(name) {
838 return Some(v);
839 }
840 layer = l.inner();
841 }
842 component.control().and_then(|cb| cb.get_prop(name))
843}
844
845#[derive(Clone, Copy, Debug, PartialEq)]
848pub(crate) struct AnchorRef {
849 pub(crate) parent: SizeF,
850 pub(crate) rect: RectF,
851}
852
853#[derive(Clone, Debug)]
858pub struct ControlBase {
859 left: f32,
861 top: f32,
863 width: f32,
865 height: f32,
867 margins: Bounds,
869 padding: Bounds,
871 align: Align,
873 anchors: Anchors,
875 visible: bool,
877 enabled: bool,
879 hit_test: bool,
881 clip_children: bool,
883 inherit_state: bool,
886 translatable: bool,
890 captures_wheel: bool,
893 can_focus: bool,
895 draggable: bool,
897 accepts_drops: bool,
899 drag: Option<Box<DragDecl>>,
902 tab_stop: bool,
904 pan_on_touch: bool,
906 pub(crate) popup_menu: Option<ComponentId>,
908 cursor: CursorShape,
910 opacity: f32,
912 rotation: f32,
914 scale: f32,
919 translate: PointF,
921 cache_layer: bool,
924 aspect_ratio: f32,
926 max_width: f32,
928 min_width: f32,
930 min_height: f32,
931 max_height: f32,
932 self_align: SelfAlign,
934 auto_size: bool,
936 column_span: u32,
938 grid_row: i16,
941 grid_column: i16,
942 row_span: u16,
943 grow: f32,
946 pub(crate) text_scale: f32,
949 pub(crate) spacing_scale: f32,
950 shadow: Option<crate::canvas::Shadow>,
952 tag: i64,
954 rare: Option<Box<RareTexts>>,
957 style: String,
959 theme: String,
961 hint: String,
963 state: ControlState,
964 pub(crate) bounds: RectF,
965 pub(crate) anchor_ref: Option<AnchorRef>,
966 pub(crate) follow: Option<(ComponentId, Option<PointF>)>,
969 pub(crate) design_hidden: bool,
972 pub(crate) design_only: bool,
974 pub(crate) keep_left_to_right: bool,
977 pub(crate) colors: ColorOverride,
979 pub(crate) font: FontOverride,
981 scroll_offset: PointF,
983 pub(crate) laid_out_scroll: PointF,
986 pub(crate) safe_area: Bounds,
988 pub(crate) content_size: SizeF,
990}
991
992impl Default for ControlBase {
993 fn default() -> Self {
994 Self::new(100.0, 100.0)
995 }
996}
997
998impl ControlBase {
999 pub const PROPS: &'static [PropInfo] = &[
1001 PropInfo::new("left", PropKind::Float, "Layout"),
1002 PropInfo::new("top", PropKind::Float, "Layout"),
1003 PropInfo::new("width", PropKind::Float, "Layout"),
1004 PropInfo::new("height", PropKind::Float, "Layout"),
1005 PropInfo::new("align", PropKind::Enum(<Align as crate::prop::PropEnum>::NAMES), "Layout"),
1006 PropInfo::new("anchors", PropKind::Set(&Anchors::NAMES), "Layout"),
1007 PropInfo::new("margins", PropKind::Bounds, "Layout"),
1008 PropInfo::new("padding", PropKind::Bounds, "Layout"),
1009 PropInfo::new("visible", PropKind::Bool, "Behavior"),
1010 PropInfo::new("enabled", PropKind::Bool, "Behavior"),
1011 PropInfo::new("hit_test", PropKind::Bool, "Behavior"),
1012 PropInfo::new("clip_children", PropKind::Bool, "Behavior"),
1013 PropInfo::new("inherit_state", PropKind::Bool, "Appearance"),
1014 PropInfo::new("translate", PropKind::Bool, "Behavior"),
1015 PropInfo::new("captures_wheel", PropKind::Bool, "Behavior"),
1016 PropInfo::new("can_focus", PropKind::Bool, "Behavior"),
1017 PropInfo::new("draggable", PropKind::Bool, "Behavior"),
1018 PropInfo::new("accepts_drops", PropKind::Bool, "Behavior"),
1019 PropInfo::new("drag_data", PropKind::String, "Behavior"),
1020 PropInfo::new("drag_label", PropKind::String, "Behavior"),
1021 PropInfo::new("drag_image", PropKind::Component, "Behavior"),
1022 PropInfo::new("drop_types", PropKind::String, "Behavior"),
1023 PropInfo::new("tab_stop", PropKind::Bool, "Behavior"),
1024 PropInfo::new("cursor", PropKind::Enum(<CursorShape as crate::prop::PropEnum>::NAMES), "Behavior"),
1025 PropInfo::new("opacity", PropKind::Float, "Appearance"),
1026 PropInfo::new("cache_layer", PropKind::Bool, "Appearance"),
1027 PropInfo::new("rotation", PropKind::Angle, "Layout"),
1028 PropInfo::new("scale", PropKind::Float, "Layout"),
1029 PropInfo::new("translate_x", PropKind::Float, "Appearance"),
1030 PropInfo::new("translate_y", PropKind::Float, "Appearance"),
1031 PropInfo::new("aspect_ratio", PropKind::Float, "Layout"),
1032 PropInfo::new("max_width", PropKind::Float, "Layout"),
1033 PropInfo::new("min_width", PropKind::Float, "Layout"),
1034 PropInfo::new("min_height", PropKind::Float, "Layout"),
1035 PropInfo::new("max_height", PropKind::Float, "Layout"),
1036 PropInfo::new("self_align", PropKind::Enum(<SelfAlign as crate::prop::PropEnum>::NAMES), "Layout"),
1037 PropInfo::new("auto_size", PropKind::Bool, "Layout"),
1038 PropInfo::new("column_span", PropKind::Int, "Layout"),
1039 PropInfo::new("grid_row", PropKind::Int, "Layout"),
1040 PropInfo::new("grid_column", PropKind::Int, "Layout"),
1041 PropInfo::new("row_span", PropKind::Int, "Layout"),
1042 PropInfo::new("grow", PropKind::Float, "Layout"),
1043 PropInfo::new("shadow", PropKind::Shadow, "Appearance"),
1044 PropInfo::new("style", PropKind::String, "Appearance"),
1045 PropInfo::new("theme", PropKind::String, "Appearance"),
1046 PropInfo::new("fill", PropKind::Color, "Appearance"),
1047 PropInfo::new("border_color", PropKind::Color, "Appearance"),
1048 PropInfo::new("text_color", PropKind::Color, "Appearance"),
1049 PropInfo::new("accent_color", PropKind::Color, "Appearance"),
1050 PropInfo::new("success_color", PropKind::Color, "Appearance"),
1051 PropInfo::new("warning_color", PropKind::Color, "Appearance"),
1052 PropInfo::new("danger_color", PropKind::Color, "Appearance"),
1053 PropInfo::new("font_family", PropKind::String, "Font"),
1054 PropInfo::new("font_size", PropKind::Float, "Font"),
1055 PropInfo::new("font_weight", PropKind::Enum(&FONT_WEIGHTS), "Font"),
1056 PropInfo::new("font_italic", PropKind::Bool, "Font"),
1057 PropInfo::new("font_underline", PropKind::Bool, "Font"),
1058 PropInfo::new("font_strikeout", PropKind::Bool, "Font"),
1059 PropInfo::new("hint", PropKind::String, "Behavior"),
1060 PropInfo::new("tag", PropKind::Int, "Misc"),
1061 PropInfo::new("tag_string", PropKind::String, "Misc"),
1062 PropInfo::new("accessible_name", PropKind::String, "Behavior"),
1063 PropInfo::new("popup_menu", PropKind::Component, "Behavior"),
1064 PropInfo::new("on_click", PropKind::Event, "Events"),
1065 PropInfo::new("on_dbl_click", PropKind::Event, "Events"),
1066 PropInfo::new("on_long_press", PropKind::Event, "Events"),
1067 PropInfo::new("on_pointer_down", PropKind::Event, "Events"),
1068 PropInfo::new("on_pointer_up", PropKind::Event, "Events"),
1069 PropInfo::new("on_pointer_move", PropKind::Event, "Events"),
1070 PropInfo::new("on_pointer_enter", PropKind::Event, "Events"),
1071 PropInfo::new("on_pointer_leave", PropKind::Event, "Events"),
1072 PropInfo::new("on_wheel", PropKind::Event, "Events"),
1073 PropInfo::new("on_gesture", PropKind::Event, "Events"),
1074 PropInfo::new("on_key_down", PropKind::Event, "Events"),
1075 PropInfo::new("on_key_press", PropKind::Event, "Events"),
1076 PropInfo::new("on_key_up", PropKind::Event, "Events"),
1077 PropInfo::new("on_enter", PropKind::Event, "Events"),
1078 PropInfo::new("on_exit", PropKind::Event, "Events"),
1079 PropInfo::new("on_resize", PropKind::Event, "Events"),
1080 PropInfo::new("on_drag_start", PropKind::Event, "Events"),
1081 PropInfo::new("on_drag_over", PropKind::Event, "Events"),
1082 PropInfo::new("on_drag_move", PropKind::Event, "Events"),
1083 PropInfo::new("on_drag_leave", PropKind::Event, "Events"),
1084 PropInfo::new("on_drag_drop", PropKind::Event, "Events"),
1085 PropInfo::new("on_drag_end", PropKind::Event, "Events"),
1086 ];
1087
1088 pub fn new(width: f32, height: f32) -> Self {
1089 Self {
1090 left: 0.0,
1091 top: 0.0,
1092 width,
1093 height,
1094 margins: Bounds::ZERO,
1095 padding: Bounds::ZERO,
1096 align: Align::None,
1097 anchors: Anchors::default(),
1098 visible: true,
1099 enabled: true,
1100 hit_test: true,
1101 clip_children: false,
1102 inherit_state: false,
1103 translatable: true,
1104 captures_wheel: false,
1105 can_focus: false,
1106 draggable: false,
1107 accepts_drops: false,
1108 drag: None,
1109 tab_stop: true,
1110 pan_on_touch: true,
1111 popup_menu: None,
1112 cursor: CursorShape::Default,
1113 opacity: 1.0,
1114 rotation: 0.0,
1115 scale: 1.0,
1116 translate: PointF::ZERO,
1117 cache_layer: false,
1118 aspect_ratio: 0.0,
1119 max_width: 0.0,
1120 min_width: 0.0,
1121 min_height: 0.0,
1122 max_height: 0.0,
1123 self_align: SelfAlign::Stretch,
1124 auto_size: false,
1125 column_span: 1,
1126 grid_row: -1,
1127 grid_column: -1,
1128 row_span: 1,
1129 grow: 0.0,
1130 text_scale: 1.0,
1131 spacing_scale: 1.0,
1132 shadow: None,
1133 tag: 0,
1134 rare: None,
1135 style: String::new(),
1136 theme: String::new(),
1137 hint: String::new(),
1138 state: ControlState::default(),
1139 bounds: RectF::new(0.0, 0.0, width, height),
1140 anchor_ref: None,
1141 follow: None,
1142 design_hidden: false,
1143 design_only: false,
1144 keep_left_to_right: false,
1145 colors: ColorOverride::default(),
1146 font: FontOverride::default(),
1147 scroll_offset: PointF::ZERO,
1148 laid_out_scroll: PointF::ZERO,
1149 content_size: SizeF::ZERO,
1150 safe_area: Bounds::default(),
1151 }
1152 }
1153
1154 pub fn left(&self) -> f32 {
1157 self.left
1158 }
1159
1160 pub fn top(&self) -> f32 {
1161 self.top
1162 }
1163
1164 pub fn width(&self) -> f32 {
1165 self.width
1166 }
1167
1168 pub fn height(&self) -> f32 {
1169 self.height
1170 }
1171
1172 pub fn position(&self) -> PointF {
1173 PointF::new(self.left, self.top)
1174 }
1175
1176 pub fn size(&self) -> SizeF {
1177 SizeF::new(self.width, self.height)
1178 }
1179
1180 pub fn margins(&self) -> Bounds {
1183 if self.spacing_scale == 1.0 { self.margins } else { self.margins.scaled(self.spacing_scale) }
1184 }
1185
1186 pub fn padding(&self) -> Bounds {
1188 if self.spacing_scale == 1.0 { self.padding } else { self.padding.scaled(self.spacing_scale) }
1189 }
1190
1191 pub fn align(&self) -> Align {
1192 self.align
1193 }
1194
1195 pub fn anchors(&self) -> Anchors {
1196 self.anchors
1197 }
1198
1199 pub fn visible(&self) -> bool {
1200 self.visible
1201 }
1202
1203 pub fn design_only(&self) -> bool {
1205 self.design_only
1206 }
1207
1208 pub fn colors(&self) -> ColorOverride {
1210 self.colors
1211 }
1212
1213 pub fn set_colors(&mut self, colors: ColorOverride) -> &mut Self {
1214 self.colors = colors;
1215 self
1216 }
1217
1218 pub fn set_design_only(&mut self, on: bool) -> &mut Self {
1219 self.design_only = on;
1220 self
1221 }
1222
1223 pub fn keep_left_to_right(&self) -> bool {
1225 self.keep_left_to_right
1226 }
1227
1228 pub fn set_keep_left_to_right(&mut self, on: bool) -> &mut Self {
1229 self.keep_left_to_right = on;
1230 self
1231 }
1232
1233 pub fn enabled(&self) -> bool {
1234 self.enabled
1235 }
1236
1237 pub fn hit_test(&self) -> bool {
1238 self.hit_test
1239 }
1240
1241 pub fn translatable(&self) -> bool {
1242 self.translatable
1243 }
1244
1245 pub fn set_translatable(&mut self, v: bool) -> &mut Self {
1246 self.translatable = v;
1247 self
1248 }
1249
1250 pub fn inherit_state(&self) -> bool {
1251 self.inherit_state
1252 }
1253
1254 pub fn set_inherit_state(&mut self, v: bool) -> &mut Self {
1255 self.inherit_state = v;
1256 self
1257 }
1258
1259 pub fn clip_children(&self) -> bool {
1260 self.clip_children
1261 }
1262
1263 pub fn captures_wheel(&self) -> bool {
1264 self.captures_wheel
1265 }
1266
1267 pub fn draggable(&self) -> bool {
1272 self.draggable
1273 }
1274
1275 pub fn set_draggable(&mut self, v: bool) -> &mut Self {
1276 self.draggable = v;
1277 self
1278 }
1279
1280 pub fn accepts_drops(&self) -> bool {
1283 self.accepts_drops
1284 }
1285
1286 pub fn set_accepts_drops(&mut self, v: bool) -> &mut Self {
1287 self.accepts_drops = v;
1288 self
1289 }
1290
1291 pub fn drag_data(&self) -> &str {
1293 self.drag.as_ref().map_or("", |d| d.data.as_str())
1294 }
1295
1296 pub fn drag_label(&self) -> &str {
1297 self.drag.as_ref().map_or("", |d| d.label.as_str())
1298 }
1299
1300 pub fn drag_image(&self) -> Option<ComponentId> {
1302 self.drag.as_ref().and_then(|d| d.image)
1303 }
1304
1305 pub(crate) fn forget_drag_image(&mut self, doomed: &[ComponentId]) {
1307 if let Some(d) = self.drag.as_mut().filter(|d| d.image.is_some_and(|i| doomed.contains(&i))) {
1308 d.image = None;
1309 }
1310 }
1311
1312 pub fn drop_types(&self) -> &str {
1314 self.drag.as_ref().map_or("", |d| d.drop_types.as_str())
1315 }
1316
1317 fn drag_decl(&mut self) -> &mut DragDecl {
1318 self.drag.get_or_insert_with(Default::default)
1319 }
1320
1321 pub fn can_focus(&self) -> bool {
1322 self.can_focus
1323 }
1324
1325 pub fn tab_stop(&self) -> bool {
1327 self.tab_stop
1328 }
1329
1330 pub fn popup_menu(&self) -> Option<ComponentId> {
1332 self.popup_menu
1333 }
1334
1335 pub fn set_popup_menu(&mut self, menu: Option<ComponentId>) -> &mut Self {
1336 self.popup_menu = menu;
1337 self
1338 }
1339
1340 pub fn pan_on_touch(&self) -> bool {
1342 self.pan_on_touch
1343 }
1344
1345 pub fn cursor(&self) -> CursorShape {
1347 self.cursor
1348 }
1349
1350 pub fn opacity(&self) -> f32 {
1351 self.opacity
1352 }
1353
1354 pub fn cache_layer(&self) -> bool {
1356 self.cache_layer
1357 }
1358
1359 pub fn set_cache_layer(&mut self, on: bool) -> &mut Self {
1360 self.cache_layer = on;
1361 self
1362 }
1363
1364 pub fn rotation(&self) -> f32 {
1367 self.rotation
1368 }
1369
1370 pub fn scale(&self) -> f32 {
1372 self.scale
1373 }
1374
1375 pub fn inner_size(&self) -> SizeF {
1378 let s = self.scale;
1379 if s == 1.0 { self.bounds.size() } else { SizeF::new(self.bounds.width / s, self.bounds.height / s) }
1380 }
1381
1382 pub fn shadow(&self) -> Option<crate::canvas::Shadow> {
1383 self.shadow
1384 }
1385
1386 pub fn aspect_ratio(&self) -> f32 {
1390 self.aspect_ratio
1391 }
1392
1393 pub fn set_aspect_ratio(&mut self, ratio: f32) -> &mut Self {
1394 self.aspect_ratio = if ratio.is_finite() { ratio.max(0.0) } else { 0.0 };
1395 self
1396 }
1397
1398 pub fn max_width(&self) -> f32 {
1402 self.max_width
1403 }
1404
1405 pub fn set_max_width(&mut self, width: f32) -> &mut Self {
1406 self.max_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1407 self
1408 }
1409
1410 pub fn min_width(&self) -> f32 {
1415 self.min_width
1416 }
1417
1418 pub fn set_min_width(&mut self, width: f32) -> &mut Self {
1419 self.min_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1420 self
1421 }
1422
1423 pub fn min_height(&self) -> f32 {
1425 self.min_height
1426 }
1427
1428 pub fn set_min_height(&mut self, height: f32) -> &mut Self {
1429 self.min_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1430 self
1431 }
1432
1433 pub fn max_height(&self) -> f32 {
1435 self.max_height
1436 }
1437
1438 pub fn set_max_height(&mut self, height: f32) -> &mut Self {
1439 self.max_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1440 self
1441 }
1442
1443 pub fn clamp_width(&self, available: f32) -> f32 {
1446 let w = if self.max_width > 0.0 { available.min(self.max_width) } else { available };
1447 w.max(self.min_width)
1448 }
1449
1450 pub fn clamp_height(&self, height: f32) -> f32 {
1452 let h = if self.max_height > 0.0 { height.min(self.max_height) } else { height };
1453 h.max(self.min_height)
1454 }
1455
1456 pub fn self_align(&self) -> SelfAlign {
1459 self.self_align
1460 }
1461
1462 pub fn set_self_align(&mut self, align: SelfAlign) -> &mut Self {
1463 self.self_align = align;
1464 self
1465 }
1466
1467 pub fn band_width(&self, available: f32) -> f32 {
1471 match self.self_align {
1472 SelfAlign::Stretch => self.clamp_width(available),
1473 _ if self.max_width > 0.0 => self.clamp_width(available),
1474 _ => self.clamp_width(self.width),
1475 }
1476 }
1477
1478 pub fn band_height(&self, available: f32) -> f32 {
1481 match self.self_align {
1482 SelfAlign::Stretch => self.clamp_height(available),
1483 _ if self.max_height > 0.0 => self.clamp_height(available),
1484 _ => self.clamp_height(self.height),
1485 }
1486 }
1487
1488 pub fn band_offset(&self, available: f32, size: f32) -> f32 {
1491 let spare = (available - size).max(0.0);
1492 match self.self_align {
1493 SelfAlign::Start => 0.0,
1494 SelfAlign::End => spare,
1495 SelfAlign::Stretch | SelfAlign::Center => spare / 2.0,
1496 }
1497 }
1498
1499 pub fn auto_size(&self) -> bool {
1504 self.auto_size
1505 }
1506
1507 pub fn set_auto_size(&mut self, on: bool) -> &mut Self {
1508 self.auto_size = on;
1509 self
1510 }
1511
1512 pub fn column_span(&self) -> u32 {
1515 self.column_span.max(1)
1516 }
1517
1518 pub fn grid_cell(&self) -> Option<(u32, u32)> {
1520 (self.grid_row >= 0 && self.grid_column >= 0).then(|| (self.grid_row as u32, self.grid_column as u32))
1521 }
1522
1523 pub fn set_grid_cell(&mut self, cell: Option<(u32, u32)>) -> &mut Self {
1524 let (r, c) = cell.map_or((-1, -1), |(r, c)| (r.min(i16::MAX as u32) as i16, c.min(i16::MAX as u32) as i16));
1525 self.grid_row = r;
1526 self.grid_column = c;
1527 self
1528 }
1529
1530 pub fn row_span(&self) -> u32 {
1532 u32::from(self.row_span.max(1))
1533 }
1534
1535 pub fn set_row_span(&mut self, span: u32) -> &mut Self {
1536 self.row_span = span.clamp(1, 64) as u16;
1537 self
1538 }
1539
1540 pub fn grow(&self) -> f32 {
1541 self.grow
1542 }
1543
1544 pub fn set_grow(&mut self, grow: f32) -> &mut Self {
1545 self.grow = grow.max(0.0);
1546 self
1547 }
1548
1549 pub fn set_column_span(&mut self, span: u32) -> &mut Self {
1550 self.column_span = span.max(1);
1551 self
1552 }
1553
1554 pub fn text_scale(&self) -> f32 {
1556 self.text_scale
1557 }
1558
1559 pub fn spacing_scale(&self) -> f32 {
1561 self.spacing_scale
1562 }
1563
1564 pub fn tag(&self) -> i64 {
1565 self.tag
1566 }
1567
1568 pub fn accessible_name(&self) -> &str {
1571 self.rare.as_ref().map_or("", |r| &r.accessible_name)
1572 }
1573
1574 pub fn set_accessible_name(&mut self, v: &str) -> &mut Self {
1575 self.rare_mut(|r| r.accessible_name = v.into());
1576 self
1577 }
1578
1579 fn rare_mut(&mut self, change: impl FnOnce(&mut RareTexts)) {
1580 let rare = self.rare.get_or_insert_with(Default::default);
1581 change(rare);
1582 if rare.tag_string.is_empty() && rare.accessible_name.is_empty() {
1583 self.rare = None;
1584 }
1585 }
1586
1587 pub fn tag_string(&self) -> &str {
1588 self.rare.as_ref().map_or("", |r| &r.tag_string)
1589 }
1590
1591 pub fn style(&self) -> &str {
1593 &self.style
1594 }
1595
1596 pub fn theme_name(&self) -> &str {
1598 &self.theme
1599 }
1600
1601 pub fn set_theme_name(&mut self, v: impl Into<String>) -> &mut Self {
1602 self.theme = v.into();
1603 self
1604 }
1605
1606 pub fn hint(&self) -> &str {
1608 &self.hint
1609 }
1610
1611 pub fn set_hint(&mut self, v: impl Into<String>) -> &mut Self {
1612 self.hint = v.into();
1613 self
1614 }
1615
1616 pub fn state(&self) -> ControlState {
1617 self.state
1618 }
1619
1620 pub fn bounds(&self) -> RectF {
1622 self.bounds
1623 }
1624
1625 pub fn scroll_offset(&self) -> PointF {
1627 self.scroll_offset
1628 }
1629
1630 pub fn laid_out_scroll(&self) -> PointF {
1633 self.laid_out_scroll
1634 }
1635
1636 pub fn set_scroll_offset(&mut self, offset: PointF) -> &mut Self {
1639 self.scroll_offset = offset;
1640 self
1641 }
1642
1643 pub fn content_size(&self) -> SizeF {
1646 self.content_size
1647 }
1648
1649 pub fn set_left(&mut self, v: f32) -> &mut Self {
1656 if self.left != v {
1657 self.left = v;
1658 self.anchor_ref = None;
1659 }
1660 self
1661 }
1662
1663 pub fn set_top(&mut self, v: f32) -> &mut Self {
1664 if self.top != v {
1665 self.top = v;
1666 self.anchor_ref = None;
1667 }
1668 self
1669 }
1670
1671 pub fn set_position(&mut self, left: f32, top: f32) -> &mut Self {
1672 self.set_left(left).set_top(top)
1673 }
1674
1675 pub fn set_width(&mut self, v: f32) -> &mut Self {
1676 if self.width != v.max(0.0) {
1677 self.width = v.max(0.0);
1678 self.anchor_ref = None;
1679 }
1680 self
1681 }
1682
1683 pub fn set_height(&mut self, v: f32) -> &mut Self {
1684 if self.height != v.max(0.0) {
1685 self.height = v.max(0.0);
1686 self.anchor_ref = None;
1687 }
1688 self
1689 }
1690
1691 pub(crate) fn set_measured_width(&mut self, v: f32) {
1693 self.width = v.max(0.0);
1694 if let Some(r) = self.anchor_ref.as_mut() {
1695 r.rect.width = self.width;
1696 }
1697 }
1698
1699 pub(crate) fn set_measured_height(&mut self, v: f32) {
1702 self.height = v.max(0.0);
1703 if let Some(r) = self.anchor_ref.as_mut() {
1704 r.rect.height = self.height;
1705 }
1706 }
1707
1708 pub fn set_size(&mut self, width: f32, height: f32) -> &mut Self {
1709 self.set_width(width).set_height(height)
1710 }
1711
1712 pub fn set_rect(&mut self, left: f32, top: f32, width: f32, height: f32) -> &mut Self {
1714 self.set_position(left, top).set_size(width, height)
1715 }
1716
1717 pub fn set_margins(&mut self, v: impl Into<Bounds>) -> &mut Self {
1718 self.margins = v.into();
1719 self
1720 }
1721
1722 pub fn set_padding(&mut self, v: impl Into<Bounds>) -> &mut Self {
1723 self.padding = v.into();
1724 self
1725 }
1726
1727 pub fn set_align(&mut self, v: Align) -> &mut Self {
1728 self.align = v;
1729 self.anchor_ref = None;
1730 self
1731 }
1732
1733 pub fn set_anchors(&mut self, v: Anchors) -> &mut Self {
1734 self.anchors = v;
1735 self.anchor_ref = None;
1736 self
1737 }
1738
1739 pub fn set_visible(&mut self, v: bool) -> &mut Self {
1740 self.visible = v;
1741 self
1742 }
1743
1744 pub fn set_enabled(&mut self, v: bool) -> &mut Self {
1745 self.enabled = v;
1746 self
1747 }
1748
1749 pub fn set_hit_test(&mut self, v: bool) -> &mut Self {
1750 self.hit_test = v;
1751 self
1752 }
1753
1754 pub fn set_clip_children(&mut self, v: bool) -> &mut Self {
1755 self.clip_children = v;
1756 self
1757 }
1758
1759 pub fn set_captures_wheel(&mut self, v: bool) -> &mut Self {
1760 self.captures_wheel = v;
1761 self
1762 }
1763
1764 pub fn set_can_focus(&mut self, v: bool) -> &mut Self {
1765 self.can_focus = v;
1766 self
1767 }
1768
1769 pub fn set_pan_on_touch(&mut self, v: bool) -> &mut Self {
1773 self.pan_on_touch = v;
1774 self
1775 }
1776
1777 pub fn set_tab_stop(&mut self, v: bool) -> &mut Self {
1778 self.tab_stop = v;
1779 self
1780 }
1781
1782 pub fn set_cursor(&mut self, v: CursorShape) -> &mut Self {
1783 self.cursor = v;
1784 self
1785 }
1786
1787 pub fn set_opacity(&mut self, v: f32) -> &mut Self {
1788 self.opacity = v.clamp(0.0, 1.0);
1789 self
1790 }
1791
1792 pub fn translate(&self) -> PointF {
1794 self.translate
1795 }
1796
1797 pub fn set_translate(&mut self, offset: PointF) -> &mut Self {
1798 self.translate = if offset.x.is_finite() && offset.y.is_finite() { offset } else { PointF::ZERO };
1799 self
1800 }
1801
1802 pub fn set_scale(&mut self, scale: f32) -> &mut Self {
1804 self.scale = if scale.is_finite() && scale > 0.0 { scale.clamp(0.05, 20.0) } else { 1.0 };
1805 self
1806 }
1807
1808 pub fn set_rotation(&mut self, degrees: f32) -> &mut Self {
1809 self.rotation = if degrees.is_finite() { degrees % 360.0 } else { 0.0 };
1810 self
1811 }
1812
1813 pub fn set_shadow(&mut self, shadow: Option<crate::canvas::Shadow>) -> &mut Self {
1814 self.shadow = shadow;
1815 self
1816 }
1817
1818 pub fn set_tag(&mut self, v: i64) -> &mut Self {
1819 self.tag = v;
1820 self
1821 }
1822
1823 pub fn set_tag_string(&mut self, v: &str) -> &mut Self {
1824 self.rare_mut(|r| r.tag_string = v.into());
1825 self
1826 }
1827
1828 pub fn set_style(&mut self, v: impl Into<String>) -> &mut Self {
1829 self.style = v.into();
1830 self
1831 }
1832
1833 pub fn state_mut(&mut self) -> &mut ControlState {
1836 &mut self.state
1837 }
1838
1839 pub fn get_prop(&self, name: &str) -> Option<PropValue> {
1842 Some(match name {
1843 "left" => self.left.into(),
1844 "top" => self.top.into(),
1845 "width" => self.width.into(),
1846 "height" => self.height.into(),
1847 "align" => self.align.into(),
1848 "anchors" => self.anchors.into(),
1849 "margins" => self.margins.into(),
1850 "padding" => self.padding.into(),
1851 "visible" => self.visible.into(),
1852 "enabled" => self.enabled.into(),
1853 "hit_test" => self.hit_test.into(),
1854 "clip_children" => self.clip_children.into(),
1855 "inherit_state" => self.inherit_state.into(),
1856 "translate" => self.translatable.into(),
1857 "captures_wheel" => self.captures_wheel.into(),
1858 "can_focus" => self.can_focus.into(),
1859 "draggable" => self.draggable.into(),
1860 "accepts_drops" => self.accepts_drops.into(),
1861 "drag_data" => self.drag_data().into(),
1862 "drag_label" => self.drag_label().into(),
1863 "drag_image" => PropValue::Component(self.drag_image()),
1864 "drop_types" => self.drop_types().into(),
1865 "tab_stop" => self.tab_stop.into(),
1866 "cursor" => PropValue::enum_value(self.cursor),
1867 "opacity" => self.opacity.into(),
1868 "cache_layer" => self.cache_layer.into(),
1869 "rotation" => self.rotation.into(),
1870 "scale" => self.scale.into(),
1871 "translate_x" => self.translate.x.into(),
1872 "translate_y" => self.translate.y.into(),
1873 "aspect_ratio" => self.aspect_ratio.into(),
1874 "max_width" => self.max_width.into(),
1875 "min_width" => self.min_width.into(),
1876 "min_height" => self.min_height.into(),
1877 "max_height" => self.max_height.into(),
1878 "self_align" => PropValue::enum_value(self.self_align),
1879 "auto_size" => self.auto_size.into(),
1880 "column_span" => (self.column_span() as i64).into(),
1881 "grid_row" => i64::from(self.grid_row).into(),
1882 "grid_column" => i64::from(self.grid_column).into(),
1883 "row_span" => (self.row_span() as i64).into(),
1884 "grow" => f64::from(self.grow).into(),
1885 "shadow" => crate::prop::PropType::to_value(&self.shadow),
1886 "style" => self.style.clone().into(),
1887 "fill" => crate::prop::PropType::to_value(&self.colors.fill),
1888 "border_color" => crate::prop::PropType::to_value(&self.colors.border),
1889 "text_color" => crate::prop::PropType::to_value(&self.colors.text),
1890 "accent_color" => crate::prop::PropType::to_value(&self.colors.accent),
1891 "success_color" => crate::prop::PropType::to_value(&self.colors.success),
1892 "warning_color" => crate::prop::PropType::to_value(&self.colors.warning),
1893 "danger_color" => crate::prop::PropType::to_value(&self.colors.danger),
1894 "font_family" => self.font.family.clone().into(),
1895 "font_size" => crate::prop::PropType::to_value(&self.font.size),
1896 "font_weight" => PropValue::Enum(FONT_WEIGHTS[self.font.weight.map_or(0, |w| w as usize + 1)].into()),
1897 "font_italic" => self.font.italic.into(),
1898 "font_underline" => self.font.underline.into(),
1899 "font_strikeout" => self.font.strikeout.into(),
1900 "theme" => self.theme.clone().into(),
1901 "hint" => self.hint.clone().into(),
1902 "tag" => self.tag.into(),
1903 "tag_string" => self.tag_string().to_string().into(),
1904 "accessible_name" => self.accessible_name().to_string().into(),
1905 "popup_menu" => PropValue::Component(self.popup_menu),
1906 _ => return None,
1907 })
1908 }
1909
1910 pub fn set_prop(&mut self, name: &str, value: PropValue) -> Result<()> {
1911 match name {
1912 "left" => self.set_left(value.to_f32()?),
1913 "top" => self.set_top(value.to_f32()?),
1914 "width" => self.set_width(value.to_f32()?),
1915 "height" => self.set_height(value.to_f32()?),
1916 "align" => self.set_align(value.to_enum()?),
1917 "anchors" => self.set_anchors(value.to_anchors()?),
1918 "margins" => self.set_margins(value.to_bounds()?),
1919 "padding" => self.set_padding(value.to_bounds()?),
1920 "visible" => self.set_visible(value.to_bool()?),
1921 "enabled" => self.set_enabled(value.to_bool()?),
1922 "hit_test" => self.set_hit_test(value.to_bool()?),
1923 "clip_children" => self.set_clip_children(value.to_bool()?),
1924 "inherit_state" => self.set_inherit_state(value.to_bool()?),
1925 "translate" => self.set_translatable(value.to_bool()?),
1926 "captures_wheel" => self.set_captures_wheel(value.to_bool()?),
1927 "can_focus" => self.set_can_focus(value.to_bool()?),
1928 "draggable" => self.set_draggable(value.to_bool()?),
1929 "accepts_drops" => self.set_accepts_drops(value.to_bool()?),
1930 "drag_data" => {
1931 self.drag_decl().data = value.to_str()?.to_string();
1932 self
1933 }
1934 "drag_label" => {
1935 self.drag_decl().label = value.to_str()?.to_string();
1936 self
1937 }
1938 "drag_image" => {
1939 self.drag_decl().image = value.to_component()?;
1940 self
1941 }
1942 "drop_types" => {
1943 self.drag_decl().drop_types = value.to_str()?.to_string();
1944 self
1945 }
1946 "tab_stop" => self.set_tab_stop(value.to_bool()?),
1947 "cursor" => self.set_cursor(value.to_enum()?),
1948 "opacity" => self.set_opacity(value.to_f32()?),
1949 "cache_layer" => self.set_cache_layer(value.to_bool()?),
1950 "rotation" => self.set_rotation(value.to_f32()?),
1951 "scale" => self.set_scale(value.to_f32()?),
1952 "translate_x" => self.set_translate(PointF::new(value.to_f32()?, self.translate.y)),
1953 "translate_y" => self.set_translate(PointF::new(self.translate.x, value.to_f32()?)),
1954 "aspect_ratio" => self.set_aspect_ratio(value.to_f32()?),
1955 "max_width" => self.set_max_width(value.to_f32()?),
1956 "min_width" => self.set_min_width(value.to_f32()?),
1957 "min_height" => self.set_min_height(value.to_f32()?),
1958 "max_height" => self.set_max_height(value.to_f32()?),
1959 "self_align" => self.set_self_align(value.to_enum()?),
1960 "auto_size" => self.set_auto_size(value.to_bool()?),
1961 "column_span" => self.set_column_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
1962 "grid_row" => {
1963 self.grid_row = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
1964 self
1965 }
1966 "grid_column" => {
1967 self.grid_column = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
1968 self
1969 }
1970 "row_span" => self.set_row_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
1971 "grow" => self.set_grow(value.to_f32()?),
1972 "shadow" => self.set_shadow(crate::prop::PropType::from_value(value)?),
1973 "style" => self.set_style(value.to_str()?),
1974 "fill" => {
1975 self.colors.fill = crate::prop::PropType::from_value(value)?;
1976 self
1977 }
1978 "border_color" => {
1979 self.colors.border = crate::prop::PropType::from_value(value)?;
1980 self
1981 }
1982 "text_color" => {
1983 self.colors.text = crate::prop::PropType::from_value(value)?;
1984 self
1985 }
1986 "accent_color" => {
1987 self.colors.accent = crate::prop::PropType::from_value(value)?;
1988 self
1989 }
1990 "success_color" => {
1991 self.colors.success = crate::prop::PropType::from_value(value)?;
1992 self
1993 }
1994 "warning_color" => {
1995 self.colors.warning = crate::prop::PropType::from_value(value)?;
1996 self
1997 }
1998 "danger_color" => {
1999 self.colors.danger = crate::prop::PropType::from_value(value)?;
2000 self
2001 }
2002 "font_family" => {
2003 self.font.family = value.to_str()?.to_string();
2004 self
2005 }
2006 "font_size" => {
2007 self.font.size = crate::prop::PropType::from_value(value)?;
2008 self
2009 }
2010 "font_weight" => {
2011 self.font.weight = font_weight_named(value.to_str()?)?;
2012 self
2013 }
2014 "font_italic" => {
2015 self.font.italic = value.to_bool()?;
2016 self
2017 }
2018 "font_underline" => {
2019 self.font.underline = value.to_bool()?;
2020 self
2021 }
2022 "font_strikeout" => {
2023 self.font.strikeout = value.to_bool()?;
2024 self
2025 }
2026 "theme" => self.set_theme_name(value.to_str()?),
2027 "hint" => self.set_hint(value.to_str()?),
2028 "tag" => self.set_tag(value.to_i64()?),
2029 "tag_string" => self.set_tag_string(value.to_str()?),
2030 "accessible_name" => self.set_accessible_name(value.to_str()?),
2031 "popup_menu" => {
2032 self.popup_menu = value.to_component()?;
2033 self
2034 }
2035 _ => return Err(Error::UnknownProperty(name.to_string())),
2036 };
2037 Ok(())
2038 }
2039}
2040
2041pub const FONT_WEIGHTS: [&str; 8] = ["default", "thin", "light", "normal", "medium", "semibold", "bold", "black"];
2043
2044fn font_weight_named(name: &str) -> Result<Option<crate::canvas::FontWeight>> {
2045 use crate::canvas::FontWeight::*;
2046 let all = [Thin, Light, Normal, Medium, SemiBold, Bold, Black];
2047 match FONT_WEIGHTS.iter().position(|n| n.eq_ignore_ascii_case(name)) {
2048 Some(0) => Ok(None),
2049 Some(i) => Ok(Some(all[i - 1])),
2050 None => Err(Error::invalid(format!("valor '{name}' inválido; use um de: {}", FONT_WEIGHTS.join(", ")))),
2051 }
2052}
2053
2054#[derive(Clone, Debug, Default, PartialEq)]
2058pub struct FontOverride {
2059 pub family: String,
2061 pub size: Option<f32>,
2062 pub weight: Option<crate::canvas::FontWeight>,
2063 pub italic: bool,
2065 pub underline: bool,
2066 pub strikeout: bool,
2067}
2068
2069impl FontOverride {
2070 pub fn is_empty(&self) -> bool {
2071 *self == Self::default()
2072 }
2073
2074 pub fn over(&self, parent: &FontOverride) -> FontOverride {
2076 FontOverride {
2077 family: if self.family.is_empty() { parent.family.clone() } else { self.family.clone() },
2078 size: self.size.or(parent.size),
2079 weight: self.weight.or(parent.weight),
2080 italic: self.italic || parent.italic,
2081 underline: self.underline || parent.underline,
2082 strikeout: self.strikeout || parent.strikeout,
2083 }
2084 }
2085
2086 pub fn apply(&self, font: &mut crate::canvas::FontSpec) {
2088 if !self.family.is_empty() {
2089 font.family = self.family.clone();
2090 }
2091 if let Some(size) = self.size.filter(|s| *s > 0.0) {
2092 font.size = size;
2093 }
2094 if let Some(weight) = self.weight {
2095 font.weight = weight;
2096 }
2097 font.italic |= self.italic;
2098 font.underline |= self.underline;
2099 font.strikeout |= self.strikeout;
2100 }
2101}
2102
2103impl ControlBase {
2104 pub fn font(&self) -> &FontOverride {
2106 &self.font
2107 }
2108
2109 pub fn set_font(&mut self, font: FontOverride) -> &mut Self {
2110 self.font = font;
2111 self
2112 }
2113}
2114
2115#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2121pub struct ColorOverride {
2122 pub fill: Option<crate::Color>,
2124 pub border: Option<crate::Color>,
2125 pub text: Option<crate::Color>,
2126 pub accent: Option<crate::Color>,
2129 pub success: Option<crate::Color>,
2131 pub warning: Option<crate::Color>,
2132 pub danger: Option<crate::Color>,
2133}
2134
2135impl ColorOverride {
2136 pub fn is_empty(&self) -> bool {
2137 *self == Self::default()
2138 }
2139
2140 pub(crate) fn recolors_theme(&self) -> bool {
2142 !self.is_empty()
2143 }
2144
2145 pub(crate) fn apply_theme(&self, theme: &crate::Theme) -> crate::Theme {
2147 use crate::Color;
2148 let mut t = theme.clone();
2149 let mut set = |name: &str, c: Color| {
2150 t.colors.insert(name.to_string(), c);
2151 };
2152 if let Some(a) = self.accent {
2153 let dark = luminance(a) < 0.5;
2154 set("primary", a);
2155 set("primary_hover", a.lerp(if dark { Color::WHITE } else { Color::BLACK }, 0.12));
2156 set("primary_pressed", a.lerp(if dark { Color::BLACK } else { Color::BLACK }, 0.15));
2157 set("on_primary", if luminance(a) < 0.6 { Color::WHITE } else { Color::BLACK });
2158 set("focus_ring", a);
2159 set("selection", a.with_opacity(0.25));
2160 set("primary_soft", a.with_opacity(0.1));
2161 set("primary_container", a.lerp(Color::WHITE, 0.82));
2162 }
2163 if let Some(c) = self.fill {
2164 let toward = if luminance(c) < 0.5 { Color::WHITE } else { Color::BLACK };
2166 set("surface", c);
2167 set("background", c);
2168 set("surface_2", c.lerp(toward.with_opacity(c.a as f32 / 255.0), 0.06));
2169 }
2170 if let Some(c) = self.text {
2171 set("text", c);
2172 set("text_muted", c.with_opacity(0.65));
2173 for on in ["on_primary", "on_danger", "on_success", "on_primary_container"] {
2174 set(on, c);
2175 }
2176 }
2177 if let Some(c) = self.border {
2178 set("border", c);
2179 set("track", c);
2180 }
2181 for (token, color) in [("success", self.success), ("warning", self.warning), ("danger", self.danger)] {
2182 if let Some(c) = color {
2183 set(token, c);
2184 set(&format!("{token}_soft"), c.with_opacity(0.1));
2185 set(&format!("{token}_hover"), c.lerp(Color::WHITE, 0.12));
2186 set(&format!("{token}_pressed"), c.lerp(Color::BLACK, 0.15));
2187 if token == "success" {
2188 set("meter_low", c);
2189 } else if token == "danger" {
2190 set("meter_high", c);
2191 } else {
2192 set("meter_mid", c);
2193 }
2194 }
2195 }
2196 t
2197 }
2198}
2199
2200pub(crate) fn luminance(c: crate::Color) -> f32 {
2202 (0.299 * c.r as f32 + 0.587 * c.g as f32 + 0.114 * c.b as f32) / 255.0
2203}
2204
2205#[derive(Clone, Copy, Debug, PartialEq)]
2207pub struct Connection {
2208 pub from: ComponentId,
2209 pub from_at: (f32, f32),
2211 pub to: ComponentId,
2212 pub to_at: (f32, f32),
2213}
2214
2215impl Connection {
2216 pub const STUB: f32 = 16.0;
2218
2219 fn normal(at: (f32, f32)) -> PointF {
2221 let (x, y) = at;
2222 let d = [(x, PointF::new(-1.0, 0.0)), (1.0 - x, PointF::new(1.0, 0.0)), (y, PointF::new(0.0, -1.0)), (1.0 - y, PointF::new(0.0, 1.0))];
2223 d.iter().min_by(|a, b| a.0.total_cmp(&b.0)).map_or(PointF::new(1.0, 0.0), |d| d.1)
2224 }
2225
2226 pub fn route(&self, from: RectF, to: RectF) -> Vec<PointF> {
2229 let p0 = PointF::new(from.x + from.width * self.from_at.0, from.y + from.height * self.from_at.1);
2230 let p1 = PointF::new(to.x + to.width * self.to_at.0, to.y + to.height * self.to_at.1);
2231 let (n0, n1) = (Self::normal(self.from_at), Self::normal(self.to_at));
2232 let s0 = PointF::new(p0.x + n0.x * Self::STUB, p0.y + n0.y * Self::STUB);
2233 let s1 = PointF::new(p1.x + n1.x * Self::STUB, p1.y + n1.y * Self::STUB);
2234 let mut points = vec![p0, s0];
2235 if n0.x != 0.0 {
2236 let mx = (s0.x + s1.x) / 2.0;
2238 points.push(PointF::new(mx, s0.y));
2239 points.push(PointF::new(mx, s1.y));
2240 } else {
2241 let my = (s0.y + s1.y) / 2.0;
2242 points.push(PointF::new(s0.x, my));
2243 points.push(PointF::new(s1.x, my));
2244 }
2245 points.push(s1);
2246 points.push(p1);
2247 points.dedup_by(|a, b| (a.x - b.x).abs() < 0.01 && (a.y - b.y).abs() < 0.01);
2248 points
2249 }
2250}
2251
2252#[derive(Clone, Debug, Default, PartialEq)]
2254pub struct DragDecl {
2255 pub data: String,
2256 pub label: String,
2257 pub drop_types: String,
2258 pub image: Option<ComponentId>,
2260}
2261
2262#[derive(Clone, Debug, Default)]
2264struct RareTexts {
2265 tag_string: Box<str>,
2267 accessible_name: Box<str>,
2268}