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 captures_gesture: bool,
933 can_focus: bool,
935 draggable: bool,
937 accepts_drops: bool,
939 drag: Option<Box<DragDecl>>,
942 tab_stop: bool,
944 pan_on_touch: bool,
946 pub(crate) popup_menu: Option<ComponentId>,
948 cursor: CursorShape,
950 opacity: f32,
952 blend_mode: crate::BlendMode,
955 rotation: f32,
957 scale: f32,
962 translate: PointF,
964 cache_layer: bool,
967 aspect_ratio: f32,
969 max_width: f32,
971 min_width: f32,
973 min_height: f32,
975 max_height: f32,
977 self_align: SelfAlign,
979 auto_size: bool,
981 column_span: u32,
983 grid_row: i16,
986 grid_column: i16,
988 row_span: u16,
990 grow: f32,
993 pub(crate) text_scale: f32,
996 pub(crate) spacing_scale: f32,
997 shadow: Option<crate::canvas::Shadow>,
999 tag: i64,
1001 rare: Option<Box<RareTexts>>,
1004 style: String,
1006 theme: String,
1008 hint: String,
1010 state: ControlState,
1011 pub(crate) bounds: RectF,
1012 pub(crate) anchor_ref: Option<AnchorRef>,
1013 pub(crate) follow: Option<(ComponentId, Option<PointF>)>,
1016 pub(crate) design_hidden: bool,
1019 pub(crate) design_only: bool,
1021 pub(crate) keep_left_to_right: bool,
1024 pub(crate) colors: ColorOverride,
1026 pub(crate) font: FontOverride,
1028 scroll_offset: PointF,
1030 pub(crate) laid_out_scroll: PointF,
1033 pub(crate) safe_area: Bounds,
1035 pub(crate) content_size: SizeF,
1037}
1038
1039impl Default for ControlBase {
1040 fn default() -> Self {
1041 Self::new(100.0, 100.0)
1042 }
1043}
1044
1045impl ControlBase {
1046 pub const PROPS: &'static [PropInfo] = &[
1048 PropInfo::new("left", PropKind::Float, "Layout"),
1049 PropInfo::new("top", PropKind::Float, "Layout"),
1050 PropInfo::new("width", PropKind::Float, "Layout"),
1051 PropInfo::new("height", PropKind::Float, "Layout"),
1052 PropInfo::new("align", PropKind::Enum(<Align as crate::prop::PropEnum>::NAMES), "Layout"),
1053 PropInfo::new("anchors", PropKind::Set(&Anchors::NAMES), "Layout"),
1054 PropInfo::new("margins", PropKind::Bounds, "Layout"),
1055 PropInfo::new("padding", PropKind::Bounds, "Layout"),
1056 PropInfo::new("visible", PropKind::Bool, "Behavior"),
1057 PropInfo::new("enabled", PropKind::Bool, "Behavior"),
1058 PropInfo::new("hit_test", PropKind::Bool, "Behavior"),
1059 PropInfo::new("clip_children", PropKind::Bool, "Behavior"),
1060 PropInfo::new("inherit_state", PropKind::Bool, "Appearance"),
1061 PropInfo::new("translate", PropKind::Bool, "Behavior"),
1062 PropInfo::new("captures_wheel", PropKind::Bool, "Behavior"),
1063 PropInfo::new("captures_gesture", PropKind::Bool, "Behavior"),
1064 PropInfo::new("can_focus", PropKind::Bool, "Behavior"),
1065 PropInfo::new("draggable", PropKind::Bool, "Behavior"),
1066 PropInfo::new("accepts_drops", PropKind::Bool, "Behavior"),
1067 PropInfo::new("drag_data", PropKind::String, "Behavior"),
1068 PropInfo::new("drag_label", PropKind::String, "Behavior"),
1069 PropInfo::new("drag_image", PropKind::Component, "Behavior"),
1070 PropInfo::new("drop_types", PropKind::String, "Behavior"),
1071 PropInfo::new("tab_stop", PropKind::Bool, "Behavior"),
1072 PropInfo::new("cursor", PropKind::Enum(<CursorShape as crate::prop::PropEnum>::NAMES), "Behavior"),
1073 PropInfo::new("opacity", PropKind::Float, "Appearance"),
1074 PropInfo::new("blend_mode", PropKind::Enum(<crate::BlendMode as crate::prop::PropEnum>::NAMES), "Appearance"),
1075 PropInfo::new("cache_layer", PropKind::Bool, "Appearance"),
1076 PropInfo::new("rotation", PropKind::Angle, "Layout"),
1077 PropInfo::new("scale", PropKind::Float, "Layout"),
1078 PropInfo::new("translate_x", PropKind::Float, "Appearance"),
1079 PropInfo::new("translate_y", PropKind::Float, "Appearance"),
1080 PropInfo::new("aspect_ratio", PropKind::Float, "Layout"),
1081 PropInfo::new("max_width", PropKind::Float, "Layout"),
1082 PropInfo::new("min_width", PropKind::Float, "Layout"),
1083 PropInfo::new("min_height", PropKind::Float, "Layout"),
1084 PropInfo::new("max_height", PropKind::Float, "Layout"),
1085 PropInfo::new("self_align", PropKind::Enum(<SelfAlign as crate::prop::PropEnum>::NAMES), "Layout"),
1086 PropInfo::new("auto_size", PropKind::Bool, "Layout"),
1087 PropInfo::new("column_span", PropKind::Int, "Layout"),
1088 PropInfo::new("grid_row", PropKind::Int, "Layout"),
1089 PropInfo::new("grid_column", PropKind::Int, "Layout"),
1090 PropInfo::new("row_span", PropKind::Int, "Layout"),
1091 PropInfo::new("grow", PropKind::Float, "Layout"),
1092 PropInfo::new("shadow", PropKind::Shadow, "Appearance"),
1093 PropInfo::new("style", PropKind::String, "Appearance"),
1094 PropInfo::new("theme", PropKind::String, "Appearance"),
1095 PropInfo::new("fill", PropKind::Color, "Appearance"),
1096 PropInfo::new("border_color", PropKind::Color, "Appearance"),
1097 PropInfo::new("text_color", PropKind::Color, "Appearance"),
1098 PropInfo::new("accent_color", PropKind::Color, "Appearance"),
1099 PropInfo::new("success_color", PropKind::Color, "Appearance"),
1100 PropInfo::new("warning_color", PropKind::Color, "Appearance"),
1101 PropInfo::new("danger_color", PropKind::Color, "Appearance"),
1102 PropInfo::new("font_family", PropKind::String, "Font"),
1103 PropInfo::new("font_size", PropKind::Float, "Font"),
1104 PropInfo::new("font_weight", PropKind::Enum(&FONT_WEIGHTS), "Font"),
1105 PropInfo::new("font_italic", PropKind::Bool, "Font"),
1106 PropInfo::new("font_underline", PropKind::Bool, "Font"),
1107 PropInfo::new("font_strikeout", PropKind::Bool, "Font"),
1108 PropInfo::new("hint", PropKind::String, "Behavior"),
1109 PropInfo::new("tag", PropKind::Int, "Misc"),
1110 PropInfo::new("tag_string", PropKind::String, "Misc"),
1111 PropInfo::new("accessible_name", PropKind::String, "Behavior"),
1112 PropInfo::new("popup_menu", PropKind::Component, "Behavior"),
1113 PropInfo::new("on_click", PropKind::Event, "Events"),
1114 PropInfo::new("on_dbl_click", PropKind::Event, "Events"),
1115 PropInfo::new("on_long_press", PropKind::Event, "Events"),
1116 PropInfo::new("on_pointer_down", PropKind::Event, "Events"),
1117 PropInfo::new("on_pointer_up", PropKind::Event, "Events"),
1118 PropInfo::new("on_pointer_move", PropKind::Event, "Events"),
1119 PropInfo::new("on_pointer_enter", PropKind::Event, "Events"),
1120 PropInfo::new("on_pointer_leave", PropKind::Event, "Events"),
1121 PropInfo::new("on_wheel", PropKind::Event, "Events"),
1122 PropInfo::new("on_gesture", PropKind::Event, "Events"),
1123 PropInfo::new("on_swipe", PropKind::Event, "Events"),
1124 PropInfo::new("on_key_down", PropKind::Event, "Events"),
1125 PropInfo::new("on_key_press", PropKind::Event, "Events"),
1126 PropInfo::new("on_key_up", PropKind::Event, "Events"),
1127 PropInfo::new("on_enter", PropKind::Event, "Events"),
1128 PropInfo::new("on_exit", PropKind::Event, "Events"),
1129 PropInfo::new("on_resize", PropKind::Event, "Events"),
1130 PropInfo::new("on_drag_start", PropKind::Event, "Events"),
1131 PropInfo::new("on_drag_over", PropKind::Event, "Events"),
1132 PropInfo::new("on_drag_move", PropKind::Event, "Events"),
1133 PropInfo::new("on_drag_leave", PropKind::Event, "Events"),
1134 PropInfo::new("on_drag_drop", PropKind::Event, "Events"),
1135 PropInfo::new("on_drag_end", PropKind::Event, "Events"),
1136 ];
1137
1138 pub fn new(width: f32, height: f32) -> Self {
1139 Self {
1140 left: 0.0,
1141 top: 0.0,
1142 width,
1143 height,
1144 margins: Bounds::ZERO,
1145 padding: Bounds::ZERO,
1146 align: Align::None,
1147 anchors: Anchors::default(),
1148 visible: true,
1149 enabled: true,
1150 hit_test: true,
1151 clip_children: false,
1152 inherit_state: false,
1153 translatable: true,
1154 captures_wheel: false,
1155 captures_gesture: false,
1156 can_focus: false,
1157 draggable: false,
1158 accepts_drops: false,
1159 drag: None,
1160 tab_stop: true,
1161 pan_on_touch: true,
1162 popup_menu: None,
1163 cursor: CursorShape::Default,
1164 opacity: 1.0,
1165 blend_mode: crate::BlendMode::Normal,
1166 rotation: 0.0,
1167 scale: 1.0,
1168 translate: PointF::ZERO,
1169 cache_layer: false,
1170 aspect_ratio: 0.0,
1171 max_width: 0.0,
1172 min_width: 0.0,
1173 min_height: 0.0,
1174 max_height: 0.0,
1175 self_align: SelfAlign::Stretch,
1176 auto_size: false,
1177 column_span: 1,
1178 grid_row: -1,
1179 grid_column: -1,
1180 row_span: 1,
1181 grow: 0.0,
1182 text_scale: 1.0,
1183 spacing_scale: 1.0,
1184 shadow: None,
1185 tag: 0,
1186 rare: None,
1187 style: String::new(),
1188 theme: String::new(),
1189 hint: String::new(),
1190 state: ControlState::default(),
1191 bounds: RectF::new(0.0, 0.0, width, height),
1192 anchor_ref: None,
1193 follow: None,
1194 design_hidden: false,
1195 design_only: false,
1196 keep_left_to_right: false,
1197 colors: ColorOverride::default(),
1198 font: FontOverride::default(),
1199 scroll_offset: PointF::ZERO,
1200 laid_out_scroll: PointF::ZERO,
1201 content_size: SizeF::ZERO,
1202 safe_area: Bounds::default(),
1203 }
1204 }
1205
1206 pub fn left(&self) -> f32 {
1209 self.left
1210 }
1211
1212 pub fn top(&self) -> f32 {
1213 self.top
1214 }
1215
1216 pub fn width(&self) -> f32 {
1217 self.width
1218 }
1219
1220 pub fn height(&self) -> f32 {
1221 self.height
1222 }
1223
1224 pub fn position(&self) -> PointF {
1225 PointF::new(self.left, self.top)
1226 }
1227
1228 pub fn size(&self) -> SizeF {
1229 SizeF::new(self.width, self.height)
1230 }
1231
1232 pub fn margins(&self) -> Bounds {
1235 if self.spacing_scale == 1.0 { self.margins } else { self.margins.scaled(self.spacing_scale) }
1236 }
1237
1238 pub fn padding(&self) -> Bounds {
1240 if self.spacing_scale == 1.0 { self.padding } else { self.padding.scaled(self.spacing_scale) }
1241 }
1242
1243 pub fn align(&self) -> Align {
1244 self.align
1245 }
1246
1247 pub fn anchors(&self) -> Anchors {
1248 self.anchors
1249 }
1250
1251 pub fn visible(&self) -> bool {
1252 self.visible
1253 }
1254
1255 pub fn design_only(&self) -> bool {
1257 self.design_only
1258 }
1259
1260 pub fn colors(&self) -> ColorOverride {
1262 self.colors
1263 }
1264
1265 pub fn set_colors(&mut self, colors: ColorOverride) -> &mut Self {
1266 self.colors = colors;
1267 self
1268 }
1269
1270 pub fn set_design_only(&mut self, on: bool) -> &mut Self {
1271 self.design_only = on;
1272 self
1273 }
1274
1275 pub fn keep_left_to_right(&self) -> bool {
1277 self.keep_left_to_right
1278 }
1279
1280 pub fn set_keep_left_to_right(&mut self, on: bool) -> &mut Self {
1281 self.keep_left_to_right = on;
1282 self
1283 }
1284
1285 pub fn enabled(&self) -> bool {
1286 self.enabled
1287 }
1288
1289 pub fn hit_test(&self) -> bool {
1290 self.hit_test
1291 }
1292
1293 pub fn translatable(&self) -> bool {
1294 self.translatable
1295 }
1296
1297 pub fn set_translatable(&mut self, v: bool) -> &mut Self {
1298 self.translatable = v;
1299 self
1300 }
1301
1302 pub fn inherit_state(&self) -> bool {
1303 self.inherit_state
1304 }
1305
1306 pub fn set_inherit_state(&mut self, v: bool) -> &mut Self {
1307 self.inherit_state = v;
1308 self
1309 }
1310
1311 pub fn clip_children(&self) -> bool {
1312 self.clip_children
1313 }
1314
1315 pub fn captures_wheel(&self) -> bool {
1316 self.captures_wheel
1317 }
1318
1319 pub fn captures_gesture(&self) -> bool {
1320 self.captures_gesture
1321 }
1322
1323 pub fn draggable(&self) -> bool {
1328 self.draggable
1329 }
1330
1331 pub fn set_draggable(&mut self, v: bool) -> &mut Self {
1332 self.draggable = v;
1333 self
1334 }
1335
1336 pub fn accepts_drops(&self) -> bool {
1339 self.accepts_drops
1340 }
1341
1342 pub fn set_accepts_drops(&mut self, v: bool) -> &mut Self {
1343 self.accepts_drops = v;
1344 self
1345 }
1346
1347 pub fn drag_data(&self) -> &str {
1349 self.drag.as_ref().map_or("", |d| d.data.as_str())
1350 }
1351
1352 pub fn drag_label(&self) -> &str {
1353 self.drag.as_ref().map_or("", |d| d.label.as_str())
1354 }
1355
1356 pub fn drag_image(&self) -> Option<ComponentId> {
1358 self.drag.as_ref().and_then(|d| d.image)
1359 }
1360
1361 pub(crate) fn forget_drag_image(&mut self, doomed: &[ComponentId]) {
1363 if let Some(d) = self.drag.as_mut().filter(|d| d.image.is_some_and(|i| doomed.contains(&i))) {
1364 d.image = None;
1365 }
1366 }
1367
1368 pub fn drop_types(&self) -> &str {
1370 self.drag.as_ref().map_or("", |d| d.drop_types.as_str())
1371 }
1372
1373 fn drag_decl(&mut self) -> &mut DragDecl {
1374 self.drag.get_or_insert_with(Default::default)
1375 }
1376
1377 pub fn can_focus(&self) -> bool {
1378 self.can_focus
1379 }
1380
1381 pub fn tab_stop(&self) -> bool {
1383 self.tab_stop
1384 }
1385
1386 pub fn popup_menu(&self) -> Option<ComponentId> {
1388 self.popup_menu
1389 }
1390
1391 pub fn set_popup_menu(&mut self, menu: Option<ComponentId>) -> &mut Self {
1392 self.popup_menu = menu;
1393 self
1394 }
1395
1396 pub fn pan_on_touch(&self) -> bool {
1398 self.pan_on_touch
1399 }
1400
1401 pub fn cursor(&self) -> CursorShape {
1403 self.cursor
1404 }
1405
1406 pub fn opacity(&self) -> f32 {
1407 self.opacity
1408 }
1409
1410 pub fn blend_mode(&self) -> crate::BlendMode {
1412 self.blend_mode
1413 }
1414
1415 pub fn set_blend_mode(&mut self, mode: crate::BlendMode) -> &mut Self {
1416 self.blend_mode = mode;
1417 self
1418 }
1419
1420 pub fn cache_layer(&self) -> bool {
1422 self.cache_layer
1423 }
1424
1425 pub fn set_cache_layer(&mut self, on: bool) -> &mut Self {
1426 self.cache_layer = on;
1427 self
1428 }
1429
1430 pub fn rotation(&self) -> f32 {
1433 self.rotation
1434 }
1435
1436 pub fn scale(&self) -> f32 {
1438 self.scale
1439 }
1440
1441 pub fn inner_size(&self) -> SizeF {
1444 let s = self.scale;
1445 if s == 1.0 { self.bounds.size() } else { SizeF::new(self.bounds.width / s, self.bounds.height / s) }
1446 }
1447
1448 pub fn shadow(&self) -> Option<crate::canvas::Shadow> {
1449 self.shadow
1450 }
1451
1452 pub fn aspect_ratio(&self) -> f32 {
1456 self.aspect_ratio
1457 }
1458
1459 pub fn set_aspect_ratio(&mut self, ratio: f32) -> &mut Self {
1460 self.aspect_ratio = if ratio.is_finite() { ratio.max(0.0) } else { 0.0 };
1461 self
1462 }
1463
1464 pub fn max_width(&self) -> f32 {
1468 self.max_width
1469 }
1470
1471 pub fn set_max_width(&mut self, width: f32) -> &mut Self {
1472 self.max_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1473 self
1474 }
1475
1476 pub fn min_width(&self) -> f32 {
1481 self.min_width
1482 }
1483
1484 pub fn set_min_width(&mut self, width: f32) -> &mut Self {
1485 self.min_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1486 self
1487 }
1488
1489 pub fn min_height(&self) -> f32 {
1491 self.min_height
1492 }
1493
1494 pub fn set_min_height(&mut self, height: f32) -> &mut Self {
1495 self.min_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1496 self
1497 }
1498
1499 pub fn max_height(&self) -> f32 {
1501 self.max_height
1502 }
1503
1504 pub fn set_max_height(&mut self, height: f32) -> &mut Self {
1505 self.max_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1506 self
1507 }
1508
1509 pub fn clamp_width(&self, available: f32) -> f32 {
1512 let w = if self.max_width > 0.0 { available.min(self.max_width) } else { available };
1513 w.max(self.min_width)
1514 }
1515
1516 pub fn clamp_height(&self, height: f32) -> f32 {
1518 let h = if self.max_height > 0.0 { height.min(self.max_height) } else { height };
1519 h.max(self.min_height)
1520 }
1521
1522 pub fn self_align(&self) -> SelfAlign {
1525 self.self_align
1526 }
1527
1528 pub fn set_self_align(&mut self, align: SelfAlign) -> &mut Self {
1529 self.self_align = align;
1530 self
1531 }
1532
1533 pub fn band_width(&self, available: f32) -> f32 {
1537 match self.self_align {
1538 SelfAlign::Stretch => self.clamp_width(available),
1539 _ if self.max_width > 0.0 => self.clamp_width(available),
1540 _ => self.clamp_width(self.width),
1541 }
1542 }
1543
1544 pub fn band_height(&self, available: f32) -> f32 {
1547 match self.self_align {
1548 SelfAlign::Stretch => self.clamp_height(available),
1549 _ if self.max_height > 0.0 => self.clamp_height(available),
1550 _ => self.clamp_height(self.height),
1551 }
1552 }
1553
1554 pub fn band_offset(&self, available: f32, size: f32) -> f32 {
1557 let spare = (available - size).max(0.0);
1558 match self.self_align {
1559 SelfAlign::Start => 0.0,
1560 SelfAlign::End => spare,
1561 SelfAlign::Stretch | SelfAlign::Center => spare / 2.0,
1562 }
1563 }
1564
1565 pub fn auto_size(&self) -> bool {
1570 self.auto_size
1571 }
1572
1573 pub fn set_auto_size(&mut self, on: bool) -> &mut Self {
1574 self.auto_size = on;
1575 self
1576 }
1577
1578 pub fn column_span(&self) -> u32 {
1581 self.column_span.max(1)
1582 }
1583
1584 pub fn grid_cell(&self) -> Option<(u32, u32)> {
1586 (self.grid_row >= 0 && self.grid_column >= 0).then(|| (self.grid_row as u32, self.grid_column as u32))
1587 }
1588
1589 pub fn set_grid_cell(&mut self, cell: Option<(u32, u32)>) -> &mut Self {
1590 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));
1591 self.grid_row = r;
1592 self.grid_column = c;
1593 self
1594 }
1595
1596 pub fn row_span(&self) -> u32 {
1598 u32::from(self.row_span.max(1))
1599 }
1600
1601 pub fn set_row_span(&mut self, span: u32) -> &mut Self {
1602 self.row_span = span.clamp(1, 64) as u16;
1603 self
1604 }
1605
1606 pub fn grow(&self) -> f32 {
1607 self.grow
1608 }
1609
1610 pub fn set_grow(&mut self, grow: f32) -> &mut Self {
1611 self.grow = grow.max(0.0);
1612 self
1613 }
1614
1615 pub fn set_column_span(&mut self, span: u32) -> &mut Self {
1616 self.column_span = span.max(1);
1617 self
1618 }
1619
1620 pub fn text_scale(&self) -> f32 {
1622 self.text_scale
1623 }
1624
1625 pub fn spacing_scale(&self) -> f32 {
1627 self.spacing_scale
1628 }
1629
1630 pub fn tag(&self) -> i64 {
1631 self.tag
1632 }
1633
1634 pub fn accessible_name(&self) -> &str {
1637 self.rare.as_ref().map_or("", |r| &r.accessible_name)
1638 }
1639
1640 pub fn set_accessible_name(&mut self, v: &str) -> &mut Self {
1641 self.rare_mut(|r| r.accessible_name = v.into());
1642 self
1643 }
1644
1645 fn rare_mut(&mut self, change: impl FnOnce(&mut RareTexts)) {
1646 let rare = self.rare.get_or_insert_with(Default::default);
1647 change(rare);
1648 if rare.tag_string.is_empty() && rare.accessible_name.is_empty() {
1649 self.rare = None;
1650 }
1651 }
1652
1653 pub fn tag_string(&self) -> &str {
1654 self.rare.as_ref().map_or("", |r| &r.tag_string)
1655 }
1656
1657 pub fn style(&self) -> &str {
1659 &self.style
1660 }
1661
1662 pub fn theme_name(&self) -> &str {
1664 &self.theme
1665 }
1666
1667 pub fn set_theme_name(&mut self, v: impl Into<String>) -> &mut Self {
1668 self.theme = v.into();
1669 self
1670 }
1671
1672 pub fn hint(&self) -> &str {
1674 &self.hint
1675 }
1676
1677 pub fn set_hint(&mut self, v: impl Into<String>) -> &mut Self {
1678 self.hint = v.into();
1679 self
1680 }
1681
1682 pub fn state(&self) -> ControlState {
1683 self.state
1684 }
1685
1686 pub fn bounds(&self) -> RectF {
1688 self.bounds
1689 }
1690
1691 pub fn scroll_offset(&self) -> PointF {
1693 self.scroll_offset
1694 }
1695
1696 pub fn laid_out_scroll(&self) -> PointF {
1699 self.laid_out_scroll
1700 }
1701
1702 pub fn set_scroll_offset(&mut self, offset: PointF) -> &mut Self {
1705 self.scroll_offset = offset;
1706 self
1707 }
1708
1709 pub fn content_size(&self) -> SizeF {
1712 self.content_size
1713 }
1714
1715 pub fn set_left(&mut self, v: f32) -> &mut Self {
1722 if self.left != v {
1723 self.left = v;
1724 self.anchor_ref = None;
1725 }
1726 self
1727 }
1728
1729 pub fn set_top(&mut self, v: f32) -> &mut Self {
1730 if self.top != v {
1731 self.top = v;
1732 self.anchor_ref = None;
1733 }
1734 self
1735 }
1736
1737 pub fn set_position(&mut self, left: f32, top: f32) -> &mut Self {
1738 self.set_left(left).set_top(top)
1739 }
1740
1741 pub fn set_width(&mut self, v: f32) -> &mut Self {
1742 if self.width != v.max(0.0) {
1743 self.width = v.max(0.0);
1744 self.anchor_ref = None;
1745 }
1746 self
1747 }
1748
1749 pub fn set_height(&mut self, v: f32) -> &mut Self {
1750 if self.height != v.max(0.0) {
1751 self.height = v.max(0.0);
1752 self.anchor_ref = None;
1753 }
1754 self
1755 }
1756
1757 pub(crate) fn set_measured_width(&mut self, v: f32) {
1759 self.width = v.max(0.0);
1760 if let Some(r) = self.anchor_ref.as_mut() {
1761 r.rect.width = self.width;
1762 }
1763 }
1764
1765 pub(crate) fn set_measured_height(&mut self, v: f32) {
1768 self.height = v.max(0.0);
1769 if let Some(r) = self.anchor_ref.as_mut() {
1770 r.rect.height = self.height;
1771 }
1772 }
1773
1774 pub fn set_size(&mut self, width: f32, height: f32) -> &mut Self {
1775 self.set_width(width).set_height(height)
1776 }
1777
1778 pub fn set_rect(&mut self, left: f32, top: f32, width: f32, height: f32) -> &mut Self {
1780 self.set_position(left, top).set_size(width, height)
1781 }
1782
1783 pub fn set_margins(&mut self, v: impl Into<Bounds>) -> &mut Self {
1784 self.margins = v.into();
1785 self
1786 }
1787
1788 pub fn set_padding(&mut self, v: impl Into<Bounds>) -> &mut Self {
1789 self.padding = v.into();
1790 self
1791 }
1792
1793 pub fn set_align(&mut self, v: Align) -> &mut Self {
1794 self.align = v;
1795 self.anchor_ref = None;
1796 self
1797 }
1798
1799 pub fn set_anchors(&mut self, v: Anchors) -> &mut Self {
1800 self.anchors = v;
1801 self.anchor_ref = None;
1802 self
1803 }
1804
1805 pub fn set_visible(&mut self, v: bool) -> &mut Self {
1806 self.visible = v;
1807 self
1808 }
1809
1810 pub fn set_enabled(&mut self, v: bool) -> &mut Self {
1811 self.enabled = v;
1812 self
1813 }
1814
1815 pub fn set_hit_test(&mut self, v: bool) -> &mut Self {
1816 self.hit_test = v;
1817 self
1818 }
1819
1820 pub fn set_clip_children(&mut self, v: bool) -> &mut Self {
1821 self.clip_children = v;
1822 self
1823 }
1824
1825 pub fn set_captures_wheel(&mut self, v: bool) -> &mut Self {
1826 self.captures_wheel = v;
1827 self
1828 }
1829
1830 pub fn set_captures_gesture(&mut self, v: bool) -> &mut Self {
1831 self.captures_gesture = v;
1832 self
1833 }
1834
1835 pub fn set_can_focus(&mut self, v: bool) -> &mut Self {
1836 self.can_focus = v;
1837 self
1838 }
1839
1840 pub fn set_pan_on_touch(&mut self, v: bool) -> &mut Self {
1844 self.pan_on_touch = v;
1845 self
1846 }
1847
1848 pub fn set_tab_stop(&mut self, v: bool) -> &mut Self {
1849 self.tab_stop = v;
1850 self
1851 }
1852
1853 pub fn set_cursor(&mut self, v: CursorShape) -> &mut Self {
1854 self.cursor = v;
1855 self
1856 }
1857
1858 pub fn set_opacity(&mut self, v: f32) -> &mut Self {
1859 self.opacity = v.clamp(0.0, 1.0);
1860 self
1861 }
1862
1863 pub fn translate(&self) -> PointF {
1865 self.translate
1866 }
1867
1868 pub fn set_translate(&mut self, offset: PointF) -> &mut Self {
1869 self.translate = if offset.x.is_finite() && offset.y.is_finite() { offset } else { PointF::ZERO };
1870 self
1871 }
1872
1873 pub fn set_scale(&mut self, scale: f32) -> &mut Self {
1875 self.scale = if scale.is_finite() && scale > 0.0 { scale.clamp(0.05, 20.0) } else { 1.0 };
1876 self
1877 }
1878
1879 pub fn set_rotation(&mut self, degrees: f32) -> &mut Self {
1880 self.rotation = if degrees.is_finite() { degrees % 360.0 } else { 0.0 };
1881 self
1882 }
1883
1884 pub fn set_shadow(&mut self, shadow: Option<crate::canvas::Shadow>) -> &mut Self {
1885 self.shadow = shadow;
1886 self
1887 }
1888
1889 pub fn set_tag(&mut self, v: i64) -> &mut Self {
1890 self.tag = v;
1891 self
1892 }
1893
1894 pub fn set_tag_string(&mut self, v: &str) -> &mut Self {
1895 self.rare_mut(|r| r.tag_string = v.into());
1896 self
1897 }
1898
1899 pub fn set_style(&mut self, v: impl Into<String>) -> &mut Self {
1900 self.style = v.into();
1901 self
1902 }
1903
1904 pub fn state_mut(&mut self) -> &mut ControlState {
1907 &mut self.state
1908 }
1909
1910 pub fn get_prop(&self, name: &str) -> Option<PropValue> {
1913 Some(match name {
1914 "left" => self.left.into(),
1915 "top" => self.top.into(),
1916 "width" => self.width.into(),
1917 "height" => self.height.into(),
1918 "align" => self.align.into(),
1919 "anchors" => self.anchors.into(),
1920 "margins" => self.margins.into(),
1921 "padding" => self.padding.into(),
1922 "visible" => self.visible.into(),
1923 "enabled" => self.enabled.into(),
1924 "hit_test" => self.hit_test.into(),
1925 "clip_children" => self.clip_children.into(),
1926 "inherit_state" => self.inherit_state.into(),
1927 "translate" => self.translatable.into(),
1928 "captures_wheel" => self.captures_wheel.into(),
1929 "captures_gesture" => self.captures_gesture.into(),
1930 "can_focus" => self.can_focus.into(),
1931 "draggable" => self.draggable.into(),
1932 "accepts_drops" => self.accepts_drops.into(),
1933 "drag_data" => self.drag_data().into(),
1934 "drag_label" => self.drag_label().into(),
1935 "drag_image" => PropValue::Component(self.drag_image()),
1936 "drop_types" => self.drop_types().into(),
1937 "tab_stop" => self.tab_stop.into(),
1938 "cursor" => PropValue::enum_value(self.cursor),
1939 "opacity" => self.opacity.into(),
1940 "blend_mode" => PropValue::enum_value(self.blend_mode),
1941 "cache_layer" => self.cache_layer.into(),
1942 "rotation" => self.rotation.into(),
1943 "scale" => self.scale.into(),
1944 "translate_x" => self.translate.x.into(),
1945 "translate_y" => self.translate.y.into(),
1946 "aspect_ratio" => self.aspect_ratio.into(),
1947 "max_width" => self.max_width.into(),
1948 "min_width" => self.min_width.into(),
1949 "min_height" => self.min_height.into(),
1950 "max_height" => self.max_height.into(),
1951 "self_align" => PropValue::enum_value(self.self_align),
1952 "auto_size" => self.auto_size.into(),
1953 "column_span" => (self.column_span() as i64).into(),
1954 "grid_row" => i64::from(self.grid_row).into(),
1955 "grid_column" => i64::from(self.grid_column).into(),
1956 "row_span" => (self.row_span() as i64).into(),
1957 "grow" => f64::from(self.grow).into(),
1958 "shadow" => crate::prop::PropType::to_value(&self.shadow),
1959 "style" => self.style.clone().into(),
1960 "fill" => crate::prop::PropType::to_value(&self.colors.fill),
1961 "border_color" => crate::prop::PropType::to_value(&self.colors.border),
1962 "text_color" => crate::prop::PropType::to_value(&self.colors.text),
1963 "accent_color" => crate::prop::PropType::to_value(&self.colors.accent),
1964 "success_color" => crate::prop::PropType::to_value(&self.colors.success),
1965 "warning_color" => crate::prop::PropType::to_value(&self.colors.warning),
1966 "danger_color" => crate::prop::PropType::to_value(&self.colors.danger),
1967 "font_family" => self.font.family.clone().into(),
1968 "font_size" => crate::prop::PropType::to_value(&self.font.size),
1969 "font_weight" => PropValue::Enum(FONT_WEIGHTS[self.font.weight.map_or(0, |w| w as usize + 1)].into()),
1970 "font_italic" => self.font.italic.into(),
1971 "font_underline" => self.font.underline.into(),
1972 "font_strikeout" => self.font.strikeout.into(),
1973 "theme" => self.theme.clone().into(),
1974 "hint" => self.hint.clone().into(),
1975 "tag" => self.tag.into(),
1976 "tag_string" => self.tag_string().to_string().into(),
1977 "accessible_name" => self.accessible_name().to_string().into(),
1978 "popup_menu" => PropValue::Component(self.popup_menu),
1979 _ => return None,
1980 })
1981 }
1982
1983 pub fn set_prop(&mut self, name: &str, value: PropValue) -> Result<()> {
1984 match name {
1985 "left" => self.set_left(value.to_f32()?),
1986 "top" => self.set_top(value.to_f32()?),
1987 "width" => self.set_width(value.to_f32()?),
1988 "height" => self.set_height(value.to_f32()?),
1989 "align" => self.set_align(value.to_enum()?),
1990 "anchors" => self.set_anchors(value.to_anchors()?),
1991 "margins" => self.set_margins(value.to_bounds()?),
1992 "padding" => self.set_padding(value.to_bounds()?),
1993 "visible" => self.set_visible(value.to_bool()?),
1994 "enabled" => self.set_enabled(value.to_bool()?),
1995 "hit_test" => self.set_hit_test(value.to_bool()?),
1996 "clip_children" => self.set_clip_children(value.to_bool()?),
1997 "inherit_state" => self.set_inherit_state(value.to_bool()?),
1998 "translate" => self.set_translatable(value.to_bool()?),
1999 "captures_wheel" => self.set_captures_wheel(value.to_bool()?),
2000 "captures_gesture" => self.set_captures_gesture(value.to_bool()?),
2001 "can_focus" => self.set_can_focus(value.to_bool()?),
2002 "draggable" => self.set_draggable(value.to_bool()?),
2003 "accepts_drops" => self.set_accepts_drops(value.to_bool()?),
2004 "drag_data" => {
2005 self.drag_decl().data = value.to_str()?.to_string();
2006 self
2007 }
2008 "drag_label" => {
2009 self.drag_decl().label = value.to_str()?.to_string();
2010 self
2011 }
2012 "drag_image" => {
2013 self.drag_decl().image = value.to_component()?;
2014 self
2015 }
2016 "drop_types" => {
2017 self.drag_decl().drop_types = value.to_str()?.to_string();
2018 self
2019 }
2020 "tab_stop" => self.set_tab_stop(value.to_bool()?),
2021 "cursor" => self.set_cursor(value.to_enum()?),
2022 "opacity" => self.set_opacity(value.to_f32()?),
2023 "blend_mode" => self.set_blend_mode(value.to_enum()?),
2024 "cache_layer" => self.set_cache_layer(value.to_bool()?),
2025 "rotation" => self.set_rotation(value.to_f32()?),
2026 "scale" => self.set_scale(value.to_f32()?),
2027 "translate_x" => self.set_translate(PointF::new(value.to_f32()?, self.translate.y)),
2028 "translate_y" => self.set_translate(PointF::new(self.translate.x, value.to_f32()?)),
2029 "aspect_ratio" => self.set_aspect_ratio(value.to_f32()?),
2030 "max_width" => self.set_max_width(value.to_f32()?),
2031 "min_width" => self.set_min_width(value.to_f32()?),
2032 "min_height" => self.set_min_height(value.to_f32()?),
2033 "max_height" => self.set_max_height(value.to_f32()?),
2034 "self_align" => self.set_self_align(value.to_enum()?),
2035 "auto_size" => self.set_auto_size(value.to_bool()?),
2036 "column_span" => self.set_column_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2037 "grid_row" => {
2038 self.grid_row = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2039 self
2040 }
2041 "grid_column" => {
2042 self.grid_column = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2043 self
2044 }
2045 "row_span" => self.set_row_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2046 "grow" => self.set_grow(value.to_f32()?),
2047 "shadow" => self.set_shadow(crate::prop::PropType::from_value(value)?),
2048 "style" => self.set_style(value.to_str()?),
2049 "fill" => {
2050 self.colors.fill = crate::prop::PropType::from_value(value)?;
2051 self
2052 }
2053 "border_color" => {
2054 self.colors.border = crate::prop::PropType::from_value(value)?;
2055 self
2056 }
2057 "text_color" => {
2058 self.colors.text = crate::prop::PropType::from_value(value)?;
2059 self
2060 }
2061 "accent_color" => {
2062 self.colors.accent = crate::prop::PropType::from_value(value)?;
2063 self
2064 }
2065 "success_color" => {
2066 self.colors.success = crate::prop::PropType::from_value(value)?;
2067 self
2068 }
2069 "warning_color" => {
2070 self.colors.warning = crate::prop::PropType::from_value(value)?;
2071 self
2072 }
2073 "danger_color" => {
2074 self.colors.danger = crate::prop::PropType::from_value(value)?;
2075 self
2076 }
2077 "font_family" => {
2078 self.font.family = value.to_str()?.to_string();
2079 self
2080 }
2081 "font_size" => {
2082 self.font.size = crate::prop::PropType::from_value(value)?;
2083 self
2084 }
2085 "font_weight" => {
2086 self.font.weight = font_weight_named(value.to_str()?)?;
2087 self
2088 }
2089 "font_italic" => {
2090 self.font.italic = value.to_bool()?;
2091 self
2092 }
2093 "font_underline" => {
2094 self.font.underline = value.to_bool()?;
2095 self
2096 }
2097 "font_strikeout" => {
2098 self.font.strikeout = value.to_bool()?;
2099 self
2100 }
2101 "theme" => self.set_theme_name(value.to_str()?),
2102 "hint" => self.set_hint(value.to_str()?),
2103 "tag" => self.set_tag(value.to_i64()?),
2104 "tag_string" => self.set_tag_string(value.to_str()?),
2105 "accessible_name" => self.set_accessible_name(value.to_str()?),
2106 "popup_menu" => {
2107 self.popup_menu = value.to_component()?;
2108 self
2109 }
2110 _ => return Err(Error::UnknownProperty(name.to_string())),
2111 };
2112 Ok(())
2113 }
2114}
2115
2116pub const FONT_WEIGHTS: [&str; 8] = ["default", "thin", "light", "normal", "medium", "semibold", "bold", "black"];
2118
2119fn font_weight_named(name: &str) -> Result<Option<crate::canvas::FontWeight>> {
2120 use crate::canvas::FontWeight::*;
2121 let all = [Thin, Light, Normal, Medium, SemiBold, Bold, Black];
2122 match FONT_WEIGHTS.iter().position(|n| n.eq_ignore_ascii_case(name)) {
2123 Some(0) => Ok(None),
2124 Some(i) => Ok(Some(all[i - 1])),
2125 None => Err(Error::invalid(format!("valor '{name}' inválido; use um de: {}", FONT_WEIGHTS.join(", ")))),
2126 }
2127}
2128
2129#[derive(Clone, Debug, Default, PartialEq)]
2133pub struct FontOverride {
2134 pub family: String,
2136 pub size: Option<f32>,
2138 pub weight: Option<crate::canvas::FontWeight>,
2140 pub italic: bool,
2142 pub underline: bool,
2144 pub strikeout: bool,
2146}
2147
2148impl FontOverride {
2149 pub fn is_empty(&self) -> bool {
2150 *self == Self::default()
2151 }
2152
2153 pub fn over(&self, parent: &FontOverride) -> FontOverride {
2155 FontOverride {
2156 family: if self.family.is_empty() { parent.family.clone() } else { self.family.clone() },
2157 size: self.size.or(parent.size),
2158 weight: self.weight.or(parent.weight),
2159 italic: self.italic || parent.italic,
2160 underline: self.underline || parent.underline,
2161 strikeout: self.strikeout || parent.strikeout,
2162 }
2163 }
2164
2165 pub fn apply(&self, font: &mut crate::canvas::FontSpec) {
2167 if !self.family.is_empty() {
2168 font.family = self.family.clone();
2169 }
2170 if let Some(size) = self.size.filter(|s| *s > 0.0) {
2171 font.size = size;
2172 }
2173 if let Some(weight) = self.weight {
2174 font.weight = weight;
2175 }
2176 font.italic |= self.italic;
2177 font.underline |= self.underline;
2178 font.strikeout |= self.strikeout;
2179 }
2180}
2181
2182impl ControlBase {
2183 pub fn font(&self) -> &FontOverride {
2185 &self.font
2186 }
2187
2188 pub fn set_font(&mut self, font: FontOverride) -> &mut Self {
2189 self.font = font;
2190 self
2191 }
2192}
2193
2194#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2200pub struct ColorOverride {
2201 pub fill: Option<crate::Color>,
2203 pub border: Option<crate::Color>,
2205 pub text: Option<crate::Color>,
2207 pub accent: Option<crate::Color>,
2210 pub success: Option<crate::Color>,
2213 pub warning: Option<crate::Color>,
2215 pub danger: Option<crate::Color>,
2217}
2218
2219impl ColorOverride {
2220 pub fn is_empty(&self) -> bool {
2221 *self == Self::default()
2222 }
2223
2224 pub(crate) fn recolors_theme(&self) -> bool {
2226 !self.is_empty()
2227 }
2228
2229 pub(crate) fn apply_theme(&self, theme: &crate::Theme) -> crate::Theme {
2231 use crate::Color;
2232 let mut t = theme.clone();
2233 let mut set = |name: &str, c: Color| {
2234 t.colors.insert(name.to_string(), c);
2235 };
2236 if let Some(a) = self.accent {
2237 let dark = luminance(a) < 0.5;
2238 set("primary", a);
2239 set("primary_hover", a.lerp(if dark { Color::WHITE } else { Color::BLACK }, 0.12));
2240 set("primary_pressed", a.lerp(if dark { Color::BLACK } else { Color::BLACK }, 0.15));
2241 set("on_primary", if luminance(a) < 0.6 { Color::WHITE } else { Color::BLACK });
2242 set("focus_ring", a);
2243 set("selection", a.with_opacity(0.25));
2244 set("primary_soft", a.with_opacity(0.1));
2245 set("primary_container", a.lerp(Color::WHITE, 0.82));
2246 }
2247 if let Some(c) = self.fill {
2248 let toward = if luminance(c) < 0.5 { Color::WHITE } else { Color::BLACK };
2250 set("surface", c);
2251 set("background", c);
2252 set("surface_2", c.lerp(toward.with_opacity(c.a as f32 / 255.0), 0.06));
2253 }
2254 if let Some(c) = self.text {
2255 set("text", c);
2256 set("text_muted", c.with_opacity(0.65));
2257 for on in ["on_primary", "on_danger", "on_success", "on_primary_container"] {
2258 set(on, c);
2259 }
2260 }
2261 if let Some(c) = self.border {
2262 set("border", c);
2263 set("track", c);
2264 }
2265 for (token, color) in [("success", self.success), ("warning", self.warning), ("danger", self.danger)] {
2266 if let Some(c) = color {
2267 set(token, c);
2268 set(&format!("{token}_soft"), c.with_opacity(0.1));
2269 set(&format!("{token}_hover"), c.lerp(Color::WHITE, 0.12));
2270 set(&format!("{token}_pressed"), c.lerp(Color::BLACK, 0.15));
2271 if token == "success" {
2272 set("meter_low", c);
2273 } else if token == "danger" {
2274 set("meter_high", c);
2275 } else {
2276 set("meter_mid", c);
2277 }
2278 }
2279 }
2280 t
2281 }
2282}
2283
2284pub(crate) fn luminance(c: crate::Color) -> f32 {
2286 (0.299 * c.r as f32 + 0.587 * c.g as f32 + 0.114 * c.b as f32) / 255.0
2287}
2288
2289#[derive(Clone, Copy, Debug, PartialEq)]
2291pub struct Connection {
2292 pub from: ComponentId,
2293 pub from_at: (f32, f32),
2295 pub to: ComponentId,
2296 pub to_at: (f32, f32),
2297}
2298
2299impl Connection {
2300 pub const STUB: f32 = 16.0;
2302
2303 fn normal(at: (f32, f32)) -> PointF {
2305 let (x, y) = at;
2306 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))];
2307 d.iter().min_by(|a, b| a.0.total_cmp(&b.0)).map_or(PointF::new(1.0, 0.0), |d| d.1)
2308 }
2309
2310 pub fn route(&self, from: RectF, to: RectF) -> Vec<PointF> {
2313 let p0 = PointF::new(from.x + from.width * self.from_at.0, from.y + from.height * self.from_at.1);
2314 let p1 = PointF::new(to.x + to.width * self.to_at.0, to.y + to.height * self.to_at.1);
2315 let (n0, n1) = (Self::normal(self.from_at), Self::normal(self.to_at));
2316 let s0 = PointF::new(p0.x + n0.x * Self::STUB, p0.y + n0.y * Self::STUB);
2317 let s1 = PointF::new(p1.x + n1.x * Self::STUB, p1.y + n1.y * Self::STUB);
2318 let mut points = vec![p0, s0];
2319 if n0.x != 0.0 {
2320 let mx = (s0.x + s1.x) / 2.0;
2322 points.push(PointF::new(mx, s0.y));
2323 points.push(PointF::new(mx, s1.y));
2324 } else {
2325 let my = (s0.y + s1.y) / 2.0;
2326 points.push(PointF::new(s0.x, my));
2327 points.push(PointF::new(s1.x, my));
2328 }
2329 points.push(s1);
2330 points.push(p1);
2331 points.dedup_by(|a, b| (a.x - b.x).abs() < 0.01 && (a.y - b.y).abs() < 0.01);
2332 points
2333 }
2334}
2335
2336#[derive(Clone, Debug, Default, PartialEq)]
2338pub struct DragDecl {
2339 pub data: String,
2342 pub label: String,
2344 pub drop_types: String,
2347 pub image: Option<ComponentId>,
2349}
2350
2351#[derive(Clone, Debug, Default)]
2353struct RareTexts {
2354 tag_string: Box<str>,
2356 accessible_name: Box<str>,
2359}