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 swipe(&mut self, ctx: &mut EventCtx<'_>, event: &crate::event::SwipeEvent) {
256 if let Some(inner) = self.inner_mut() {
257 inner.swipe(ctx, event);
258 }
259 }
260
261 fn touch_end(&mut self, ctx: &mut EventCtx<'_>) {
265 if let Some(inner) = self.inner_mut() {
266 inner.touch_end(ctx);
267 }
268 }
269
270 fn edge_swipe(&mut self, ctx: &mut EventCtx<'_>, event: &crate::event::SwipeEvent) -> bool {
273 match self.inner_mut() {
274 Some(inner) => inner.edge_swipe(ctx, event),
275 None => false,
276 }
277 }
278
279 fn context_menu(&mut self, ctx: &mut EventCtx<'_>, at: PointF, touch: bool) -> bool {
284 match self.inner_mut() {
285 Some(inner) => inner.context_menu(ctx, at, touch),
286 None => false,
287 }
288 }
289
290 fn key_down(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
291 if let Some(inner) = self.inner_mut() {
292 inner.key_down(ctx, event);
293 }
294 }
295
296 fn key_up(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
297 if let Some(inner) = self.inner_mut() {
298 inner.key_up(ctx, event);
299 }
300 }
301
302 fn shortcut(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
306 if let Some(inner) = self.inner_mut() {
307 inner.shortcut(ctx, event);
308 }
309 }
310
311 fn platform_hooks(&self) -> Option<&dyn PlatformHooks> {
314 self.inner().and_then(|i| i.platform_hooks())
315 }
316
317 fn platform_hooks_mut(&mut self) -> Option<&mut dyn PlatformHooks> {
318 self.inner_mut().and_then(|i| i.platform_hooks_mut())
319 }
320
321 fn design_hooks(&self) -> Option<&dyn DesignHooks> {
324 self.inner().and_then(|i| i.design_hooks())
325 }
326
327 fn design_hooks_mut(&mut self) -> Option<&mut dyn DesignHooks> {
328 self.inner_mut().and_then(|i| i.design_hooks_mut())
329 }
330
331 fn contains_point(&self, local: PointF, size: SizeF) -> bool {
334 self.inner().map_or(true, |i| i.contains_point(local, size))
335 }
336
337 fn hit_slop(&self) -> f32 {
341 self.inner().map_or(0.0, |i| i.hit_slop())
342 }
343
344 fn scroll_axes(&self) -> (bool, bool) {
347 self.inner().map_or((false, false), |i| i.scroll_axes())
348 }
349
350 fn drag_item(&self, _ui: &crate::Ui) -> Option<(String, String)> {
355 None
356 }
357
358 fn drag_visual(&self, _ui: &crate::Ui, _source: ComponentId) -> Option<crate::DragVisual> {
361 None
362 }
363
364 fn drop_accepts(&self, _ui: &crate::Ui, _id: ComponentId, _data: &str) -> bool {
369 false
370 }
371
372 fn drop_hover(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
375
376 fn drop_leave(&mut self, _ctx: &mut EventCtx<'_>) {}
378
379 fn drop_receive(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
381
382 fn drag_ended(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _end: crate::DragEnd) {}
384
385 fn accepts_text(&self) -> bool {
386 self.inner().is_some_and(|i| i.accepts_text())
387 }
388
389 fn captures_key(&self, event: &KeyEvent) -> bool {
392 self.inner().is_some_and(|i| i.captures_key(event))
393 }
394
395 fn mnemonic_text(&self) -> Option<&str> {
398 self.inner().and_then(|i| i.mnemonic_text())
399 }
400
401 fn mnemonic_focuses_child(&self) -> bool {
403 self.inner().is_some_and(|i| i.mnemonic_focuses_child())
404 }
405
406 fn text_input(&mut self, ctx: &mut EventCtx<'_>, text: &str) {
408 if let Some(inner) = self.inner_mut() {
409 inner.text_input(ctx, text);
410 }
411 }
412
413 fn focus_enter(&mut self, ctx: &mut EventCtx<'_>) {
414 if let Some(inner) = self.inner_mut() {
415 inner.focus_enter(ctx);
416 }
417 }
418
419 fn focus_exit(&mut self, ctx: &mut EventCtx<'_>) {
420 if let Some(inner) = self.inner_mut() {
421 inner.focus_exit(ctx);
422 }
423 }
424
425 fn property_changed(&mut self, ctx: &mut EventCtx<'_>, name: &str) {
429 if let Some(inner) = self.inner_mut() {
430 inner.property_changed(ctx, name);
431 }
432 }
433
434 fn checked_state(&self) -> Option<bool> {
437 self.inner().and_then(|i| i.checked_state())
438 }
439
440 fn change_args(&self, name: &str, value: &PropValue) -> Option<EventArgs> {
447 match self.inner() {
448 Some(inner) => inner.change_args(name, value),
449 None => Some(EventArgs::Value(value.clone())),
450 }
451 }
452
453 fn paint_only_property(&self, name: &str) -> bool {
458 self.inner().is_some_and(|inner| inner.paint_only_property(name))
459 }
460
461 fn timer_interval(&self) -> Option<f64> {
464 self.inner().and_then(|i| i.timer_interval())
465 }
466
467 fn timer_precision(&self) -> crate::TimerPrecision {
469 self.inner().map(|i| i.timer_precision()).unwrap_or_default()
470 }
471
472 fn timer_background(&self) -> bool {
475 self.inner().is_some_and(|i| i.timer_background())
476 }
477
478 fn timer_late(&mut self, late: f64) {
480 if let Some(inner) = self.inner_mut() {
481 inner.timer_late(late);
482 }
483 }
484
485 fn frame(&mut self, ctx: &mut EventCtx<'_>) -> bool {
489 self.inner_mut().is_some_and(|inner| inner.frame(ctx))
490 }
491
492 fn timer(&mut self, ctx: &mut EventCtx<'_>) {
495 match self.inner_mut() {
496 Some(inner) => inner.timer(ctx),
497 None => ctx.fire("on_timer", EventArgs::None),
498 }
499 }
500
501 fn arrange(&self, area: RectF, children: &[ArrangeItem]) -> Option<Vec<RectF>> {
506 self.inner().and_then(|i| i.arrange(area, children))
507 }
508
509 fn baseline(&self, ui: &crate::Ui, id: ComponentId, height: f32) -> Option<f32> {
513 self.inner().and_then(|i| i.baseline(ui, id, height))
514 }
515
516 fn dock_gap(&self) -> f32 {
519 self.inner().map_or(0.0, |i| i.dock_gap())
520 }
521
522 fn auto_height(&self, ui: &crate::Ui, id: ComponentId, width: f32) -> Option<f32> {
526 self.inner().and_then(|i| i.auto_height(ui, id, width))
527 }
528
529 fn auto_width(&self, ui: &crate::Ui, id: ComponentId) -> Option<f32> {
533 self.inner().and_then(|i| i.auto_width(ui, id))
534 }
535
536 fn closes_on_outside_press(&self) -> bool {
543 false
544 }
545
546 fn dismissed(&mut self, ctx: &mut EventCtx<'_>) -> bool {
552 let _ = ctx;
553 false
554 }
555
556 fn dies_with(&self) -> Option<ComponentId> {
560 self.inner().and_then(|i| i.dies_with())
561 }
562
563 fn access_items(&self, ui: &crate::Ui, id: ComponentId) -> Vec<crate::automation::AccessItem> {
566 self.inner().map_or_else(Vec::new, |i| i.access_items(ui, id))
567 }
568
569 fn access_action(&mut self, ctx: &mut EventCtx<'_>, index: usize, action: crate::automation::ItemAction) -> bool {
572 self.inner_mut().is_some_and(|i| i.access_action(ctx, index, action))
573 }
574
575 fn attach(&self) -> Option<(ComponentId, f32, f32)> {
579 self.inner().and_then(|i| i.attach())
580 }
581
582 fn event_info(&self, event: &str) -> Vec<(&'static str, String)> {
586 self.inner().map(|i| i.event_info(event)).unwrap_or_default()
587 }
588
589 fn message(&mut self, ctx: &mut EventCtx<'_>, message: &dyn Any) {
593 if let Some(inner) = self.inner_mut() {
594 inner.message(ctx, message);
595 }
596 }
597
598 fn methods(&self) -> &'static [MethodInfo] {
601 &[]
602 }
603
604 fn invoke(&mut self, ctx: &mut EventCtx<'_>, method: &str, args: &[PropValue]) -> std::result::Result<PropValue, String> {
608 let class = self.class_name();
609 match self.inner_mut() {
610 Some(inner) => inner.invoke(ctx, method, args),
611 None => Err(format!("{class} não tem o método '{method}'")),
612 }
613 }
614
615}
616
617pub trait PlatformHooks {
621 fn taskbar(&self) -> Option<crate::taskbar::TaskbarSpec> {
623 None
624 }
625
626 fn tray(&self) -> Option<crate::tray::TraySpec> {
628 None
629 }
630
631 fn native_view(&self) -> Option<NativeView> {
635 None
636 }
637
638 fn take_native_commands(&self) -> Vec<NativeCommand> {
640 Vec::new()
641 }
642
643 fn connection(&self) -> Option<Connection> {
647 None
648 }
649
650 fn files_dropped(&mut self, _ctx: &mut EventCtx<'_>, _paths: &[String]) -> bool {
653 false
654 }
655
656 fn files_hovering(&mut self, _ctx: &mut EventCtx<'_>, _paths: Option<&[String]>) -> bool {
659 false
660 }
661}
662
663pub trait DesignHooks {
667 fn design_grip(&self, _local: PointF) -> Option<usize> {
670 None
671 }
672
673 fn design_drag(&mut self, _grip: usize, _delta: PointF, _begin: bool) -> Option<(Option<ComponentId>, &'static str, PropValue)> {
677 None
678 }
679}
680
681#[derive(Clone, Copy, Debug, PartialEq)]
683pub struct ArrangeItem {
684 pub id: ComponentId,
685 pub size: SizeF,
686 pub margins: Bounds,
688 pub span: u32,
690 pub row_span: u32,
692 pub cell: Option<(u32, u32)>,
694 pub grow: f32,
696 pub baseline: Option<f32>,
698 pub spacing: f32,
701}
702
703impl ArrangeItem {
704 pub fn of(id: ComponentId, cb: &ControlBase) -> Self {
706 ArrangeItem {
707 id,
708 size: SizeF::new(cb.width(), cb.height()),
709 margins: cb.margins(),
710 span: cb.column_span(),
711 row_span: cb.row_span(),
712 cell: cb.grid_cell(),
713 grow: cb.grow(),
714 baseline: None,
715 spacing: cb.spacing_scale(),
716 }
717 }
718}
719
720#[derive(Clone, Debug, PartialEq)]
722pub enum NativeView {
723 Web { url: String, html: String, generation: u64, navigation: Vec<String>, sandbox: String, background: Option<crate::Color>, data_dir: String, browser_args: String },
732}
733
734#[derive(Clone, Debug, PartialEq)]
738pub struct WebMessage {
739 pub text: String,
740 pub url: String,
741}
742
743pub fn navigation_allowed(allowed: &[String], url: &str) -> bool {
747 allowed.is_empty()
748 || url.starts_with("about:")
749 || url.starts_with("data:")
750 || allowed.iter().any(|a| {
751 let a = a.trim_end_matches('/');
752 url == a || url.starts_with(&format!("{a}/")) || url.starts_with(&format!("{a}?")) || (a.matches('/').count() <= 2 && url_origin(url) == url_origin(a))
753 })
754}
755
756pub fn url_origin(url: &str) -> String {
758 let Some((scheme, rest)) = url.split_once("://") else { return url.to_string() };
759 let host = rest.split(['/', '?', '#']).next().unwrap_or_default();
760 format!("{}://{}", scheme.to_ascii_lowercase(), host.to_ascii_lowercase())
761}
762
763#[derive(Clone, Copy, Debug, PartialEq)]
766pub struct MethodInfo {
767 pub name: &'static str,
768 pub args: &'static [PropKind],
769 pub returns: Option<PropKind>,
770 pub params: &'static [&'static str],
773}
774
775impl MethodInfo {
776 pub const fn new(name: &'static str, args: &'static [PropKind], returns: Option<PropKind>) -> Self {
777 Self { name, args, returns, params: &[] }
778 }
779
780 pub const fn params(self, names: &'static [&'static str]) -> Self {
783 Self { params: names, ..self }
784 }
785}
786
787#[derive(Clone, Debug, PartialEq)]
789pub enum NativeCommand {
790 Eval(String),
792 Back,
793 Forward,
794 Reload,
795}
796
797#[derive(Clone, Debug, PartialEq)]
799pub struct NativeViewSlot {
800 pub id: ComponentId,
801 pub rect: crate::RectF,
802 pub clip: Option<crate::RectF>,
805 pub visible: bool,
806 pub view: NativeView,
807 pub commands: Vec<NativeCommand>,
808}
809
810pub fn prop_infos_of(component: &dyn Component) -> Vec<PropInfo> {
816 let mut out: Vec<PropInfo> = Vec::new();
817 let mut seen = std::collections::HashSet::new();
820 let mut push = |info: &PropInfo| {
821 if seen.insert(info.name) {
822 out.push(*info);
823 }
824 };
825 let mut layer = Some(component);
826 while let Some(l) = layer {
827 l.props().iter().for_each(&mut push);
828 layer = l.inner();
829 }
830 if component.control().is_some() {
831 ControlBase::PROPS.iter().for_each(&mut push);
832 }
833 out
834}
835
836pub fn prop_info_of(component: &dyn Component, name: &str) -> Option<PropInfo> {
839 let mut layer = Some(component);
840 while let Some(l) = layer {
841 if let Some(info) = l.props().iter().find(|p| p.name == name) {
842 return Some(*info);
843 }
844 layer = l.inner();
845 }
846 if component.control().is_some() {
847 return ControlBase::PROPS.iter().find(|p| p.name == name).copied();
848 }
849 None
850}
851
852pub const VALUE_PROPERTIES: [&str; 19] = [
857 "text", "checked", "value", "item_index", "selected", "date", "end_date", "time", "color", "files", "html", "index", "page", "hotkey",
858 "angle", "pressed", "position", "low", "high",
859];
860
861pub fn is_value_property(info: &PropInfo) -> bool {
865 match info.name {
866 "tab_index" | "expanded" => true,
867 name => matches!(info.category, "Data" | "Runtime") && VALUE_PROPERTIES.contains(&name),
868 }
869}
870
871pub fn read_prop(component: &dyn Component, name: &str) -> Option<PropValue> {
872 let mut layer = Some(component);
873 while let Some(l) = layer {
874 if let Some(v) = l.get_prop(name) {
875 return Some(v);
876 }
877 layer = l.inner();
878 }
879 component.control().and_then(|cb| cb.get_prop(name))
880}
881
882#[derive(Clone, Copy, Debug, PartialEq)]
885pub(crate) struct AnchorRef {
886 pub(crate) parent: SizeF,
887 pub(crate) rect: RectF,
888}
889
890#[derive(Clone, Debug)]
895pub struct ControlBase {
896 left: f32,
898 top: f32,
900 width: f32,
902 height: f32,
904 margins: Bounds,
906 padding: Bounds,
908 align: Align,
910 anchors: Anchors,
912 visible: bool,
914 enabled: bool,
916 hit_test: bool,
918 clip_children: bool,
920 inherit_state: bool,
923 translatable: bool,
927 captures_wheel: bool,
930 can_focus: bool,
932 draggable: bool,
934 accepts_drops: bool,
936 drag: Option<Box<DragDecl>>,
939 tab_stop: bool,
941 pan_on_touch: bool,
943 pub(crate) popup_menu: Option<ComponentId>,
945 cursor: CursorShape,
947 opacity: f32,
949 blend_mode: crate::BlendMode,
952 rotation: f32,
954 scale: f32,
959 translate: PointF,
961 cache_layer: bool,
964 aspect_ratio: f32,
966 max_width: f32,
968 min_width: f32,
970 min_height: f32,
972 max_height: f32,
974 self_align: SelfAlign,
976 auto_size: bool,
978 column_span: u32,
980 grid_row: i16,
983 grid_column: i16,
985 row_span: u16,
987 grow: f32,
990 pub(crate) text_scale: f32,
993 pub(crate) spacing_scale: f32,
994 shadow: Option<crate::canvas::Shadow>,
996 tag: i64,
998 rare: Option<Box<RareTexts>>,
1001 style: String,
1003 theme: String,
1005 hint: String,
1007 state: ControlState,
1008 pub(crate) bounds: RectF,
1009 pub(crate) anchor_ref: Option<AnchorRef>,
1010 pub(crate) follow: Option<(ComponentId, Option<PointF>)>,
1013 pub(crate) design_hidden: bool,
1016 pub(crate) design_only: bool,
1018 pub(crate) keep_left_to_right: bool,
1021 pub(crate) colors: ColorOverride,
1023 pub(crate) font: FontOverride,
1025 scroll_offset: PointF,
1027 pub(crate) laid_out_scroll: PointF,
1030 pub(crate) safe_area: Bounds,
1032 pub(crate) content_size: SizeF,
1034}
1035
1036impl Default for ControlBase {
1037 fn default() -> Self {
1038 Self::new(100.0, 100.0)
1039 }
1040}
1041
1042impl ControlBase {
1043 pub const PROPS: &'static [PropInfo] = &[
1045 PropInfo::new("left", PropKind::Float, "Layout"),
1046 PropInfo::new("top", PropKind::Float, "Layout"),
1047 PropInfo::new("width", PropKind::Float, "Layout"),
1048 PropInfo::new("height", PropKind::Float, "Layout"),
1049 PropInfo::new("align", PropKind::Enum(<Align as crate::prop::PropEnum>::NAMES), "Layout"),
1050 PropInfo::new("anchors", PropKind::Set(&Anchors::NAMES), "Layout"),
1051 PropInfo::new("margins", PropKind::Bounds, "Layout"),
1052 PropInfo::new("padding", PropKind::Bounds, "Layout"),
1053 PropInfo::new("visible", PropKind::Bool, "Behavior"),
1054 PropInfo::new("enabled", PropKind::Bool, "Behavior"),
1055 PropInfo::new("hit_test", PropKind::Bool, "Behavior"),
1056 PropInfo::new("clip_children", PropKind::Bool, "Behavior"),
1057 PropInfo::new("inherit_state", PropKind::Bool, "Appearance"),
1058 PropInfo::new("translate", PropKind::Bool, "Behavior"),
1059 PropInfo::new("captures_wheel", PropKind::Bool, "Behavior"),
1060 PropInfo::new("can_focus", PropKind::Bool, "Behavior"),
1061 PropInfo::new("draggable", PropKind::Bool, "Behavior"),
1062 PropInfo::new("accepts_drops", PropKind::Bool, "Behavior"),
1063 PropInfo::new("drag_data", PropKind::String, "Behavior"),
1064 PropInfo::new("drag_label", PropKind::String, "Behavior"),
1065 PropInfo::new("drag_image", PropKind::Component, "Behavior"),
1066 PropInfo::new("drop_types", PropKind::String, "Behavior"),
1067 PropInfo::new("tab_stop", PropKind::Bool, "Behavior"),
1068 PropInfo::new("cursor", PropKind::Enum(<CursorShape as crate::prop::PropEnum>::NAMES), "Behavior"),
1069 PropInfo::new("opacity", PropKind::Float, "Appearance"),
1070 PropInfo::new("blend_mode", PropKind::Enum(<crate::BlendMode as crate::prop::PropEnum>::NAMES), "Appearance"),
1071 PropInfo::new("cache_layer", PropKind::Bool, "Appearance"),
1072 PropInfo::new("rotation", PropKind::Angle, "Layout"),
1073 PropInfo::new("scale", PropKind::Float, "Layout"),
1074 PropInfo::new("translate_x", PropKind::Float, "Appearance"),
1075 PropInfo::new("translate_y", PropKind::Float, "Appearance"),
1076 PropInfo::new("aspect_ratio", PropKind::Float, "Layout"),
1077 PropInfo::new("max_width", PropKind::Float, "Layout"),
1078 PropInfo::new("min_width", PropKind::Float, "Layout"),
1079 PropInfo::new("min_height", PropKind::Float, "Layout"),
1080 PropInfo::new("max_height", PropKind::Float, "Layout"),
1081 PropInfo::new("self_align", PropKind::Enum(<SelfAlign as crate::prop::PropEnum>::NAMES), "Layout"),
1082 PropInfo::new("auto_size", PropKind::Bool, "Layout"),
1083 PropInfo::new("column_span", PropKind::Int, "Layout"),
1084 PropInfo::new("grid_row", PropKind::Int, "Layout"),
1085 PropInfo::new("grid_column", PropKind::Int, "Layout"),
1086 PropInfo::new("row_span", PropKind::Int, "Layout"),
1087 PropInfo::new("grow", PropKind::Float, "Layout"),
1088 PropInfo::new("shadow", PropKind::Shadow, "Appearance"),
1089 PropInfo::new("style", PropKind::String, "Appearance"),
1090 PropInfo::new("theme", PropKind::String, "Appearance"),
1091 PropInfo::new("fill", PropKind::Color, "Appearance"),
1092 PropInfo::new("border_color", PropKind::Color, "Appearance"),
1093 PropInfo::new("text_color", PropKind::Color, "Appearance"),
1094 PropInfo::new("accent_color", PropKind::Color, "Appearance"),
1095 PropInfo::new("success_color", PropKind::Color, "Appearance"),
1096 PropInfo::new("warning_color", PropKind::Color, "Appearance"),
1097 PropInfo::new("danger_color", PropKind::Color, "Appearance"),
1098 PropInfo::new("font_family", PropKind::String, "Font"),
1099 PropInfo::new("font_size", PropKind::Float, "Font"),
1100 PropInfo::new("font_weight", PropKind::Enum(&FONT_WEIGHTS), "Font"),
1101 PropInfo::new("font_italic", PropKind::Bool, "Font"),
1102 PropInfo::new("font_underline", PropKind::Bool, "Font"),
1103 PropInfo::new("font_strikeout", PropKind::Bool, "Font"),
1104 PropInfo::new("hint", PropKind::String, "Behavior"),
1105 PropInfo::new("tag", PropKind::Int, "Misc"),
1106 PropInfo::new("tag_string", PropKind::String, "Misc"),
1107 PropInfo::new("accessible_name", PropKind::String, "Behavior"),
1108 PropInfo::new("popup_menu", PropKind::Component, "Behavior"),
1109 PropInfo::new("on_click", PropKind::Event, "Events"),
1110 PropInfo::new("on_dbl_click", PropKind::Event, "Events"),
1111 PropInfo::new("on_long_press", PropKind::Event, "Events"),
1112 PropInfo::new("on_pointer_down", PropKind::Event, "Events"),
1113 PropInfo::new("on_pointer_up", PropKind::Event, "Events"),
1114 PropInfo::new("on_pointer_move", PropKind::Event, "Events"),
1115 PropInfo::new("on_pointer_enter", PropKind::Event, "Events"),
1116 PropInfo::new("on_pointer_leave", PropKind::Event, "Events"),
1117 PropInfo::new("on_wheel", PropKind::Event, "Events"),
1118 PropInfo::new("on_gesture", PropKind::Event, "Events"),
1119 PropInfo::new("on_swipe", PropKind::Event, "Events"),
1120 PropInfo::new("on_key_down", PropKind::Event, "Events"),
1121 PropInfo::new("on_key_press", PropKind::Event, "Events"),
1122 PropInfo::new("on_key_up", PropKind::Event, "Events"),
1123 PropInfo::new("on_enter", PropKind::Event, "Events"),
1124 PropInfo::new("on_exit", PropKind::Event, "Events"),
1125 PropInfo::new("on_resize", PropKind::Event, "Events"),
1126 PropInfo::new("on_drag_start", PropKind::Event, "Events"),
1127 PropInfo::new("on_drag_over", PropKind::Event, "Events"),
1128 PropInfo::new("on_drag_move", PropKind::Event, "Events"),
1129 PropInfo::new("on_drag_leave", PropKind::Event, "Events"),
1130 PropInfo::new("on_drag_drop", PropKind::Event, "Events"),
1131 PropInfo::new("on_drag_end", PropKind::Event, "Events"),
1132 ];
1133
1134 pub fn new(width: f32, height: f32) -> Self {
1135 Self {
1136 left: 0.0,
1137 top: 0.0,
1138 width,
1139 height,
1140 margins: Bounds::ZERO,
1141 padding: Bounds::ZERO,
1142 align: Align::None,
1143 anchors: Anchors::default(),
1144 visible: true,
1145 enabled: true,
1146 hit_test: true,
1147 clip_children: false,
1148 inherit_state: false,
1149 translatable: true,
1150 captures_wheel: false,
1151 can_focus: false,
1152 draggable: false,
1153 accepts_drops: false,
1154 drag: None,
1155 tab_stop: true,
1156 pan_on_touch: true,
1157 popup_menu: None,
1158 cursor: CursorShape::Default,
1159 opacity: 1.0,
1160 blend_mode: crate::BlendMode::Normal,
1161 rotation: 0.0,
1162 scale: 1.0,
1163 translate: PointF::ZERO,
1164 cache_layer: false,
1165 aspect_ratio: 0.0,
1166 max_width: 0.0,
1167 min_width: 0.0,
1168 min_height: 0.0,
1169 max_height: 0.0,
1170 self_align: SelfAlign::Stretch,
1171 auto_size: false,
1172 column_span: 1,
1173 grid_row: -1,
1174 grid_column: -1,
1175 row_span: 1,
1176 grow: 0.0,
1177 text_scale: 1.0,
1178 spacing_scale: 1.0,
1179 shadow: None,
1180 tag: 0,
1181 rare: None,
1182 style: String::new(),
1183 theme: String::new(),
1184 hint: String::new(),
1185 state: ControlState::default(),
1186 bounds: RectF::new(0.0, 0.0, width, height),
1187 anchor_ref: None,
1188 follow: None,
1189 design_hidden: false,
1190 design_only: false,
1191 keep_left_to_right: false,
1192 colors: ColorOverride::default(),
1193 font: FontOverride::default(),
1194 scroll_offset: PointF::ZERO,
1195 laid_out_scroll: PointF::ZERO,
1196 content_size: SizeF::ZERO,
1197 safe_area: Bounds::default(),
1198 }
1199 }
1200
1201 pub fn left(&self) -> f32 {
1204 self.left
1205 }
1206
1207 pub fn top(&self) -> f32 {
1208 self.top
1209 }
1210
1211 pub fn width(&self) -> f32 {
1212 self.width
1213 }
1214
1215 pub fn height(&self) -> f32 {
1216 self.height
1217 }
1218
1219 pub fn position(&self) -> PointF {
1220 PointF::new(self.left, self.top)
1221 }
1222
1223 pub fn size(&self) -> SizeF {
1224 SizeF::new(self.width, self.height)
1225 }
1226
1227 pub fn margins(&self) -> Bounds {
1230 if self.spacing_scale == 1.0 { self.margins } else { self.margins.scaled(self.spacing_scale) }
1231 }
1232
1233 pub fn padding(&self) -> Bounds {
1235 if self.spacing_scale == 1.0 { self.padding } else { self.padding.scaled(self.spacing_scale) }
1236 }
1237
1238 pub fn align(&self) -> Align {
1239 self.align
1240 }
1241
1242 pub fn anchors(&self) -> Anchors {
1243 self.anchors
1244 }
1245
1246 pub fn visible(&self) -> bool {
1247 self.visible
1248 }
1249
1250 pub fn design_only(&self) -> bool {
1252 self.design_only
1253 }
1254
1255 pub fn colors(&self) -> ColorOverride {
1257 self.colors
1258 }
1259
1260 pub fn set_colors(&mut self, colors: ColorOverride) -> &mut Self {
1261 self.colors = colors;
1262 self
1263 }
1264
1265 pub fn set_design_only(&mut self, on: bool) -> &mut Self {
1266 self.design_only = on;
1267 self
1268 }
1269
1270 pub fn keep_left_to_right(&self) -> bool {
1272 self.keep_left_to_right
1273 }
1274
1275 pub fn set_keep_left_to_right(&mut self, on: bool) -> &mut Self {
1276 self.keep_left_to_right = on;
1277 self
1278 }
1279
1280 pub fn enabled(&self) -> bool {
1281 self.enabled
1282 }
1283
1284 pub fn hit_test(&self) -> bool {
1285 self.hit_test
1286 }
1287
1288 pub fn translatable(&self) -> bool {
1289 self.translatable
1290 }
1291
1292 pub fn set_translatable(&mut self, v: bool) -> &mut Self {
1293 self.translatable = v;
1294 self
1295 }
1296
1297 pub fn inherit_state(&self) -> bool {
1298 self.inherit_state
1299 }
1300
1301 pub fn set_inherit_state(&mut self, v: bool) -> &mut Self {
1302 self.inherit_state = v;
1303 self
1304 }
1305
1306 pub fn clip_children(&self) -> bool {
1307 self.clip_children
1308 }
1309
1310 pub fn captures_wheel(&self) -> bool {
1311 self.captures_wheel
1312 }
1313
1314 pub fn draggable(&self) -> bool {
1319 self.draggable
1320 }
1321
1322 pub fn set_draggable(&mut self, v: bool) -> &mut Self {
1323 self.draggable = v;
1324 self
1325 }
1326
1327 pub fn accepts_drops(&self) -> bool {
1330 self.accepts_drops
1331 }
1332
1333 pub fn set_accepts_drops(&mut self, v: bool) -> &mut Self {
1334 self.accepts_drops = v;
1335 self
1336 }
1337
1338 pub fn drag_data(&self) -> &str {
1340 self.drag.as_ref().map_or("", |d| d.data.as_str())
1341 }
1342
1343 pub fn drag_label(&self) -> &str {
1344 self.drag.as_ref().map_or("", |d| d.label.as_str())
1345 }
1346
1347 pub fn drag_image(&self) -> Option<ComponentId> {
1349 self.drag.as_ref().and_then(|d| d.image)
1350 }
1351
1352 pub(crate) fn forget_drag_image(&mut self, doomed: &[ComponentId]) {
1354 if let Some(d) = self.drag.as_mut().filter(|d| d.image.is_some_and(|i| doomed.contains(&i))) {
1355 d.image = None;
1356 }
1357 }
1358
1359 pub fn drop_types(&self) -> &str {
1361 self.drag.as_ref().map_or("", |d| d.drop_types.as_str())
1362 }
1363
1364 fn drag_decl(&mut self) -> &mut DragDecl {
1365 self.drag.get_or_insert_with(Default::default)
1366 }
1367
1368 pub fn can_focus(&self) -> bool {
1369 self.can_focus
1370 }
1371
1372 pub fn tab_stop(&self) -> bool {
1374 self.tab_stop
1375 }
1376
1377 pub fn popup_menu(&self) -> Option<ComponentId> {
1379 self.popup_menu
1380 }
1381
1382 pub fn set_popup_menu(&mut self, menu: Option<ComponentId>) -> &mut Self {
1383 self.popup_menu = menu;
1384 self
1385 }
1386
1387 pub fn pan_on_touch(&self) -> bool {
1389 self.pan_on_touch
1390 }
1391
1392 pub fn cursor(&self) -> CursorShape {
1394 self.cursor
1395 }
1396
1397 pub fn opacity(&self) -> f32 {
1398 self.opacity
1399 }
1400
1401 pub fn blend_mode(&self) -> crate::BlendMode {
1403 self.blend_mode
1404 }
1405
1406 pub fn set_blend_mode(&mut self, mode: crate::BlendMode) -> &mut Self {
1407 self.blend_mode = mode;
1408 self
1409 }
1410
1411 pub fn cache_layer(&self) -> bool {
1413 self.cache_layer
1414 }
1415
1416 pub fn set_cache_layer(&mut self, on: bool) -> &mut Self {
1417 self.cache_layer = on;
1418 self
1419 }
1420
1421 pub fn rotation(&self) -> f32 {
1424 self.rotation
1425 }
1426
1427 pub fn scale(&self) -> f32 {
1429 self.scale
1430 }
1431
1432 pub fn inner_size(&self) -> SizeF {
1435 let s = self.scale;
1436 if s == 1.0 { self.bounds.size() } else { SizeF::new(self.bounds.width / s, self.bounds.height / s) }
1437 }
1438
1439 pub fn shadow(&self) -> Option<crate::canvas::Shadow> {
1440 self.shadow
1441 }
1442
1443 pub fn aspect_ratio(&self) -> f32 {
1447 self.aspect_ratio
1448 }
1449
1450 pub fn set_aspect_ratio(&mut self, ratio: f32) -> &mut Self {
1451 self.aspect_ratio = if ratio.is_finite() { ratio.max(0.0) } else { 0.0 };
1452 self
1453 }
1454
1455 pub fn max_width(&self) -> f32 {
1459 self.max_width
1460 }
1461
1462 pub fn set_max_width(&mut self, width: f32) -> &mut Self {
1463 self.max_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1464 self
1465 }
1466
1467 pub fn min_width(&self) -> f32 {
1472 self.min_width
1473 }
1474
1475 pub fn set_min_width(&mut self, width: f32) -> &mut Self {
1476 self.min_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1477 self
1478 }
1479
1480 pub fn min_height(&self) -> f32 {
1482 self.min_height
1483 }
1484
1485 pub fn set_min_height(&mut self, height: f32) -> &mut Self {
1486 self.min_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1487 self
1488 }
1489
1490 pub fn max_height(&self) -> f32 {
1492 self.max_height
1493 }
1494
1495 pub fn set_max_height(&mut self, height: f32) -> &mut Self {
1496 self.max_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1497 self
1498 }
1499
1500 pub fn clamp_width(&self, available: f32) -> f32 {
1503 let w = if self.max_width > 0.0 { available.min(self.max_width) } else { available };
1504 w.max(self.min_width)
1505 }
1506
1507 pub fn clamp_height(&self, height: f32) -> f32 {
1509 let h = if self.max_height > 0.0 { height.min(self.max_height) } else { height };
1510 h.max(self.min_height)
1511 }
1512
1513 pub fn self_align(&self) -> SelfAlign {
1516 self.self_align
1517 }
1518
1519 pub fn set_self_align(&mut self, align: SelfAlign) -> &mut Self {
1520 self.self_align = align;
1521 self
1522 }
1523
1524 pub fn band_width(&self, available: f32) -> f32 {
1528 match self.self_align {
1529 SelfAlign::Stretch => self.clamp_width(available),
1530 _ if self.max_width > 0.0 => self.clamp_width(available),
1531 _ => self.clamp_width(self.width),
1532 }
1533 }
1534
1535 pub fn band_height(&self, available: f32) -> f32 {
1538 match self.self_align {
1539 SelfAlign::Stretch => self.clamp_height(available),
1540 _ if self.max_height > 0.0 => self.clamp_height(available),
1541 _ => self.clamp_height(self.height),
1542 }
1543 }
1544
1545 pub fn band_offset(&self, available: f32, size: f32) -> f32 {
1548 let spare = (available - size).max(0.0);
1549 match self.self_align {
1550 SelfAlign::Start => 0.0,
1551 SelfAlign::End => spare,
1552 SelfAlign::Stretch | SelfAlign::Center => spare / 2.0,
1553 }
1554 }
1555
1556 pub fn auto_size(&self) -> bool {
1561 self.auto_size
1562 }
1563
1564 pub fn set_auto_size(&mut self, on: bool) -> &mut Self {
1565 self.auto_size = on;
1566 self
1567 }
1568
1569 pub fn column_span(&self) -> u32 {
1572 self.column_span.max(1)
1573 }
1574
1575 pub fn grid_cell(&self) -> Option<(u32, u32)> {
1577 (self.grid_row >= 0 && self.grid_column >= 0).then(|| (self.grid_row as u32, self.grid_column as u32))
1578 }
1579
1580 pub fn set_grid_cell(&mut self, cell: Option<(u32, u32)>) -> &mut Self {
1581 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));
1582 self.grid_row = r;
1583 self.grid_column = c;
1584 self
1585 }
1586
1587 pub fn row_span(&self) -> u32 {
1589 u32::from(self.row_span.max(1))
1590 }
1591
1592 pub fn set_row_span(&mut self, span: u32) -> &mut Self {
1593 self.row_span = span.clamp(1, 64) as u16;
1594 self
1595 }
1596
1597 pub fn grow(&self) -> f32 {
1598 self.grow
1599 }
1600
1601 pub fn set_grow(&mut self, grow: f32) -> &mut Self {
1602 self.grow = grow.max(0.0);
1603 self
1604 }
1605
1606 pub fn set_column_span(&mut self, span: u32) -> &mut Self {
1607 self.column_span = span.max(1);
1608 self
1609 }
1610
1611 pub fn text_scale(&self) -> f32 {
1613 self.text_scale
1614 }
1615
1616 pub fn spacing_scale(&self) -> f32 {
1618 self.spacing_scale
1619 }
1620
1621 pub fn tag(&self) -> i64 {
1622 self.tag
1623 }
1624
1625 pub fn accessible_name(&self) -> &str {
1628 self.rare.as_ref().map_or("", |r| &r.accessible_name)
1629 }
1630
1631 pub fn set_accessible_name(&mut self, v: &str) -> &mut Self {
1632 self.rare_mut(|r| r.accessible_name = v.into());
1633 self
1634 }
1635
1636 fn rare_mut(&mut self, change: impl FnOnce(&mut RareTexts)) {
1637 let rare = self.rare.get_or_insert_with(Default::default);
1638 change(rare);
1639 if rare.tag_string.is_empty() && rare.accessible_name.is_empty() {
1640 self.rare = None;
1641 }
1642 }
1643
1644 pub fn tag_string(&self) -> &str {
1645 self.rare.as_ref().map_or("", |r| &r.tag_string)
1646 }
1647
1648 pub fn style(&self) -> &str {
1650 &self.style
1651 }
1652
1653 pub fn theme_name(&self) -> &str {
1655 &self.theme
1656 }
1657
1658 pub fn set_theme_name(&mut self, v: impl Into<String>) -> &mut Self {
1659 self.theme = v.into();
1660 self
1661 }
1662
1663 pub fn hint(&self) -> &str {
1665 &self.hint
1666 }
1667
1668 pub fn set_hint(&mut self, v: impl Into<String>) -> &mut Self {
1669 self.hint = v.into();
1670 self
1671 }
1672
1673 pub fn state(&self) -> ControlState {
1674 self.state
1675 }
1676
1677 pub fn bounds(&self) -> RectF {
1679 self.bounds
1680 }
1681
1682 pub fn scroll_offset(&self) -> PointF {
1684 self.scroll_offset
1685 }
1686
1687 pub fn laid_out_scroll(&self) -> PointF {
1690 self.laid_out_scroll
1691 }
1692
1693 pub fn set_scroll_offset(&mut self, offset: PointF) -> &mut Self {
1696 self.scroll_offset = offset;
1697 self
1698 }
1699
1700 pub fn content_size(&self) -> SizeF {
1703 self.content_size
1704 }
1705
1706 pub fn set_left(&mut self, v: f32) -> &mut Self {
1713 if self.left != v {
1714 self.left = v;
1715 self.anchor_ref = None;
1716 }
1717 self
1718 }
1719
1720 pub fn set_top(&mut self, v: f32) -> &mut Self {
1721 if self.top != v {
1722 self.top = v;
1723 self.anchor_ref = None;
1724 }
1725 self
1726 }
1727
1728 pub fn set_position(&mut self, left: f32, top: f32) -> &mut Self {
1729 self.set_left(left).set_top(top)
1730 }
1731
1732 pub fn set_width(&mut self, v: f32) -> &mut Self {
1733 if self.width != v.max(0.0) {
1734 self.width = v.max(0.0);
1735 self.anchor_ref = None;
1736 }
1737 self
1738 }
1739
1740 pub fn set_height(&mut self, v: f32) -> &mut Self {
1741 if self.height != v.max(0.0) {
1742 self.height = v.max(0.0);
1743 self.anchor_ref = None;
1744 }
1745 self
1746 }
1747
1748 pub(crate) fn set_measured_width(&mut self, v: f32) {
1750 self.width = v.max(0.0);
1751 if let Some(r) = self.anchor_ref.as_mut() {
1752 r.rect.width = self.width;
1753 }
1754 }
1755
1756 pub(crate) fn set_measured_height(&mut self, v: f32) {
1759 self.height = v.max(0.0);
1760 if let Some(r) = self.anchor_ref.as_mut() {
1761 r.rect.height = self.height;
1762 }
1763 }
1764
1765 pub fn set_size(&mut self, width: f32, height: f32) -> &mut Self {
1766 self.set_width(width).set_height(height)
1767 }
1768
1769 pub fn set_rect(&mut self, left: f32, top: f32, width: f32, height: f32) -> &mut Self {
1771 self.set_position(left, top).set_size(width, height)
1772 }
1773
1774 pub fn set_margins(&mut self, v: impl Into<Bounds>) -> &mut Self {
1775 self.margins = v.into();
1776 self
1777 }
1778
1779 pub fn set_padding(&mut self, v: impl Into<Bounds>) -> &mut Self {
1780 self.padding = v.into();
1781 self
1782 }
1783
1784 pub fn set_align(&mut self, v: Align) -> &mut Self {
1785 self.align = v;
1786 self.anchor_ref = None;
1787 self
1788 }
1789
1790 pub fn set_anchors(&mut self, v: Anchors) -> &mut Self {
1791 self.anchors = v;
1792 self.anchor_ref = None;
1793 self
1794 }
1795
1796 pub fn set_visible(&mut self, v: bool) -> &mut Self {
1797 self.visible = v;
1798 self
1799 }
1800
1801 pub fn set_enabled(&mut self, v: bool) -> &mut Self {
1802 self.enabled = v;
1803 self
1804 }
1805
1806 pub fn set_hit_test(&mut self, v: bool) -> &mut Self {
1807 self.hit_test = v;
1808 self
1809 }
1810
1811 pub fn set_clip_children(&mut self, v: bool) -> &mut Self {
1812 self.clip_children = v;
1813 self
1814 }
1815
1816 pub fn set_captures_wheel(&mut self, v: bool) -> &mut Self {
1817 self.captures_wheel = v;
1818 self
1819 }
1820
1821 pub fn set_can_focus(&mut self, v: bool) -> &mut Self {
1822 self.can_focus = v;
1823 self
1824 }
1825
1826 pub fn set_pan_on_touch(&mut self, v: bool) -> &mut Self {
1830 self.pan_on_touch = v;
1831 self
1832 }
1833
1834 pub fn set_tab_stop(&mut self, v: bool) -> &mut Self {
1835 self.tab_stop = v;
1836 self
1837 }
1838
1839 pub fn set_cursor(&mut self, v: CursorShape) -> &mut Self {
1840 self.cursor = v;
1841 self
1842 }
1843
1844 pub fn set_opacity(&mut self, v: f32) -> &mut Self {
1845 self.opacity = v.clamp(0.0, 1.0);
1846 self
1847 }
1848
1849 pub fn translate(&self) -> PointF {
1851 self.translate
1852 }
1853
1854 pub fn set_translate(&mut self, offset: PointF) -> &mut Self {
1855 self.translate = if offset.x.is_finite() && offset.y.is_finite() { offset } else { PointF::ZERO };
1856 self
1857 }
1858
1859 pub fn set_scale(&mut self, scale: f32) -> &mut Self {
1861 self.scale = if scale.is_finite() && scale > 0.0 { scale.clamp(0.05, 20.0) } else { 1.0 };
1862 self
1863 }
1864
1865 pub fn set_rotation(&mut self, degrees: f32) -> &mut Self {
1866 self.rotation = if degrees.is_finite() { degrees % 360.0 } else { 0.0 };
1867 self
1868 }
1869
1870 pub fn set_shadow(&mut self, shadow: Option<crate::canvas::Shadow>) -> &mut Self {
1871 self.shadow = shadow;
1872 self
1873 }
1874
1875 pub fn set_tag(&mut self, v: i64) -> &mut Self {
1876 self.tag = v;
1877 self
1878 }
1879
1880 pub fn set_tag_string(&mut self, v: &str) -> &mut Self {
1881 self.rare_mut(|r| r.tag_string = v.into());
1882 self
1883 }
1884
1885 pub fn set_style(&mut self, v: impl Into<String>) -> &mut Self {
1886 self.style = v.into();
1887 self
1888 }
1889
1890 pub fn state_mut(&mut self) -> &mut ControlState {
1893 &mut self.state
1894 }
1895
1896 pub fn get_prop(&self, name: &str) -> Option<PropValue> {
1899 Some(match name {
1900 "left" => self.left.into(),
1901 "top" => self.top.into(),
1902 "width" => self.width.into(),
1903 "height" => self.height.into(),
1904 "align" => self.align.into(),
1905 "anchors" => self.anchors.into(),
1906 "margins" => self.margins.into(),
1907 "padding" => self.padding.into(),
1908 "visible" => self.visible.into(),
1909 "enabled" => self.enabled.into(),
1910 "hit_test" => self.hit_test.into(),
1911 "clip_children" => self.clip_children.into(),
1912 "inherit_state" => self.inherit_state.into(),
1913 "translate" => self.translatable.into(),
1914 "captures_wheel" => self.captures_wheel.into(),
1915 "can_focus" => self.can_focus.into(),
1916 "draggable" => self.draggable.into(),
1917 "accepts_drops" => self.accepts_drops.into(),
1918 "drag_data" => self.drag_data().into(),
1919 "drag_label" => self.drag_label().into(),
1920 "drag_image" => PropValue::Component(self.drag_image()),
1921 "drop_types" => self.drop_types().into(),
1922 "tab_stop" => self.tab_stop.into(),
1923 "cursor" => PropValue::enum_value(self.cursor),
1924 "opacity" => self.opacity.into(),
1925 "blend_mode" => PropValue::enum_value(self.blend_mode),
1926 "cache_layer" => self.cache_layer.into(),
1927 "rotation" => self.rotation.into(),
1928 "scale" => self.scale.into(),
1929 "translate_x" => self.translate.x.into(),
1930 "translate_y" => self.translate.y.into(),
1931 "aspect_ratio" => self.aspect_ratio.into(),
1932 "max_width" => self.max_width.into(),
1933 "min_width" => self.min_width.into(),
1934 "min_height" => self.min_height.into(),
1935 "max_height" => self.max_height.into(),
1936 "self_align" => PropValue::enum_value(self.self_align),
1937 "auto_size" => self.auto_size.into(),
1938 "column_span" => (self.column_span() as i64).into(),
1939 "grid_row" => i64::from(self.grid_row).into(),
1940 "grid_column" => i64::from(self.grid_column).into(),
1941 "row_span" => (self.row_span() as i64).into(),
1942 "grow" => f64::from(self.grow).into(),
1943 "shadow" => crate::prop::PropType::to_value(&self.shadow),
1944 "style" => self.style.clone().into(),
1945 "fill" => crate::prop::PropType::to_value(&self.colors.fill),
1946 "border_color" => crate::prop::PropType::to_value(&self.colors.border),
1947 "text_color" => crate::prop::PropType::to_value(&self.colors.text),
1948 "accent_color" => crate::prop::PropType::to_value(&self.colors.accent),
1949 "success_color" => crate::prop::PropType::to_value(&self.colors.success),
1950 "warning_color" => crate::prop::PropType::to_value(&self.colors.warning),
1951 "danger_color" => crate::prop::PropType::to_value(&self.colors.danger),
1952 "font_family" => self.font.family.clone().into(),
1953 "font_size" => crate::prop::PropType::to_value(&self.font.size),
1954 "font_weight" => PropValue::Enum(FONT_WEIGHTS[self.font.weight.map_or(0, |w| w as usize + 1)].into()),
1955 "font_italic" => self.font.italic.into(),
1956 "font_underline" => self.font.underline.into(),
1957 "font_strikeout" => self.font.strikeout.into(),
1958 "theme" => self.theme.clone().into(),
1959 "hint" => self.hint.clone().into(),
1960 "tag" => self.tag.into(),
1961 "tag_string" => self.tag_string().to_string().into(),
1962 "accessible_name" => self.accessible_name().to_string().into(),
1963 "popup_menu" => PropValue::Component(self.popup_menu),
1964 _ => return None,
1965 })
1966 }
1967
1968 pub fn set_prop(&mut self, name: &str, value: PropValue) -> Result<()> {
1969 match name {
1970 "left" => self.set_left(value.to_f32()?),
1971 "top" => self.set_top(value.to_f32()?),
1972 "width" => self.set_width(value.to_f32()?),
1973 "height" => self.set_height(value.to_f32()?),
1974 "align" => self.set_align(value.to_enum()?),
1975 "anchors" => self.set_anchors(value.to_anchors()?),
1976 "margins" => self.set_margins(value.to_bounds()?),
1977 "padding" => self.set_padding(value.to_bounds()?),
1978 "visible" => self.set_visible(value.to_bool()?),
1979 "enabled" => self.set_enabled(value.to_bool()?),
1980 "hit_test" => self.set_hit_test(value.to_bool()?),
1981 "clip_children" => self.set_clip_children(value.to_bool()?),
1982 "inherit_state" => self.set_inherit_state(value.to_bool()?),
1983 "translate" => self.set_translatable(value.to_bool()?),
1984 "captures_wheel" => self.set_captures_wheel(value.to_bool()?),
1985 "can_focus" => self.set_can_focus(value.to_bool()?),
1986 "draggable" => self.set_draggable(value.to_bool()?),
1987 "accepts_drops" => self.set_accepts_drops(value.to_bool()?),
1988 "drag_data" => {
1989 self.drag_decl().data = value.to_str()?.to_string();
1990 self
1991 }
1992 "drag_label" => {
1993 self.drag_decl().label = value.to_str()?.to_string();
1994 self
1995 }
1996 "drag_image" => {
1997 self.drag_decl().image = value.to_component()?;
1998 self
1999 }
2000 "drop_types" => {
2001 self.drag_decl().drop_types = value.to_str()?.to_string();
2002 self
2003 }
2004 "tab_stop" => self.set_tab_stop(value.to_bool()?),
2005 "cursor" => self.set_cursor(value.to_enum()?),
2006 "opacity" => self.set_opacity(value.to_f32()?),
2007 "blend_mode" => self.set_blend_mode(value.to_enum()?),
2008 "cache_layer" => self.set_cache_layer(value.to_bool()?),
2009 "rotation" => self.set_rotation(value.to_f32()?),
2010 "scale" => self.set_scale(value.to_f32()?),
2011 "translate_x" => self.set_translate(PointF::new(value.to_f32()?, self.translate.y)),
2012 "translate_y" => self.set_translate(PointF::new(self.translate.x, value.to_f32()?)),
2013 "aspect_ratio" => self.set_aspect_ratio(value.to_f32()?),
2014 "max_width" => self.set_max_width(value.to_f32()?),
2015 "min_width" => self.set_min_width(value.to_f32()?),
2016 "min_height" => self.set_min_height(value.to_f32()?),
2017 "max_height" => self.set_max_height(value.to_f32()?),
2018 "self_align" => self.set_self_align(value.to_enum()?),
2019 "auto_size" => self.set_auto_size(value.to_bool()?),
2020 "column_span" => self.set_column_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2021 "grid_row" => {
2022 self.grid_row = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2023 self
2024 }
2025 "grid_column" => {
2026 self.grid_column = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2027 self
2028 }
2029 "row_span" => self.set_row_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2030 "grow" => self.set_grow(value.to_f32()?),
2031 "shadow" => self.set_shadow(crate::prop::PropType::from_value(value)?),
2032 "style" => self.set_style(value.to_str()?),
2033 "fill" => {
2034 self.colors.fill = crate::prop::PropType::from_value(value)?;
2035 self
2036 }
2037 "border_color" => {
2038 self.colors.border = crate::prop::PropType::from_value(value)?;
2039 self
2040 }
2041 "text_color" => {
2042 self.colors.text = crate::prop::PropType::from_value(value)?;
2043 self
2044 }
2045 "accent_color" => {
2046 self.colors.accent = crate::prop::PropType::from_value(value)?;
2047 self
2048 }
2049 "success_color" => {
2050 self.colors.success = crate::prop::PropType::from_value(value)?;
2051 self
2052 }
2053 "warning_color" => {
2054 self.colors.warning = crate::prop::PropType::from_value(value)?;
2055 self
2056 }
2057 "danger_color" => {
2058 self.colors.danger = crate::prop::PropType::from_value(value)?;
2059 self
2060 }
2061 "font_family" => {
2062 self.font.family = value.to_str()?.to_string();
2063 self
2064 }
2065 "font_size" => {
2066 self.font.size = crate::prop::PropType::from_value(value)?;
2067 self
2068 }
2069 "font_weight" => {
2070 self.font.weight = font_weight_named(value.to_str()?)?;
2071 self
2072 }
2073 "font_italic" => {
2074 self.font.italic = value.to_bool()?;
2075 self
2076 }
2077 "font_underline" => {
2078 self.font.underline = value.to_bool()?;
2079 self
2080 }
2081 "font_strikeout" => {
2082 self.font.strikeout = value.to_bool()?;
2083 self
2084 }
2085 "theme" => self.set_theme_name(value.to_str()?),
2086 "hint" => self.set_hint(value.to_str()?),
2087 "tag" => self.set_tag(value.to_i64()?),
2088 "tag_string" => self.set_tag_string(value.to_str()?),
2089 "accessible_name" => self.set_accessible_name(value.to_str()?),
2090 "popup_menu" => {
2091 self.popup_menu = value.to_component()?;
2092 self
2093 }
2094 _ => return Err(Error::UnknownProperty(name.to_string())),
2095 };
2096 Ok(())
2097 }
2098}
2099
2100pub const FONT_WEIGHTS: [&str; 8] = ["default", "thin", "light", "normal", "medium", "semibold", "bold", "black"];
2102
2103fn font_weight_named(name: &str) -> Result<Option<crate::canvas::FontWeight>> {
2104 use crate::canvas::FontWeight::*;
2105 let all = [Thin, Light, Normal, Medium, SemiBold, Bold, Black];
2106 match FONT_WEIGHTS.iter().position(|n| n.eq_ignore_ascii_case(name)) {
2107 Some(0) => Ok(None),
2108 Some(i) => Ok(Some(all[i - 1])),
2109 None => Err(Error::invalid(format!("valor '{name}' inválido; use um de: {}", FONT_WEIGHTS.join(", ")))),
2110 }
2111}
2112
2113#[derive(Clone, Debug, Default, PartialEq)]
2117pub struct FontOverride {
2118 pub family: String,
2120 pub size: Option<f32>,
2122 pub weight: Option<crate::canvas::FontWeight>,
2124 pub italic: bool,
2126 pub underline: bool,
2128 pub strikeout: bool,
2130}
2131
2132impl FontOverride {
2133 pub fn is_empty(&self) -> bool {
2134 *self == Self::default()
2135 }
2136
2137 pub fn over(&self, parent: &FontOverride) -> FontOverride {
2139 FontOverride {
2140 family: if self.family.is_empty() { parent.family.clone() } else { self.family.clone() },
2141 size: self.size.or(parent.size),
2142 weight: self.weight.or(parent.weight),
2143 italic: self.italic || parent.italic,
2144 underline: self.underline || parent.underline,
2145 strikeout: self.strikeout || parent.strikeout,
2146 }
2147 }
2148
2149 pub fn apply(&self, font: &mut crate::canvas::FontSpec) {
2151 if !self.family.is_empty() {
2152 font.family = self.family.clone();
2153 }
2154 if let Some(size) = self.size.filter(|s| *s > 0.0) {
2155 font.size = size;
2156 }
2157 if let Some(weight) = self.weight {
2158 font.weight = weight;
2159 }
2160 font.italic |= self.italic;
2161 font.underline |= self.underline;
2162 font.strikeout |= self.strikeout;
2163 }
2164}
2165
2166impl ControlBase {
2167 pub fn font(&self) -> &FontOverride {
2169 &self.font
2170 }
2171
2172 pub fn set_font(&mut self, font: FontOverride) -> &mut Self {
2173 self.font = font;
2174 self
2175 }
2176}
2177
2178#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2184pub struct ColorOverride {
2185 pub fill: Option<crate::Color>,
2187 pub border: Option<crate::Color>,
2189 pub text: Option<crate::Color>,
2191 pub accent: Option<crate::Color>,
2194 pub success: Option<crate::Color>,
2197 pub warning: Option<crate::Color>,
2199 pub danger: Option<crate::Color>,
2201}
2202
2203impl ColorOverride {
2204 pub fn is_empty(&self) -> bool {
2205 *self == Self::default()
2206 }
2207
2208 pub(crate) fn recolors_theme(&self) -> bool {
2210 !self.is_empty()
2211 }
2212
2213 pub(crate) fn apply_theme(&self, theme: &crate::Theme) -> crate::Theme {
2215 use crate::Color;
2216 let mut t = theme.clone();
2217 let mut set = |name: &str, c: Color| {
2218 t.colors.insert(name.to_string(), c);
2219 };
2220 if let Some(a) = self.accent {
2221 let dark = luminance(a) < 0.5;
2222 set("primary", a);
2223 set("primary_hover", a.lerp(if dark { Color::WHITE } else { Color::BLACK }, 0.12));
2224 set("primary_pressed", a.lerp(if dark { Color::BLACK } else { Color::BLACK }, 0.15));
2225 set("on_primary", if luminance(a) < 0.6 { Color::WHITE } else { Color::BLACK });
2226 set("focus_ring", a);
2227 set("selection", a.with_opacity(0.25));
2228 set("primary_soft", a.with_opacity(0.1));
2229 set("primary_container", a.lerp(Color::WHITE, 0.82));
2230 }
2231 if let Some(c) = self.fill {
2232 let toward = if luminance(c) < 0.5 { Color::WHITE } else { Color::BLACK };
2234 set("surface", c);
2235 set("background", c);
2236 set("surface_2", c.lerp(toward.with_opacity(c.a as f32 / 255.0), 0.06));
2237 }
2238 if let Some(c) = self.text {
2239 set("text", c);
2240 set("text_muted", c.with_opacity(0.65));
2241 for on in ["on_primary", "on_danger", "on_success", "on_primary_container"] {
2242 set(on, c);
2243 }
2244 }
2245 if let Some(c) = self.border {
2246 set("border", c);
2247 set("track", c);
2248 }
2249 for (token, color) in [("success", self.success), ("warning", self.warning), ("danger", self.danger)] {
2250 if let Some(c) = color {
2251 set(token, c);
2252 set(&format!("{token}_soft"), c.with_opacity(0.1));
2253 set(&format!("{token}_hover"), c.lerp(Color::WHITE, 0.12));
2254 set(&format!("{token}_pressed"), c.lerp(Color::BLACK, 0.15));
2255 if token == "success" {
2256 set("meter_low", c);
2257 } else if token == "danger" {
2258 set("meter_high", c);
2259 } else {
2260 set("meter_mid", c);
2261 }
2262 }
2263 }
2264 t
2265 }
2266}
2267
2268pub(crate) fn luminance(c: crate::Color) -> f32 {
2270 (0.299 * c.r as f32 + 0.587 * c.g as f32 + 0.114 * c.b as f32) / 255.0
2271}
2272
2273#[derive(Clone, Copy, Debug, PartialEq)]
2275pub struct Connection {
2276 pub from: ComponentId,
2277 pub from_at: (f32, f32),
2279 pub to: ComponentId,
2280 pub to_at: (f32, f32),
2281}
2282
2283impl Connection {
2284 pub const STUB: f32 = 16.0;
2286
2287 fn normal(at: (f32, f32)) -> PointF {
2289 let (x, y) = at;
2290 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))];
2291 d.iter().min_by(|a, b| a.0.total_cmp(&b.0)).map_or(PointF::new(1.0, 0.0), |d| d.1)
2292 }
2293
2294 pub fn route(&self, from: RectF, to: RectF) -> Vec<PointF> {
2297 let p0 = PointF::new(from.x + from.width * self.from_at.0, from.y + from.height * self.from_at.1);
2298 let p1 = PointF::new(to.x + to.width * self.to_at.0, to.y + to.height * self.to_at.1);
2299 let (n0, n1) = (Self::normal(self.from_at), Self::normal(self.to_at));
2300 let s0 = PointF::new(p0.x + n0.x * Self::STUB, p0.y + n0.y * Self::STUB);
2301 let s1 = PointF::new(p1.x + n1.x * Self::STUB, p1.y + n1.y * Self::STUB);
2302 let mut points = vec![p0, s0];
2303 if n0.x != 0.0 {
2304 let mx = (s0.x + s1.x) / 2.0;
2306 points.push(PointF::new(mx, s0.y));
2307 points.push(PointF::new(mx, s1.y));
2308 } else {
2309 let my = (s0.y + s1.y) / 2.0;
2310 points.push(PointF::new(s0.x, my));
2311 points.push(PointF::new(s1.x, my));
2312 }
2313 points.push(s1);
2314 points.push(p1);
2315 points.dedup_by(|a, b| (a.x - b.x).abs() < 0.01 && (a.y - b.y).abs() < 0.01);
2316 points
2317 }
2318}
2319
2320#[derive(Clone, Debug, Default, PartialEq)]
2322pub struct DragDecl {
2323 pub data: String,
2326 pub label: String,
2328 pub drop_types: String,
2331 pub image: Option<ComponentId>,
2333}
2334
2335#[derive(Clone, Debug, Default)]
2337struct RareTexts {
2338 tag_string: Box<str>,
2340 accessible_name: Box<str>,
2343}