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