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
55#[derive(Clone, Debug, PartialEq)]
57pub enum ClipShape {
58 RoundRect(f32),
60 Corners(crate::Bounds),
63 Ellipse,
65 Path(std::sync::Arc<crate::Path>),
68}
69
70impl ClipShape {
71 pub fn from_corners(c: crate::Bounds) -> ClipShape {
74 if c.left == c.top && c.top == c.right && c.right == c.bottom && c.left >= 0.0 {
75 ClipShape::RoundRect(c.left)
76 } else {
77 ClipShape::Corners(c)
78 }
79 }
80
81 pub fn radius(&self, rect: crate::RectF) -> f32 {
84 let limit = rect.width.min(rect.height) / 2.0;
85 match self {
86 ClipShape::RoundRect(r) => r.clamp(0.0, limit),
87 ClipShape::Corners(c) => c.left.max(c.top).max(c.right).max(c.bottom).clamp(0.0, limit),
88 ClipShape::Ellipse => limit,
89 ClipShape::Path(_) => 0.0,
90 }
91 }
92
93 pub fn outline(&self, rect: crate::RectF) -> crate::Path {
95 let mut path = crate::Path::new();
96 match self {
97 ClipShape::RoundRect(_) => {
98 path.rect(rect, self.radius(rect));
99 }
100 ClipShape::Corners(c) => path = crate::Path::corners(rect, *c),
101 ClipShape::Ellipse => {
102 path.ellipse(rect);
103 }
104 ClipShape::Path(p) => {
105 let b = p.bounds();
106 let sx = if b.width > 0.0 { rect.width / b.width } else { 1.0 };
107 let sy = if b.height > 0.0 { rect.height / b.height } else { 1.0 };
108 path = p.map_points(|q| crate::PointF::new(rect.x + (q.x - b.x) * sx, rect.y + (q.y - b.y) * sy));
109 }
110 }
111 path
112 }
113
114 pub fn fill(&self, canvas: &mut dyn crate::Canvas, rect: crate::RectF, brush: &crate::Brush) {
116 match self {
117 ClipShape::RoundRect(_) => canvas.fill_rect(rect, self.radius(rect), brush),
118 ClipShape::Ellipse => canvas.fill_ellipse(rect, brush),
119 _ => canvas.fill_path(&self.outline(rect), brush),
120 }
121 }
122
123 pub fn clip(&self, canvas: &mut dyn crate::Canvas, rect: crate::RectF) {
125 match self {
126 ClipShape::RoundRect(_) => canvas.clip_round_rect(rect, self.radius(rect)),
127 _ => canvas.clip_path(&self.outline(rect), false),
128 }
129 }
130}
131
132pub trait Component: Any {
163 fn class_name(&self) -> &'static str;
166
167 fn inner(&self) -> Option<&dyn Component> {
169 None
170 }
171
172 fn inner_mut(&mut self) -> Option<&mut dyn Component> {
173 None
174 }
175
176 fn class_chain(&self, out: &mut Vec<&'static str>) {
179 out.push(self.class_name());
180 if let Some(inner) = self.inner() {
181 inner.class_chain(out);
182 }
183 }
184
185 fn control(&self) -> Option<&ControlBase> {
187 self.inner().and_then(|i| i.control())
188 }
189
190 fn control_mut(&mut self) -> Option<&mut ControlBase> {
191 self.inner_mut().and_then(|i| i.control_mut())
192 }
193
194 fn props(&self) -> &'static [PropInfo] {
197 &[]
198 }
199
200 fn default_event(&self) -> Option<&'static str> {
204 self.inner().and_then(|c| c.default_event())
205 }
206
207 fn get_prop(&self, _name: &str) -> Option<PropValue> {
209 None
210 }
211
212 fn set_prop(&mut self, name: &str, _value: PropValue) -> Result<()> {
215 Err(Error::UnknownProperty(name.to_string()))
216 }
217
218 fn notification(&mut self, freed: ComponentId) {
221 if let Some(inner) = self.inner_mut() {
222 inner.notification(freed);
223 }
224 }
225
226 fn paint(&self, canvas: &mut dyn Canvas, ctx: &PaintCtx<'_>) {
228 if let Some(inner) = self.inner() {
229 inner.paint(canvas, ctx);
230 }
231 }
232
233 fn transparent_background(&self) -> bool {
237 self.inner().is_some_and(|i| i.transparent_background())
238 }
239
240 fn opaque(&self, style: &crate::theme::ResolvedStyle) -> bool {
245 self.inner().is_some_and(|i| i.opaque(style))
246 }
247
248 fn clip_shape(&self) -> Option<ClipShape> {
252 self.inner().and_then(|i| i.clip_shape())
253 }
254
255 fn painted_shape(&self) -> Option<ClipShape> {
260 self.clip_shape().or_else(|| self.inner().and_then(|i| i.painted_shape()))
261 }
262
263 fn paint_after(&self, canvas: &mut dyn Canvas, ctx: &PaintCtx<'_>) {
265 if let Some(inner) = self.inner() {
266 inner.paint_after(canvas, ctx);
267 }
268 }
269
270 fn paint_is_pure(&self) -> bool {
277 false
278 }
279
280 fn provided(&self) -> Option<crate::providers::Provided<'_>> {
283 None
284 }
285
286 fn resized(&mut self, ctx: &mut EventCtx<'_>) {
288 if let Some(inner) = self.inner_mut() {
289 inner.resized(ctx);
290 }
291 }
292
293 fn click(&mut self, ctx: &mut EventCtx<'_>) {
295 match self.inner_mut() {
296 Some(inner) => inner.click(ctx),
297 None => ctx.fire("on_click", EventArgs::None),
298 }
299 }
300
301 fn pointer_down(&mut self, ctx: &mut EventCtx<'_>, event: &PointerEvent) {
302 if let Some(inner) = self.inner_mut() {
303 inner.pointer_down(ctx, event);
304 }
305 }
306
307 fn pointer_up(&mut self, ctx: &mut EventCtx<'_>, event: &PointerEvent) {
308 if let Some(inner) = self.inner_mut() {
309 inner.pointer_up(ctx, event);
310 }
311 }
312
313 fn pointer_move(&mut self, ctx: &mut EventCtx<'_>, event: &PointerEvent) {
314 if let Some(inner) = self.inner_mut() {
315 inner.pointer_move(ctx, event);
316 }
317 }
318
319 fn pointer_enter(&mut self, ctx: &mut EventCtx<'_>) {
320 if let Some(inner) = self.inner_mut() {
321 inner.pointer_enter(ctx);
322 }
323 }
324
325 fn pointer_leave(&mut self, ctx: &mut EventCtx<'_>) {
326 if let Some(inner) = self.inner_mut() {
327 inner.pointer_leave(ctx);
328 }
329 }
330
331 fn wheel(&mut self, ctx: &mut EventCtx<'_>, event: &WheelEvent) {
332 if let Some(inner) = self.inner_mut() {
333 inner.wheel(ctx, event);
334 }
335 }
336
337 fn gesture(&mut self, ctx: &mut EventCtx<'_>, event: &crate::event::GestureEvent) {
340 if let Some(inner) = self.inner_mut() {
341 inner.gesture(ctx, event);
342 }
343 }
344
345 fn swipe(&mut self, ctx: &mut EventCtx<'_>, event: &crate::event::SwipeEvent) {
348 if let Some(inner) = self.inner_mut() {
349 inner.swipe(ctx, event);
350 }
351 }
352
353 fn touch_end(&mut self, ctx: &mut EventCtx<'_>) {
357 if let Some(inner) = self.inner_mut() {
358 inner.touch_end(ctx);
359 }
360 }
361
362 fn edge_swipe(&mut self, ctx: &mut EventCtx<'_>, event: &crate::event::SwipeEvent) -> bool {
365 match self.inner_mut() {
366 Some(inner) => inner.edge_swipe(ctx, event),
367 None => false,
368 }
369 }
370
371 fn context_menu(&mut self, ctx: &mut EventCtx<'_>, at: PointF, touch: bool) -> bool {
376 match self.inner_mut() {
377 Some(inner) => inner.context_menu(ctx, at, touch),
378 None => false,
379 }
380 }
381
382 fn key_down(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
383 if let Some(inner) = self.inner_mut() {
384 inner.key_down(ctx, event);
385 }
386 }
387
388 fn key_up(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
389 if let Some(inner) = self.inner_mut() {
390 inner.key_up(ctx, event);
391 }
392 }
393
394 fn shortcut(&mut self, ctx: &mut EventCtx<'_>, event: &KeyEvent) {
398 if let Some(inner) = self.inner_mut() {
399 inner.shortcut(ctx, event);
400 }
401 }
402
403 fn platform_hooks(&self) -> Option<&dyn PlatformHooks> {
406 self.inner().and_then(|i| i.platform_hooks())
407 }
408
409 fn platform_hooks_mut(&mut self) -> Option<&mut dyn PlatformHooks> {
410 self.inner_mut().and_then(|i| i.platform_hooks_mut())
411 }
412
413 fn design_hooks(&self) -> Option<&dyn DesignHooks> {
416 self.inner().and_then(|i| i.design_hooks())
417 }
418
419 fn design_hooks_mut(&mut self) -> Option<&mut dyn DesignHooks> {
420 self.inner_mut().and_then(|i| i.design_hooks_mut())
421 }
422
423 fn contains_point(&self, local: PointF, size: SizeF) -> bool {
426 self.inner().map_or(true, |i| i.contains_point(local, size))
427 }
428
429 fn hit_slop(&self) -> f32 {
433 self.inner().map_or(0.0, |i| i.hit_slop())
434 }
435
436 fn scroll_axes(&self) -> (bool, bool) {
439 self.inner().map_or((false, false), |i| i.scroll_axes())
440 }
441
442 fn drag_drop(&self) -> Option<&dyn DragDrop> {
445 None
446 }
447
448 fn drag_drop_mut(&mut self) -> Option<&mut dyn DragDrop> {
449 None
450 }
451
452 fn accepts_text(&self) -> bool {
453 self.inner().is_some_and(|i| i.accepts_text())
454 }
455
456 fn captures_key(&self, event: &KeyEvent) -> bool {
459 self.inner().is_some_and(|i| i.captures_key(event))
460 }
461
462 fn mnemonic_text(&self) -> Option<&str> {
465 self.inner().and_then(|i| i.mnemonic_text())
466 }
467
468 fn mnemonic_focuses_child(&self) -> bool {
470 self.inner().is_some_and(|i| i.mnemonic_focuses_child())
471 }
472
473 fn text_input(&mut self, ctx: &mut EventCtx<'_>, text: &str) {
475 if let Some(inner) = self.inner_mut() {
476 inner.text_input(ctx, text);
477 }
478 }
479
480 fn focus_enter(&mut self, ctx: &mut EventCtx<'_>) {
481 if let Some(inner) = self.inner_mut() {
482 inner.focus_enter(ctx);
483 }
484 }
485
486 fn focus_exit(&mut self, ctx: &mut EventCtx<'_>) {
487 if let Some(inner) = self.inner_mut() {
488 inner.focus_exit(ctx);
489 }
490 }
491
492 fn property_changed(&mut self, ctx: &mut EventCtx<'_>, name: &str) {
496 if let Some(inner) = self.inner_mut() {
497 inner.property_changed(ctx, name);
498 }
499 }
500
501 fn checked_state(&self) -> Option<bool> {
504 self.inner().and_then(|i| i.checked_state())
505 }
506
507 fn change_args(&self, name: &str, value: &PropValue) -> Option<EventArgs> {
514 match self.inner() {
515 Some(inner) => inner.change_args(name, value),
516 None => Some(EventArgs::Value(value.clone())),
517 }
518 }
519
520 fn paint_only_property(&self, name: &str) -> bool {
525 self.inner().is_some_and(|inner| inner.paint_only_property(name))
526 }
527
528 fn timed(&self) -> Option<&dyn Timed> {
530 self.inner().and_then(|i| i.timed())
531 }
532
533 fn timed_mut(&mut self) -> Option<&mut dyn Timed> {
534 self.inner_mut().and_then(|i| i.timed_mut())
535 }
536
537 fn layout_hooks(&self) -> Option<&dyn LayoutHooks> {
540 self.inner().and_then(|i| i.layout_hooks())
541 }
542
543 fn child_pointer_mut(&mut self) -> Option<&mut dyn ChildPointer> {
546 self.inner_mut().and_then(|i| i.child_pointer_mut())
547 }
548
549 fn text_caret(&self) -> Option<&dyn TextCaret> {
552 self.inner().and_then(|i| i.text_caret())
553 }
554
555 fn popup(&self) -> Option<&dyn Popup> {
558 self.inner().and_then(|i| i.popup())
559 }
560
561 fn popup_mut(&mut self) -> Option<&mut dyn Popup> {
562 self.inner_mut().and_then(|i| i.popup_mut())
563 }
564
565 fn accessible_items(&self) -> Option<&dyn AccessibleItems> {
568 self.inner().and_then(|i| i.accessible_items())
569 }
570
571 fn accessible_items_mut(&mut self) -> Option<&mut dyn AccessibleItems> {
572 self.inner_mut().and_then(|i| i.accessible_items_mut())
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 AccessibleItems {
622 fn access_items(&self, _ui: &crate::Ui, _id: ComponentId) -> Vec<crate::automation::AccessItem> {
625 Vec::new()
626 }
627
628 fn access_action(&mut self, _ctx: &mut EventCtx<'_>, _index: usize, _action: crate::automation::ItemAction) -> bool {
631 false
632 }
633
634 fn access_table_size(&self, _ui: &crate::Ui) -> Option<(usize, usize)> {
638 None
639 }
640}
641
642pub trait TextCaret {
646 fn caret_rect(&self, ui: &crate::Ui) -> Option<RectF>;
648}
649
650pub trait Popup {
655 fn closes_on_outside_press(&self) -> bool {
662 false
663 }
664
665 fn dismissed(&mut self, _ctx: &mut EventCtx<'_>) -> bool {
671 false
672 }
673
674 fn dies_with(&self) -> Option<ComponentId> {
678 None
679 }
680}
681
682pub trait LayoutHooks {
686 fn arrange(&self, _area: RectF, _children: &[ArrangeItem]) -> Option<Vec<RectF>> {
691 None
692 }
693
694 fn baseline(&self, _ui: &crate::Ui, _id: ComponentId, _height: f32) -> Option<f32> {
698 None
699 }
700
701 fn dock_gap(&self) -> f32 {
704 0.0
705 }
706
707 fn auto_height(&self, _ui: &crate::Ui, _id: ComponentId, _width: f32) -> Option<f32> {
711 None
712 }
713
714 fn auto_width(&self, _ui: &crate::Ui, _id: ComponentId) -> Option<f32> {
718 None
719 }
720}
721
722pub trait Timed {
727 fn timer_interval(&self) -> Option<f64> {
730 None
731 }
732
733 fn timer_precision(&self) -> crate::TimerPrecision {
735 crate::TimerPrecision::default()
736 }
737
738 fn timer_background(&self) -> bool {
741 false
742 }
743
744 fn timer_late(&mut self, _late: f64) {}
746
747 fn frame(&mut self, _ctx: &mut EventCtx<'_>) -> bool {
751 false
752 }
753
754 fn timer(&mut self, ctx: &mut EventCtx<'_>) {
757 ctx.fire("on_timer", EventArgs::None);
758 }
759}
760
761pub trait ChildPointer {
772 fn child_pressed(&mut self, ctx: &mut EventCtx<'_>, child: ComponentId, event: &PointerEvent);
775 fn child_clicked(&mut self, ctx: &mut EventCtx<'_>, child: ComponentId);
777}
778
779pub trait DragDrop {
780 fn drag_item(&self, _ui: &crate::Ui) -> Option<(String, String)> {
785 None
786 }
787
788 fn drag_visual(&self, _ui: &crate::Ui, _source: ComponentId) -> Option<crate::DragVisual> {
791 None
792 }
793
794 fn drop_accepts(&self, _ui: &crate::Ui, _id: ComponentId, _data: &str) -> bool {
799 false
800 }
801
802 fn drop_hover(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
805
806 fn drop_leave(&mut self, _ctx: &mut EventCtx<'_>) {}
808
809 fn drop_receive(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
811
812 fn drag_ended(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _end: crate::DragEnd) {}
814}
815
816pub trait PlatformHooks {
820 fn taskbar(&self) -> Option<crate::taskbar::TaskbarSpec> {
822 None
823 }
824
825 fn tray(&self) -> Option<crate::tray::TraySpec> {
827 None
828 }
829
830 fn native_view(&self) -> Option<NativeView> {
834 None
835 }
836
837 fn take_native_commands(&self) -> Vec<NativeCommand> {
839 Vec::new()
840 }
841
842 fn connection(&self) -> Option<Connection> {
846 None
847 }
848
849 fn files_dropped(&mut self, _ctx: &mut EventCtx<'_>, _paths: &[String]) -> bool {
852 false
853 }
854
855 fn files_hovering(&mut self, _ctx: &mut EventCtx<'_>, _paths: Option<&[String]>) -> bool {
858 false
859 }
860}
861
862pub trait DesignHooks {
866 fn design_grip(&self, _local: PointF) -> Option<usize> {
869 None
870 }
871
872 fn design_drag(&mut self, _grip: usize, _delta: PointF, _begin: bool) -> Option<(Option<ComponentId>, &'static str, PropValue)> {
876 None
877 }
878}
879
880#[derive(Clone, Copy, Debug, PartialEq)]
882pub struct ArrangeItem {
883 pub id: ComponentId,
884 pub size: SizeF,
885 pub margins: Bounds,
887 pub span: u32,
889 pub row_span: u32,
891 pub cell: Option<(u32, u32)>,
893 pub grow: f32,
895 pub baseline: Option<f32>,
897 pub spacing: f32,
900}
901
902impl ArrangeItem {
903 pub fn of(id: ComponentId, cb: &ControlBase) -> Self {
905 ArrangeItem {
906 id,
907 size: SizeF::new(cb.width(), cb.height()),
908 margins: cb.margins(),
909 span: cb.column_span(),
910 row_span: cb.row_span(),
911 cell: cb.grid_cell(),
912 grow: cb.grow(),
913 baseline: None,
914 spacing: cb.spacing_scale(),
915 }
916 }
917}
918
919#[derive(Clone, Debug, PartialEq)]
921pub enum NativeView {
922 Web { url: String, html: String, generation: u64, navigation: Vec<String>, sandbox: String, background: Option<crate::Color>, data_dir: String, browser_args: String },
931}
932
933#[derive(Clone, Debug, PartialEq)]
937pub struct WebMessage {
938 pub text: String,
939 pub url: String,
940}
941
942pub fn navigation_allowed(allowed: &[String], url: &str) -> bool {
946 allowed.is_empty()
947 || url.starts_with("about:")
948 || url.starts_with("data:")
949 || allowed.iter().any(|a| {
950 let a = a.trim_end_matches('/');
951 url == a || url.starts_with(&format!("{a}/")) || url.starts_with(&format!("{a}?")) || (a.matches('/').count() <= 2 && url_origin(url) == url_origin(a))
952 })
953}
954
955pub fn url_origin(url: &str) -> String {
957 let Some((scheme, rest)) = url.split_once("://") else { return url.to_string() };
958 let host = rest.split(['/', '?', '#']).next().unwrap_or_default();
959 format!("{}://{}", scheme.to_ascii_lowercase(), host.to_ascii_lowercase())
960}
961
962#[derive(Clone, Copy, Debug, PartialEq)]
965pub struct MethodInfo {
966 pub name: &'static str,
967 pub args: &'static [PropKind],
968 pub returns: Option<PropKind>,
969 pub params: &'static [&'static str],
972 pub arg_types: &'static [&'static str],
975 pub returns_type: &'static str,
976}
977
978impl MethodInfo {
979 pub const fn new(name: &'static str, args: &'static [PropKind], returns: Option<PropKind>) -> Self {
980 Self { name, args, returns, params: &[], arg_types: &[], returns_type: "" }
981 }
982
983 pub const fn types(self, args: &'static [&'static str], returns: &'static str) -> Self {
985 Self { arg_types: args, returns_type: returns, ..self }
986 }
987
988 pub const fn params(self, names: &'static [&'static str]) -> Self {
991 Self { params: names, ..self }
992 }
993}
994
995#[derive(Clone, Debug, PartialEq)]
997pub enum NativeCommand {
998 Eval(String),
1000 Back,
1001 Forward,
1002 Reload,
1003}
1004
1005#[derive(Clone, Debug, PartialEq)]
1007pub struct NativeViewSlot {
1008 pub id: ComponentId,
1009 pub rect: crate::RectF,
1010 pub clip: Option<crate::RectF>,
1013 pub visible: bool,
1014 pub view: NativeView,
1015 pub commands: Vec<NativeCommand>,
1016}
1017
1018pub fn prop_infos_of(component: &dyn Component) -> Vec<PropInfo> {
1024 let mut out: Vec<PropInfo> = Vec::new();
1025 let mut seen = std::collections::HashSet::new();
1028 let mut push = |info: &PropInfo| {
1029 if seen.insert(info.name) {
1030 out.push(*info);
1031 }
1032 };
1033 let mut layer = Some(component);
1034 while let Some(l) = layer {
1035 l.props().iter().for_each(&mut push);
1036 layer = l.inner();
1037 }
1038 if component.control().is_some() {
1039 ControlBase::PROPS.iter().for_each(&mut push);
1040 }
1041 out
1042}
1043
1044pub fn prop_info_of(component: &dyn Component, name: &str) -> Option<PropInfo> {
1047 let mut layer = Some(component);
1048 while let Some(l) = layer {
1049 if let Some(info) = l.props().iter().find(|p| p.name == name) {
1050 return Some(*info);
1051 }
1052 layer = l.inner();
1053 }
1054 if component.control().is_some() {
1055 return ControlBase::PROPS.iter().find(|p| p.name == name).copied();
1056 }
1057 None
1058}
1059
1060pub const VALUE_PROPERTIES: [&str; 19] = [
1065 "text", "checked", "value", "item_index", "selected", "date", "end_date", "time", "color", "files", "html", "index", "page", "hotkey",
1066 "angle", "pressed", "position", "low", "high",
1067];
1068
1069pub fn is_value_property(info: &PropInfo) -> bool {
1073 match info.name {
1074 "tab_index" | "expanded" => true,
1075 name => matches!(info.category, "Data" | "Runtime") && VALUE_PROPERTIES.contains(&name),
1076 }
1077}
1078
1079pub fn read_prop(component: &dyn Component, name: &str) -> Option<PropValue> {
1080 let mut layer = Some(component);
1081 while let Some(l) = layer {
1082 if let Some(v) = l.get_prop(name) {
1083 return Some(v);
1084 }
1085 layer = l.inner();
1086 }
1087 component.control().and_then(|cb| cb.get_prop(name))
1088}
1089
1090#[derive(Clone, Copy, Debug, PartialEq)]
1093pub(crate) struct AnchorRef {
1094 pub(crate) parent: SizeF,
1095 pub(crate) rect: RectF,
1096}
1097
1098#[derive(Clone, Copy, Debug, PartialEq)]
1100pub(crate) struct Follow {
1101 pub(crate) target: ComponentId,
1102 pub(crate) at: Option<PointF>,
1104 pub(crate) hidden: bool,
1107}
1108
1109#[derive(Clone, Debug)]
1114pub struct ControlBase {
1115 left: f32,
1117 top: f32,
1119 width: f32,
1121 height: f32,
1123 margins: Bounds,
1125 padding: Bounds,
1127 align: Align,
1129 anchors: Anchors,
1131 visible: bool,
1133 enabled: bool,
1135 hit_test: bool,
1137 clip_children: bool,
1139 inherit_state: bool,
1142 translatable: bool,
1146 captures_wheel: bool,
1149 captures_gesture: bool,
1152 can_focus: bool,
1154 focus_declined: bool,
1157 draggable: bool,
1159 accepts_drops: bool,
1161 accepts_files: bool,
1164 captures_zoom_wheel: bool,
1166 drag: Option<Box<DragDecl>>,
1169 tab_stop: bool,
1171 pan_on_touch: bool,
1173 hover_within: bool,
1176 pub(crate) popup_menu: Option<ComponentId>,
1178 cursor: CursorShape,
1180 opacity: f32,
1182 blend_mode: crate::BlendMode,
1185 rotation: f32,
1187 scale: f32,
1192 translate: PointF,
1194 cache_layer: bool,
1197 aspect_ratio: f32,
1199 max_width: f32,
1201 min_width: f32,
1203 min_height: f32,
1205 max_height: f32,
1207 self_align: SelfAlign,
1209 auto_size: bool,
1211 auto_width: bool,
1214 column_span: u32,
1216 grid_row: i16,
1219 grid_column: i16,
1221 row_span: u16,
1223 grow: f32,
1226 pub(crate) text_scale: f32,
1229 pub(crate) spacing_scale: f32,
1230 shadow: Option<crate::canvas::Shadow>,
1232 tag: i64,
1234 rare: Option<Box<RareTexts>>,
1237 style: String,
1239 theme: String,
1241 hint: String,
1243 state: ControlState,
1244 pub(crate) bounds: RectF,
1245 pub(crate) anchor_ref: Option<AnchorRef>,
1246 pub(crate) follow: Option<Follow>,
1248 pub(crate) design_hidden: bool,
1251 pub(crate) design_only: bool,
1253 pub(crate) keep_left_to_right: bool,
1256 pub(crate) colors: ColorOverride,
1258 pub(crate) font: FontOverride,
1260 scroll_offset: PointF,
1262 pub(crate) laid_out_scroll: PointF,
1265 pub(crate) safe_area: Bounds,
1267 pub(crate) content_size: SizeF,
1269}
1270
1271impl Default for ControlBase {
1272 fn default() -> Self {
1273 Self::new(100.0, 100.0)
1274 }
1275}
1276
1277impl ControlBase {
1278 pub const PROPS: &'static [PropInfo] = &[
1280 PropInfo::new("left", PropKind::Float, "Layout"),
1281 PropInfo::new("top", PropKind::Float, "Layout"),
1282 PropInfo::new("width", PropKind::Float, "Layout"),
1283 PropInfo::new("height", PropKind::Float, "Layout"),
1284 PropInfo::new("align", PropKind::Enum(<Align as crate::prop::PropEnum>::NAMES), "Layout").rust_type("Align"),
1285 PropInfo::new("anchors", PropKind::Set(&Anchors::NAMES), "Layout").rust_type("Anchors"),
1286 PropInfo::new("margins", PropKind::Bounds, "Layout"),
1287 PropInfo::new("padding", PropKind::Bounds, "Layout"),
1288 PropInfo::new("visible", PropKind::Bool, "Behavior"),
1289 PropInfo::new("enabled", PropKind::Bool, "Behavior"),
1290 PropInfo::new("hit_test", PropKind::Bool, "Behavior"),
1291 PropInfo::new("clip_children", PropKind::Bool, "Behavior"),
1292 PropInfo::new("inherit_state", PropKind::Bool, "Appearance"),
1293 PropInfo::new("translate", PropKind::Bool, "Behavior"),
1294 PropInfo::new("captures_wheel", PropKind::Bool, "Behavior"),
1295 PropInfo::new("captures_gesture", PropKind::Bool, "Behavior"),
1296 PropInfo::new("can_focus", PropKind::Bool, "Behavior"),
1297 PropInfo::new("draggable", PropKind::Bool, "Behavior"),
1298 PropInfo::new("accepts_drops", PropKind::Bool, "Behavior"),
1299 PropInfo::new("accepts_files", PropKind::Bool, "Behavior"),
1300 PropInfo::new("captures_zoom_wheel", PropKind::Bool, "Behavior"),
1301 PropInfo::new("drag_data", PropKind::String, "Behavior"),
1302 PropInfo::new("drag_label", PropKind::String, "Behavior"),
1303 PropInfo::new("drag_image", PropKind::Component, "Behavior"),
1304 PropInfo::new("drop_types", PropKind::String, "Behavior"),
1305 PropInfo::new("tab_stop", PropKind::Bool, "Behavior"),
1306 PropInfo::new("cursor", PropKind::Enum(<CursorShape as crate::prop::PropEnum>::NAMES), "Behavior").rust_type("CursorShape"),
1307 PropInfo::new("opacity", PropKind::Float, "Appearance"),
1308 PropInfo::new("blend_mode", PropKind::Enum(<crate::BlendMode as crate::prop::PropEnum>::NAMES), "Appearance").rust_type("BlendMode"),
1309 PropInfo::new("cache_layer", PropKind::Bool, "Appearance"),
1310 PropInfo::new("rotation", PropKind::Angle, "Layout"),
1311 PropInfo::new("scale", PropKind::Float, "Layout"),
1312 PropInfo::new("translate_x", PropKind::Float, "Appearance"),
1313 PropInfo::new("translate_y", PropKind::Float, "Appearance"),
1314 PropInfo::new("aspect_ratio", PropKind::Float, "Layout"),
1315 PropInfo::new("max_width", PropKind::Float, "Layout"),
1316 PropInfo::new("min_width", PropKind::Float, "Layout"),
1317 PropInfo::new("min_height", PropKind::Float, "Layout"),
1318 PropInfo::new("max_height", PropKind::Float, "Layout"),
1319 PropInfo::new("self_align", PropKind::Enum(<SelfAlign as crate::prop::PropEnum>::NAMES), "Layout").rust_type("SelfAlign"),
1320 PropInfo::new("auto_size", PropKind::Bool, "Layout"),
1321 PropInfo::new("auto_width", PropKind::Bool, "Layout"),
1322 PropInfo::new("column_span", PropKind::Int, "Layout"),
1323 PropInfo::new("grid_row", PropKind::Int, "Layout"),
1324 PropInfo::new("grid_column", PropKind::Int, "Layout"),
1325 PropInfo::new("row_span", PropKind::Int, "Layout"),
1326 PropInfo::new("grow", PropKind::Float, "Layout"),
1327 PropInfo::new("shadow", PropKind::Shadow, "Appearance"),
1328 PropInfo::new("style", PropKind::StyleName, "Appearance"),
1329 PropInfo::new("theme", PropKind::ThemeName, "Appearance"),
1330 PropInfo::new("fill", PropKind::Color, "Appearance"),
1331 PropInfo::new("border_color", PropKind::Color, "Appearance"),
1332 PropInfo::new("text_color", PropKind::Color, "Appearance"),
1333 PropInfo::new("accent_color", PropKind::Color, "Appearance"),
1334 PropInfo::new("success_color", PropKind::Color, "Appearance"),
1335 PropInfo::new("warning_color", PropKind::Color, "Appearance"),
1336 PropInfo::new("danger_color", PropKind::Color, "Appearance"),
1337 PropInfo::new("font_family", PropKind::String, "Font"),
1338 PropInfo::new("font_size", PropKind::Float, "Font"),
1339 PropInfo::new("font_weight", PropKind::Enum(&FONT_WEIGHTS), "Font"),
1340 PropInfo::new("font_italic", PropKind::Bool, "Font"),
1341 PropInfo::new("font_underline", PropKind::Bool, "Font"),
1342 PropInfo::new("font_strikeout", PropKind::Bool, "Font"),
1343 PropInfo::new("hint", PropKind::String, "Behavior"),
1344 PropInfo::new("tag", PropKind::Int, "Misc"),
1345 PropInfo::new("tag_string", PropKind::String, "Misc"),
1346 PropInfo::new("accessible_name", PropKind::String, "Behavior"),
1347 PropInfo::new("popup_menu", PropKind::Component, "Behavior"),
1348 PropInfo::new("hover_card", PropKind::Component, "Behavior"),
1349 PropInfo::new("hover_delay", PropKind::Duration, "Behavior"),
1350 PropInfo::new("on_click", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1351 PropInfo::new("on_dbl_click", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1352 PropInfo::new("on_long_press", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1353 PropInfo::new("on_pointer_down", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1354 PropInfo::new("on_pointer_up", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1355 PropInfo::new("on_pointer_move", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1356 PropInfo::new("on_pointer_enter", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1357 PropInfo::new("on_pointer_leave", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1358 PropInfo::new("on_wheel", PropKind::Event, "Events").event_arg(crate::EventArg::Wheel),
1359 PropInfo::new("on_gesture", PropKind::Event, "Events").event_arg(crate::EventArg::Gesture),
1360 PropInfo::new("on_swipe", PropKind::Event, "Events").event_arg(crate::EventArg::Swipe),
1361 PropInfo::new("on_key_down", PropKind::Event, "Events").event_arg(crate::EventArg::Key),
1364 PropInfo::new("on_key_press", PropKind::Event, "Events"),
1365 PropInfo::new("on_key_up", PropKind::Event, "Events").event_arg(crate::EventArg::Key),
1366 PropInfo::new("on_enter", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1367 PropInfo::new("on_exit", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1368 PropInfo::new("on_resize", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1369 PropInfo::new("on_drag_start", PropKind::Event, "Events"),
1370 PropInfo::new("on_drag_over", PropKind::Event, "Events"),
1371 PropInfo::new("on_drag_move", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1372 PropInfo::new("on_drag_leave", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::String)),
1373 PropInfo::new("on_drag_drop", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::String)),
1374 PropInfo::new("on_drag_end", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::Bool)),
1375 PropInfo::new("on_files_enter", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::List)).rust_type("Vec<String>"),
1376 PropInfo::new("on_files_over", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1377 PropInfo::new("on_files_leave", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1378 PropInfo::new("on_files_drop", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::List)).rust_type("Vec<String>"),
1379 ];
1380
1381 pub fn new(width: f32, height: f32) -> Self {
1382 Self {
1383 left: 0.0,
1384 top: 0.0,
1385 width,
1386 height,
1387 margins: Bounds::ZERO,
1388 padding: Bounds::ZERO,
1389 align: Align::None,
1390 anchors: Anchors::default(),
1391 visible: true,
1392 enabled: true,
1393 hit_test: true,
1394 clip_children: false,
1395 inherit_state: false,
1396 translatable: true,
1397 captures_wheel: false,
1398 captures_gesture: false,
1399 can_focus: false,
1400 focus_declined: false,
1401 draggable: false,
1402 accepts_drops: false,
1403 accepts_files: false,
1404 captures_zoom_wheel: false,
1405 drag: None,
1406 tab_stop: true,
1407 pan_on_touch: true,
1408 hover_within: false,
1409 popup_menu: None,
1410 cursor: CursorShape::Default,
1411 opacity: 1.0,
1412 blend_mode: crate::BlendMode::Normal,
1413 rotation: 0.0,
1414 scale: 1.0,
1415 translate: PointF::ZERO,
1416 cache_layer: false,
1417 aspect_ratio: 0.0,
1418 max_width: 0.0,
1419 min_width: 0.0,
1420 min_height: 0.0,
1421 max_height: 0.0,
1422 self_align: SelfAlign::Stretch,
1423 auto_size: false,
1424 auto_width: false,
1425 column_span: 1,
1426 grid_row: -1,
1427 grid_column: -1,
1428 row_span: 1,
1429 grow: 0.0,
1430 text_scale: 1.0,
1431 spacing_scale: 1.0,
1432 shadow: None,
1433 tag: 0,
1434 rare: None,
1435 style: String::new(),
1436 theme: String::new(),
1437 hint: String::new(),
1438 state: ControlState::default(),
1439 bounds: RectF::new(0.0, 0.0, width, height),
1440 anchor_ref: None,
1441 follow: None,
1442 design_hidden: false,
1443 design_only: false,
1444 keep_left_to_right: false,
1445 colors: ColorOverride::default(),
1446 font: FontOverride::default(),
1447 scroll_offset: PointF::ZERO,
1448 laid_out_scroll: PointF::ZERO,
1449 content_size: SizeF::ZERO,
1450 safe_area: Bounds::default(),
1451 }
1452 }
1453
1454 pub fn left(&self) -> f32 {
1457 self.left
1458 }
1459
1460 pub fn top(&self) -> f32 {
1461 self.top
1462 }
1463
1464 pub fn width(&self) -> f32 {
1465 self.width
1466 }
1467
1468 pub fn height(&self) -> f32 {
1469 self.height
1470 }
1471
1472 pub fn position(&self) -> PointF {
1473 PointF::new(self.left, self.top)
1474 }
1475
1476 pub fn size(&self) -> SizeF {
1477 SizeF::new(self.width, self.height)
1478 }
1479
1480 pub fn margins(&self) -> Bounds {
1483 if self.spacing_scale == 1.0 { self.margins } else { self.margins.scaled(self.spacing_scale) }
1484 }
1485
1486 pub fn padding(&self) -> Bounds {
1488 if self.spacing_scale == 1.0 { self.padding } else { self.padding.scaled(self.spacing_scale) }
1489 }
1490
1491 pub fn align(&self) -> Align {
1492 self.align
1493 }
1494
1495 pub fn anchors(&self) -> Anchors {
1496 self.anchors
1497 }
1498
1499 pub fn visible(&self) -> bool {
1500 self.visible
1501 }
1502
1503 pub fn design_only(&self) -> bool {
1505 self.design_only
1506 }
1507
1508 pub fn colors(&self) -> ColorOverride {
1510 self.colors
1511 }
1512
1513 pub fn set_colors(&mut self, colors: ColorOverride) -> &mut Self {
1514 self.colors = colors;
1515 self
1516 }
1517
1518 pub fn set_design_only(&mut self, on: bool) -> &mut Self {
1519 self.design_only = on;
1520 self
1521 }
1522
1523 pub fn keep_left_to_right(&self) -> bool {
1525 self.keep_left_to_right
1526 }
1527
1528 pub fn set_keep_left_to_right(&mut self, on: bool) -> &mut Self {
1529 self.keep_left_to_right = on;
1530 self
1531 }
1532
1533 pub fn enabled(&self) -> bool {
1534 self.enabled
1535 }
1536
1537 pub fn hit_test(&self) -> bool {
1538 self.hit_test
1539 }
1540
1541 pub fn translatable(&self) -> bool {
1542 self.translatable
1543 }
1544
1545 pub fn set_translatable(&mut self, v: bool) -> &mut Self {
1546 self.translatable = v;
1547 self
1548 }
1549
1550 pub fn inherit_state(&self) -> bool {
1551 self.inherit_state
1552 }
1553
1554 pub fn set_inherit_state(&mut self, v: bool) -> &mut Self {
1555 self.inherit_state = v;
1556 self
1557 }
1558
1559 pub fn clip_children(&self) -> bool {
1560 self.clip_children
1561 }
1562
1563 pub fn captures_wheel(&self) -> bool {
1564 self.captures_wheel
1565 }
1566
1567 pub fn captures_gesture(&self) -> bool {
1568 self.captures_gesture
1569 }
1570
1571 pub fn draggable(&self) -> bool {
1576 self.draggable
1577 }
1578
1579 pub fn set_draggable(&mut self, v: bool) -> &mut Self {
1580 self.draggable = v;
1581 self
1582 }
1583
1584 pub fn accepts_drops(&self) -> bool {
1587 self.accepts_drops
1588 }
1589
1590 pub fn set_accepts_drops(&mut self, v: bool) -> &mut Self {
1591 self.accepts_drops = v;
1592 self
1593 }
1594
1595 pub fn accepts_files(&self) -> bool {
1599 self.accepts_files
1600 }
1601
1602 pub fn set_accepts_files(&mut self, v: bool) -> &mut Self {
1603 self.accepts_files = v;
1604 self
1605 }
1606
1607 pub fn captures_zoom_wheel(&self) -> bool {
1609 self.captures_zoom_wheel
1610 }
1611
1612 pub fn set_captures_zoom_wheel(&mut self, v: bool) -> &mut Self {
1613 self.captures_zoom_wheel = v;
1614 self
1615 }
1616
1617 pub fn drag_data(&self) -> &str {
1619 self.drag.as_ref().map_or("", |d| d.data.as_str())
1620 }
1621
1622 pub fn drag_label(&self) -> &str {
1623 self.drag.as_ref().map_or("", |d| d.label.as_str())
1624 }
1625
1626 pub fn drag_image(&self) -> Option<ComponentId> {
1628 self.drag.as_ref().and_then(|d| d.image)
1629 }
1630
1631 pub(crate) fn forget_drag_image(&mut self, doomed: &[ComponentId]) {
1633 if let Some(d) = self.drag.as_mut().filter(|d| d.image.is_some_and(|i| doomed.contains(&i))) {
1634 d.image = None;
1635 }
1636 }
1637
1638 pub fn drop_types(&self) -> &str {
1640 self.drag.as_ref().map_or("", |d| d.drop_types.as_str())
1641 }
1642
1643 fn drag_decl(&mut self) -> &mut DragDecl {
1644 self.drag.get_or_insert_with(Default::default)
1645 }
1646
1647 pub fn can_focus(&self) -> bool {
1648 self.can_focus
1649 }
1650
1651 pub fn focus_declined(&self) -> bool {
1654 self.focus_declined
1655 }
1656
1657 pub fn tab_stop(&self) -> bool {
1659 self.tab_stop
1660 }
1661
1662 pub fn popup_menu(&self) -> Option<ComponentId> {
1664 self.popup_menu
1665 }
1666
1667 pub fn set_popup_menu(&mut self, menu: Option<ComponentId>) -> &mut Self {
1668 self.popup_menu = menu;
1669 self
1670 }
1671
1672 pub fn pan_on_touch(&self) -> bool {
1674 self.pan_on_touch
1675 }
1676
1677 pub fn hover_within(&self) -> bool {
1679 self.hover_within
1680 }
1681
1682 pub fn set_hover_within(&mut self, v: bool) -> &mut Self {
1686 self.hover_within = v;
1687 self
1688 }
1689
1690 pub fn cursor(&self) -> CursorShape {
1692 self.cursor
1693 }
1694
1695 pub fn opacity(&self) -> f32 {
1696 self.opacity
1697 }
1698
1699 pub fn blend_mode(&self) -> crate::BlendMode {
1701 self.blend_mode
1702 }
1703
1704 pub fn set_blend_mode(&mut self, mode: crate::BlendMode) -> &mut Self {
1705 self.blend_mode = mode;
1706 self
1707 }
1708
1709 pub fn cache_layer(&self) -> bool {
1711 self.cache_layer
1712 }
1713
1714 pub fn set_cache_layer(&mut self, on: bool) -> &mut Self {
1715 self.cache_layer = on;
1716 self
1717 }
1718
1719 pub fn rotation(&self) -> f32 {
1722 self.rotation
1723 }
1724
1725 pub fn scale(&self) -> f32 {
1727 self.scale
1728 }
1729
1730 pub fn inner_size(&self) -> SizeF {
1733 let s = self.scale;
1734 if s == 1.0 { self.bounds.size() } else { SizeF::new(self.bounds.width / s, self.bounds.height / s) }
1735 }
1736
1737 pub fn shadow(&self) -> Option<crate::canvas::Shadow> {
1738 self.shadow
1739 }
1740
1741 pub fn aspect_ratio(&self) -> f32 {
1745 self.aspect_ratio
1746 }
1747
1748 pub fn set_aspect_ratio(&mut self, ratio: f32) -> &mut Self {
1749 self.aspect_ratio = if ratio.is_finite() { ratio.max(0.0) } else { 0.0 };
1750 self
1751 }
1752
1753 pub fn max_width(&self) -> f32 {
1757 self.max_width
1758 }
1759
1760 pub fn set_max_width(&mut self, width: f32) -> &mut Self {
1761 self.max_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1762 self
1763 }
1764
1765 pub fn min_width(&self) -> f32 {
1770 self.min_width
1771 }
1772
1773 pub fn set_min_width(&mut self, width: f32) -> &mut Self {
1774 self.min_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1775 self
1776 }
1777
1778 pub fn min_height(&self) -> f32 {
1780 self.min_height
1781 }
1782
1783 pub fn set_min_height(&mut self, height: f32) -> &mut Self {
1784 self.min_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1785 self
1786 }
1787
1788 pub fn max_height(&self) -> f32 {
1790 self.max_height
1791 }
1792
1793 pub fn set_max_height(&mut self, height: f32) -> &mut Self {
1794 self.max_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1795 self
1796 }
1797
1798 pub fn clamp_width(&self, available: f32) -> f32 {
1801 let w = if self.max_width > 0.0 { available.min(self.max_width) } else { available };
1802 w.max(self.min_width)
1803 }
1804
1805 pub fn clamp_height(&self, height: f32) -> f32 {
1807 let h = if self.max_height > 0.0 { height.min(self.max_height) } else { height };
1808 h.max(self.min_height)
1809 }
1810
1811 pub fn self_align(&self) -> SelfAlign {
1814 self.self_align
1815 }
1816
1817 pub fn set_self_align(&mut self, align: SelfAlign) -> &mut Self {
1818 self.self_align = align;
1819 self
1820 }
1821
1822 pub fn band_width(&self, available: f32) -> f32 {
1826 match self.self_align {
1827 SelfAlign::Stretch => self.clamp_width(available),
1828 _ if self.max_width > 0.0 => self.clamp_width(available),
1829 _ => self.clamp_width(self.width),
1830 }
1831 }
1832
1833 pub fn band_height(&self, available: f32) -> f32 {
1836 match self.self_align {
1837 SelfAlign::Stretch => self.clamp_height(available),
1838 _ if self.max_height > 0.0 => self.clamp_height(available),
1839 _ => self.clamp_height(self.height),
1840 }
1841 }
1842
1843 pub fn band_offset(&self, available: f32, size: f32) -> f32 {
1846 let spare = (available - size).max(0.0);
1847 match self.self_align {
1848 SelfAlign::Start => 0.0,
1849 SelfAlign::End => spare,
1850 SelfAlign::Stretch | SelfAlign::Center => spare / 2.0,
1851 }
1852 }
1853
1854 pub fn auto_size(&self) -> bool {
1859 self.auto_size
1860 }
1861
1862 pub fn set_auto_size(&mut self, on: bool) -> &mut Self {
1863 self.auto_size = on;
1864 self
1865 }
1866
1867 pub fn auto_width(&self) -> bool {
1871 self.auto_width
1872 }
1873
1874 pub fn set_auto_width(&mut self, on: bool) -> &mut Self {
1875 self.auto_width = on;
1876 self
1877 }
1878
1879 pub fn column_span(&self) -> u32 {
1882 self.column_span.max(1)
1883 }
1884
1885 pub fn grid_cell(&self) -> Option<(u32, u32)> {
1887 (self.grid_row >= 0 && self.grid_column >= 0).then(|| (self.grid_row as u32, self.grid_column as u32))
1888 }
1889
1890 pub fn set_grid_cell(&mut self, cell: Option<(u32, u32)>) -> &mut Self {
1891 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));
1892 self.grid_row = r;
1893 self.grid_column = c;
1894 self
1895 }
1896
1897 pub fn row_span(&self) -> u32 {
1899 u32::from(self.row_span.max(1))
1900 }
1901
1902 pub fn set_row_span(&mut self, span: u32) -> &mut Self {
1903 self.row_span = span.clamp(1, 64) as u16;
1904 self
1905 }
1906
1907 pub fn grow(&self) -> f32 {
1908 self.grow
1909 }
1910
1911 pub fn set_grow(&mut self, grow: f32) -> &mut Self {
1912 self.grow = grow.max(0.0);
1913 self
1914 }
1915
1916 pub fn set_column_span(&mut self, span: u32) -> &mut Self {
1917 self.column_span = span.max(1);
1918 self
1919 }
1920
1921 pub fn text_scale(&self) -> f32 {
1923 self.text_scale
1924 }
1925
1926 pub fn spacing_scale(&self) -> f32 {
1928 self.spacing_scale
1929 }
1930
1931 pub fn tag(&self) -> i64 {
1932 self.tag
1933 }
1934
1935 pub fn accessible_name(&self) -> &str {
1938 self.rare.as_ref().map_or("", |r| &r.accessible_name)
1939 }
1940
1941 pub fn set_accessible_name(&mut self, v: &str) -> &mut Self {
1942 self.rare_mut(|r| r.accessible_name = v.into());
1943 self
1944 }
1945
1946 fn rare_mut(&mut self, change: impl FnOnce(&mut RareTexts)) {
1947 let rare = self.rare.get_or_insert_with(Default::default);
1948 change(rare);
1949 if rare.tag_string.is_empty() && rare.accessible_name.is_empty() && rare.hover_card.is_none() && rare.hover_delay == 0.0 {
1950 self.rare = None;
1951 }
1952 }
1953
1954 pub fn hover_card(&self) -> Option<ComponentId> {
1958 self.rare.as_ref().and_then(|r| r.hover_card)
1959 }
1960
1961 pub fn set_hover_card(&mut self, card: Option<ComponentId>) -> &mut Self {
1962 self.rare_mut(|r| r.hover_card = card);
1963 self
1964 }
1965
1966 pub fn hover_delay(&self) -> f32 {
1969 self.rare.as_ref().map_or(0.0, |r| r.hover_delay)
1970 }
1971
1972 pub fn set_hover_delay(&mut self, ms: f32) -> &mut Self {
1973 self.rare_mut(|r| r.hover_delay = ms.max(0.0));
1974 self
1975 }
1976
1977 pub fn tag_string(&self) -> &str {
1978 self.rare.as_ref().map_or("", |r| &r.tag_string)
1979 }
1980
1981 pub fn style(&self) -> &str {
1983 &self.style
1984 }
1985
1986 pub fn theme_name(&self) -> &str {
1988 &self.theme
1989 }
1990
1991 pub fn set_theme_name(&mut self, v: impl Into<String>) -> &mut Self {
1992 self.theme = v.into();
1993 self
1994 }
1995
1996 pub fn hint(&self) -> &str {
1998 &self.hint
1999 }
2000
2001 pub fn set_hint(&mut self, v: impl Into<String>) -> &mut Self {
2002 self.hint = v.into();
2003 self
2004 }
2005
2006 pub fn state(&self) -> ControlState {
2007 self.state
2008 }
2009
2010 pub fn bounds(&self) -> RectF {
2012 self.bounds
2013 }
2014
2015 pub fn scroll_offset(&self) -> PointF {
2017 self.scroll_offset
2018 }
2019
2020 pub fn laid_out_scroll(&self) -> PointF {
2023 self.laid_out_scroll
2024 }
2025
2026 pub fn set_scroll_offset(&mut self, offset: PointF) -> &mut Self {
2029 self.scroll_offset = offset;
2030 self
2031 }
2032
2033 pub fn content_size(&self) -> SizeF {
2036 self.content_size
2037 }
2038
2039 pub fn set_left(&mut self, v: f32) -> &mut Self {
2046 if self.left != v {
2047 self.left = v;
2048 self.anchor_ref = None;
2049 }
2050 self
2051 }
2052
2053 pub fn set_top(&mut self, v: f32) -> &mut Self {
2054 if self.top != v {
2055 self.top = v;
2056 self.anchor_ref = None;
2057 }
2058 self
2059 }
2060
2061 pub fn set_position(&mut self, left: f32, top: f32) -> &mut Self {
2062 self.set_left(left).set_top(top)
2063 }
2064
2065 pub fn set_width(&mut self, v: f32) -> &mut Self {
2066 if self.width != v.max(0.0) {
2067 self.width = v.max(0.0);
2068 self.anchor_ref = None;
2069 }
2070 self
2071 }
2072
2073 pub fn set_height(&mut self, v: f32) -> &mut Self {
2074 if self.height != v.max(0.0) {
2075 self.height = v.max(0.0);
2076 self.anchor_ref = None;
2077 }
2078 self
2079 }
2080
2081 pub(crate) fn set_measured_width(&mut self, v: f32) {
2083 self.width = v.max(0.0);
2084 if let Some(r) = self.anchor_ref.as_mut() {
2085 r.rect.width = self.width;
2086 }
2087 }
2088
2089 pub(crate) fn set_measured_height(&mut self, v: f32) {
2092 self.height = v.max(0.0);
2093 if let Some(r) = self.anchor_ref.as_mut() {
2094 r.rect.height = self.height;
2095 }
2096 }
2097
2098 pub fn set_size(&mut self, width: f32, height: f32) -> &mut Self {
2099 self.set_width(width).set_height(height)
2100 }
2101
2102 pub fn set_rect(&mut self, left: f32, top: f32, width: f32, height: f32) -> &mut Self {
2104 self.set_position(left, top).set_size(width, height)
2105 }
2106
2107 pub fn set_margins(&mut self, v: impl Into<Bounds>) -> &mut Self {
2108 self.margins = v.into();
2109 self
2110 }
2111
2112 pub fn set_padding(&mut self, v: impl Into<Bounds>) -> &mut Self {
2113 self.padding = v.into();
2114 self
2115 }
2116
2117 pub fn set_align(&mut self, v: Align) -> &mut Self {
2118 self.align = v;
2119 self.anchor_ref = None;
2120 self
2121 }
2122
2123 pub fn set_anchors(&mut self, v: Anchors) -> &mut Self {
2124 self.anchors = v;
2125 self.anchor_ref = None;
2126 self
2127 }
2128
2129 pub fn set_visible(&mut self, v: bool) -> &mut Self {
2130 self.visible = v;
2131 if let Some(f) = &mut self.follow {
2133 f.hidden = false;
2134 }
2135 self
2136 }
2137
2138 pub fn unfollow(&mut self) -> &mut Self {
2142 self.follow = None;
2143 self
2144 }
2145
2146 pub fn set_enabled(&mut self, v: bool) -> &mut Self {
2147 self.enabled = v;
2148 self
2149 }
2150
2151 pub fn set_hit_test(&mut self, v: bool) -> &mut Self {
2152 self.hit_test = v;
2153 self
2154 }
2155
2156 pub fn set_clip_children(&mut self, v: bool) -> &mut Self {
2157 self.clip_children = v;
2158 self
2159 }
2160
2161 pub fn set_captures_wheel(&mut self, v: bool) -> &mut Self {
2162 self.captures_wheel = v;
2163 self
2164 }
2165
2166 pub fn set_captures_gesture(&mut self, v: bool) -> &mut Self {
2167 self.captures_gesture = v;
2168 self
2169 }
2170
2171 pub fn set_can_focus(&mut self, v: bool) -> &mut Self {
2172 self.can_focus = v;
2173 self
2174 }
2175
2176 pub fn set_pan_on_touch(&mut self, v: bool) -> &mut Self {
2180 self.pan_on_touch = v;
2181 self
2182 }
2183
2184 pub fn set_tab_stop(&mut self, v: bool) -> &mut Self {
2185 self.tab_stop = v;
2186 self
2187 }
2188
2189 pub fn set_cursor(&mut self, v: CursorShape) -> &mut Self {
2190 self.cursor = v;
2191 self
2192 }
2193
2194 pub fn set_opacity(&mut self, v: f32) -> &mut Self {
2195 self.opacity = v.clamp(0.0, 1.0);
2196 self
2197 }
2198
2199 pub fn translate(&self) -> PointF {
2201 self.translate
2202 }
2203
2204 pub fn set_translate(&mut self, offset: PointF) -> &mut Self {
2205 self.translate = if offset.x.is_finite() && offset.y.is_finite() { offset } else { PointF::ZERO };
2206 self
2207 }
2208
2209 pub fn set_scale(&mut self, scale: f32) -> &mut Self {
2211 self.scale = if scale.is_finite() && scale > 0.0 { scale.clamp(0.05, 20.0) } else { 1.0 };
2212 self
2213 }
2214
2215 pub fn set_rotation(&mut self, degrees: f32) -> &mut Self {
2216 self.rotation = if degrees.is_finite() { degrees % 360.0 } else { 0.0 };
2217 self
2218 }
2219
2220 pub fn set_shadow(&mut self, shadow: Option<crate::canvas::Shadow>) -> &mut Self {
2221 self.shadow = shadow;
2222 self
2223 }
2224
2225 pub fn set_tag(&mut self, v: i64) -> &mut Self {
2226 self.tag = v;
2227 self
2228 }
2229
2230 pub fn set_tag_string(&mut self, v: &str) -> &mut Self {
2231 self.rare_mut(|r| r.tag_string = v.into());
2232 self
2233 }
2234
2235 pub fn set_style(&mut self, v: impl Into<String>) -> &mut Self {
2236 self.style = v.into();
2237 self
2238 }
2239
2240 pub fn state_mut(&mut self) -> &mut ControlState {
2243 &mut self.state
2244 }
2245
2246 pub fn get_prop(&self, name: &str) -> Option<PropValue> {
2249 Some(match name {
2250 "left" => self.left.into(),
2251 "top" => self.top.into(),
2252 "width" => self.width.into(),
2253 "height" => self.height.into(),
2254 "align" => self.align.into(),
2255 "anchors" => self.anchors.into(),
2256 "margins" => self.margins.into(),
2257 "padding" => self.padding.into(),
2258 "visible" => self.visible.into(),
2259 "enabled" => self.enabled.into(),
2260 "hit_test" => self.hit_test.into(),
2261 "clip_children" => self.clip_children.into(),
2262 "inherit_state" => self.inherit_state.into(),
2263 "translate" => self.translatable.into(),
2264 "captures_wheel" => self.captures_wheel.into(),
2265 "captures_gesture" => self.captures_gesture.into(),
2266 "can_focus" => self.can_focus.into(),
2267 "draggable" => self.draggable.into(),
2268 "accepts_drops" => self.accepts_drops.into(),
2269 "accepts_files" => self.accepts_files.into(),
2270 "captures_zoom_wheel" => self.captures_zoom_wheel.into(),
2271 "drag_data" => self.drag_data().into(),
2272 "drag_label" => self.drag_label().into(),
2273 "drag_image" => PropValue::Component(self.drag_image()),
2274 "drop_types" => self.drop_types().into(),
2275 "tab_stop" => self.tab_stop.into(),
2276 "cursor" => PropValue::enum_value(self.cursor),
2277 "opacity" => self.opacity.into(),
2278 "blend_mode" => PropValue::enum_value(self.blend_mode),
2279 "cache_layer" => self.cache_layer.into(),
2280 "rotation" => self.rotation.into(),
2281 "scale" => self.scale.into(),
2282 "translate_x" => self.translate.x.into(),
2283 "translate_y" => self.translate.y.into(),
2284 "aspect_ratio" => self.aspect_ratio.into(),
2285 "max_width" => self.max_width.into(),
2286 "min_width" => self.min_width.into(),
2287 "min_height" => self.min_height.into(),
2288 "max_height" => self.max_height.into(),
2289 "self_align" => PropValue::enum_value(self.self_align),
2290 "auto_size" => self.auto_size.into(),
2291 "auto_width" => self.auto_width.into(),
2292 "column_span" => (self.column_span() as i64).into(),
2293 "grid_row" => i64::from(self.grid_row).into(),
2294 "grid_column" => i64::from(self.grid_column).into(),
2295 "row_span" => (self.row_span() as i64).into(),
2296 "grow" => f64::from(self.grow).into(),
2297 "shadow" => crate::prop::PropType::to_value(&self.shadow),
2298 "style" => self.style.clone().into(),
2299 "fill" => crate::prop::PropType::to_value(&self.colors.fill),
2300 "border_color" => crate::prop::PropType::to_value(&self.colors.border),
2301 "text_color" => crate::prop::PropType::to_value(&self.colors.text),
2302 "accent_color" => crate::prop::PropType::to_value(&self.colors.accent),
2303 "success_color" => crate::prop::PropType::to_value(&self.colors.success),
2304 "warning_color" => crate::prop::PropType::to_value(&self.colors.warning),
2305 "danger_color" => crate::prop::PropType::to_value(&self.colors.danger),
2306 "font_family" => self.font.family.clone().into(),
2307 "font_size" => crate::prop::PropType::to_value(&self.font.size),
2308 "font_weight" => PropValue::Enum(FONT_WEIGHTS[self.font.weight.map_or(0, |w| w as usize + 1)].into()),
2309 "font_italic" => self.font.italic.into(),
2310 "font_underline" => self.font.underline.into(),
2311 "font_strikeout" => self.font.strikeout.into(),
2312 "theme" => self.theme.clone().into(),
2313 "hint" => self.hint.clone().into(),
2314 "tag" => self.tag.into(),
2315 "tag_string" => self.tag_string().to_string().into(),
2316 "accessible_name" => self.accessible_name().to_string().into(),
2317 "popup_menu" => PropValue::Component(self.popup_menu),
2318 "hover_card" => PropValue::Component(self.hover_card()),
2319 "hover_delay" => (self.hover_delay() as f64).into(),
2320 _ => return None,
2321 })
2322 }
2323
2324 pub fn set_prop(&mut self, name: &str, value: PropValue) -> Result<()> {
2325 match name {
2326 "left" => self.set_left(value.to_f32()?),
2327 "top" => self.set_top(value.to_f32()?),
2328 "width" => self.set_width(value.to_f32()?),
2329 "height" => self.set_height(value.to_f32()?),
2330 "align" => self.set_align(value.to_enum()?),
2331 "anchors" => self.set_anchors(value.to_anchors()?),
2332 "margins" => self.set_margins(value.to_bounds()?),
2333 "padding" => self.set_padding(value.to_bounds()?),
2334 "visible" => self.set_visible(value.to_bool()?),
2335 "enabled" => self.set_enabled(value.to_bool()?),
2336 "hit_test" => self.set_hit_test(value.to_bool()?),
2337 "clip_children" => self.set_clip_children(value.to_bool()?),
2338 "inherit_state" => self.set_inherit_state(value.to_bool()?),
2339 "translate" => self.set_translatable(value.to_bool()?),
2340 "captures_wheel" => self.set_captures_wheel(value.to_bool()?),
2341 "captures_gesture" => self.set_captures_gesture(value.to_bool()?),
2342 "can_focus" => {
2343 let on = value.to_bool()?;
2344 self.focus_declined = !on && (self.can_focus || self.focus_declined);
2345 self.set_can_focus(on)
2346 }
2347 "draggable" => self.set_draggable(value.to_bool()?),
2348 "accepts_drops" => self.set_accepts_drops(value.to_bool()?),
2349 "accepts_files" => self.set_accepts_files(value.to_bool()?),
2350 "captures_zoom_wheel" => self.set_captures_zoom_wheel(value.to_bool()?),
2351 "drag_data" => {
2352 self.drag_decl().data = value.to_str()?.to_string();
2353 self
2354 }
2355 "drag_label" => {
2356 self.drag_decl().label = value.to_str()?.to_string();
2357 self
2358 }
2359 "drag_image" => {
2360 self.drag_decl().image = value.to_component()?;
2361 self
2362 }
2363 "drop_types" => {
2364 self.drag_decl().drop_types = value.to_str()?.to_string();
2365 self
2366 }
2367 "tab_stop" => self.set_tab_stop(value.to_bool()?),
2368 "cursor" => self.set_cursor(value.to_enum()?),
2369 "opacity" => self.set_opacity(value.to_f32()?),
2370 "blend_mode" => self.set_blend_mode(value.to_enum()?),
2371 "cache_layer" => self.set_cache_layer(value.to_bool()?),
2372 "rotation" => self.set_rotation(value.to_f32()?),
2373 "scale" => self.set_scale(value.to_f32()?),
2374 "translate_x" => self.set_translate(PointF::new(value.to_f32()?, self.translate.y)),
2375 "translate_y" => self.set_translate(PointF::new(self.translate.x, value.to_f32()?)),
2376 "aspect_ratio" => self.set_aspect_ratio(value.to_f32()?),
2377 "max_width" => self.set_max_width(value.to_f32()?),
2378 "min_width" => self.set_min_width(value.to_f32()?),
2379 "min_height" => self.set_min_height(value.to_f32()?),
2380 "max_height" => self.set_max_height(value.to_f32()?),
2381 "self_align" => self.set_self_align(value.to_enum()?),
2382 "auto_size" => self.set_auto_size(value.to_bool()?),
2383 "auto_width" => self.set_auto_width(value.to_bool()?),
2384 "column_span" => self.set_column_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2385 "grid_row" => {
2386 self.grid_row = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2387 self
2388 }
2389 "grid_column" => {
2390 self.grid_column = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2391 self
2392 }
2393 "row_span" => self.set_row_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2394 "grow" => self.set_grow(value.to_f32()?),
2395 "shadow" => self.set_shadow(crate::prop::PropType::from_value(value)?),
2396 "style" => self.set_style(value.to_str()?),
2397 "fill" => {
2398 self.colors.fill = crate::prop::PropType::from_value(value)?;
2399 self
2400 }
2401 "border_color" => {
2402 self.colors.border = crate::prop::PropType::from_value(value)?;
2403 self
2404 }
2405 "text_color" => {
2406 self.colors.text = crate::prop::PropType::from_value(value)?;
2407 self
2408 }
2409 "accent_color" => {
2410 self.colors.accent = crate::prop::PropType::from_value(value)?;
2411 self
2412 }
2413 "success_color" => {
2414 self.colors.success = crate::prop::PropType::from_value(value)?;
2415 self
2416 }
2417 "warning_color" => {
2418 self.colors.warning = crate::prop::PropType::from_value(value)?;
2419 self
2420 }
2421 "danger_color" => {
2422 self.colors.danger = crate::prop::PropType::from_value(value)?;
2423 self
2424 }
2425 "font_family" => {
2426 self.font.family = value.to_str()?.to_string();
2427 self
2428 }
2429 "font_size" => {
2430 self.font.size = crate::prop::PropType::from_value(value)?;
2431 self
2432 }
2433 "font_weight" => {
2434 self.font.weight = font_weight_named(value.to_str()?)?;
2435 self
2436 }
2437 "font_italic" => {
2438 self.font.italic = value.to_bool()?;
2439 self
2440 }
2441 "font_underline" => {
2442 self.font.underline = value.to_bool()?;
2443 self
2444 }
2445 "font_strikeout" => {
2446 self.font.strikeout = value.to_bool()?;
2447 self
2448 }
2449 "theme" => self.set_theme_name(value.to_str()?),
2450 "hint" => self.set_hint(value.to_str()?),
2451 "tag" => self.set_tag(value.to_i64()?),
2452 "tag_string" => self.set_tag_string(value.to_str()?),
2453 "accessible_name" => self.set_accessible_name(value.to_str()?),
2454 "popup_menu" => {
2455 self.popup_menu = value.to_component()?;
2456 self
2457 }
2458 "hover_card" => self.set_hover_card(value.to_component()?),
2459 "hover_delay" => self.set_hover_delay(value.to_f64()? as f32),
2460 _ => return Err(Error::UnknownProperty(name.to_string())),
2461 };
2462 Ok(())
2463 }
2464}
2465
2466pub const FONT_WEIGHTS: [&str; 8] = ["default", "thin", "light", "normal", "medium", "semibold", "bold", "black"];
2468
2469fn font_weight_named(name: &str) -> Result<Option<crate::canvas::FontWeight>> {
2470 use crate::canvas::FontWeight::*;
2471 let all = [Thin, Light, Normal, Medium, SemiBold, Bold, Black];
2472 match FONT_WEIGHTS.iter().position(|n| n.eq_ignore_ascii_case(name)) {
2473 Some(0) => Ok(None),
2474 Some(i) => Ok(Some(all[i - 1])),
2475 None => Err(Error::invalid(format!("valor '{name}' inválido; use um de: {}", FONT_WEIGHTS.join(", ")))),
2476 }
2477}
2478
2479#[derive(Clone, Debug, Default, PartialEq)]
2483pub struct FontOverride {
2484 pub family: String,
2486 pub size: Option<f32>,
2488 pub weight: Option<crate::canvas::FontWeight>,
2490 pub italic: bool,
2492 pub underline: bool,
2494 pub strikeout: bool,
2496}
2497
2498impl FontOverride {
2499 pub fn is_empty(&self) -> bool {
2500 *self == Self::default()
2501 }
2502
2503 pub fn over(&self, parent: &FontOverride) -> FontOverride {
2505 FontOverride {
2506 family: if self.family.is_empty() { parent.family.clone() } else { self.family.clone() },
2507 size: self.size.or(parent.size),
2508 weight: self.weight.or(parent.weight),
2509 italic: self.italic || parent.italic,
2510 underline: self.underline || parent.underline,
2511 strikeout: self.strikeout || parent.strikeout,
2512 }
2513 }
2514
2515 pub fn apply(&self, font: &mut crate::canvas::FontSpec) {
2517 if !self.family.is_empty() {
2518 font.family = self.family.clone();
2519 }
2520 if let Some(size) = self.size.filter(|s| *s > 0.0) {
2521 font.size = size;
2522 }
2523 if let Some(weight) = self.weight {
2524 font.weight = weight;
2525 }
2526 font.italic |= self.italic;
2527 font.underline |= self.underline;
2528 font.strikeout |= self.strikeout;
2529 }
2530}
2531
2532impl ControlBase {
2533 pub fn font(&self) -> &FontOverride {
2535 &self.font
2536 }
2537
2538 pub fn set_font(&mut self, font: FontOverride) -> &mut Self {
2539 self.font = font;
2540 self
2541 }
2542}
2543
2544#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2550pub struct ColorOverride {
2551 pub fill: Option<crate::Color>,
2553 pub border: Option<crate::Color>,
2555 pub text: Option<crate::Color>,
2557 pub accent: Option<crate::Color>,
2560 pub success: Option<crate::Color>,
2563 pub warning: Option<crate::Color>,
2565 pub danger: Option<crate::Color>,
2567}
2568
2569impl ColorOverride {
2570 pub fn is_empty(&self) -> bool {
2571 *self == Self::default()
2572 }
2573
2574 pub(crate) fn recolors_theme(&self) -> bool {
2576 !self.is_empty()
2577 }
2578
2579 pub(crate) fn apply_theme(&self, theme: &crate::Theme) -> crate::Theme {
2581 use crate::Color;
2582 let mut t = theme.clone();
2583 let mut set = |name: &str, c: Color| {
2584 t.colors.insert(name.to_string(), c);
2585 };
2586 if let Some(a) = self.accent {
2587 let dark = luminance(a) < 0.5;
2588 set("primary", a);
2589 set("primary_hover", a.lerp(if dark { Color::WHITE } else { Color::BLACK }, 0.12));
2590 set("primary_pressed", a.lerp(if dark { Color::BLACK } else { Color::BLACK }, 0.15));
2591 set("on_primary", if luminance(a) < 0.6 { Color::WHITE } else { Color::BLACK });
2592 set("focus_ring", a);
2593 set("selection", a.with_opacity(0.25));
2594 set("primary_soft", a.with_opacity(0.1));
2595 set("primary_container", a.lerp(Color::WHITE, 0.82));
2596 }
2597 if let Some(c) = self.fill {
2598 let toward = if luminance(c) < 0.5 { Color::WHITE } else { Color::BLACK };
2600 set("surface", c);
2601 set("background", c);
2602 set("surface_2", c.lerp(toward.with_opacity(c.a as f32 / 255.0), 0.06));
2603 }
2604 if let Some(c) = self.text {
2605 set("text", c);
2606 set("text_muted", c.with_opacity(0.65));
2607 for on in ["on_primary", "on_danger", "on_success", "on_primary_container"] {
2608 set(on, c);
2609 }
2610 }
2611 if let Some(c) = self.border {
2612 let lines = theme.colors.get("grid_line").copied().unwrap_or_else(|| theme.color("border"));
2617 set("grid_line", lines);
2618 set("border", c);
2619 set("track", c);
2620 }
2621 for (token, color) in [("success", self.success), ("warning", self.warning), ("danger", self.danger)] {
2622 if let Some(c) = color {
2623 set(token, c);
2624 set(&format!("{token}_soft"), c.with_opacity(0.1));
2625 set(&format!("{token}_hover"), c.lerp(Color::WHITE, 0.12));
2626 set(&format!("{token}_pressed"), c.lerp(Color::BLACK, 0.15));
2627 if token == "success" {
2628 set("meter_low", c);
2629 } else if token == "danger" {
2630 set("meter_high", c);
2631 } else {
2632 set("meter_mid", c);
2633 }
2634 }
2635 }
2636 t
2637 }
2638}
2639
2640pub(crate) fn luminance(c: crate::Color) -> f32 {
2642 (0.299 * c.r as f32 + 0.587 * c.g as f32 + 0.114 * c.b as f32) / 255.0
2643}
2644
2645#[derive(Clone, Copy, Debug, PartialEq)]
2647pub struct Connection {
2648 pub from: ComponentId,
2649 pub from_at: (f32, f32),
2651 pub to: ComponentId,
2652 pub to_at: (f32, f32),
2653}
2654
2655impl Connection {
2656 pub const STUB: f32 = 16.0;
2658
2659 fn normal(at: (f32, f32)) -> PointF {
2661 let (x, y) = at;
2662 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))];
2663 d.iter().min_by(|a, b| a.0.total_cmp(&b.0)).map_or(PointF::new(1.0, 0.0), |d| d.1)
2664 }
2665
2666 pub fn route(&self, from: RectF, to: RectF) -> Vec<PointF> {
2669 let p0 = PointF::new(from.x + from.width * self.from_at.0, from.y + from.height * self.from_at.1);
2670 let p1 = PointF::new(to.x + to.width * self.to_at.0, to.y + to.height * self.to_at.1);
2671 let (n0, n1) = (Self::normal(self.from_at), Self::normal(self.to_at));
2672 let s0 = PointF::new(p0.x + n0.x * Self::STUB, p0.y + n0.y * Self::STUB);
2673 let s1 = PointF::new(p1.x + n1.x * Self::STUB, p1.y + n1.y * Self::STUB);
2674 let mut points = vec![p0, s0];
2675 if n0.x != 0.0 {
2676 let mx = (s0.x + s1.x) / 2.0;
2678 points.push(PointF::new(mx, s0.y));
2679 points.push(PointF::new(mx, s1.y));
2680 } else {
2681 let my = (s0.y + s1.y) / 2.0;
2682 points.push(PointF::new(s0.x, my));
2683 points.push(PointF::new(s1.x, my));
2684 }
2685 points.push(s1);
2686 points.push(p1);
2687 points.dedup_by(|a, b| (a.x - b.x).abs() < 0.01 && (a.y - b.y).abs() < 0.01);
2688 points
2689 }
2690}
2691
2692#[derive(Clone, Debug, Default, PartialEq)]
2694pub struct DragDecl {
2695 pub data: String,
2698 pub label: String,
2700 pub drop_types: String,
2703 pub image: Option<ComponentId>,
2705}
2706
2707#[derive(Clone, Debug, Default)]
2709struct RareTexts {
2710 tag_string: Box<str>,
2712 accessible_name: Box<str>,
2715 hover_card: Option<ComponentId>,
2718 hover_delay: f32,
2720}