1use std::sync::Arc;
9
10use super::{
11 Accessible, AnyElement, Interactive, IntoReactive,
12 builder::{ElNode, Element, IntoElement, IntoListener, Listener, ParentElement},
13 events::{
14 self, CaretEvent, Composition, NativeEvent, OverlayPosition, Selection, VideoInfo,
15 VideoProgress, WebNavigation,
16 },
17};
18use crate::{
19 AudioSession, AutoCapitalize, AutoFill, Color, EventKind, HeaderSlot, Hsla, JsonValue,
20 KeyboardAppearance, KeyboardType, ModalAnimation, ModalPresentation, ModalProp, NativeProp,
21 OverlayProp, PasteBehavior, PasteEvent, Points, Prop, ResizeMode, ReturnKeyType, Rgba,
22 ScreenAnimation, ScreenProp, ScrollProp, Size, StatusBarSetting, SubmitBehavior, Tag,
23 WebSource,
24 canvas::{
25 CanvasFont, CanvasGradient, CanvasImage, CanvasOp, CanvasTextAlign, CanvasTextBaseline,
26 Composite, Drawing, FillRule, LineCap, LineJoin, Num, Paint, Path, PathOp, ViewPaint,
27 },
28 geometry::{Bounds, Corners, Point, Radians, ScaledPixels},
29 js::host::HostProps,
30 overlay::{Align, OverlayAnchor, Placement},
31 style::SvgPaint,
32};
33
34pub(crate) fn same_kind(a: &Prop, b: &Prop) -> bool {
36 match (a, b) {
37 (Prop::ComponentProp(a, _), Prop::ComponentProp(b, _)) => a == b,
38 (Prop::ComponentPropBinding(a, _), Prop::ComponentPropBinding(b, _)) => a == b,
39 (Prop::RustProp(a, _), Prop::RustProp(b, _)) => a == b,
40 _ => HostProps::same_kind(a, b),
41 }
42}
43
44pub(crate) fn reset(prop: &Prop) -> Option<Prop> {
47 match prop {
48 Prop::ComponentProp(name, _) => Some(Prop::ComponentProp(name.clone(), None)),
49 Prop::ComponentPropBinding(name, _) => Some(Prop::ComponentPropBinding(name.clone(), None)),
50 Prop::RustProp(name, _) => Some(Prop::RustProp(name, None)),
51 _ => HostProps::reset(prop),
52 }
53}
54
55pub(crate) fn set(node: &mut ElNode, prop: Prop) {
57 set_in(&mut node.attrs.props, prop);
58}
59
60crate::static_reactive!(Color: Color);
61crate::static_reactive!(Hsla: Hsla, Rgba, Color);
62crate::static_reactive!(crate::AccessibilityRole: crate::AccessibilityRole);
63crate::static_reactive!(AutoCapitalize: AutoCapitalize);
64crate::static_reactive!(AutoFill: AutoFill);
65crate::static_reactive!(crate::HeaderSystemItem: crate::HeaderSystemItem);
66crate::static_reactive!(KeyboardAppearance: KeyboardAppearance);
67crate::static_reactive!(KeyboardType: KeyboardType);
68crate::static_reactive!(ModalAnimation: ModalAnimation);
69crate::static_reactive!(ModalPresentation: ModalPresentation);
70crate::static_reactive!(ResizeMode: ResizeMode);
71crate::static_reactive!(ReturnKeyType: ReturnKeyType);
72crate::static_reactive!(SubmitBehavior: SubmitBehavior);
73crate::static_reactive!(PasteBehavior: PasteBehavior);
74crate::static_reactive!(crate::EllipsizeMode: crate::EllipsizeMode);
75crate::static_reactive!(JsonValue: JsonValue);
76crate::static_reactive!(StatusBarSetting: StatusBarSetting, crate::StatusBarStyle);
77crate::static_reactive!(Placement: Placement);
78crate::static_reactive!(Align: Align);
79
80pub(crate) fn set_in(props: &mut Vec<Prop>, prop: Prop) {
82 match props.iter_mut().find(|p| same_kind(p, &prop)) {
83 Some(p) => *p = prop,
84 None => props.push(prop),
85 }
86}
87
88pub(crate) fn bind_prop<T: 'static>(
91 node: &mut ElNode,
92 value: impl IntoReactive<T>,
93 prop: impl Fn(T) -> Prop + 'static,
94) {
95 node.bind_attr(value, move |attrs, v| set_in(&mut attrs.props, prop(v)));
96}
97
98fn listen<T: ?Sized + ToOwned + 'static>(
100 node: &mut ElNode,
101 kind: EventKind,
102 listener: Listener<T>,
103 decode: events::Decode<T>,
104) {
105 node.handlers.push((kind, events::typed(listener, decode)));
106}
107
108macro_rules! host_element {
109 ($(#[$doc:meta])* $name:ident) => {
110 $(#[$doc])*
111 #[derive(Clone)]
112 pub struct $name(ElNode);
113
114 impl Element for $name {
115 fn node(&mut self) -> &mut ElNode {
116 &mut self.0
117 }
118 }
119
120 impl IntoElement for $name {
121 fn into_element(self) -> AnyElement {
122 AnyElement(self.0)
123 }
124 }
125
126 impl Accessible for $name {}
127 impl Interactive for $name {}
128
129 impl super::builder::IntoTooltip for $name {
130 fn apply_tooltip(self, node: &mut ElNode) {
131 self.into_element().apply_tooltip(node);
132 }
133 }
134
135 #[allow(dead_code)]
137 impl $name {
138 fn set(mut self, prop: Prop) -> Self {
139 set(&mut self.0, prop);
140 self
141 }
142
143 fn bind_prop<T: 'static>(
144 mut self,
145 value: impl IntoReactive<T>,
146 prop: impl Fn(T) -> Prop + 'static,
147 ) -> Self {
148 bind_prop(&mut self.0, value, prop);
149 self
150 }
151
152 fn listen<T: ?Sized + ToOwned + 'static>(
153 mut self,
154 kind: EventKind,
155 listener: Listener<T>,
156 decode: events::Decode<T>,
157 ) -> Self {
158 listen(&mut self.0, kind, listener, decode);
159 self
160 }
161 }
162 };
163}
164
165host_element! {
166 Input
169}
170
171host_element! {
172 Scroll
175}
176
177host_element! {
178 Stack
184}
185
186host_element! {
187 Screen
191}
192
193host_element! {
194 Modal
198}
199
200host_element! {
201 InputAccessoryView
205}
206
207host_element! {
208 CanvasView
210}
211
212host_element! {
213 WebView
215}
216
217host_element! {
218 Video
220}
221
222host_element! {
223 NativeView
226}
227
228host_element! {
229 Overlay
234}
235
236impl ParentElement for Overlay {}
237impl ParentElement for Scroll {}
238impl ParentElement for Stack {}
239impl ParentElement for Screen {}
240impl ParentElement for Modal {}
241
242impl ParentElement for InputAccessoryView {
244 fn child<M>(mut self, child: impl super::IntoChild<M>) -> Self {
245 if !cfg!(target_os = "android") {
246 child.__push_into(&mut self.0.children);
247 }
248 self
249 }
250
251 fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self {
252 if !cfg!(target_os = "android") {
253 self.0
254 .children
255 .extend(children.into_iter().map(|c| c.into_element().0));
256 }
257 self
258 }
259}
260
261pub fn overlay() -> Overlay {
269 Overlay(ElNode::new(Tag::Overlay))
270}
271
272pub fn input() -> Input {
274 Input(ElNode::new(Tag::Input))
275}
276
277pub fn scroll() -> Scroll {
279 Scroll(ElNode::new(Tag::Scroll))
280}
281
282#[doc(hidden)]
288pub fn stack() -> Stack {
289 Stack(ElNode::new(Tag::Stack))
290}
291
292#[doc(hidden)]
297pub fn screen() -> Screen {
298 let mut node = ElNode::new(Tag::Screen);
299 node.handlers.push((EventKind::Insets, Listener::noop()));
301 Screen(node)
302}
303
304#[doc(hidden)]
309pub fn modal() -> Modal {
310 let mut node = ElNode::new(Tag::Modal);
311 node.handlers.push((EventKind::Insets, Listener::noop()));
313 Modal(node)
314}
315
316pub fn input_accessory_view(native_id: &str) -> InputAccessoryView {
322 let tag = if cfg!(target_os = "android") {
323 Tag::View
324 } else {
325 Tag::InputAccessory
326 };
327 let mut node = ElNode::new(tag);
328 if cfg!(target_os = "android") {
329 node.style.keyword(crate::StyleKey::Display, "none");
330 } else {
331 set(&mut node, Prop::NativeId(Some(native_id.into())));
332 }
333 InputAccessoryView(node)
334}
335
336pub fn canvas(painter: impl IntoReactive<Painter>) -> CanvasView {
341 let mut node = ElNode::new(Tag::Canvas);
342 node.bind_attr(painter, |attrs, painter: Painter| {
343 attrs.drawing = Some(Arc::new(painter.drawing));
344 attrs.bound = painter.nodes;
345 });
346 CanvasView(node)
347}
348
349crate::static_reactive!(Painter: Painter, Drawing);
350
351pub fn web_view(url: impl IntoReactive<String>) -> WebView {
353 WebView(ElNode::new(Tag::WebView)).bind_prop(url, |url: String| {
354 Prop::Native(NativeProp::Source(WebSource::Uri(url.into())))
355 })
356}
357
358pub fn web_view_html(html: impl IntoReactive<String>, base_url: Option<&str>) -> WebView {
361 WebView(ElNode::new(Tag::WebView)).html(html, base_url)
362}
363
364pub fn video(url: impl IntoReactive<String>) -> Video {
367 Video(ElNode::new(Tag::Video)).bind_prop(url, |url: String| {
368 Prop::Native(NativeProp::Source(WebSource::Uri(url.into())))
369 })
370}
371
372pub fn native(name: &str) -> NativeView {
374 NativeView(ElNode::new(Tag::Native)).set(Prop::Component(name.into()))
375}
376
377impl Input {
378 pub fn value(self, text: impl IntoReactive<String>) -> Self {
381 self.bind_prop(text, |text: String| Prop::Value(text))
382 }
383
384 pub fn default_value(self, text: impl IntoReactive<String>) -> Self {
386 self.bind_prop(text, |text: String| Prop::DefaultValue(text))
387 }
388
389 pub fn placeholder(self, text: impl IntoReactive<String>) -> Self {
391 self.bind_prop(text, |text: String| Prop::Placeholder(text))
392 }
393
394 pub fn placeholder_color(self, color: impl IntoReactive<Hsla>) -> Self {
396 self.bind_prop(color, |color: Hsla| {
397 Prop::PlaceholderColor(Some(color.into()))
398 })
399 }
400
401 pub fn selection_color(self, color: impl IntoReactive<Hsla>) -> Self {
403 self.bind_prop(color, |color: Hsla| {
404 Prop::SelectionColor(Some(color.into()))
405 })
406 }
407
408 pub fn masked(self, secure: impl IntoReactive<bool>) -> Self {
410 self.bind_prop(secure, |secure: bool| Prop::Secure(secure))
411 }
412
413 pub fn keyboard_type(self, kind: impl IntoReactive<KeyboardType>) -> Self {
415 self.bind_prop(kind, |kind: KeyboardType| Prop::KeyboardType(kind))
416 }
417
418 pub fn keyboard_appearance(self, appearance: impl IntoReactive<KeyboardAppearance>) -> Self {
421 self.bind_prop(appearance, |appearance: KeyboardAppearance| {
422 Prop::KeyboardAppearance(appearance)
423 })
424 }
425
426 pub fn return_key_type(self, kind: impl IntoReactive<ReturnKeyType>) -> Self {
428 self.bind_prop(kind, |kind: ReturnKeyType| Prop::ReturnKeyType(kind))
429 }
430
431 pub fn auto_capitalize(self, mode: impl IntoReactive<AutoCapitalize>) -> Self {
433 self.bind_prop(mode, |mode: AutoCapitalize| Prop::AutoCapitalize(mode))
434 }
435
436 pub fn auto_correct(self, on: impl IntoReactive<bool>) -> Self {
438 self.bind_prop(on, |on: bool| Prop::AutoCorrect(on))
439 }
440
441 pub fn auto_fill(self, kind: impl IntoReactive<AutoFill>) -> Self {
443 self.bind_prop(kind, |kind: AutoFill| Prop::AutoFill(kind))
444 }
445
446 pub fn multiline(self, multiline: impl IntoReactive<bool>) -> Self {
448 self.bind_prop(multiline, |multiline: bool| Prop::Multiline(multiline))
449 }
450
451 pub fn max_length(self, length: impl IntoReactive<u32>) -> Self {
453 self.bind_prop(length, |length: u32| Prop::MaxLength(Some(length)))
454 }
455
456 pub fn readonly(self, readonly: impl IntoReactive<bool>) -> Self {
458 self.bind_prop(readonly, |readonly: bool| Prop::Readonly(readonly))
459 }
460
461 pub fn submit_behavior(self, behavior: impl IntoReactive<SubmitBehavior>) -> Self {
464 self.bind_prop(behavior, |behavior: SubmitBehavior| {
465 Prop::SubmitBehavior(Some(behavior))
466 })
467 }
468
469 pub fn paste_behavior(self, behavior: impl IntoReactive<PasteBehavior>) -> Self {
472 self.bind_prop(behavior, Prop::PasteBehavior)
473 }
474
475 pub fn on_paste<M>(self, listener: impl IntoListener<PasteEvent, M>) -> Self {
479 self.listen(EventKind::Paste, listener.into_listener(), events::paste)
480 }
481
482 pub fn auto_focus(self, focus: impl IntoReactive<bool>) -> Self {
484 self.bind_prop(focus, |focus: bool| Prop::AutoFocus(focus))
485 }
486
487 pub fn caret(self, caret: impl IntoReactive<bool>) -> Self {
489 self.bind_prop(caret, |caret: bool| Prop::Caret(caret))
490 }
491
492 pub fn input_accessory(self, native_id: impl IntoReactive<String>) -> Self {
495 self.bind_prop(native_id, |id: String| {
496 Prop::InputAccessoryViewId(Some(id.into()))
497 })
498 }
499
500 pub fn selected_range(self, range: impl IntoReactive<(u32, u32)>) -> Self {
505 self.bind_prop(range, |(start, end): (u32, u32)| {
506 Prop::Selection(start, end)
507 })
508 }
509
510 pub fn on_change<M>(self, listener: impl IntoListener<str, M>) -> Self {
512 self.listen(
513 EventKind::ChangeText,
514 listener.into_listener(),
515 events::text,
516 )
517 }
518
519 pub fn on_submit<M>(self, listener: impl IntoListener<str, M>) -> Self {
521 self.listen(
522 EventKind::SubmitEditing,
523 listener.into_listener(),
524 events::text,
525 )
526 }
527
528 pub fn on_selection_change<M>(self, listener: impl IntoListener<Selection, M>) -> Self {
530 self.listen(
531 EventKind::SelectionChange,
532 listener.into_listener(),
533 events::selection,
534 )
535 }
536
537 pub fn on_caret_move<M>(self, listener: impl IntoListener<CaretEvent, M>) -> Self {
541 self.listen(
542 EventKind::CaretMove,
543 listener.into_listener(),
544 events::caret,
545 )
546 }
547
548 pub fn on_key_press<M>(self, listener: impl IntoListener<str, M>) -> Self {
550 self.listen(EventKind::KeyPress, listener.into_listener(), events::key)
551 }
552
553 pub fn on_composition_change<M>(self, listener: impl IntoListener<Composition, M>) -> Self {
557 self.listen(
558 EventKind::CompositionChange,
559 listener.into_listener(),
560 events::composition,
561 )
562 }
563
564 pub fn on_content_size_change<M>(self, listener: impl IntoListener<Size<Points>, M>) -> Self {
566 self.listen(
567 EventKind::ContentSizeChange,
568 listener.into_listener(),
569 events::size,
570 )
571 }
572}
573
574impl Scroll {
575 pub fn horizontal(self) -> Self {
577 self.set(Prop::Horizontal(true))
578 }
579
580 pub fn scrollbar(self, shows: impl IntoReactive<bool>) -> Self {
582 self.bind_prop(shows, |shows: bool| Prop::Scrollbar(shows))
583 }
584
585 pub fn zoom_range(self, min: f32, max: f32) -> Self {
587 self.set(Prop::Scroll(ScrollProp::MinZoom(min)))
588 .set(Prop::Scroll(ScrollProp::MaxZoom(max)))
589 }
590
591 pub fn zoom(self, scale: impl IntoReactive<f32>) -> Self {
593 self.bind_prop(scale, |scale: f32| Prop::Scroll(ScrollProp::Zoom(scale)))
594 }
595
596 pub fn bounces_zoom(self, bounces: impl IntoReactive<bool>) -> Self {
599 self.bind_prop(bounces, |bounces: bool| {
600 Prop::Scroll(ScrollProp::BouncesZoom(bounces))
601 })
602 }
603
604 pub fn zoomable(self, enabled: impl IntoReactive<bool>) -> Self {
606 self.bind_prop(enabled, |enabled: bool| {
607 Prop::Scroll(ScrollProp::Pinch(enabled))
608 })
609 }
610
611 pub fn center_content(self, center: impl IntoReactive<bool>) -> Self {
613 self.bind_prop(center, |center: bool| {
614 Prop::Scroll(ScrollProp::CenterContent(center))
615 })
616 }
617}
618
619impl super::Scrollable for Scroll {}
620
621impl Screen {
622 fn header_item(mut self, slot: HeaderSlot, key: &'static str, item: impl IntoElement) -> Self {
623 let mut node = ElNode::new(Tag::HeaderItem);
624 node.key = Some(key.into());
625 set(&mut node, Prop::HeaderSlot(slot));
626 node.children.push(item.into_element().0);
627 self.0.children.push(node);
628 self
629 }
630
631 pub fn title(self, title: impl IntoReactive<super::SharedString>) -> Self {
633 self.bind_prop(title, |t| {
634 Prop::Screen(ScreenProp::Title(Some(Arc::from(t.as_str()))))
635 })
636 }
637
638 pub fn header_visible(self, shown: impl IntoReactive<bool>) -> Self {
640 self.bind_prop(shown, |v| Prop::Screen(ScreenProp::HeaderVisible(v)))
641 }
642
643 pub fn header_large_title(self, large: impl IntoReactive<bool>) -> Self {
645 self.bind_prop(large, |v| Prop::Screen(ScreenProp::HeaderLargeTitle(v)))
646 }
647
648 pub fn header_transparent(self, transparent: impl IntoReactive<bool>) -> Self {
650 self.bind_prop(transparent, |v| {
651 Prop::Screen(ScreenProp::HeaderTransparent(v))
652 })
653 }
654
655 pub fn header_style(mut self, style: impl IntoReactive<super::Style>) -> Self {
660 let style = match style.into_reactive() {
661 super::Reactive::Static(s) if s.1.is_bound() => {
663 super::Reactive::Bound(std::rc::Rc::new(move || s.clone()))
664 }
665 style => style,
666 };
667 self.0.bind_attr(style, |a, s: super::Style| {
668 set_in(&mut a.props, Prop::HeaderStyle(s.0));
669 if !s.1.is_empty() {
670 let typed = s.1.resolved();
671 let list = (!typed.is_empty()).then(|| Arc::new(typed.lower()));
672 set_in(&mut a.props, Prop::HeaderClassList(list));
673 }
674 });
675 self
676 }
677
678 pub fn header_style_with(
685 self,
686 f: impl Fn(super::StyleRefinement) -> super::StyleRefinement + 'static,
687 ) -> Self {
688 let list = move || {
689 let style = f(super::StyleRefinement::default()).resolved();
690 (!style.is_empty()).then(|| Arc::new(style.lower()))
691 };
692 self.bind_prop(super::Reactive::Bound(std::rc::Rc::new(list)), |list| {
693 Prop::HeaderClassList(list)
694 })
695 }
696
697 pub fn header_back_title(self, title: impl IntoReactive<super::SharedString>) -> Self {
699 self.bind_prop(title, |t| {
700 Prop::Screen(ScreenProp::HeaderBackTitle(Some(Arc::from(t.as_str()))))
701 })
702 }
703
704 pub fn header_right_system_item(
708 self,
709 item: impl IntoReactive<crate::HeaderSystemItem>,
710 ) -> Self {
711 self.bind_prop(item, |item| {
712 Prop::Screen(ScreenProp::HeaderRightSystemItem(Some(item)))
713 })
714 }
715
716 pub fn header_back_visible(self, visible: impl IntoReactive<bool>) -> Self {
718 self.bind_prop(visible, |v| Prop::Screen(ScreenProp::HeaderBackVisible(v)))
719 }
720
721 pub fn pop_on_swipe(self, enabled: impl IntoReactive<bool>) -> Self {
724 self.bind_prop(enabled, |v| Prop::Screen(ScreenProp::PopOnSwipe(v)))
725 }
726
727 pub fn pop_on_back(self, pop: impl IntoReactive<bool>) -> Self {
731 self.bind_prop(pop, |v| Prop::Screen(ScreenProp::PopOnBack(v)))
732 }
733
734 pub fn animation(self, animation: impl IntoReactive<ScreenAnimation>) -> Self {
736 self.bind_prop(animation, |a| Prop::Screen(ScreenProp::Animation(a)))
737 }
738
739 pub fn status_bar(self, setting: impl IntoReactive<StatusBarSetting>) -> Self {
743 self.bind_prop(setting, |s| Prop::Screen(ScreenProp::StatusBar(Some(s))))
744 }
745
746 pub fn header_left(self, item: impl IntoElement) -> Self {
748 self.header_item(HeaderSlot::Left, "header-left", item)
749 }
750
751 pub fn header_right(self, item: impl IntoElement) -> Self {
753 self.header_item(HeaderSlot::Right, "header-right", item)
754 }
755
756 pub fn header_title(self, item: impl IntoElement) -> Self {
758 self.header_item(HeaderSlot::Title, "header-title", item)
759 }
760
761 pub fn header(self, item: impl IntoElement) -> Self {
765 self.header_item(HeaderSlot::Header, "header", item)
766 }
767
768 pub fn on_dismiss<M>(self, listener: impl IntoListener<(), M>) -> Self {
771 self.listen(EventKind::Dismiss, listener.into_listener(), events::unit)
772 }
773
774 pub fn on_back_press<M>(self, listener: impl IntoListener<(), M>) -> Self {
777 self.listen(
778 EventKind::BackRequest,
779 listener.into_listener(),
780 events::unit,
781 )
782 }
783}
784
785impl Overlay {
786 pub fn anchor(self, anchor: &super::ElementRef) -> Self {
790 let anchor = *anchor;
791 self.bind_prop(
792 move || match anchor.node() {
793 Some(id) => OverlayAnchor::Node(id),
794 None => OverlayAnchor::None,
795 },
796 |a| Prop::Overlay(OverlayProp::Anchor(a)),
797 )
798 }
799
800 pub fn anchor_content(self, anchor: &super::ElementRef) -> Self {
804 let anchor = *anchor;
805 self.bind_prop(
806 move || match anchor.node() {
807 Some(id) => OverlayAnchor::Content(id),
808 None => OverlayAnchor::None,
809 },
810 |a| Prop::Overlay(OverlayProp::Anchor(a)),
811 )
812 }
813
814 pub fn anchor_point(self, point: impl IntoReactive<Point<Points>>) -> Self {
817 self.bind_prop(point, |p| {
818 Prop::Overlay(OverlayProp::Anchor(OverlayAnchor::Point(p)))
819 })
820 }
821
822 pub fn anchor_bounds(self, bounds: impl IntoReactive<Bounds<Points>>) -> Self {
826 self.bind_prop(bounds, |b| {
827 Prop::Overlay(OverlayProp::Anchor(OverlayAnchor::Bounds(b)))
828 })
829 }
830
831 pub fn placement(self, placement: impl IntoReactive<Placement>) -> Self {
833 self.bind_prop(placement, |p| Prop::Overlay(OverlayProp::Placement(p)))
834 }
835
836 pub fn align(self, align: impl IntoReactive<Align>) -> Self {
838 self.bind_prop(align, |a| Prop::Overlay(OverlayProp::Align(a)))
839 }
840
841 pub fn offset(self, offset: impl IntoReactive<Points>) -> Self {
843 self.bind_prop(offset, |o| Prop::Overlay(OverlayProp::Offset(o)))
844 }
845
846 pub fn margin(self, margin: impl IntoReactive<Points>) -> Self {
848 self.bind_prop(margin, |m| Prop::Overlay(OverlayProp::Margin(m)))
849 }
850
851 pub fn on_press_outside<M>(self, listener: impl IntoListener<Point<Points>, M>) -> Self {
855 self.listen(
856 EventKind::PressOutside,
857 listener.into_listener(),
858 events::page_point,
859 )
860 }
861
862 pub fn on_cancel<M>(self, listener: impl IntoListener<(), M>) -> Self {
866 self.listen(EventKind::Cancel, listener.into_listener(), events::unit)
867 }
868
869 pub fn hint(self, delay: impl IntoReactive<std::time::Duration>) -> Self {
876 self.bind_prop(delay, |d| Prop::Overlay(OverlayProp::Hint(Some(d))))
877 }
878
879 pub fn hint_open(self, open: impl IntoReactive<Option<bool>>) -> Self {
884 self.bind_prop(open, |o| Prop::Overlay(OverlayProp::Open(o)))
885 }
886
887 pub fn on_hint_show<M>(self, listener: impl IntoListener<(), M>) -> Self {
890 self.listen(EventKind::Show, listener.into_listener(), events::unit)
891 }
892
893 pub fn on_hint_hide<M>(self, listener: impl IntoListener<(), M>) -> Self {
896 self.listen(EventKind::Dismiss, listener.into_listener(), events::unit)
897 }
898
899 pub fn on_position<M>(self, listener: impl IntoListener<OverlayPosition, M>) -> Self {
902 self.listen(
903 EventKind::Position,
904 listener.into_listener(),
905 events::overlay_position,
906 )
907 }
908}
909
910impl Modal {
911 pub fn animation(self, animation: impl IntoReactive<ModalAnimation>) -> Self {
913 self.bind_prop(animation, |animation: ModalAnimation| {
914 Prop::Modal(ModalProp::Animation(animation))
915 })
916 }
917
918 pub fn presentation(self, presentation: impl IntoReactive<ModalPresentation>) -> Self {
920 self.bind_prop(presentation, |presentation: ModalPresentation| {
921 Prop::Modal(ModalProp::Presentation(presentation))
922 })
923 }
924
925 pub fn status_bar(self, setting: impl IntoReactive<StatusBarSetting>) -> Self {
928 self.bind_prop(setting, |s| Prop::Screen(ScreenProp::StatusBar(Some(s))))
929 }
930
931 pub fn status_bar_translucent(self, translucent: impl IntoReactive<bool>) -> Self {
933 self.bind_prop(translucent, |translucent: bool| {
934 Prop::Modal(ModalProp::StatusBarTranslucent(translucent))
935 })
936 }
937
938 pub fn supported_orientations(
941 self,
942 orientations: impl IntoIterator<Item = Orientation>,
943 ) -> Self {
944 let bits = orientations.into_iter().fold(0, |bits, o| bits | o.bits());
945 self.set(Prop::Modal(ModalProp::Orientations(bits)))
946 }
947
948 pub fn close_on_escape(self, close: impl IntoReactive<bool>) -> Self {
951 self.bind_prop(close, |v: bool| Prop::Modal(ModalProp::CloseOnEscape(v)))
952 }
953
954 pub fn close_on_backdrop_press(self, close: impl IntoReactive<bool>) -> Self {
958 self.bind_prop(close, |v: bool| {
959 Prop::Modal(ModalProp::CloseOnBackdropPress(v))
960 })
961 }
962
963 pub fn close_on_swipe(self, close: impl IntoReactive<bool>) -> Self {
965 self.bind_prop(close, |v: bool| Prop::Modal(ModalProp::CloseOnSwipe(v)))
966 }
967
968 pub fn on_cancel<M>(self, listener: impl IntoListener<(), M>) -> Self {
972 self.listen(
973 EventKind::RequestClose,
974 listener.into_listener(),
975 events::unit,
976 )
977 }
978
979 pub fn on_open<M>(self, listener: impl IntoListener<(), M>) -> Self {
981 self.listen(EventKind::Show, listener.into_listener(), events::unit)
982 }
983
984 pub fn on_close<M>(self, listener: impl IntoListener<(), M>) -> Self {
986 self.listen(EventKind::Dismiss, listener.into_listener(), events::unit)
987 }
988}
989
990#[derive(Debug, Clone, Copy, PartialEq, Eq)]
992pub enum Orientation {
993 Portrait,
995 PortraitUpsideDown,
997 Landscape,
999 LandscapeLeft,
1001 LandscapeRight,
1003}
1004
1005impl Orientation {
1006 fn bits(self) -> u8 {
1007 use crate::ModalConfig as M;
1008 match self {
1009 Orientation::Portrait => M::PORTRAIT,
1010 Orientation::PortraitUpsideDown => M::PORTRAIT_UPSIDE_DOWN,
1011 Orientation::Landscape => M::LANDSCAPE_LEFT | M::LANDSCAPE_RIGHT,
1012 Orientation::LandscapeLeft => M::LANDSCAPE_LEFT,
1013 Orientation::LandscapeRight => M::LANDSCAPE_RIGHT,
1014 }
1015 }
1016}
1017
1018impl WebView {
1019 pub fn html(self, html: impl IntoReactive<String>, base_url: Option<&str>) -> Self {
1022 let base_url: Option<Arc<str>> = base_url.map(Into::into);
1023 self.bind_prop(html, move |html: String| {
1024 Prop::Native(NativeProp::Source(WebSource::Html {
1025 html: html.into(),
1026 base_url: base_url.clone(),
1027 }))
1028 })
1029 }
1030
1031 pub fn injected_java_script(self, script: impl IntoReactive<String>) -> Self {
1033 self.bind_prop(script, |script: String| {
1034 Prop::Native(NativeProp::InjectedJavaScript(Some(script.into())))
1035 })
1036 }
1037
1038 pub fn injected_java_script_before_content(self, script: impl IntoReactive<String>) -> Self {
1040 self.bind_prop(script, |script: String| {
1041 Prop::Native(NativeProp::InjectedJavaScriptBefore(Some(script.into())))
1042 })
1043 }
1044
1045 pub fn user_agent(self, agent: impl IntoReactive<String>) -> Self {
1047 self.bind_prop(agent, |agent: String| {
1048 Prop::Native(NativeProp::UserAgent(Some(agent.into())))
1049 })
1050 }
1051
1052 pub fn scrollable(self, enabled: impl IntoReactive<bool>) -> Self {
1054 self.bind_prop(enabled, |enabled: bool| {
1055 Prop::Native(NativeProp::Scrollable(enabled))
1056 })
1057 }
1058
1059 pub fn java_script_enabled(self, enabled: impl IntoReactive<bool>) -> Self {
1061 self.bind_prop(enabled, |enabled: bool| {
1062 Prop::Native(NativeProp::JavaScriptEnabled(enabled))
1063 })
1064 }
1065
1066 pub fn back_forward_gestures(self, enabled: impl IntoReactive<bool>) -> Self {
1068 self.bind_prop(enabled, |enabled: bool| {
1069 Prop::Native(NativeProp::BackForwardGestures(enabled))
1070 })
1071 }
1072
1073 pub fn on_load_start<M>(self, listener: impl IntoListener<WebNavigation, M>) -> Self {
1075 self.listen(
1076 EventKind::LoadStart,
1077 listener.into_listener(),
1078 events::navigation,
1079 )
1080 }
1081
1082 pub fn on_load<M>(self, listener: impl IntoListener<WebNavigation, M>) -> Self {
1084 self.listen(
1085 EventKind::Load,
1086 listener.into_listener(),
1087 events::navigation,
1088 )
1089 }
1090
1091 pub fn on_error<M>(self, listener: impl IntoListener<str, M>) -> Self {
1093 self.listen(EventKind::Error, listener.into_listener(), events::message)
1094 }
1095
1096 pub fn on_message<M>(self, listener: impl IntoListener<str, M>) -> Self {
1098 self.listen(EventKind::Message, listener.into_listener(), events::data)
1099 }
1100
1101 pub fn on_navigation_state_change<M>(
1103 self,
1104 listener: impl IntoListener<WebNavigation, M>,
1105 ) -> Self {
1106 self.listen(
1107 EventKind::NavigationStateChange,
1108 listener.into_listener(),
1109 events::navigation,
1110 )
1111 }
1112}
1113
1114impl Video {
1115 pub fn paused(self, paused: impl IntoReactive<bool>) -> Self {
1117 self.bind_prop(paused, |paused: bool| {
1118 Prop::Native(NativeProp::Paused(paused))
1119 })
1120 }
1121
1122 pub fn muted(self, muted: impl IntoReactive<bool>) -> Self {
1124 self.bind_prop(muted, |muted: bool| Prop::Native(NativeProp::Muted(muted)))
1125 }
1126
1127 pub fn volume(self, volume: impl IntoReactive<f32>) -> Self {
1129 self.bind_prop(volume, |volume: f32| {
1130 Prop::Native(NativeProp::Volume(volume))
1131 })
1132 }
1133
1134 pub fn rate(self, rate: impl IntoReactive<f32>) -> Self {
1136 self.bind_prop(rate, |rate: f32| Prop::Native(NativeProp::Rate(rate)))
1137 }
1138
1139 pub fn repeat(self, repeat: impl IntoReactive<bool>) -> Self {
1141 self.bind_prop(repeat, |repeat: bool| {
1142 Prop::Native(NativeProp::Repeat(repeat))
1143 })
1144 }
1145
1146 pub fn resize_mode(self, mode: impl IntoReactive<ResizeMode>) -> Self {
1148 self.bind_prop(mode, |mode: ResizeMode| {
1149 Prop::Native(NativeProp::VideoResizeMode(mode))
1150 })
1151 }
1152
1153 pub fn controls(self, controls: impl IntoReactive<bool>) -> Self {
1155 self.bind_prop(controls, |controls: bool| {
1156 Prop::Native(NativeProp::Controls(controls))
1157 })
1158 }
1159
1160 pub fn progress_interval(self, ms: impl IntoReactive<f32>) -> Self {
1162 self.bind_prop(ms, |ms: f32| Prop::Native(NativeProp::ProgressInterval(ms)))
1163 }
1164
1165 pub fn audio_session(self, session: impl IntoReactive<AudioSession>) -> Self {
1168 self.bind_prop(session, |session: AudioSession| {
1169 Prop::Native(NativeProp::AudioSession(session))
1170 })
1171 }
1172
1173 pub fn on_load_start<M>(self, listener: impl IntoListener<(), M>) -> Self {
1175 self.listen(EventKind::LoadStart, listener.into_listener(), events::unit)
1176 }
1177
1178 pub fn on_load<M>(self, listener: impl IntoListener<VideoInfo, M>) -> Self {
1180 self.listen(EventKind::Load, listener.into_listener(), events::video)
1181 }
1182
1183 pub fn on_progress<M>(self, listener: impl IntoListener<VideoProgress, M>) -> Self {
1185 self.listen(
1186 EventKind::Progress,
1187 listener.into_listener(),
1188 events::progress,
1189 )
1190 }
1191
1192 pub fn on_end<M>(self, listener: impl IntoListener<(), M>) -> Self {
1194 self.listen(EventKind::End, listener.into_listener(), events::unit)
1195 }
1196
1197 pub fn on_buffer<M>(self, listener: impl IntoListener<bool, M>) -> Self {
1199 self.listen(
1200 EventKind::Buffer,
1201 listener.into_listener(),
1202 events::buffering,
1203 )
1204 }
1205
1206 pub fn on_error<M>(self, listener: impl IntoListener<str, M>) -> Self {
1208 self.listen(EventKind::Error, listener.into_listener(), events::message)
1209 }
1210}
1211
1212impl NativeView {
1213 pub fn prop(self, name: &str, value: impl IntoReactive<JsonValue>) -> Self {
1216 let name: Arc<str> = name.into();
1217 self.bind_prop(value, move |value| {
1218 Prop::ComponentProp(name.clone(), Some(value))
1219 })
1220 }
1221
1222 pub fn bind_animated_prop(mut self, name: &str, value: &impl super::Animated) -> Self {
1228 if let Some(node) = value.animated_node().live() {
1229 set(
1230 &mut self.0,
1231 Prop::ComponentPropBinding(name.into(), Some(node.id())),
1232 );
1233 self.0.attrs.bound.push(node);
1234 }
1235 self
1236 }
1237
1238 pub fn on_native_event<M>(self, listener: impl IntoListener<NativeEvent, M>) -> Self {
1240 self.listen(
1241 EventKind::NativeEvent,
1242 listener.into_listener(),
1243 events::native,
1244 )
1245 }
1246}
1247
1248#[derive(Debug, Clone, PartialEq)]
1251pub struct CanvasArg(pub(crate) CanvasArgKind);
1252
1253#[derive(Debug, Clone, PartialEq)]
1254pub(crate) enum CanvasArgKind {
1255 Const(f32),
1256 Node(super::LiveNode),
1257}
1258
1259impl From<f32> for CanvasArg {
1260 fn from(n: f32) -> Self {
1261 Self(CanvasArgKind::Const(n))
1262 }
1263}
1264
1265impl From<Points> for CanvasArg {
1267 fn from(n: Points) -> Self {
1268 Self(CanvasArgKind::Const(n.0))
1269 }
1270}
1271
1272impl From<Radians> for CanvasArg {
1274 fn from(n: Radians) -> Self {
1275 Self(CanvasArgKind::Const(n.0))
1276 }
1277}
1278
1279impl<A: super::Animated> From<&A> for CanvasArg {
1281 fn from(a: &A) -> Self {
1282 Self(match a.animated_node().live() {
1283 Some(node) => CanvasArgKind::Node(node),
1284 None => CanvasArgKind::Const(0.0),
1285 })
1286 }
1287}
1288
1289#[derive(Debug, Clone, PartialEq)]
1294pub struct CanvasPaint(CanvasPaintKind);
1295
1296#[derive(Debug, Clone, PartialEq)]
1297enum CanvasPaintKind {
1298 Color(Color),
1299 Gradient(Arc<CanvasGradient>),
1300 Node(super::LiveNode),
1301}
1302
1303impl From<Color> for CanvasPaint {
1304 fn from(color: Color) -> Self {
1305 Self(CanvasPaintKind::Color(color))
1306 }
1307}
1308
1309impl From<Hsla> for CanvasPaint {
1310 fn from(color: Hsla) -> Self {
1311 Color::from(color).into()
1312 }
1313}
1314
1315impl From<Rgba> for CanvasPaint {
1316 fn from(color: Rgba) -> Self {
1317 Color::from(color).into()
1318 }
1319}
1320
1321impl From<CanvasGradient> for CanvasPaint {
1322 fn from(gradient: CanvasGradient) -> Self {
1323 Self(CanvasPaintKind::Gradient(Arc::new(gradient)))
1324 }
1325}
1326
1327impl From<Arc<CanvasGradient>> for CanvasPaint {
1328 fn from(gradient: Arc<CanvasGradient>) -> Self {
1329 Self(CanvasPaintKind::Gradient(gradient))
1330 }
1331}
1332
1333impl<A: super::Animated> From<&A> for CanvasPaint {
1335 fn from(a: &A) -> Self {
1336 Self(match a.animated_node().live() {
1337 Some(node) => CanvasPaintKind::Node(node),
1338 None => CanvasPaintKind::Color(Color::TRANSPARENT),
1339 })
1340 }
1341}
1342
1343#[derive(Debug, Clone, Default)]
1351pub struct Painter {
1352 drawing: Drawing,
1353 nodes: Vec<super::LiveNode>,
1355}
1356
1357impl Painter {
1358 pub fn new() -> Self {
1360 Self::default()
1361 }
1362
1363 pub fn finish(self) -> Drawing {
1365 self.drawing
1366 }
1367
1368 pub fn num(&mut self, value: impl Into<CanvasArg>) -> Num {
1372 match value.into().0 {
1373 CanvasArgKind::Const(n) => Num::Const(n),
1374 CanvasArgKind::Node(node) => Num::Slot(self.slot(node)),
1375 }
1376 }
1377
1378 pub fn paint(&mut self, paint: impl Into<CanvasPaint>) -> Paint {
1381 match paint.into().0 {
1382 CanvasPaintKind::Color(color) => Paint::Color(color),
1383 CanvasPaintKind::Gradient(gradient) => Paint::Gradient(gradient),
1384 CanvasPaintKind::Node(node) => Paint::Slot(self.slot(node)),
1385 }
1386 }
1387
1388 fn slot(&mut self, node: super::LiveNode) -> u16 {
1389 let slot = self.drawing.slot(node.id());
1390 self.nodes.push(node);
1391 slot
1392 }
1393
1394 pub fn node(mut self, node: crate::canvas::CanvasNode) -> Self {
1397 self.drawing.nodes.push(node);
1398 self
1399 }
1400
1401 pub fn op(mut self, op: CanvasOp) -> Self {
1404 self.drawing.ops.push(op);
1405 self
1406 }
1407
1408 fn path(self, op: PathOp) -> Self {
1409 self.op(CanvasOp::Path(op))
1410 }
1411
1412 pub fn is_point_in_path(&self, point: Point<Points>, rule: FillRule) -> bool {
1419 crate::canvas::point_in_path(&self.drawing.ops, None, rule, point, |_| 0.0)
1420 }
1421
1422 pub fn is_point_in_stroke(&self, point: Point<Points>) -> bool {
1426 crate::canvas::point_in_stroke(&self.drawing.ops, None, point, |_| 0.0)
1427 }
1428
1429 pub fn path_contains(&self, path: &Path, point: Point<Points>, rule: FillRule) -> bool {
1434 crate::canvas::point_in_path(&self.drawing.ops, Some(path), rule, point, |_| 0.0)
1435 }
1436
1437 pub fn stroke_contains(&self, path: &Path, point: Point<Points>) -> bool {
1441 crate::canvas::point_in_stroke(&self.drawing.ops, Some(path), point, |_| 0.0)
1442 }
1443
1444 pub fn save(self) -> Self {
1446 self.op(CanvasOp::Save)
1447 }
1448
1449 pub fn restore(self) -> Self {
1451 self.op(CanvasOp::Restore)
1452 }
1453
1454 pub fn translate(mut self, x: impl Into<CanvasArg>, y: impl Into<CanvasArg>) -> Self {
1456 let (x, y) = (self.num(x), self.num(y));
1457 self.op(CanvasOp::Translate(x, y))
1458 }
1459
1460 pub fn scale(mut self, x: impl Into<CanvasArg>, y: impl Into<CanvasArg>) -> Self {
1462 let (x, y) = (self.num(x), self.num(y));
1463 self.op(CanvasOp::Scale(x, y))
1464 }
1465
1466 pub fn rotate(mut self, angle: impl Into<CanvasArg>) -> Self {
1469 let a = self.num(angle);
1470 self.op(CanvasOp::Rotate(a))
1471 }
1472
1473 pub fn transform(mut self, m: [impl Into<CanvasArg>; 6]) -> Self {
1476 let m = m.map(|n| self.num(n));
1477 self.op(CanvasOp::Transform(m))
1478 }
1479
1480 pub fn set_transform(mut self, m: [impl Into<CanvasArg>; 6]) -> Self {
1483 let m = m.map(|n| self.num(n));
1484 self.op(CanvasOp::SetTransform(m))
1485 }
1486
1487 pub fn reset_transform(self) -> Self {
1489 self.set_transform([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
1490 }
1491
1492 pub fn begin_path(self) -> Self {
1494 self.op(CanvasOp::BeginPath)
1495 }
1496
1497 pub fn move_to(mut self, x: impl Into<CanvasArg>, y: impl Into<CanvasArg>) -> Self {
1499 let (x, y) = (self.num(x), self.num(y));
1500 self.path(PathOp::MoveTo(x, y))
1501 }
1502
1503 pub fn line_to(mut self, x: impl Into<CanvasArg>, y: impl Into<CanvasArg>) -> Self {
1505 let (x, y) = (self.num(x), self.num(y));
1506 self.path(PathOp::LineTo(x, y))
1507 }
1508
1509 pub fn quadratic_curve_to(self, control: Point<Points>, to: Point<Points>) -> Self {
1511 let c = |v: Points| Num::Const(v.0);
1512 self.path(PathOp::QuadTo([
1513 c(control.x),
1514 c(control.y),
1515 c(to.x),
1516 c(to.y),
1517 ]))
1518 }
1519
1520 pub fn bezier_curve_to(self, c1: Point<Points>, c2: Point<Points>, to: Point<Points>) -> Self {
1522 let c = |v: Points| Num::Const(v.0);
1523 self.path(PathOp::CubicTo([
1524 c(c1.x),
1525 c(c1.y),
1526 c(c2.x),
1527 c(c2.y),
1528 c(to.x),
1529 c(to.y),
1530 ]))
1531 }
1532
1533 pub fn arc(
1536 self,
1537 x: impl Into<CanvasArg>,
1538 y: impl Into<CanvasArg>,
1539 radius: impl Into<CanvasArg>,
1540 start: impl Into<CanvasArg>,
1541 end: impl Into<CanvasArg>,
1542 ) -> Self {
1543 self.arc_in(x, y, radius, start, end, false)
1544 }
1545
1546 pub fn arc_counterclockwise(
1548 self,
1549 x: impl Into<CanvasArg>,
1550 y: impl Into<CanvasArg>,
1551 radius: impl Into<CanvasArg>,
1552 start: impl Into<CanvasArg>,
1553 end: impl Into<CanvasArg>,
1554 ) -> Self {
1555 self.arc_in(x, y, radius, start, end, true)
1556 }
1557
1558 fn arc_in(
1559 mut self,
1560 x: impl Into<CanvasArg>,
1561 y: impl Into<CanvasArg>,
1562 radius: impl Into<CanvasArg>,
1563 start: impl Into<CanvasArg>,
1564 end: impl Into<CanvasArg>,
1565 ccw: bool,
1566 ) -> Self {
1567 let (x, y, radius, start, end) = (
1568 self.num(x),
1569 self.num(y),
1570 self.num(radius),
1571 self.num(start),
1572 self.num(end),
1573 );
1574 self.path(PathOp::Arc {
1575 x,
1576 y,
1577 radius,
1578 start,
1579 end,
1580 ccw,
1581 })
1582 }
1583
1584 pub fn arc_to(self, p1: Point<Points>, p2: Point<Points>, radius: Points) -> Self {
1587 let c = |v: Points| Num::Const(v.0);
1588 self.path(PathOp::ArcTo([
1589 c(p1.x),
1590 c(p1.y),
1591 c(p2.x),
1592 c(p2.y),
1593 c(radius),
1594 ]))
1595 }
1596
1597 pub fn ellipse(
1601 self,
1602 center: Point<Points>,
1603 radii: Size<Points>,
1604 rotation: Radians,
1605 start: Radians,
1606 end: Radians,
1607 counterclockwise: bool,
1608 ) -> Self {
1609 let c = Num::Const;
1610 self.path(PathOp::Ellipse {
1611 geometry: [
1612 c(center.x.0),
1613 c(center.y.0),
1614 c(radii.width.0),
1615 c(radii.height.0),
1616 c(rotation.0),
1617 ],
1618 start: c(start.0),
1619 end: c(end.0),
1620 ccw: counterclockwise,
1621 })
1622 }
1623
1624 pub fn rect(self, rect: Bounds<Points>) -> Self {
1626 self.path(PathOp::Rect(nums(rect)))
1627 }
1628
1629 pub fn round_rect(self, rect: Bounds<Points>, radius: Points) -> Self {
1631 self.round_rect_corners(rect, Corners::all(radius))
1632 }
1633
1634 pub fn round_rect_corners(self, rect: Bounds<Points>, radii: Corners<Points>) -> Self {
1636 let c = |v: Points| Num::Const(v.0);
1637 self.path(PathOp::RoundRect(
1638 nums(rect),
1639 [
1640 c(radii.top_left),
1641 c(radii.top_right),
1642 c(radii.bottom_right),
1643 c(radii.bottom_left),
1644 ],
1645 ))
1646 }
1647
1648 pub fn close_path(self) -> Self {
1650 self.path(PathOp::Close)
1651 }
1652
1653 pub fn fill(self) -> Self {
1655 self.fill_with(FillRule::NonZero)
1656 }
1657
1658 pub fn fill_with(self, rule: FillRule) -> Self {
1660 self.op(CanvasOp::Fill(None, rule))
1661 }
1662
1663 pub fn fill_path(self, path: &Path, rule: FillRule) -> Self {
1666 self.op(CanvasOp::Fill(Some(path.clone()), rule))
1667 }
1668
1669 pub fn fill_svg(self, data: &str) -> Self {
1672 self.fill_path(&crate::canvas::svg_path(data), FillRule::NonZero)
1673 }
1674
1675 pub fn stroke(self) -> Self {
1677 self.op(CanvasOp::Stroke(None))
1678 }
1679
1680 pub fn stroke_path(self, path: &Path) -> Self {
1682 self.op(CanvasOp::Stroke(Some(path.clone())))
1683 }
1684
1685 pub fn stroke_svg(self, data: &str) -> Self {
1687 self.stroke_path(&crate::canvas::svg_path(data))
1688 }
1689
1690 pub fn clip(self) -> Self {
1692 self.clip_with(FillRule::NonZero)
1693 }
1694
1695 pub fn clip_with(self, rule: FillRule) -> Self {
1697 self.op(CanvasOp::Clip(None, rule))
1698 }
1699
1700 pub fn clip_path(self, path: &Path, rule: FillRule) -> Self {
1702 self.op(CanvasOp::Clip(Some(path.clone()), rule))
1703 }
1704
1705 pub fn fill_rect(
1707 mut self,
1708 x: impl Into<CanvasArg>,
1709 y: impl Into<CanvasArg>,
1710 w: impl Into<CanvasArg>,
1711 h: impl Into<CanvasArg>,
1712 ) -> Self {
1713 let (x, y, w, h) = (self.num(x), self.num(y), self.num(w), self.num(h));
1714 self.op(CanvasOp::FillRect([x, y, w, h]))
1715 }
1716
1717 pub fn stroke_rect(self, rect: Bounds<Points>) -> Self {
1719 self.op(CanvasOp::StrokeRect(nums(rect)))
1720 }
1721
1722 pub fn clear_rect(self, rect: Bounds<Points>) -> Self {
1724 self.op(CanvasOp::ClearRect(nums(rect)))
1725 }
1726
1727 pub fn fill_color(self, color: impl Into<Hsla>) -> Self {
1729 self.fill_style(color.into())
1730 }
1731
1732 pub fn stroke_color(self, color: impl Into<Hsla>) -> Self {
1734 self.stroke_style(color.into())
1735 }
1736
1737 pub fn fill_style(mut self, paint: impl Into<CanvasPaint>) -> Self {
1740 let paint = self.paint(paint);
1741 self.op(CanvasOp::FillStyle(paint))
1742 }
1743
1744 pub fn fill_style_from_view(self, initial: SvgPaint) -> Self {
1750 self.view_paint(
1751 ViewPaint::Fill(initial),
1752 CanvasOp::FillStyle(Paint::Color(Color::TRANSPARENT)),
1753 )
1754 }
1755
1756 pub fn stroke_style_from_view(self, initial: SvgPaint) -> Self {
1759 self.view_paint(
1760 ViewPaint::Stroke(initial),
1761 CanvasOp::StrokeStyle(Paint::Color(Color::TRANSPARENT)),
1762 )
1763 }
1764
1765 pub fn line_width_from_view(self) -> Self {
1768 self.view_paint(ViewPaint::StrokeWidth, CanvasOp::LineWidth(Num::Const(1.0)))
1769 }
1770
1771 pub fn line_dash_from_view(self) -> Self {
1774 self.view_paint(
1775 ViewPaint::StrokeDasharray,
1776 CanvasOp::LineDash(Arc::from([])),
1777 )
1778 }
1779
1780 pub fn fill_background_from_view(self, area: Bounds<Points>) -> Self {
1789 self.view_paint(
1790 ViewPaint::Background {
1791 area,
1792 rule: FillRule::NonZero,
1793 },
1794 CanvasOp::Fill(None, FillRule::NonZero),
1795 )
1796 }
1797
1798 fn view_paint(mut self, view: ViewPaint, placeholder: CanvasOp) -> Self {
1799 let at = self.drawing.ops.len() as u32;
1800 self.drawing.view_paints.push((at, view));
1801 self.op(placeholder)
1802 }
1803
1804 pub fn stroke_style(mut self, paint: impl Into<CanvasPaint>) -> Self {
1806 let paint = self.paint(paint);
1807 self.op(CanvasOp::StrokeStyle(paint))
1808 }
1809
1810 pub fn line_width(mut self, width: impl Into<CanvasArg>) -> Self {
1812 let w = self.num(width);
1813 self.op(CanvasOp::LineWidth(w))
1814 }
1815
1816 pub fn line_cap(self, cap: LineCap) -> Self {
1818 self.op(CanvasOp::LineCap(cap))
1819 }
1820
1821 pub fn line_join(self, join: LineJoin) -> Self {
1823 self.op(CanvasOp::LineJoin(join))
1824 }
1825
1826 pub fn miter_limit(self, limit: f32) -> Self {
1829 self.op(CanvasOp::MiterLimit(limit.max(1.0)))
1830 }
1831
1832 pub fn line_dash(self, segments: &[Points]) -> Self {
1834 self.op(CanvasOp::LineDash(segments.into()))
1835 }
1836
1837 pub fn line_dash_offset(mut self, offset: impl Into<CanvasArg>) -> Self {
1840 let o = self.num(offset);
1841 self.op(CanvasOp::LineDashOffset(o))
1842 }
1843
1844 pub fn global_alpha(mut self, alpha: impl Into<CanvasArg>) -> Self {
1846 let a = self.num(alpha);
1847 self.op(CanvasOp::GlobalAlpha(a))
1848 }
1849
1850 pub fn composite(self, composite: Composite) -> Self {
1852 self.op(CanvasOp::Composite(composite))
1853 }
1854
1855 pub fn shadow(self, color: impl Into<Hsla>, blur: Points, offset: Point<Points>) -> Self {
1857 self.op(CanvasOp::ShadowColor(Color::from(color.into())))
1858 .op(CanvasOp::ShadowBlur(blur.max(Points::ZERO)))
1859 .op(CanvasOp::ShadowOffset(offset))
1860 }
1861
1862 pub fn font(self, font: &str) -> Self {
1865 match CanvasFont::parse(font) {
1866 Some(font) => self.op(CanvasOp::Font(font)),
1867 None => self,
1868 }
1869 }
1870
1871 pub fn font_variant_numeric(self, numeric: crate::style::FontVariant) -> Self {
1875 self.op(CanvasOp::FontVariantNumeric(crate::style::FontVariant(
1876 numeric.0 & !crate::style::FontVariant::SMALL_CAPS,
1877 )))
1878 }
1879
1880 pub fn text_align(self, align: CanvasTextAlign) -> Self {
1882 self.op(CanvasOp::TextAlign(align))
1883 }
1884
1885 pub fn text_baseline(self, baseline: CanvasTextBaseline) -> Self {
1887 self.op(CanvasOp::TextBaseline(baseline))
1888 }
1889
1890 pub fn fill_text(self, text: &str, x: impl Into<CanvasArg>, y: impl Into<CanvasArg>) -> Self {
1892 self.text(text, x, y, None, false)
1893 }
1894
1895 pub fn fill_text_max_width(
1897 self,
1898 text: &str,
1899 x: impl Into<CanvasArg>,
1900 y: impl Into<CanvasArg>,
1901 max_width: Points,
1902 ) -> Self {
1903 self.text(text, x, y, Some(max_width), false)
1904 }
1905
1906 pub fn stroke_text(self, text: &str, x: impl Into<CanvasArg>, y: impl Into<CanvasArg>) -> Self {
1908 self.text(text, x, y, None, true)
1909 }
1910
1911 pub fn stroke_text_max_width(
1914 self,
1915 text: &str,
1916 x: impl Into<CanvasArg>,
1917 y: impl Into<CanvasArg>,
1918 max_width: Points,
1919 ) -> Self {
1920 self.text(text, x, y, Some(max_width), true)
1921 }
1922
1923 fn text(
1924 mut self,
1925 text: &str,
1926 x: impl Into<CanvasArg>,
1927 y: impl Into<CanvasArg>,
1928 max_width: Option<Points>,
1929 stroke: bool,
1930 ) -> Self {
1931 let (text, x, y) = (text.into(), self.num(x), self.num(y));
1932 self.op(if stroke {
1933 CanvasOp::StrokeText {
1934 text,
1935 x,
1936 y,
1937 max_width,
1938 }
1939 } else {
1940 CanvasOp::FillText {
1941 text,
1942 x,
1943 y,
1944 max_width,
1945 }
1946 })
1947 }
1948
1949 pub fn draw_image(self, uri: &str, dest: Bounds<Points>) -> Self {
1951 self.draw_image_part(uri, None, dest)
1952 }
1953
1954 pub fn draw_image_part(
1957 self,
1958 uri: &str,
1959 source: Option<Bounds<ScaledPixels>>,
1960 dest: Bounds<Points>,
1961 ) -> Self {
1962 self.op(CanvasOp::DrawImage {
1963 image: CanvasImage { uri: uri.into() },
1964 source,
1965 dest: nums(dest),
1966 })
1967 }
1968}
1969
1970fn nums(rect: Bounds<Points>) -> [Num; 4] {
1972 [
1973 rect.origin.x,
1974 rect.origin.y,
1975 rect.size.width,
1976 rect.size.height,
1977 ]
1978 .map(|v| Num::Const(v.0))
1979}
1980
1981impl From<Drawing> for Painter {
1982 fn from(drawing: Drawing) -> Self {
1983 Self {
1984 drawing,
1985 nodes: Vec::new(),
1986 }
1987 }
1988}
1989
1990crate::static_reactive!(ScreenAnimation: ScreenAnimation);