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 popup(&self) -> Option<&dyn Popup> {
552 self.inner().and_then(|i| i.popup())
553 }
554
555 fn popup_mut(&mut self) -> Option<&mut dyn Popup> {
556 self.inner_mut().and_then(|i| i.popup_mut())
557 }
558
559 fn accessible_items(&self) -> Option<&dyn AccessibleItems> {
562 self.inner().and_then(|i| i.accessible_items())
563 }
564
565 fn accessible_items_mut(&mut self) -> Option<&mut dyn AccessibleItems> {
566 self.inner_mut().and_then(|i| i.accessible_items_mut())
567 }
568
569 fn attach(&self) -> Option<(ComponentId, f32, f32)> {
573 self.inner().and_then(|i| i.attach())
574 }
575
576 fn event_info(&self, event: &str) -> Vec<(&'static str, String)> {
580 self.inner().map(|i| i.event_info(event)).unwrap_or_default()
581 }
582
583 fn message(&mut self, ctx: &mut EventCtx<'_>, message: &dyn Any) {
587 if let Some(inner) = self.inner_mut() {
588 inner.message(ctx, message);
589 }
590 }
591
592 fn methods(&self) -> &'static [MethodInfo] {
595 &[]
596 }
597
598 fn invoke(&mut self, ctx: &mut EventCtx<'_>, method: &str, args: &[PropValue]) -> std::result::Result<PropValue, String> {
602 let class = self.class_name();
603 match self.inner_mut() {
604 Some(inner) => inner.invoke(ctx, method, args),
605 None => Err(format!("{class} não tem o método '{method}'")),
606 }
607 }
608
609}
610
611pub trait AccessibleItems {
616 fn access_items(&self, _ui: &crate::Ui, _id: ComponentId) -> Vec<crate::automation::AccessItem> {
619 Vec::new()
620 }
621
622 fn access_action(&mut self, _ctx: &mut EventCtx<'_>, _index: usize, _action: crate::automation::ItemAction) -> bool {
625 false
626 }
627
628 fn access_table_size(&self, _ui: &crate::Ui) -> Option<(usize, usize)> {
632 None
633 }
634}
635
636pub trait Popup {
641 fn closes_on_outside_press(&self) -> bool {
648 false
649 }
650
651 fn dismissed(&mut self, _ctx: &mut EventCtx<'_>) -> bool {
657 false
658 }
659
660 fn dies_with(&self) -> Option<ComponentId> {
664 None
665 }
666}
667
668pub trait LayoutHooks {
672 fn arrange(&self, _area: RectF, _children: &[ArrangeItem]) -> Option<Vec<RectF>> {
677 None
678 }
679
680 fn baseline(&self, _ui: &crate::Ui, _id: ComponentId, _height: f32) -> Option<f32> {
684 None
685 }
686
687 fn dock_gap(&self) -> f32 {
690 0.0
691 }
692
693 fn auto_height(&self, _ui: &crate::Ui, _id: ComponentId, _width: f32) -> Option<f32> {
697 None
698 }
699
700 fn auto_width(&self, _ui: &crate::Ui, _id: ComponentId) -> Option<f32> {
704 None
705 }
706}
707
708pub trait Timed {
713 fn timer_interval(&self) -> Option<f64> {
716 None
717 }
718
719 fn timer_precision(&self) -> crate::TimerPrecision {
721 crate::TimerPrecision::default()
722 }
723
724 fn timer_background(&self) -> bool {
727 false
728 }
729
730 fn timer_late(&mut self, _late: f64) {}
732
733 fn frame(&mut self, _ctx: &mut EventCtx<'_>) -> bool {
737 false
738 }
739
740 fn timer(&mut self, ctx: &mut EventCtx<'_>) {
743 ctx.fire("on_timer", EventArgs::None);
744 }
745}
746
747pub trait ChildPointer {
758 fn child_pressed(&mut self, ctx: &mut EventCtx<'_>, child: ComponentId, event: &PointerEvent);
761 fn child_clicked(&mut self, ctx: &mut EventCtx<'_>, child: ComponentId);
763}
764
765pub trait DragDrop {
766 fn drag_item(&self, _ui: &crate::Ui) -> Option<(String, String)> {
771 None
772 }
773
774 fn drag_visual(&self, _ui: &crate::Ui, _source: ComponentId) -> Option<crate::DragVisual> {
777 None
778 }
779
780 fn drop_accepts(&self, _ui: &crate::Ui, _id: ComponentId, _data: &str) -> bool {
785 false
786 }
787
788 fn drop_hover(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
791
792 fn drop_leave(&mut self, _ctx: &mut EventCtx<'_>) {}
794
795 fn drop_receive(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _position: PointF) {}
797
798 fn drag_ended(&mut self, _ctx: &mut EventCtx<'_>, _data: &str, _end: crate::DragEnd) {}
800}
801
802pub trait PlatformHooks {
806 fn taskbar(&self) -> Option<crate::taskbar::TaskbarSpec> {
808 None
809 }
810
811 fn tray(&self) -> Option<crate::tray::TraySpec> {
813 None
814 }
815
816 fn native_view(&self) -> Option<NativeView> {
820 None
821 }
822
823 fn take_native_commands(&self) -> Vec<NativeCommand> {
825 Vec::new()
826 }
827
828 fn connection(&self) -> Option<Connection> {
832 None
833 }
834
835 fn files_dropped(&mut self, _ctx: &mut EventCtx<'_>, _paths: &[String]) -> bool {
838 false
839 }
840
841 fn files_hovering(&mut self, _ctx: &mut EventCtx<'_>, _paths: Option<&[String]>) -> bool {
844 false
845 }
846}
847
848pub trait DesignHooks {
852 fn design_grip(&self, _local: PointF) -> Option<usize> {
855 None
856 }
857
858 fn design_drag(&mut self, _grip: usize, _delta: PointF, _begin: bool) -> Option<(Option<ComponentId>, &'static str, PropValue)> {
862 None
863 }
864}
865
866#[derive(Clone, Copy, Debug, PartialEq)]
868pub struct ArrangeItem {
869 pub id: ComponentId,
870 pub size: SizeF,
871 pub margins: Bounds,
873 pub span: u32,
875 pub row_span: u32,
877 pub cell: Option<(u32, u32)>,
879 pub grow: f32,
881 pub baseline: Option<f32>,
883 pub spacing: f32,
886}
887
888impl ArrangeItem {
889 pub fn of(id: ComponentId, cb: &ControlBase) -> Self {
891 ArrangeItem {
892 id,
893 size: SizeF::new(cb.width(), cb.height()),
894 margins: cb.margins(),
895 span: cb.column_span(),
896 row_span: cb.row_span(),
897 cell: cb.grid_cell(),
898 grow: cb.grow(),
899 baseline: None,
900 spacing: cb.spacing_scale(),
901 }
902 }
903}
904
905#[derive(Clone, Debug, PartialEq)]
907pub enum NativeView {
908 Web { url: String, html: String, generation: u64, navigation: Vec<String>, sandbox: String, background: Option<crate::Color>, data_dir: String, browser_args: String },
917}
918
919#[derive(Clone, Debug, PartialEq)]
923pub struct WebMessage {
924 pub text: String,
925 pub url: String,
926}
927
928pub fn navigation_allowed(allowed: &[String], url: &str) -> bool {
932 allowed.is_empty()
933 || url.starts_with("about:")
934 || url.starts_with("data:")
935 || allowed.iter().any(|a| {
936 let a = a.trim_end_matches('/');
937 url == a || url.starts_with(&format!("{a}/")) || url.starts_with(&format!("{a}?")) || (a.matches('/').count() <= 2 && url_origin(url) == url_origin(a))
938 })
939}
940
941pub fn url_origin(url: &str) -> String {
943 let Some((scheme, rest)) = url.split_once("://") else { return url.to_string() };
944 let host = rest.split(['/', '?', '#']).next().unwrap_or_default();
945 format!("{}://{}", scheme.to_ascii_lowercase(), host.to_ascii_lowercase())
946}
947
948#[derive(Clone, Copy, Debug, PartialEq)]
951pub struct MethodInfo {
952 pub name: &'static str,
953 pub args: &'static [PropKind],
954 pub returns: Option<PropKind>,
955 pub params: &'static [&'static str],
958 pub arg_types: &'static [&'static str],
961 pub returns_type: &'static str,
962}
963
964impl MethodInfo {
965 pub const fn new(name: &'static str, args: &'static [PropKind], returns: Option<PropKind>) -> Self {
966 Self { name, args, returns, params: &[], arg_types: &[], returns_type: "" }
967 }
968
969 pub const fn types(self, args: &'static [&'static str], returns: &'static str) -> Self {
971 Self { arg_types: args, returns_type: returns, ..self }
972 }
973
974 pub const fn params(self, names: &'static [&'static str]) -> Self {
977 Self { params: names, ..self }
978 }
979}
980
981#[derive(Clone, Debug, PartialEq)]
983pub enum NativeCommand {
984 Eval(String),
986 Back,
987 Forward,
988 Reload,
989}
990
991#[derive(Clone, Debug, PartialEq)]
993pub struct NativeViewSlot {
994 pub id: ComponentId,
995 pub rect: crate::RectF,
996 pub clip: Option<crate::RectF>,
999 pub visible: bool,
1000 pub view: NativeView,
1001 pub commands: Vec<NativeCommand>,
1002}
1003
1004pub fn prop_infos_of(component: &dyn Component) -> Vec<PropInfo> {
1010 let mut out: Vec<PropInfo> = Vec::new();
1011 let mut seen = std::collections::HashSet::new();
1014 let mut push = |info: &PropInfo| {
1015 if seen.insert(info.name) {
1016 out.push(*info);
1017 }
1018 };
1019 let mut layer = Some(component);
1020 while let Some(l) = layer {
1021 l.props().iter().for_each(&mut push);
1022 layer = l.inner();
1023 }
1024 if component.control().is_some() {
1025 ControlBase::PROPS.iter().for_each(&mut push);
1026 }
1027 out
1028}
1029
1030pub fn prop_info_of(component: &dyn Component, name: &str) -> Option<PropInfo> {
1033 let mut layer = Some(component);
1034 while let Some(l) = layer {
1035 if let Some(info) = l.props().iter().find(|p| p.name == name) {
1036 return Some(*info);
1037 }
1038 layer = l.inner();
1039 }
1040 if component.control().is_some() {
1041 return ControlBase::PROPS.iter().find(|p| p.name == name).copied();
1042 }
1043 None
1044}
1045
1046pub const VALUE_PROPERTIES: [&str; 19] = [
1051 "text", "checked", "value", "item_index", "selected", "date", "end_date", "time", "color", "files", "html", "index", "page", "hotkey",
1052 "angle", "pressed", "position", "low", "high",
1053];
1054
1055pub fn is_value_property(info: &PropInfo) -> bool {
1059 match info.name {
1060 "tab_index" | "expanded" => true,
1061 name => matches!(info.category, "Data" | "Runtime") && VALUE_PROPERTIES.contains(&name),
1062 }
1063}
1064
1065pub fn read_prop(component: &dyn Component, name: &str) -> Option<PropValue> {
1066 let mut layer = Some(component);
1067 while let Some(l) = layer {
1068 if let Some(v) = l.get_prop(name) {
1069 return Some(v);
1070 }
1071 layer = l.inner();
1072 }
1073 component.control().and_then(|cb| cb.get_prop(name))
1074}
1075
1076#[derive(Clone, Copy, Debug, PartialEq)]
1079pub(crate) struct AnchorRef {
1080 pub(crate) parent: SizeF,
1081 pub(crate) rect: RectF,
1082}
1083
1084#[derive(Clone, Copy, Debug, PartialEq)]
1086pub(crate) struct Follow {
1087 pub(crate) target: ComponentId,
1088 pub(crate) at: Option<PointF>,
1090 pub(crate) hidden: bool,
1093}
1094
1095#[derive(Clone, Debug)]
1100pub struct ControlBase {
1101 left: f32,
1103 top: f32,
1105 width: f32,
1107 height: f32,
1109 margins: Bounds,
1111 padding: Bounds,
1113 align: Align,
1115 anchors: Anchors,
1117 visible: bool,
1119 enabled: bool,
1121 hit_test: bool,
1123 clip_children: bool,
1125 inherit_state: bool,
1128 translatable: bool,
1132 captures_wheel: bool,
1135 captures_gesture: bool,
1138 can_focus: bool,
1140 draggable: bool,
1142 accepts_drops: bool,
1144 drag: Option<Box<DragDecl>>,
1147 tab_stop: bool,
1149 pan_on_touch: bool,
1151 hover_within: bool,
1154 pub(crate) popup_menu: Option<ComponentId>,
1156 cursor: CursorShape,
1158 opacity: f32,
1160 blend_mode: crate::BlendMode,
1163 rotation: f32,
1165 scale: f32,
1170 translate: PointF,
1172 cache_layer: bool,
1175 aspect_ratio: f32,
1177 max_width: f32,
1179 min_width: f32,
1181 min_height: f32,
1183 max_height: f32,
1185 self_align: SelfAlign,
1187 auto_size: bool,
1189 column_span: u32,
1191 grid_row: i16,
1194 grid_column: i16,
1196 row_span: u16,
1198 grow: f32,
1201 pub(crate) text_scale: f32,
1204 pub(crate) spacing_scale: f32,
1205 shadow: Option<crate::canvas::Shadow>,
1207 tag: i64,
1209 rare: Option<Box<RareTexts>>,
1212 style: String,
1214 theme: String,
1216 hint: String,
1218 state: ControlState,
1219 pub(crate) bounds: RectF,
1220 pub(crate) anchor_ref: Option<AnchorRef>,
1221 pub(crate) follow: Option<Follow>,
1223 pub(crate) design_hidden: bool,
1226 pub(crate) design_only: bool,
1228 pub(crate) keep_left_to_right: bool,
1231 pub(crate) colors: ColorOverride,
1233 pub(crate) font: FontOverride,
1235 scroll_offset: PointF,
1237 pub(crate) laid_out_scroll: PointF,
1240 pub(crate) safe_area: Bounds,
1242 pub(crate) content_size: SizeF,
1244}
1245
1246impl Default for ControlBase {
1247 fn default() -> Self {
1248 Self::new(100.0, 100.0)
1249 }
1250}
1251
1252impl ControlBase {
1253 pub const PROPS: &'static [PropInfo] = &[
1255 PropInfo::new("left", PropKind::Float, "Layout"),
1256 PropInfo::new("top", PropKind::Float, "Layout"),
1257 PropInfo::new("width", PropKind::Float, "Layout"),
1258 PropInfo::new("height", PropKind::Float, "Layout"),
1259 PropInfo::new("align", PropKind::Enum(<Align as crate::prop::PropEnum>::NAMES), "Layout").rust_type("Align"),
1260 PropInfo::new("anchors", PropKind::Set(&Anchors::NAMES), "Layout").rust_type("Anchors"),
1261 PropInfo::new("margins", PropKind::Bounds, "Layout"),
1262 PropInfo::new("padding", PropKind::Bounds, "Layout"),
1263 PropInfo::new("visible", PropKind::Bool, "Behavior"),
1264 PropInfo::new("enabled", PropKind::Bool, "Behavior"),
1265 PropInfo::new("hit_test", PropKind::Bool, "Behavior"),
1266 PropInfo::new("clip_children", PropKind::Bool, "Behavior"),
1267 PropInfo::new("inherit_state", PropKind::Bool, "Appearance"),
1268 PropInfo::new("translate", PropKind::Bool, "Behavior"),
1269 PropInfo::new("captures_wheel", PropKind::Bool, "Behavior"),
1270 PropInfo::new("captures_gesture", PropKind::Bool, "Behavior"),
1271 PropInfo::new("can_focus", PropKind::Bool, "Behavior"),
1272 PropInfo::new("draggable", PropKind::Bool, "Behavior"),
1273 PropInfo::new("accepts_drops", PropKind::Bool, "Behavior"),
1274 PropInfo::new("drag_data", PropKind::String, "Behavior"),
1275 PropInfo::new("drag_label", PropKind::String, "Behavior"),
1276 PropInfo::new("drag_image", PropKind::Component, "Behavior"),
1277 PropInfo::new("drop_types", PropKind::String, "Behavior"),
1278 PropInfo::new("tab_stop", PropKind::Bool, "Behavior"),
1279 PropInfo::new("cursor", PropKind::Enum(<CursorShape as crate::prop::PropEnum>::NAMES), "Behavior").rust_type("CursorShape"),
1280 PropInfo::new("opacity", PropKind::Float, "Appearance"),
1281 PropInfo::new("blend_mode", PropKind::Enum(<crate::BlendMode as crate::prop::PropEnum>::NAMES), "Appearance").rust_type("BlendMode"),
1282 PropInfo::new("cache_layer", PropKind::Bool, "Appearance"),
1283 PropInfo::new("rotation", PropKind::Angle, "Layout"),
1284 PropInfo::new("scale", PropKind::Float, "Layout"),
1285 PropInfo::new("translate_x", PropKind::Float, "Appearance"),
1286 PropInfo::new("translate_y", PropKind::Float, "Appearance"),
1287 PropInfo::new("aspect_ratio", PropKind::Float, "Layout"),
1288 PropInfo::new("max_width", PropKind::Float, "Layout"),
1289 PropInfo::new("min_width", PropKind::Float, "Layout"),
1290 PropInfo::new("min_height", PropKind::Float, "Layout"),
1291 PropInfo::new("max_height", PropKind::Float, "Layout"),
1292 PropInfo::new("self_align", PropKind::Enum(<SelfAlign as crate::prop::PropEnum>::NAMES), "Layout").rust_type("SelfAlign"),
1293 PropInfo::new("auto_size", PropKind::Bool, "Layout"),
1294 PropInfo::new("column_span", PropKind::Int, "Layout"),
1295 PropInfo::new("grid_row", PropKind::Int, "Layout"),
1296 PropInfo::new("grid_column", PropKind::Int, "Layout"),
1297 PropInfo::new("row_span", PropKind::Int, "Layout"),
1298 PropInfo::new("grow", PropKind::Float, "Layout"),
1299 PropInfo::new("shadow", PropKind::Shadow, "Appearance"),
1300 PropInfo::new("style", PropKind::StyleName, "Appearance"),
1301 PropInfo::new("theme", PropKind::ThemeName, "Appearance"),
1302 PropInfo::new("fill", PropKind::Color, "Appearance"),
1303 PropInfo::new("border_color", PropKind::Color, "Appearance"),
1304 PropInfo::new("text_color", PropKind::Color, "Appearance"),
1305 PropInfo::new("accent_color", PropKind::Color, "Appearance"),
1306 PropInfo::new("success_color", PropKind::Color, "Appearance"),
1307 PropInfo::new("warning_color", PropKind::Color, "Appearance"),
1308 PropInfo::new("danger_color", PropKind::Color, "Appearance"),
1309 PropInfo::new("font_family", PropKind::String, "Font"),
1310 PropInfo::new("font_size", PropKind::Float, "Font"),
1311 PropInfo::new("font_weight", PropKind::Enum(&FONT_WEIGHTS), "Font"),
1312 PropInfo::new("font_italic", PropKind::Bool, "Font"),
1313 PropInfo::new("font_underline", PropKind::Bool, "Font"),
1314 PropInfo::new("font_strikeout", PropKind::Bool, "Font"),
1315 PropInfo::new("hint", PropKind::String, "Behavior"),
1316 PropInfo::new("tag", PropKind::Int, "Misc"),
1317 PropInfo::new("tag_string", PropKind::String, "Misc"),
1318 PropInfo::new("accessible_name", PropKind::String, "Behavior"),
1319 PropInfo::new("popup_menu", PropKind::Component, "Behavior"),
1320 PropInfo::new("on_click", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1321 PropInfo::new("on_dbl_click", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1322 PropInfo::new("on_long_press", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1323 PropInfo::new("on_pointer_down", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1324 PropInfo::new("on_pointer_up", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1325 PropInfo::new("on_pointer_move", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1326 PropInfo::new("on_pointer_enter", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1327 PropInfo::new("on_pointer_leave", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1328 PropInfo::new("on_wheel", PropKind::Event, "Events").event_arg(crate::EventArg::Wheel),
1329 PropInfo::new("on_gesture", PropKind::Event, "Events").event_arg(crate::EventArg::Gesture),
1330 PropInfo::new("on_swipe", PropKind::Event, "Events").event_arg(crate::EventArg::Swipe),
1331 PropInfo::new("on_key_down", PropKind::Event, "Events").event_arg(crate::EventArg::Key),
1334 PropInfo::new("on_key_press", PropKind::Event, "Events"),
1335 PropInfo::new("on_key_up", PropKind::Event, "Events").event_arg(crate::EventArg::Key),
1336 PropInfo::new("on_enter", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1337 PropInfo::new("on_exit", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1338 PropInfo::new("on_resize", PropKind::Event, "Events").event_arg(crate::EventArg::None),
1339 PropInfo::new("on_drag_start", PropKind::Event, "Events"),
1340 PropInfo::new("on_drag_over", PropKind::Event, "Events"),
1341 PropInfo::new("on_drag_move", PropKind::Event, "Events").event_arg(crate::EventArg::Pointer),
1342 PropInfo::new("on_drag_leave", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::String)),
1343 PropInfo::new("on_drag_drop", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::String)),
1344 PropInfo::new("on_drag_end", PropKind::Event, "Events").event_arg(crate::EventArg::Value(PropKind::Bool)),
1345 ];
1346
1347 pub fn new(width: f32, height: f32) -> Self {
1348 Self {
1349 left: 0.0,
1350 top: 0.0,
1351 width,
1352 height,
1353 margins: Bounds::ZERO,
1354 padding: Bounds::ZERO,
1355 align: Align::None,
1356 anchors: Anchors::default(),
1357 visible: true,
1358 enabled: true,
1359 hit_test: true,
1360 clip_children: false,
1361 inherit_state: false,
1362 translatable: true,
1363 captures_wheel: false,
1364 captures_gesture: false,
1365 can_focus: false,
1366 draggable: false,
1367 accepts_drops: false,
1368 drag: None,
1369 tab_stop: true,
1370 pan_on_touch: true,
1371 hover_within: false,
1372 popup_menu: None,
1373 cursor: CursorShape::Default,
1374 opacity: 1.0,
1375 blend_mode: crate::BlendMode::Normal,
1376 rotation: 0.0,
1377 scale: 1.0,
1378 translate: PointF::ZERO,
1379 cache_layer: false,
1380 aspect_ratio: 0.0,
1381 max_width: 0.0,
1382 min_width: 0.0,
1383 min_height: 0.0,
1384 max_height: 0.0,
1385 self_align: SelfAlign::Stretch,
1386 auto_size: false,
1387 column_span: 1,
1388 grid_row: -1,
1389 grid_column: -1,
1390 row_span: 1,
1391 grow: 0.0,
1392 text_scale: 1.0,
1393 spacing_scale: 1.0,
1394 shadow: None,
1395 tag: 0,
1396 rare: None,
1397 style: String::new(),
1398 theme: String::new(),
1399 hint: String::new(),
1400 state: ControlState::default(),
1401 bounds: RectF::new(0.0, 0.0, width, height),
1402 anchor_ref: None,
1403 follow: None,
1404 design_hidden: false,
1405 design_only: false,
1406 keep_left_to_right: false,
1407 colors: ColorOverride::default(),
1408 font: FontOverride::default(),
1409 scroll_offset: PointF::ZERO,
1410 laid_out_scroll: PointF::ZERO,
1411 content_size: SizeF::ZERO,
1412 safe_area: Bounds::default(),
1413 }
1414 }
1415
1416 pub fn left(&self) -> f32 {
1419 self.left
1420 }
1421
1422 pub fn top(&self) -> f32 {
1423 self.top
1424 }
1425
1426 pub fn width(&self) -> f32 {
1427 self.width
1428 }
1429
1430 pub fn height(&self) -> f32 {
1431 self.height
1432 }
1433
1434 pub fn position(&self) -> PointF {
1435 PointF::new(self.left, self.top)
1436 }
1437
1438 pub fn size(&self) -> SizeF {
1439 SizeF::new(self.width, self.height)
1440 }
1441
1442 pub fn margins(&self) -> Bounds {
1445 if self.spacing_scale == 1.0 { self.margins } else { self.margins.scaled(self.spacing_scale) }
1446 }
1447
1448 pub fn padding(&self) -> Bounds {
1450 if self.spacing_scale == 1.0 { self.padding } else { self.padding.scaled(self.spacing_scale) }
1451 }
1452
1453 pub fn align(&self) -> Align {
1454 self.align
1455 }
1456
1457 pub fn anchors(&self) -> Anchors {
1458 self.anchors
1459 }
1460
1461 pub fn visible(&self) -> bool {
1462 self.visible
1463 }
1464
1465 pub fn design_only(&self) -> bool {
1467 self.design_only
1468 }
1469
1470 pub fn colors(&self) -> ColorOverride {
1472 self.colors
1473 }
1474
1475 pub fn set_colors(&mut self, colors: ColorOverride) -> &mut Self {
1476 self.colors = colors;
1477 self
1478 }
1479
1480 pub fn set_design_only(&mut self, on: bool) -> &mut Self {
1481 self.design_only = on;
1482 self
1483 }
1484
1485 pub fn keep_left_to_right(&self) -> bool {
1487 self.keep_left_to_right
1488 }
1489
1490 pub fn set_keep_left_to_right(&mut self, on: bool) -> &mut Self {
1491 self.keep_left_to_right = on;
1492 self
1493 }
1494
1495 pub fn enabled(&self) -> bool {
1496 self.enabled
1497 }
1498
1499 pub fn hit_test(&self) -> bool {
1500 self.hit_test
1501 }
1502
1503 pub fn translatable(&self) -> bool {
1504 self.translatable
1505 }
1506
1507 pub fn set_translatable(&mut self, v: bool) -> &mut Self {
1508 self.translatable = v;
1509 self
1510 }
1511
1512 pub fn inherit_state(&self) -> bool {
1513 self.inherit_state
1514 }
1515
1516 pub fn set_inherit_state(&mut self, v: bool) -> &mut Self {
1517 self.inherit_state = v;
1518 self
1519 }
1520
1521 pub fn clip_children(&self) -> bool {
1522 self.clip_children
1523 }
1524
1525 pub fn captures_wheel(&self) -> bool {
1526 self.captures_wheel
1527 }
1528
1529 pub fn captures_gesture(&self) -> bool {
1530 self.captures_gesture
1531 }
1532
1533 pub fn draggable(&self) -> bool {
1538 self.draggable
1539 }
1540
1541 pub fn set_draggable(&mut self, v: bool) -> &mut Self {
1542 self.draggable = v;
1543 self
1544 }
1545
1546 pub fn accepts_drops(&self) -> bool {
1549 self.accepts_drops
1550 }
1551
1552 pub fn set_accepts_drops(&mut self, v: bool) -> &mut Self {
1553 self.accepts_drops = v;
1554 self
1555 }
1556
1557 pub fn drag_data(&self) -> &str {
1559 self.drag.as_ref().map_or("", |d| d.data.as_str())
1560 }
1561
1562 pub fn drag_label(&self) -> &str {
1563 self.drag.as_ref().map_or("", |d| d.label.as_str())
1564 }
1565
1566 pub fn drag_image(&self) -> Option<ComponentId> {
1568 self.drag.as_ref().and_then(|d| d.image)
1569 }
1570
1571 pub(crate) fn forget_drag_image(&mut self, doomed: &[ComponentId]) {
1573 if let Some(d) = self.drag.as_mut().filter(|d| d.image.is_some_and(|i| doomed.contains(&i))) {
1574 d.image = None;
1575 }
1576 }
1577
1578 pub fn drop_types(&self) -> &str {
1580 self.drag.as_ref().map_or("", |d| d.drop_types.as_str())
1581 }
1582
1583 fn drag_decl(&mut self) -> &mut DragDecl {
1584 self.drag.get_or_insert_with(Default::default)
1585 }
1586
1587 pub fn can_focus(&self) -> bool {
1588 self.can_focus
1589 }
1590
1591 pub fn tab_stop(&self) -> bool {
1593 self.tab_stop
1594 }
1595
1596 pub fn popup_menu(&self) -> Option<ComponentId> {
1598 self.popup_menu
1599 }
1600
1601 pub fn set_popup_menu(&mut self, menu: Option<ComponentId>) -> &mut Self {
1602 self.popup_menu = menu;
1603 self
1604 }
1605
1606 pub fn pan_on_touch(&self) -> bool {
1608 self.pan_on_touch
1609 }
1610
1611 pub fn hover_within(&self) -> bool {
1613 self.hover_within
1614 }
1615
1616 pub fn set_hover_within(&mut self, v: bool) -> &mut Self {
1620 self.hover_within = v;
1621 self
1622 }
1623
1624 pub fn cursor(&self) -> CursorShape {
1626 self.cursor
1627 }
1628
1629 pub fn opacity(&self) -> f32 {
1630 self.opacity
1631 }
1632
1633 pub fn blend_mode(&self) -> crate::BlendMode {
1635 self.blend_mode
1636 }
1637
1638 pub fn set_blend_mode(&mut self, mode: crate::BlendMode) -> &mut Self {
1639 self.blend_mode = mode;
1640 self
1641 }
1642
1643 pub fn cache_layer(&self) -> bool {
1645 self.cache_layer
1646 }
1647
1648 pub fn set_cache_layer(&mut self, on: bool) -> &mut Self {
1649 self.cache_layer = on;
1650 self
1651 }
1652
1653 pub fn rotation(&self) -> f32 {
1656 self.rotation
1657 }
1658
1659 pub fn scale(&self) -> f32 {
1661 self.scale
1662 }
1663
1664 pub fn inner_size(&self) -> SizeF {
1667 let s = self.scale;
1668 if s == 1.0 { self.bounds.size() } else { SizeF::new(self.bounds.width / s, self.bounds.height / s) }
1669 }
1670
1671 pub fn shadow(&self) -> Option<crate::canvas::Shadow> {
1672 self.shadow
1673 }
1674
1675 pub fn aspect_ratio(&self) -> f32 {
1679 self.aspect_ratio
1680 }
1681
1682 pub fn set_aspect_ratio(&mut self, ratio: f32) -> &mut Self {
1683 self.aspect_ratio = if ratio.is_finite() { ratio.max(0.0) } else { 0.0 };
1684 self
1685 }
1686
1687 pub fn max_width(&self) -> f32 {
1691 self.max_width
1692 }
1693
1694 pub fn set_max_width(&mut self, width: f32) -> &mut Self {
1695 self.max_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1696 self
1697 }
1698
1699 pub fn min_width(&self) -> f32 {
1704 self.min_width
1705 }
1706
1707 pub fn set_min_width(&mut self, width: f32) -> &mut Self {
1708 self.min_width = if width.is_finite() { width.max(0.0) } else { 0.0 };
1709 self
1710 }
1711
1712 pub fn min_height(&self) -> f32 {
1714 self.min_height
1715 }
1716
1717 pub fn set_min_height(&mut self, height: f32) -> &mut Self {
1718 self.min_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1719 self
1720 }
1721
1722 pub fn max_height(&self) -> f32 {
1724 self.max_height
1725 }
1726
1727 pub fn set_max_height(&mut self, height: f32) -> &mut Self {
1728 self.max_height = if height.is_finite() { height.max(0.0) } else { 0.0 };
1729 self
1730 }
1731
1732 pub fn clamp_width(&self, available: f32) -> f32 {
1735 let w = if self.max_width > 0.0 { available.min(self.max_width) } else { available };
1736 w.max(self.min_width)
1737 }
1738
1739 pub fn clamp_height(&self, height: f32) -> f32 {
1741 let h = if self.max_height > 0.0 { height.min(self.max_height) } else { height };
1742 h.max(self.min_height)
1743 }
1744
1745 pub fn self_align(&self) -> SelfAlign {
1748 self.self_align
1749 }
1750
1751 pub fn set_self_align(&mut self, align: SelfAlign) -> &mut Self {
1752 self.self_align = align;
1753 self
1754 }
1755
1756 pub fn band_width(&self, available: f32) -> f32 {
1760 match self.self_align {
1761 SelfAlign::Stretch => self.clamp_width(available),
1762 _ if self.max_width > 0.0 => self.clamp_width(available),
1763 _ => self.clamp_width(self.width),
1764 }
1765 }
1766
1767 pub fn band_height(&self, available: f32) -> f32 {
1770 match self.self_align {
1771 SelfAlign::Stretch => self.clamp_height(available),
1772 _ if self.max_height > 0.0 => self.clamp_height(available),
1773 _ => self.clamp_height(self.height),
1774 }
1775 }
1776
1777 pub fn band_offset(&self, available: f32, size: f32) -> f32 {
1780 let spare = (available - size).max(0.0);
1781 match self.self_align {
1782 SelfAlign::Start => 0.0,
1783 SelfAlign::End => spare,
1784 SelfAlign::Stretch | SelfAlign::Center => spare / 2.0,
1785 }
1786 }
1787
1788 pub fn auto_size(&self) -> bool {
1793 self.auto_size
1794 }
1795
1796 pub fn set_auto_size(&mut self, on: bool) -> &mut Self {
1797 self.auto_size = on;
1798 self
1799 }
1800
1801 pub fn column_span(&self) -> u32 {
1804 self.column_span.max(1)
1805 }
1806
1807 pub fn grid_cell(&self) -> Option<(u32, u32)> {
1809 (self.grid_row >= 0 && self.grid_column >= 0).then(|| (self.grid_row as u32, self.grid_column as u32))
1810 }
1811
1812 pub fn set_grid_cell(&mut self, cell: Option<(u32, u32)>) -> &mut Self {
1813 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));
1814 self.grid_row = r;
1815 self.grid_column = c;
1816 self
1817 }
1818
1819 pub fn row_span(&self) -> u32 {
1821 u32::from(self.row_span.max(1))
1822 }
1823
1824 pub fn set_row_span(&mut self, span: u32) -> &mut Self {
1825 self.row_span = span.clamp(1, 64) as u16;
1826 self
1827 }
1828
1829 pub fn grow(&self) -> f32 {
1830 self.grow
1831 }
1832
1833 pub fn set_grow(&mut self, grow: f32) -> &mut Self {
1834 self.grow = grow.max(0.0);
1835 self
1836 }
1837
1838 pub fn set_column_span(&mut self, span: u32) -> &mut Self {
1839 self.column_span = span.max(1);
1840 self
1841 }
1842
1843 pub fn text_scale(&self) -> f32 {
1845 self.text_scale
1846 }
1847
1848 pub fn spacing_scale(&self) -> f32 {
1850 self.spacing_scale
1851 }
1852
1853 pub fn tag(&self) -> i64 {
1854 self.tag
1855 }
1856
1857 pub fn accessible_name(&self) -> &str {
1860 self.rare.as_ref().map_or("", |r| &r.accessible_name)
1861 }
1862
1863 pub fn set_accessible_name(&mut self, v: &str) -> &mut Self {
1864 self.rare_mut(|r| r.accessible_name = v.into());
1865 self
1866 }
1867
1868 fn rare_mut(&mut self, change: impl FnOnce(&mut RareTexts)) {
1869 let rare = self.rare.get_or_insert_with(Default::default);
1870 change(rare);
1871 if rare.tag_string.is_empty() && rare.accessible_name.is_empty() {
1872 self.rare = None;
1873 }
1874 }
1875
1876 pub fn tag_string(&self) -> &str {
1877 self.rare.as_ref().map_or("", |r| &r.tag_string)
1878 }
1879
1880 pub fn style(&self) -> &str {
1882 &self.style
1883 }
1884
1885 pub fn theme_name(&self) -> &str {
1887 &self.theme
1888 }
1889
1890 pub fn set_theme_name(&mut self, v: impl Into<String>) -> &mut Self {
1891 self.theme = v.into();
1892 self
1893 }
1894
1895 pub fn hint(&self) -> &str {
1897 &self.hint
1898 }
1899
1900 pub fn set_hint(&mut self, v: impl Into<String>) -> &mut Self {
1901 self.hint = v.into();
1902 self
1903 }
1904
1905 pub fn state(&self) -> ControlState {
1906 self.state
1907 }
1908
1909 pub fn bounds(&self) -> RectF {
1911 self.bounds
1912 }
1913
1914 pub fn scroll_offset(&self) -> PointF {
1916 self.scroll_offset
1917 }
1918
1919 pub fn laid_out_scroll(&self) -> PointF {
1922 self.laid_out_scroll
1923 }
1924
1925 pub fn set_scroll_offset(&mut self, offset: PointF) -> &mut Self {
1928 self.scroll_offset = offset;
1929 self
1930 }
1931
1932 pub fn content_size(&self) -> SizeF {
1935 self.content_size
1936 }
1937
1938 pub fn set_left(&mut self, v: f32) -> &mut Self {
1945 if self.left != v {
1946 self.left = v;
1947 self.anchor_ref = None;
1948 }
1949 self
1950 }
1951
1952 pub fn set_top(&mut self, v: f32) -> &mut Self {
1953 if self.top != v {
1954 self.top = v;
1955 self.anchor_ref = None;
1956 }
1957 self
1958 }
1959
1960 pub fn set_position(&mut self, left: f32, top: f32) -> &mut Self {
1961 self.set_left(left).set_top(top)
1962 }
1963
1964 pub fn set_width(&mut self, v: f32) -> &mut Self {
1965 if self.width != v.max(0.0) {
1966 self.width = v.max(0.0);
1967 self.anchor_ref = None;
1968 }
1969 self
1970 }
1971
1972 pub fn set_height(&mut self, v: f32) -> &mut Self {
1973 if self.height != v.max(0.0) {
1974 self.height = v.max(0.0);
1975 self.anchor_ref = None;
1976 }
1977 self
1978 }
1979
1980 pub(crate) fn set_measured_width(&mut self, v: f32) {
1982 self.width = v.max(0.0);
1983 if let Some(r) = self.anchor_ref.as_mut() {
1984 r.rect.width = self.width;
1985 }
1986 }
1987
1988 pub(crate) fn set_measured_height(&mut self, v: f32) {
1991 self.height = v.max(0.0);
1992 if let Some(r) = self.anchor_ref.as_mut() {
1993 r.rect.height = self.height;
1994 }
1995 }
1996
1997 pub fn set_size(&mut self, width: f32, height: f32) -> &mut Self {
1998 self.set_width(width).set_height(height)
1999 }
2000
2001 pub fn set_rect(&mut self, left: f32, top: f32, width: f32, height: f32) -> &mut Self {
2003 self.set_position(left, top).set_size(width, height)
2004 }
2005
2006 pub fn set_margins(&mut self, v: impl Into<Bounds>) -> &mut Self {
2007 self.margins = v.into();
2008 self
2009 }
2010
2011 pub fn set_padding(&mut self, v: impl Into<Bounds>) -> &mut Self {
2012 self.padding = v.into();
2013 self
2014 }
2015
2016 pub fn set_align(&mut self, v: Align) -> &mut Self {
2017 self.align = v;
2018 self.anchor_ref = None;
2019 self
2020 }
2021
2022 pub fn set_anchors(&mut self, v: Anchors) -> &mut Self {
2023 self.anchors = v;
2024 self.anchor_ref = None;
2025 self
2026 }
2027
2028 pub fn set_visible(&mut self, v: bool) -> &mut Self {
2029 self.visible = v;
2030 if let Some(f) = &mut self.follow {
2032 f.hidden = false;
2033 }
2034 self
2035 }
2036
2037 pub fn unfollow(&mut self) -> &mut Self {
2041 self.follow = None;
2042 self
2043 }
2044
2045 pub fn set_enabled(&mut self, v: bool) -> &mut Self {
2046 self.enabled = v;
2047 self
2048 }
2049
2050 pub fn set_hit_test(&mut self, v: bool) -> &mut Self {
2051 self.hit_test = v;
2052 self
2053 }
2054
2055 pub fn set_clip_children(&mut self, v: bool) -> &mut Self {
2056 self.clip_children = v;
2057 self
2058 }
2059
2060 pub fn set_captures_wheel(&mut self, v: bool) -> &mut Self {
2061 self.captures_wheel = v;
2062 self
2063 }
2064
2065 pub fn set_captures_gesture(&mut self, v: bool) -> &mut Self {
2066 self.captures_gesture = v;
2067 self
2068 }
2069
2070 pub fn set_can_focus(&mut self, v: bool) -> &mut Self {
2071 self.can_focus = v;
2072 self
2073 }
2074
2075 pub fn set_pan_on_touch(&mut self, v: bool) -> &mut Self {
2079 self.pan_on_touch = v;
2080 self
2081 }
2082
2083 pub fn set_tab_stop(&mut self, v: bool) -> &mut Self {
2084 self.tab_stop = v;
2085 self
2086 }
2087
2088 pub fn set_cursor(&mut self, v: CursorShape) -> &mut Self {
2089 self.cursor = v;
2090 self
2091 }
2092
2093 pub fn set_opacity(&mut self, v: f32) -> &mut Self {
2094 self.opacity = v.clamp(0.0, 1.0);
2095 self
2096 }
2097
2098 pub fn translate(&self) -> PointF {
2100 self.translate
2101 }
2102
2103 pub fn set_translate(&mut self, offset: PointF) -> &mut Self {
2104 self.translate = if offset.x.is_finite() && offset.y.is_finite() { offset } else { PointF::ZERO };
2105 self
2106 }
2107
2108 pub fn set_scale(&mut self, scale: f32) -> &mut Self {
2110 self.scale = if scale.is_finite() && scale > 0.0 { scale.clamp(0.05, 20.0) } else { 1.0 };
2111 self
2112 }
2113
2114 pub fn set_rotation(&mut self, degrees: f32) -> &mut Self {
2115 self.rotation = if degrees.is_finite() { degrees % 360.0 } else { 0.0 };
2116 self
2117 }
2118
2119 pub fn set_shadow(&mut self, shadow: Option<crate::canvas::Shadow>) -> &mut Self {
2120 self.shadow = shadow;
2121 self
2122 }
2123
2124 pub fn set_tag(&mut self, v: i64) -> &mut Self {
2125 self.tag = v;
2126 self
2127 }
2128
2129 pub fn set_tag_string(&mut self, v: &str) -> &mut Self {
2130 self.rare_mut(|r| r.tag_string = v.into());
2131 self
2132 }
2133
2134 pub fn set_style(&mut self, v: impl Into<String>) -> &mut Self {
2135 self.style = v.into();
2136 self
2137 }
2138
2139 pub fn state_mut(&mut self) -> &mut ControlState {
2142 &mut self.state
2143 }
2144
2145 pub fn get_prop(&self, name: &str) -> Option<PropValue> {
2148 Some(match name {
2149 "left" => self.left.into(),
2150 "top" => self.top.into(),
2151 "width" => self.width.into(),
2152 "height" => self.height.into(),
2153 "align" => self.align.into(),
2154 "anchors" => self.anchors.into(),
2155 "margins" => self.margins.into(),
2156 "padding" => self.padding.into(),
2157 "visible" => self.visible.into(),
2158 "enabled" => self.enabled.into(),
2159 "hit_test" => self.hit_test.into(),
2160 "clip_children" => self.clip_children.into(),
2161 "inherit_state" => self.inherit_state.into(),
2162 "translate" => self.translatable.into(),
2163 "captures_wheel" => self.captures_wheel.into(),
2164 "captures_gesture" => self.captures_gesture.into(),
2165 "can_focus" => self.can_focus.into(),
2166 "draggable" => self.draggable.into(),
2167 "accepts_drops" => self.accepts_drops.into(),
2168 "drag_data" => self.drag_data().into(),
2169 "drag_label" => self.drag_label().into(),
2170 "drag_image" => PropValue::Component(self.drag_image()),
2171 "drop_types" => self.drop_types().into(),
2172 "tab_stop" => self.tab_stop.into(),
2173 "cursor" => PropValue::enum_value(self.cursor),
2174 "opacity" => self.opacity.into(),
2175 "blend_mode" => PropValue::enum_value(self.blend_mode),
2176 "cache_layer" => self.cache_layer.into(),
2177 "rotation" => self.rotation.into(),
2178 "scale" => self.scale.into(),
2179 "translate_x" => self.translate.x.into(),
2180 "translate_y" => self.translate.y.into(),
2181 "aspect_ratio" => self.aspect_ratio.into(),
2182 "max_width" => self.max_width.into(),
2183 "min_width" => self.min_width.into(),
2184 "min_height" => self.min_height.into(),
2185 "max_height" => self.max_height.into(),
2186 "self_align" => PropValue::enum_value(self.self_align),
2187 "auto_size" => self.auto_size.into(),
2188 "column_span" => (self.column_span() as i64).into(),
2189 "grid_row" => i64::from(self.grid_row).into(),
2190 "grid_column" => i64::from(self.grid_column).into(),
2191 "row_span" => (self.row_span() as i64).into(),
2192 "grow" => f64::from(self.grow).into(),
2193 "shadow" => crate::prop::PropType::to_value(&self.shadow),
2194 "style" => self.style.clone().into(),
2195 "fill" => crate::prop::PropType::to_value(&self.colors.fill),
2196 "border_color" => crate::prop::PropType::to_value(&self.colors.border),
2197 "text_color" => crate::prop::PropType::to_value(&self.colors.text),
2198 "accent_color" => crate::prop::PropType::to_value(&self.colors.accent),
2199 "success_color" => crate::prop::PropType::to_value(&self.colors.success),
2200 "warning_color" => crate::prop::PropType::to_value(&self.colors.warning),
2201 "danger_color" => crate::prop::PropType::to_value(&self.colors.danger),
2202 "font_family" => self.font.family.clone().into(),
2203 "font_size" => crate::prop::PropType::to_value(&self.font.size),
2204 "font_weight" => PropValue::Enum(FONT_WEIGHTS[self.font.weight.map_or(0, |w| w as usize + 1)].into()),
2205 "font_italic" => self.font.italic.into(),
2206 "font_underline" => self.font.underline.into(),
2207 "font_strikeout" => self.font.strikeout.into(),
2208 "theme" => self.theme.clone().into(),
2209 "hint" => self.hint.clone().into(),
2210 "tag" => self.tag.into(),
2211 "tag_string" => self.tag_string().to_string().into(),
2212 "accessible_name" => self.accessible_name().to_string().into(),
2213 "popup_menu" => PropValue::Component(self.popup_menu),
2214 _ => return None,
2215 })
2216 }
2217
2218 pub fn set_prop(&mut self, name: &str, value: PropValue) -> Result<()> {
2219 match name {
2220 "left" => self.set_left(value.to_f32()?),
2221 "top" => self.set_top(value.to_f32()?),
2222 "width" => self.set_width(value.to_f32()?),
2223 "height" => self.set_height(value.to_f32()?),
2224 "align" => self.set_align(value.to_enum()?),
2225 "anchors" => self.set_anchors(value.to_anchors()?),
2226 "margins" => self.set_margins(value.to_bounds()?),
2227 "padding" => self.set_padding(value.to_bounds()?),
2228 "visible" => self.set_visible(value.to_bool()?),
2229 "enabled" => self.set_enabled(value.to_bool()?),
2230 "hit_test" => self.set_hit_test(value.to_bool()?),
2231 "clip_children" => self.set_clip_children(value.to_bool()?),
2232 "inherit_state" => self.set_inherit_state(value.to_bool()?),
2233 "translate" => self.set_translatable(value.to_bool()?),
2234 "captures_wheel" => self.set_captures_wheel(value.to_bool()?),
2235 "captures_gesture" => self.set_captures_gesture(value.to_bool()?),
2236 "can_focus" => self.set_can_focus(value.to_bool()?),
2237 "draggable" => self.set_draggable(value.to_bool()?),
2238 "accepts_drops" => self.set_accepts_drops(value.to_bool()?),
2239 "drag_data" => {
2240 self.drag_decl().data = value.to_str()?.to_string();
2241 self
2242 }
2243 "drag_label" => {
2244 self.drag_decl().label = value.to_str()?.to_string();
2245 self
2246 }
2247 "drag_image" => {
2248 self.drag_decl().image = value.to_component()?;
2249 self
2250 }
2251 "drop_types" => {
2252 self.drag_decl().drop_types = value.to_str()?.to_string();
2253 self
2254 }
2255 "tab_stop" => self.set_tab_stop(value.to_bool()?),
2256 "cursor" => self.set_cursor(value.to_enum()?),
2257 "opacity" => self.set_opacity(value.to_f32()?),
2258 "blend_mode" => self.set_blend_mode(value.to_enum()?),
2259 "cache_layer" => self.set_cache_layer(value.to_bool()?),
2260 "rotation" => self.set_rotation(value.to_f32()?),
2261 "scale" => self.set_scale(value.to_f32()?),
2262 "translate_x" => self.set_translate(PointF::new(value.to_f32()?, self.translate.y)),
2263 "translate_y" => self.set_translate(PointF::new(self.translate.x, value.to_f32()?)),
2264 "aspect_ratio" => self.set_aspect_ratio(value.to_f32()?),
2265 "max_width" => self.set_max_width(value.to_f32()?),
2266 "min_width" => self.set_min_width(value.to_f32()?),
2267 "min_height" => self.set_min_height(value.to_f32()?),
2268 "max_height" => self.set_max_height(value.to_f32()?),
2269 "self_align" => self.set_self_align(value.to_enum()?),
2270 "auto_size" => self.set_auto_size(value.to_bool()?),
2271 "column_span" => self.set_column_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2272 "grid_row" => {
2273 self.grid_row = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2274 self
2275 }
2276 "grid_column" => {
2277 self.grid_column = value.to_f64()?.clamp(-1.0, 4096.0) as i16;
2278 self
2279 }
2280 "row_span" => self.set_row_span(value.to_f64()?.clamp(1.0, 64.0) as u32),
2281 "grow" => self.set_grow(value.to_f32()?),
2282 "shadow" => self.set_shadow(crate::prop::PropType::from_value(value)?),
2283 "style" => self.set_style(value.to_str()?),
2284 "fill" => {
2285 self.colors.fill = crate::prop::PropType::from_value(value)?;
2286 self
2287 }
2288 "border_color" => {
2289 self.colors.border = crate::prop::PropType::from_value(value)?;
2290 self
2291 }
2292 "text_color" => {
2293 self.colors.text = crate::prop::PropType::from_value(value)?;
2294 self
2295 }
2296 "accent_color" => {
2297 self.colors.accent = crate::prop::PropType::from_value(value)?;
2298 self
2299 }
2300 "success_color" => {
2301 self.colors.success = crate::prop::PropType::from_value(value)?;
2302 self
2303 }
2304 "warning_color" => {
2305 self.colors.warning = crate::prop::PropType::from_value(value)?;
2306 self
2307 }
2308 "danger_color" => {
2309 self.colors.danger = crate::prop::PropType::from_value(value)?;
2310 self
2311 }
2312 "font_family" => {
2313 self.font.family = value.to_str()?.to_string();
2314 self
2315 }
2316 "font_size" => {
2317 self.font.size = crate::prop::PropType::from_value(value)?;
2318 self
2319 }
2320 "font_weight" => {
2321 self.font.weight = font_weight_named(value.to_str()?)?;
2322 self
2323 }
2324 "font_italic" => {
2325 self.font.italic = value.to_bool()?;
2326 self
2327 }
2328 "font_underline" => {
2329 self.font.underline = value.to_bool()?;
2330 self
2331 }
2332 "font_strikeout" => {
2333 self.font.strikeout = value.to_bool()?;
2334 self
2335 }
2336 "theme" => self.set_theme_name(value.to_str()?),
2337 "hint" => self.set_hint(value.to_str()?),
2338 "tag" => self.set_tag(value.to_i64()?),
2339 "tag_string" => self.set_tag_string(value.to_str()?),
2340 "accessible_name" => self.set_accessible_name(value.to_str()?),
2341 "popup_menu" => {
2342 self.popup_menu = value.to_component()?;
2343 self
2344 }
2345 _ => return Err(Error::UnknownProperty(name.to_string())),
2346 };
2347 Ok(())
2348 }
2349}
2350
2351pub const FONT_WEIGHTS: [&str; 8] = ["default", "thin", "light", "normal", "medium", "semibold", "bold", "black"];
2353
2354fn font_weight_named(name: &str) -> Result<Option<crate::canvas::FontWeight>> {
2355 use crate::canvas::FontWeight::*;
2356 let all = [Thin, Light, Normal, Medium, SemiBold, Bold, Black];
2357 match FONT_WEIGHTS.iter().position(|n| n.eq_ignore_ascii_case(name)) {
2358 Some(0) => Ok(None),
2359 Some(i) => Ok(Some(all[i - 1])),
2360 None => Err(Error::invalid(format!("valor '{name}' inválido; use um de: {}", FONT_WEIGHTS.join(", ")))),
2361 }
2362}
2363
2364#[derive(Clone, Debug, Default, PartialEq)]
2368pub struct FontOverride {
2369 pub family: String,
2371 pub size: Option<f32>,
2373 pub weight: Option<crate::canvas::FontWeight>,
2375 pub italic: bool,
2377 pub underline: bool,
2379 pub strikeout: bool,
2381}
2382
2383impl FontOverride {
2384 pub fn is_empty(&self) -> bool {
2385 *self == Self::default()
2386 }
2387
2388 pub fn over(&self, parent: &FontOverride) -> FontOverride {
2390 FontOverride {
2391 family: if self.family.is_empty() { parent.family.clone() } else { self.family.clone() },
2392 size: self.size.or(parent.size),
2393 weight: self.weight.or(parent.weight),
2394 italic: self.italic || parent.italic,
2395 underline: self.underline || parent.underline,
2396 strikeout: self.strikeout || parent.strikeout,
2397 }
2398 }
2399
2400 pub fn apply(&self, font: &mut crate::canvas::FontSpec) {
2402 if !self.family.is_empty() {
2403 font.family = self.family.clone();
2404 }
2405 if let Some(size) = self.size.filter(|s| *s > 0.0) {
2406 font.size = size;
2407 }
2408 if let Some(weight) = self.weight {
2409 font.weight = weight;
2410 }
2411 font.italic |= self.italic;
2412 font.underline |= self.underline;
2413 font.strikeout |= self.strikeout;
2414 }
2415}
2416
2417impl ControlBase {
2418 pub fn font(&self) -> &FontOverride {
2420 &self.font
2421 }
2422
2423 pub fn set_font(&mut self, font: FontOverride) -> &mut Self {
2424 self.font = font;
2425 self
2426 }
2427}
2428
2429#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2435pub struct ColorOverride {
2436 pub fill: Option<crate::Color>,
2438 pub border: Option<crate::Color>,
2440 pub text: Option<crate::Color>,
2442 pub accent: Option<crate::Color>,
2445 pub success: Option<crate::Color>,
2448 pub warning: Option<crate::Color>,
2450 pub danger: Option<crate::Color>,
2452}
2453
2454impl ColorOverride {
2455 pub fn is_empty(&self) -> bool {
2456 *self == Self::default()
2457 }
2458
2459 pub(crate) fn recolors_theme(&self) -> bool {
2461 !self.is_empty()
2462 }
2463
2464 pub(crate) fn apply_theme(&self, theme: &crate::Theme) -> crate::Theme {
2466 use crate::Color;
2467 let mut t = theme.clone();
2468 let mut set = |name: &str, c: Color| {
2469 t.colors.insert(name.to_string(), c);
2470 };
2471 if let Some(a) = self.accent {
2472 let dark = luminance(a) < 0.5;
2473 set("primary", a);
2474 set("primary_hover", a.lerp(if dark { Color::WHITE } else { Color::BLACK }, 0.12));
2475 set("primary_pressed", a.lerp(if dark { Color::BLACK } else { Color::BLACK }, 0.15));
2476 set("on_primary", if luminance(a) < 0.6 { Color::WHITE } else { Color::BLACK });
2477 set("focus_ring", a);
2478 set("selection", a.with_opacity(0.25));
2479 set("primary_soft", a.with_opacity(0.1));
2480 set("primary_container", a.lerp(Color::WHITE, 0.82));
2481 }
2482 if let Some(c) = self.fill {
2483 let toward = if luminance(c) < 0.5 { Color::WHITE } else { Color::BLACK };
2485 set("surface", c);
2486 set("background", c);
2487 set("surface_2", c.lerp(toward.with_opacity(c.a as f32 / 255.0), 0.06));
2488 }
2489 if let Some(c) = self.text {
2490 set("text", c);
2491 set("text_muted", c.with_opacity(0.65));
2492 for on in ["on_primary", "on_danger", "on_success", "on_primary_container"] {
2493 set(on, c);
2494 }
2495 }
2496 if let Some(c) = self.border {
2497 let lines = theme.colors.get("grid_line").copied().unwrap_or_else(|| theme.color("border"));
2502 set("grid_line", lines);
2503 set("border", c);
2504 set("track", c);
2505 }
2506 for (token, color) in [("success", self.success), ("warning", self.warning), ("danger", self.danger)] {
2507 if let Some(c) = color {
2508 set(token, c);
2509 set(&format!("{token}_soft"), c.with_opacity(0.1));
2510 set(&format!("{token}_hover"), c.lerp(Color::WHITE, 0.12));
2511 set(&format!("{token}_pressed"), c.lerp(Color::BLACK, 0.15));
2512 if token == "success" {
2513 set("meter_low", c);
2514 } else if token == "danger" {
2515 set("meter_high", c);
2516 } else {
2517 set("meter_mid", c);
2518 }
2519 }
2520 }
2521 t
2522 }
2523}
2524
2525pub(crate) fn luminance(c: crate::Color) -> f32 {
2527 (0.299 * c.r as f32 + 0.587 * c.g as f32 + 0.114 * c.b as f32) / 255.0
2528}
2529
2530#[derive(Clone, Copy, Debug, PartialEq)]
2532pub struct Connection {
2533 pub from: ComponentId,
2534 pub from_at: (f32, f32),
2536 pub to: ComponentId,
2537 pub to_at: (f32, f32),
2538}
2539
2540impl Connection {
2541 pub const STUB: f32 = 16.0;
2543
2544 fn normal(at: (f32, f32)) -> PointF {
2546 let (x, y) = at;
2547 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))];
2548 d.iter().min_by(|a, b| a.0.total_cmp(&b.0)).map_or(PointF::new(1.0, 0.0), |d| d.1)
2549 }
2550
2551 pub fn route(&self, from: RectF, to: RectF) -> Vec<PointF> {
2554 let p0 = PointF::new(from.x + from.width * self.from_at.0, from.y + from.height * self.from_at.1);
2555 let p1 = PointF::new(to.x + to.width * self.to_at.0, to.y + to.height * self.to_at.1);
2556 let (n0, n1) = (Self::normal(self.from_at), Self::normal(self.to_at));
2557 let s0 = PointF::new(p0.x + n0.x * Self::STUB, p0.y + n0.y * Self::STUB);
2558 let s1 = PointF::new(p1.x + n1.x * Self::STUB, p1.y + n1.y * Self::STUB);
2559 let mut points = vec![p0, s0];
2560 if n0.x != 0.0 {
2561 let mx = (s0.x + s1.x) / 2.0;
2563 points.push(PointF::new(mx, s0.y));
2564 points.push(PointF::new(mx, s1.y));
2565 } else {
2566 let my = (s0.y + s1.y) / 2.0;
2567 points.push(PointF::new(s0.x, my));
2568 points.push(PointF::new(s1.x, my));
2569 }
2570 points.push(s1);
2571 points.push(p1);
2572 points.dedup_by(|a, b| (a.x - b.x).abs() < 0.01 && (a.y - b.y).abs() < 0.01);
2573 points
2574 }
2575}
2576
2577#[derive(Clone, Debug, Default, PartialEq)]
2579pub struct DragDecl {
2580 pub data: String,
2583 pub label: String,
2585 pub drop_types: String,
2588 pub image: Option<ComponentId>,
2590}
2591
2592#[derive(Clone, Debug, Default)]
2594struct RareTexts {
2595 tag_string: Box<str>,
2597 accessible_name: Box<str>,
2600}