1use crate::{
4 handler::{ArcHandler, Handler, HandlerExt},
5 widget::node::IntoUiNode,
6};
7use std::{
8 any::{Any, TypeId},
9 collections::{HashMap, hash_map},
10 fmt, ops,
11 sync::Arc,
12};
13
14#[doc(hidden)]
15pub use zng_var::{var_getter, var_state};
16
17#[macro_export]
21macro_rules! source_location {
22 () => {
23 $crate::widget::builder::SourceLocation::new(std::file!(), std::line!(), std::column!())
24 };
25}
26#[doc(inline)]
27pub use crate::source_location;
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
35#[non_exhaustive]
36pub struct SourceLocation {
37 pub file: &'static str,
39 pub line: u32,
41 pub column: u32,
43}
44
45impl SourceLocation {
46 #[doc(hidden)]
47 pub fn new(file: &'static str, line: u32, column: u32) -> Self {
48 Self { file, line, column }
49 }
50}
51impl fmt::Display for SourceLocation {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "{}:{}:{}", self.file, self.line, self.column)
54 }
55}
56
57#[doc(hidden)]
58pub struct WgtInfo;
59impl WidgetExt for WgtInfo {
60 fn ext_property__(&mut self, _: Box<dyn PropertyArgs>) {
61 panic!("WgtInfo is for extracting info only")
62 }
63
64 fn ext_property_unset__(&mut self, _: PropertyId) {
65 panic!("WgtInfo is for extracting info only")
66 }
67}
68
69#[macro_export]
102macro_rules! property_id {
103 ($($tt:tt)*) => {
104 $crate::widget::property_meta!($($tt)*).id()
105 }
106}
107#[doc(inline)]
108pub use crate::property_id;
109
110#[macro_export]
137macro_rules! property_info {
138 ($($property:ident)::+ <$($generics:ty),*>) => {
139 $crate::widget::property_meta!($($property)::+).info::<$($generics),*>()
140 };
141 ($($tt:tt)*) => {
142 $crate::widget::property_meta!($($tt)*).info()
143 }
144}
145#[doc(inline)]
146pub use crate::property_info;
147
148#[macro_export]
157macro_rules! property_input_types {
158 ($($tt:tt)*) => {
159 $crate::widget::property_meta!($($tt)*).input_types()
160 }
161}
162#[doc(inline)]
163pub use crate::property_input_types;
164
165#[macro_export]
183macro_rules! property_args {
184 ($($property:ident)::+ = $($value:tt)*) => {
185 {
186 $crate::widget::builder::PropertyArgsGetter! {
187 $($property)::+ = $($value)*
188 }
189 }
190 };
191 ($($property:ident)::+ ::<$($generics:ty),*> = $($value:tt)*) => {
192 {
193 $crate::widget::builder::PropertyArgsGetter! {
194 $($property)::+ ::<$($generics),*> = $($value)*
195 }
196 }
197 };
198 ($property:ident $(;)?) => {
199 {
200 $crate::widget::builder::PropertyArgsGetter! {
201 $property
202 }
203 }
204 }
205}
206#[doc(inline)]
207pub use crate::property_args;
208
209#[macro_export]
211macro_rules! widget_type {
212 ($($widget:ident)::+) => {
213 $($widget)::+::widget_type()
214 };
215}
216use parking_lot::Mutex;
217#[doc(inline)]
218pub use widget_type;
219use zng_app_context::context_local;
220use zng_app_proc_macros::widget;
221use zng_txt::{Txt, formatx};
222use zng_unique_id::{IdEntry, IdMap, IdSet, unique_id_32};
223use zng_var::{
224 AnyVar, AnyVarValue, AnyWhenVarBuilder, ContextInitHandle, IntoValue, IntoVar, Var, VarValue, WeakContextInitHandle, any_const_var,
225 const_var, contextual_var, impl_from_and_into_var,
226};
227
228use super::{
229 base::{WidgetBase, WidgetExt},
230 node::{ArcNode, FillUiNode, UiNode, WhenUiNodeBuilder, with_new_context_init_id},
231};
232
233#[doc(hidden)]
234#[widget($crate::widget::builder::PropertyArgsGetter)]
235pub struct PropertyArgsGetter(WidgetBase);
236impl PropertyArgsGetter {
237 pub fn widget_build(&mut self) -> Box<dyn PropertyArgs> {
238 let mut wgt = self.widget_take();
239 if !wgt.p.items.is_empty() {
240 if wgt.p.items.len() > 1 {
241 tracing::error!("properties ignored, `property_args!` only collects args for first property");
242 }
243 match wgt.p.items.remove(0).item {
244 WidgetItem::Property { args, .. } => args,
245 WidgetItem::Intrinsic { .. } => unreachable!(),
246 }
247 } else if wgt.unset.is_empty() {
248 panic!("missing property");
249 } else {
250 panic!("cannot use `unset!` in `property_args!`")
251 }
252 }
253}
254
255#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
259pub struct NestPosition {
260 pub group: NestGroup,
262 pub index: u16,
264}
265impl NestPosition {
266 pub const INTRINSIC_INDEX: u16 = u16::MAX / 3;
268
269 pub const PROPERTY_INDEX: u16 = Self::INTRINSIC_INDEX * 2;
271
272 pub fn property(group: NestGroup) -> Self {
274 NestPosition {
275 group,
276 index: Self::PROPERTY_INDEX,
277 }
278 }
279
280 pub fn intrinsic(group: NestGroup) -> Self {
282 NestPosition {
283 group,
284 index: Self::INTRINSIC_INDEX,
285 }
286 }
287}
288impl fmt::Debug for NestPosition {
289 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
290 struct IndexName(u16);
291 impl fmt::Debug for IndexName {
292 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
293 match self.0 {
294 NestPosition::INTRINSIC_INDEX => write!(f, "INTRINSIC_INDEX"),
295 NestPosition::PROPERTY_INDEX => write!(f, "PROPERTY_INDEX"),
296 i => write!(f, "{i}"),
297 }
298 }
299 }
300
301 f.debug_struct("NestPosition")
302 .field("group", &self.group)
303 .field("index", &IndexName(self.index))
304 .finish()
305 }
306}
307
308macro_rules! nest_group_items {
309 () => {
310 pub const WIDGET: NestGroup = NestGroup(0);
314
315 pub const CONTEXT: NestGroup = NestGroup(NestGroup::NEXT_GROUP);
322 pub const EVENT: NestGroup = NestGroup(NestGroup::CONTEXT.0 + NestGroup::NEXT_GROUP);
330 pub const LAYOUT: NestGroup = NestGroup(NestGroup::EVENT.0 + NestGroup::NEXT_GROUP);
335
336 pub const SIZE: NestGroup = NestGroup(NestGroup::LAYOUT.0 + NestGroup::NEXT_GROUP);
341
342 pub const WIDGET_INNER: NestGroup = NestGroup(NestGroup::SIZE.0 + NestGroup::NEXT_GROUP);
348
349 pub const BORDER: NestGroup = NestGroup(NestGroup::WIDGET_INNER.0 + NestGroup::NEXT_GROUP);
356 pub const FILL: NestGroup = NestGroup(NestGroup::BORDER.0 + NestGroup::NEXT_GROUP);
361 pub const CHILD_CONTEXT: NestGroup = NestGroup(NestGroup::FILL.0 + NestGroup::NEXT_GROUP);
364 pub const CHILD_LAYOUT: NestGroup = NestGroup(NestGroup::CHILD_CONTEXT.0 + NestGroup::NEXT_GROUP);
368
369 pub const CHILD: NestGroup = NestGroup(u16::MAX);
374 };
375}
376
377#[doc(hidden)]
378pub mod nest_group_items {
379 use super::NestGroup;
383
384 nest_group_items!();
385}
386
387#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
394pub struct NestGroup(u16);
395impl NestGroup {
396 const NEXT_GROUP: u16 = u16::MAX / 10;
397
398 nest_group_items!();
399
400 pub const ITEMS: [Self; 11] = [
405 Self::WIDGET,
406 Self::CONTEXT,
407 Self::EVENT,
408 Self::LAYOUT,
409 Self::SIZE,
410 Self::WIDGET_INNER,
411 Self::BORDER,
412 Self::FILL,
413 Self::CHILD_CONTEXT,
414 Self::CHILD_LAYOUT,
415 Self::CHILD,
416 ];
417
418 fn exact_name(self) -> &'static str {
419 if self.0 == Self::WIDGET.0 {
420 "WIDGET"
421 } else if self.0 == Self::CONTEXT.0 {
422 "CONTEXT"
423 } else if self.0 == Self::EVENT.0 {
424 "EVENT"
425 } else if self.0 == Self::LAYOUT.0 {
426 "LAYOUT"
427 } else if self.0 == Self::SIZE.0 {
428 "SIZE"
429 } else if self.0 == Self::WIDGET_INNER.0 {
430 "WIDGET_INNER"
431 } else if self.0 == Self::BORDER.0 {
432 "BORDER"
433 } else if self.0 == Self::FILL.0 {
434 "FILL"
435 } else if self.0 == Self::CHILD_CONTEXT.0 {
436 "CHILD_CONTEXT"
437 } else if self.0 == Self::CHILD_LAYOUT.0 {
438 "CHILD_LAYOUT"
439 } else if self.0 == Self::CHILD.0 {
440 "CHILD"
441 } else {
442 ""
443 }
444 }
445
446 pub fn name(self) -> Txt {
448 let name = self.exact_name();
449 if name.is_empty() {
450 let closest = Self::ITEMS.into_iter().min_by_key(|i| (self.0 as i32 - i.0 as i32).abs()).unwrap();
451 let diff = self.0 as i32 - closest.0 as i32;
452
453 let name = closest.exact_name();
454 debug_assert!(!name.is_empty());
455
456 formatx!("{closest}{diff:+}")
457 } else {
458 Txt::from_static(name)
459 }
460 }
461}
462impl fmt::Debug for NestGroup {
463 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
464 if f.alternate() {
465 write!(f, "NestGroup::")?;
466 }
467 write!(f, "{}", self.name())
468 }
469}
470impl fmt::Display for NestGroup {
471 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
472 write!(f, "{}", self.name())
473 }
474}
475impl ops::Add<i16> for NestGroup {
476 type Output = Self;
477
478 fn add(self, rhs: i16) -> Self::Output {
479 let r = (self.0 as i32) + rhs as i32;
480
481 Self(r.clamp(0, u16::MAX as i32) as u16)
482 }
483}
484impl ops::Sub<i16> for NestGroup {
485 type Output = Self;
486
487 fn sub(self, rhs: i16) -> Self::Output {
488 let r = (self.0 as i32) - rhs as i32;
489
490 Self(r.clamp(0, u16::MAX as i32) as u16)
491 }
492}
493impl ops::AddAssign<i16> for NestGroup {
494 fn add_assign(&mut self, rhs: i16) {
495 *self = *self + rhs;
496 }
497}
498impl ops::SubAssign<i16> for NestGroup {
499 fn sub_assign(&mut self, rhs: i16) {
500 *self = *self - rhs;
501 }
502}
503#[test]
504fn nest_group_spacing() {
505 let mut expected = NestGroup::NEXT_GROUP;
506 for g in &NestGroup::ITEMS[1..NestGroup::ITEMS.len() - 1] {
507 assert_eq!(expected, g.0);
508 expected += NestGroup::NEXT_GROUP;
509 }
510 assert_eq!(expected, (u16::MAX / 10) * 10); }
512#[derive(serde::Deserialize)]
513#[serde(untagged)]
514enum NestGroupSerde<'s> {
515 Named(&'s str),
516 Unnamed(u16),
517}
518impl serde::Serialize for NestGroup {
519 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
520 where
521 S: serde::Serializer,
522 {
523 if serializer.is_human_readable() {
524 self.name().serialize(serializer)
525 } else {
526 self.0.serialize(serializer)
527 }
528 }
529}
530impl<'de> serde::Deserialize<'de> for NestGroup {
531 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
532 where
533 D: serde::Deserializer<'de>,
534 {
535 use serde::de::Error;
536
537 match NestGroupSerde::deserialize(deserializer)? {
538 NestGroupSerde::Named(n) => match n.parse() {
539 Ok(g) => Ok(g),
540 Err(e) => Err(D::Error::custom(e)),
541 },
542 NestGroupSerde::Unnamed(i) => Ok(NestGroup(i)),
543 }
544 }
545}
546impl std::str::FromStr for NestGroup {
547 type Err = String;
548
549 fn from_str(s: &str) -> Result<Self, Self::Err> {
550 let mut name = s;
551 let mut add = 0i16;
552
553 if let Some((n, a)) = s.split_once('+') {
554 add = a.parse().map_err(|e| format!("{e}"))?;
555 name = n;
556 } else if let Some((n, s)) = s.split_once('-') {
557 add = -s.parse().map_err(|e| format!("{e}"))?;
558 name = n;
559 }
560
561 match name {
562 "WIDGET" => Ok(NestGroup::WIDGET + add),
563 "CONTEXT" => Ok(NestGroup::CONTEXT + add),
564 "EVENT" => Ok(NestGroup::EVENT + add),
565 "LAYOUT" => Ok(NestGroup::LAYOUT + add),
566 "SIZE" => Ok(NestGroup::SIZE + add),
567 "BORDER" => Ok(NestGroup::BORDER + add),
568 "FILL" => Ok(NestGroup::FILL + add),
569 "CHILD_CONTEXT" => Ok(NestGroup::CHILD_CONTEXT + add),
570 "CHILD_LAYOUT" => Ok(NestGroup::CHILD_LAYOUT + add),
571 "CHILD" => Ok(NestGroup::CHILD + add),
572 ukn => Err(format!("unknown nest group {ukn:?}")),
573 }
574 }
575}
576
577#[derive(PartialEq, Eq, Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
579pub enum InputKind {
580 Var,
582 Value,
584 UiNode,
586 Handler,
588}
589
590pub trait AnyArcHandler: Any {
592 fn as_any(&self) -> &dyn Any;
594
595 fn into_any(self: Box<Self>) -> Box<dyn Any>;
597
598 fn clone_boxed(&self) -> Box<dyn AnyArcHandler>;
600}
601impl<A: Clone + 'static> AnyArcHandler for ArcHandler<A> {
602 fn clone_boxed(&self) -> Box<dyn AnyArcHandler> {
603 Box::new(self.clone())
604 }
605
606 fn as_any(&self) -> &dyn Any {
607 self
608 }
609
610 fn into_any(self: Box<Self>) -> Box<dyn Any> {
611 self
612 }
613}
614
615pub struct AnyWhenArcHandlerBuilder {
619 default: Box<dyn AnyArcHandler>,
620 conditions: Vec<(Var<bool>, Box<dyn AnyArcHandler>)>,
621}
622impl AnyWhenArcHandlerBuilder {
623 pub fn new(default: Box<dyn AnyArcHandler>) -> Self {
625 Self {
626 default,
627 conditions: vec![],
628 }
629 }
630
631 pub fn push(&mut self, condition: Var<bool>, handler: Box<dyn AnyArcHandler>) {
633 self.conditions.push((condition, handler));
634 }
635
636 pub fn build<A: Clone + 'static>(self) -> ArcHandler<A> {
638 match self.default.into_any().downcast::<ArcHandler<A>>() {
639 Ok(default) => {
640 let mut conditions = Vec::with_capacity(self.conditions.len());
641 for (c, h) in self.conditions {
642 match h.into_any().downcast::<ArcHandler<A>>() {
643 Ok(h) => conditions.push((c, *h)),
644 Err(_) => continue,
645 }
646 }
647 let handler: Handler<A> = Box::new(move |args: &A| {
648 for (c, h) in &conditions {
649 if c.get() {
650 return h.call(args);
651 }
652 }
653 default.call(args)
654 });
655 handler.into_arc()
656 }
657 Err(_) => panic!("unexpected build type in widget handler when builder"),
658 }
659 }
660}
661
662pub type PropertyAttributes = Vec<Vec<Box<dyn AnyPropertyAttribute>>>;
666
667pub type PropertyAttributesWhenData = Vec<Vec<Option<PropertyAttributeWhenData>>>;
671
672#[non_exhaustive]
674pub struct PropertyNewArgs {
675 pub args: Vec<Box<dyn Any>>,
694
695 pub attributes: PropertyAttributes,
714
715 pub attributes_when_data: PropertyAttributesWhenData,
721}
722
723#[derive(Debug, Clone)]
727pub struct PropertyInfo {
728 pub group: NestGroup,
730 pub build_action: bool,
734
735 pub id: PropertyId,
737 pub name: &'static str,
739
740 pub location: SourceLocation,
742
743 pub default: Option<fn() -> Box<dyn PropertyArgs>>,
747
748 pub new: fn(PropertyNewArgs) -> Box<dyn PropertyArgs>,
758
759 pub inputs: Box<[PropertyInput]>,
761
762 #[doc(hidden)] pub _non_exhaustive: (),
764}
765impl PropertyInfo {
766 pub fn input_idx(&self, name: &str) -> Option<usize> {
768 self.inputs.iter().position(|i| i.name == name)
769 }
770}
771
772#[derive(Debug, Clone)]
774pub struct PropertyInput {
775 pub name: &'static str,
777 pub kind: InputKind,
779
780 pub ty: TypeId,
796 pub ty_name: &'static str,
800
801 #[doc(hidden)] pub _non_exhaustive: (),
803}
804impl PropertyInput {
805 pub fn short_ty_name(&self) -> Txt {
809 pretty_type_name::pretty_type_name_str(self.ty_name).into()
810 }
811
812 pub fn actual_ty_name(&self) -> Txt {
814 match self.kind {
815 InputKind::Var => formatx!("Var<{}>", self.short_ty_name()),
816 InputKind::Handler => formatx!("Handler<{}>", self.short_ty_name()),
817 InputKind::Value => self.short_ty_name(),
818 InputKind::UiNode => self.short_ty_name(),
819 }
820 }
821
822 pub fn input_ty_name(&self) -> Txt {
836 match self.kind {
837 InputKind::Var => formatx!("impl IntoVar<{}>", self.short_ty_name()),
838 InputKind::Value => formatx!("impl IntoValue<{}>", self.short_ty_name()),
839 InputKind::UiNode => Txt::from_static("impl IntoUiNode"),
840 InputKind::Handler => formatx!("Handler<{}>", self.short_ty_name()),
841 }
842 }
843}
844
845pub trait PropertyArgs: Send + Sync {
847 fn clone_boxed(&self) -> Box<dyn PropertyArgs>;
849
850 fn property(&self) -> PropertyInfo;
852
853 fn var(&self, i: usize) -> &AnyVar {
855 panic_input(&self.property(), i, InputKind::Var)
856 }
857
858 fn value(&self, i: usize) -> &dyn AnyVarValue {
860 panic_input(&self.property(), i, InputKind::Value)
861 }
862
863 fn ui_node(&self, i: usize) -> &ArcNode {
865 panic_input(&self.property(), i, InputKind::UiNode)
866 }
867
868 fn handler(&self, i: usize) -> &dyn AnyArcHandler {
872 panic_input(&self.property(), i, InputKind::Handler)
873 }
874
875 fn build_action(&self, wgt: &mut WidgetBuilding);
879
880 fn instantiate(&self, child: UiNode) -> UiNode;
884}
885impl dyn PropertyArgs + '_ {
886 pub fn id(&self) -> PropertyId {
888 self.property().id
889 }
890
891 pub fn downcast_value<T>(&self, i: usize) -> &T
897 where
898 T: VarValue,
899 {
900 self.value(i).downcast_ref::<T>().expect("cannot downcast value to type")
901 }
902 pub fn downcast_var<T>(&self, i: usize) -> Var<T>
908 where
909 T: VarValue,
910 {
911 self.var(i)
912 .clone()
913 .downcast::<T>()
914 .unwrap_or_else(|_| panic!("cannot downcast var to type"))
915 }
916
917 pub fn downcast_handler<A>(&self, i: usize) -> &ArcHandler<A>
923 where
924 A: 'static + Clone,
925 {
926 self.handler(i)
927 .as_any()
928 .downcast_ref::<ArcHandler<A>>()
929 .expect("cannot downcast handler to type")
930 }
931
932 pub fn live_debug(&self, i: usize) -> Var<Txt> {
938 let p = self.property();
939 match p.inputs[i].kind {
940 InputKind::Var => self.var(i).map_debug(false),
941 InputKind::Value => const_var(formatx!("{:?}", self.value(i))),
942 InputKind::UiNode => const_var(Txt::from_static("UiNode")),
943 InputKind::Handler => const_var(formatx!("Handler<{}>", p.inputs[i].short_ty_name())),
944 }
945 }
946
947 pub fn debug(&self, i: usize) -> Txt {
951 let p = self.property();
952 match p.inputs[i].kind {
953 InputKind::Var => formatx!("{:?}", self.var(i).get()),
954 InputKind::Value => formatx!("{:?}", self.value(i)),
955 InputKind::UiNode => Txt::from_static("UiNode"),
956 InputKind::Handler => formatx!("Handler<{}>", p.inputs[i].short_ty_name()),
957 }
958 }
959
960 pub fn new_build(&self, attributes: PropertyAttributes, attributes_when_data: PropertyAttributesWhenData) -> Box<dyn PropertyArgs> {
964 let p = self.property();
965
966 let mut args: Vec<Box<dyn Any>> = Vec::with_capacity(p.inputs.len());
967 for (i, input) in p.inputs.iter().enumerate() {
968 match input.kind {
969 InputKind::Var => args.push(Box::new(self.var(i).clone())),
970 InputKind::Value => args.push(Box::new(self.value(i).clone_boxed())),
971 InputKind::UiNode => args.push(Box::new(self.ui_node(i).clone())),
972 InputKind::Handler => args.push(self.handler(i).clone_boxed().into_any()),
973 }
974 }
975
976 (p.new)(PropertyNewArgs {
977 args,
978 attributes,
979 attributes_when_data,
980 })
981 }
982}
983
984#[doc(hidden)]
985pub fn panic_input(info: &PropertyInfo, i: usize, kind: InputKind) -> ! {
986 if i > info.inputs.len() {
987 panic!("index out of bounds, the input len is {}, but the index is {i}", info.inputs.len())
988 } else if info.inputs[i].kind != kind {
989 panic!(
990 "invalid input request `{:?}`, but `{}` is `{:?}`",
991 kind, info.inputs[i].name, info.inputs[i].kind
992 )
993 } else {
994 panic!("invalid input `{}`", info.inputs[i].name)
995 }
996}
997
998#[doc(hidden)]
999pub fn var_to_args<T: VarValue>(var: impl IntoVar<T>) -> Var<T> {
1000 var.into_var()
1001}
1002
1003#[doc(hidden)]
1004pub fn value_to_args<T: VarValue>(value: impl IntoValue<T>) -> T {
1005 value.into()
1006}
1007
1008#[doc(hidden)]
1009pub fn ui_node_to_args(node: impl IntoUiNode) -> ArcNode {
1010 ArcNode::new(node)
1011}
1012
1013#[doc(hidden)]
1014pub fn handler_to_args<A: Clone + 'static>(handler: Handler<A>) -> ArcHandler<A> {
1015 handler.into_arc()
1016}
1017
1018#[doc(hidden)]
1019pub fn iter_input_attributes<'a>(
1020 attributes: &'a PropertyAttributes,
1021 data: &'a PropertyAttributesWhenData,
1022 index: usize,
1023) -> impl Iterator<Item = (&'a dyn AnyPropertyAttribute, &'a [Option<PropertyAttributeWhenData>])> {
1024 let mut attributes = attributes.iter();
1025 let mut data = data.iter();
1026
1027 std::iter::from_fn(move || {
1028 let action = &*attributes.next()?[index];
1029 let data = if let Some(data) = data.next() { &data[..] } else { &[] };
1030
1031 Some((action, data))
1032 })
1033}
1034
1035fn apply_attributes<'a, I: Any + Send>(
1036 mut item: I,
1037 mut attributes: impl Iterator<Item = (&'a dyn AnyPropertyAttribute, &'a [Option<PropertyAttributeWhenData>])>,
1038) -> I {
1039 if let Some((attribute, data)) = attributes.next() {
1040 let attribute = attribute
1041 .as_any()
1042 .downcast_ref::<PropertyAttribute<I>>()
1043 .expect("property attribute build action type did not match expected var type");
1044
1045 item = attribute.build(PropertyAttributeArgs {
1046 input: item,
1047 when_conditions_data: data,
1048 });
1049 }
1050 item
1051}
1052
1053#[doc(hidden)]
1054pub fn new_dyn_var<'a, T: VarValue>(
1055 inputs: &mut std::vec::IntoIter<Box<dyn Any>>,
1056 attributes: impl Iterator<Item = (&'a dyn AnyPropertyAttribute, &'a [Option<PropertyAttributeWhenData>])>,
1057) -> Var<T> {
1058 let item = inputs.next().expect("missing input");
1059
1060 let item = match item.downcast::<AnyWhenVarBuilder>() {
1061 Ok(builder) => builder.into_typed::<T>().build(),
1062 Err(item) => {
1063 let any = *item.downcast::<AnyVar>().expect("input did not match expected var types");
1064 any.downcast::<T>().expect("input did not match expected var types")
1065 }
1066 };
1067
1068 apply_attributes(item, attributes)
1069}
1070
1071#[doc(hidden)]
1072pub fn new_dyn_ui_node<'a>(
1073 inputs: &mut std::vec::IntoIter<Box<dyn Any>>,
1074 attributes: impl Iterator<Item = (&'a dyn AnyPropertyAttribute, &'a [Option<PropertyAttributeWhenData>])>,
1075) -> ArcNode {
1076 let item = inputs.next().expect("missing input");
1077
1078 let item = match item.downcast::<WhenUiNodeBuilder>() {
1079 Ok(builder) => ArcNode::new(builder.build()),
1080 Err(item) => *item.downcast::<ArcNode>().expect("input did not match expected UiNode types"),
1081 };
1082
1083 apply_attributes(item, attributes)
1084}
1085
1086#[doc(hidden)]
1087pub fn new_dyn_handler<'a, A: Clone + 'static>(
1088 inputs: &mut std::vec::IntoIter<Box<dyn Any>>,
1089 attributes: impl Iterator<Item = (&'a dyn AnyPropertyAttribute, &'a [Option<PropertyAttributeWhenData>])>,
1090) -> ArcHandler<A> {
1091 let item = inputs.next().expect("missing input");
1092
1093 let item = match item.downcast::<AnyWhenArcHandlerBuilder>() {
1094 Ok(builder) => builder.build(),
1095 Err(item) => *item
1096 .downcast::<ArcHandler<A>>()
1097 .expect("input did not match expected Handler types"),
1098 };
1099
1100 apply_attributes(item, attributes)
1101}
1102
1103#[doc(hidden)]
1104pub fn new_dyn_other<'a, T: Any + Send>(
1105 inputs: &mut std::vec::IntoIter<Box<dyn Any>>,
1106 attributes: impl Iterator<Item = (&'a dyn AnyPropertyAttribute, &'a [Option<PropertyAttributeWhenData>])>,
1107) -> T {
1108 let item = *inputs
1109 .next()
1110 .expect("missing input")
1111 .downcast::<T>()
1112 .expect("input did not match expected var type");
1113
1114 apply_attributes(item, attributes)
1115}
1116
1117#[derive(Clone, PartialEq)]
1121pub struct UiNodeInWhenExprError;
1122impl fmt::Debug for UiNodeInWhenExprError {
1123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1124 write!(f, "{self}")
1125 }
1126}
1127impl fmt::Display for UiNodeInWhenExprError {
1128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1129 write!(f, "cannot ref `UiNode` in when expression, only var and value properties allowed")
1130 }
1131}
1132impl std::error::Error for UiNodeInWhenExprError {}
1133
1134#[derive(Clone, PartialEq)]
1138pub struct HandlerInWhenExprError;
1139impl fmt::Debug for HandlerInWhenExprError {
1140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1141 write!(f, "{self}")
1142 }
1143}
1144impl fmt::Display for HandlerInWhenExprError {
1145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1146 write!(
1147 f,
1148 "cannot ref `Handler<A>` in when expression, only var and value properties allowed"
1149 )
1150 }
1151}
1152impl std::error::Error for HandlerInWhenExprError {}
1153
1154#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
1162pub struct Importance(pub u32);
1163impl Importance {
1164 pub const WIDGET: Importance = Importance(1000);
1166 pub const INSTANCE: Importance = Importance(1000 * 10);
1168}
1169impl_from_and_into_var! {
1170 fn from(imp: u32) -> Importance {
1171 Importance(imp)
1172 }
1173}
1174
1175unique_id_32! {
1176 pub struct PropertyId;
1178}
1179zng_unique_id::impl_unique_id_bytemuck!(PropertyId);
1180impl fmt::Debug for PropertyId {
1181 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1182 f.debug_tuple("PropertyId").field(&self.get()).finish()
1183 }
1184}
1185
1186#[derive(Clone, Copy, Debug)]
1192#[non_exhaustive]
1193pub struct WidgetType {
1194 pub type_id: TypeId,
1196 pub path: &'static str,
1198 pub location: SourceLocation,
1200}
1201impl WidgetType {
1202 #[doc(hidden)]
1203 pub fn new(type_id: TypeId, path: &'static str, location: SourceLocation) -> Self {
1204 Self { type_id, path, location }
1205 }
1206
1207 pub fn name(&self) -> &'static str {
1209 self.path.rsplit_once(':').map(|(_, n)| n).unwrap_or(self.path)
1210 }
1211}
1212impl PartialEq for WidgetType {
1213 fn eq(&self, other: &Self) -> bool {
1214 self.type_id == other.type_id
1215 }
1216}
1217impl Eq for WidgetType {}
1218impl std::hash::Hash for WidgetType {
1219 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1220 self.type_id.hash(state);
1221 }
1222}
1223
1224#[derive(Clone, Copy, Debug)]
1226pub enum WhenInputMember {
1227 Named(&'static str),
1229 Index(usize),
1231}
1232
1233#[derive(Clone)]
1235pub struct WhenInput {
1236 pub property: PropertyId,
1238 pub member: WhenInputMember,
1240 pub var: WhenInputVar,
1242 pub property_default: Option<fn() -> Box<dyn PropertyArgs>>,
1244
1245 #[doc(hidden)] pub _non_exhaustive: (),
1247}
1248impl fmt::Debug for WhenInput {
1249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1250 f.debug_struct("WhenInput")
1251 .field("property", &self.property)
1252 .field("member", &self.member)
1253 .finish_non_exhaustive()
1254 }
1255}
1256
1257context_local! {
1258 static WHEN_INPUT_CONTEXT_INIT_ID: ContextInitHandle = ContextInitHandle::no_context();
1260}
1261
1262#[derive(Clone)]
1264pub struct WhenInputVar {
1265 var: Arc<Mutex<Vec<(WeakContextInitHandle, AnyVar)>>>,
1266}
1267impl WhenInputVar {
1268 pub fn new<T: VarValue>() -> (Self, Var<T>) {
1272 let arc = Arc::new(Mutex::new(vec![]));
1273 (
1274 WhenInputVar { var: arc.clone() },
1275 contextual_var(move || {
1276 let mut data = arc.lock();
1277
1278 let current_id = WHEN_INPUT_CONTEXT_INIT_ID.get();
1279 let current_id = current_id.downgrade();
1280
1281 let mut r = None;
1282 data.retain(|(id, val)| {
1283 let retain = id.is_alive();
1284 if retain && id == ¤t_id {
1285 r = Some(val.clone());
1286 }
1287 retain
1288 });
1289 match r {
1290 Some(r) => r,
1291 None => {
1292 if !data.is_empty() {
1294 let last = data.len() - 1;
1296 let last = &data[last];
1297 tracing::error!(
1298 "when input not inited for context {:?}, using value from {:?} to avoid crash",
1299 current_id,
1300 last.0
1301 );
1302 last.1.clone()
1303 } else {
1304 panic!("when input not inited for context {current_id:?}")
1306 }
1307 }
1308 }
1309 .downcast()
1310 .expect("incorrect when input var type")
1311 }),
1312 )
1313 }
1314
1315 fn set(&self, handle: WeakContextInitHandle, var: AnyVar) {
1316 let mut data = self.var.lock();
1317
1318 if let Some(i) = data.iter().position(|(i, _)| i == &handle) {
1319 data[i].1 = var;
1320 } else {
1321 data.push((handle, var));
1322 }
1323 }
1324}
1325
1326type PropertyAttributeWhenData = Arc<dyn Any + Send + Sync>;
1327type PropertyAttributeWhenDefault = Arc<dyn Fn() -> Vec<Box<dyn AnyPropertyAttribute>> + Send + Sync>;
1328
1329#[derive(Clone)]
1331#[non_exhaustive]
1332pub struct PropertyAttributeWhen {
1333 pub data: PropertyAttributeWhenData,
1335 pub default: Option<PropertyAttributeWhenDefault>,
1341}
1342impl PropertyAttributeWhen {
1343 pub fn new<D, F>(data: D, default_action: F) -> Self
1345 where
1346 D: Any + Send + Sync + 'static,
1347 F: Fn() -> Vec<Box<dyn AnyPropertyAttribute>> + Send + Sync + 'static,
1348 {
1349 Self {
1350 data: Arc::new(data),
1351 default: Some(Arc::new(default_action)),
1352 }
1353 }
1354
1355 pub fn new_no_default(data: impl Any + Send + Sync + 'static) -> Self {
1357 Self {
1358 data: Arc::new(data),
1359 default: None,
1360 }
1361 }
1362}
1363
1364#[derive(Clone)]
1366#[non_exhaustive]
1367pub struct WhenInfo {
1368 pub inputs: Box<[WhenInput]>,
1376
1377 pub state: Var<bool>,
1381
1382 pub assigns: Vec<Box<dyn PropertyArgs>>,
1385
1386 pub attributes_data: Vec<((PropertyId, &'static str), PropertyAttributeWhen)>,
1388
1389 pub expr: &'static str,
1391
1392 pub location: SourceLocation,
1394}
1395impl fmt::Debug for WhenInfo {
1396 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1397 struct DebugBuildActions<'a>(&'a WhenInfo);
1398 impl fmt::Debug for DebugBuildActions<'_> {
1399 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1400 f.debug_list().entries(self.0.attributes_data.iter().map(|(k, _)| k)).finish()
1401 }
1402 }
1403
1404 f.debug_struct("WhenInfo")
1405 .field("inputs", &self.inputs)
1406 .field("state", &self.state.get_debug(false))
1407 .field("assigns", &self.assigns)
1408 .field("attributes_data", &DebugBuildActions(self))
1409 .field("expr", &self.expr)
1410 .finish()
1411 }
1412}
1413impl Clone for Box<dyn PropertyArgs> {
1414 fn clone(&self) -> Self {
1415 PropertyArgs::clone_boxed(&**self)
1416 }
1417}
1418impl fmt::Debug for &dyn PropertyArgs {
1419 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1420 f.debug_struct("dyn PropertyArgs")
1421 .field("property", &self.property())
1422 .finish_non_exhaustive()
1423 }
1424}
1425impl fmt::Debug for Box<dyn PropertyArgs> {
1426 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1427 f.debug_struct("dyn PropertyArgs")
1428 .field("property", &self.property())
1429 .finish_non_exhaustive()
1430 }
1431}
1432
1433#[derive(Clone)]
1434struct WidgetItemPositioned {
1435 position: NestPosition,
1436 insert_idx: u32,
1437 item: WidgetItem,
1438}
1439impl WidgetItemPositioned {
1440 fn sort_key(&self) -> (NestPosition, u32) {
1441 (self.position, self.insert_idx)
1442 }
1443}
1444
1445#[derive(Clone, Debug)]
1446struct WhenItemPositioned {
1447 importance: Importance,
1448 insert_idx: u32,
1449 when: WhenInfo,
1450}
1451impl WhenItemPositioned {
1452 fn sort_key(&self) -> (Importance, u32) {
1453 (self.importance, self.insert_idx)
1454 }
1455}
1456
1457enum WidgetItem {
1458 Property {
1459 importance: Importance,
1460 args: Box<dyn PropertyArgs>,
1461 captured: bool,
1462 },
1463 Intrinsic {
1464 name: &'static str,
1465 new: Box<dyn FnOnce(UiNode) -> UiNode + Send + Sync>,
1466 },
1467}
1468impl Clone for WidgetItem {
1469 fn clone(&self) -> Self {
1470 match self {
1471 Self::Property {
1472 importance,
1473 args,
1474 captured,
1475 } => Self::Property {
1476 importance: *importance,
1477 captured: *captured,
1478 args: args.clone(),
1479 },
1480 Self::Intrinsic { .. } => unreachable!("only WidgetBuilder clones, and it does not insert intrinsic"),
1481 }
1482 }
1483}
1484
1485type PropertyAttributesMap = HashMap<(PropertyId, &'static str), (Importance, Vec<Box<dyn AnyPropertyAttribute>>)>;
1487type PropertyAttributesVec = Vec<((PropertyId, &'static str), (Importance, Vec<Box<dyn AnyPropertyAttribute>>))>;
1488
1489pub struct WidgetBuilder {
1491 widget_type: WidgetType,
1492
1493 insert_idx: u32,
1494 p: WidgetBuilderProperties,
1495 unset: HashMap<PropertyId, Importance>,
1496
1497 whens: Vec<WhenItemPositioned>,
1498 when_insert_idx: u32,
1499
1500 p_attributes: PropertyAttributesMap,
1501 p_attributes_unset: HashMap<(PropertyId, &'static str), Importance>,
1502
1503 build_actions: Vec<Arc<Mutex<dyn FnMut(&mut WidgetBuilding) + Send>>>,
1504 pre_build_actions: Vec<Arc<Mutex<dyn FnMut(&mut WidgetBuilding) + Send>>>,
1505
1506 custom_build: Option<Arc<Mutex<dyn FnMut(WidgetBuilder) -> UiNode + Send>>>,
1507}
1508impl Clone for WidgetBuilder {
1509 fn clone(&self) -> Self {
1510 Self {
1511 widget_type: self.widget_type,
1512 p: WidgetBuilderProperties { items: self.items.clone() },
1513 p_attributes: self.p_attributes.clone(),
1514 insert_idx: self.insert_idx,
1515 unset: self.unset.clone(),
1516 p_attributes_unset: self.p_attributes_unset.clone(),
1517 whens: self.whens.clone(),
1518 when_insert_idx: self.when_insert_idx,
1519 build_actions: self.build_actions.clone(),
1520 pre_build_actions: self.pre_build_actions.clone(),
1521 custom_build: self.custom_build.clone(),
1522 }
1523 }
1524}
1525impl fmt::Debug for WidgetBuilder {
1526 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1527 struct PropertiesDebug<'a>(&'a WidgetBuilderProperties);
1528 impl fmt::Debug for PropertiesDebug<'_> {
1529 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1530 f.debug_list().entries(self.0.properties()).finish()
1531 }
1532 }
1533 f.debug_struct("WidgetBuilder")
1534 .field("widget_type", &self.widget_type)
1535 .field("properties", &PropertiesDebug(&self.p))
1536 .field("unset", &self.unset)
1537 .field("whens", &self.whens)
1538 .field("build_actions.len", &self.build_actions.len())
1539 .field("pre_build_actions.len", &self.pre_build_actions.len())
1540 .field("is_custom_build", &self.is_custom_build())
1541 .finish()
1542 }
1543}
1544impl WidgetBuilder {
1545 pub fn new(widget: WidgetType) -> Self {
1547 Self {
1548 widget_type: widget,
1549 p: WidgetBuilderProperties { items: Default::default() },
1550 insert_idx: 0,
1551 unset: Default::default(),
1552 whens: Default::default(),
1553 p_attributes: Default::default(),
1554 p_attributes_unset: Default::default(),
1555 when_insert_idx: 0,
1556 build_actions: Default::default(),
1557 pre_build_actions: Default::default(),
1558 custom_build: Default::default(),
1559 }
1560 }
1561
1562 pub fn widget_type(&self) -> WidgetType {
1564 self.widget_type
1565 }
1566
1567 pub fn push_property(&mut self, importance: Importance, args: Box<dyn PropertyArgs>) {
1571 let pos = NestPosition::property(args.property().group);
1572 self.push_property_positioned(importance, pos, args);
1573 }
1574
1575 pub fn push_property_positioned(&mut self, importance: Importance, position: NestPosition, args: Box<dyn PropertyArgs>) {
1577 self.push_property_positioned_impl(importance, position, args, false)
1578 }
1579 fn push_property_positioned_impl(
1580 &mut self,
1581 importance: Importance,
1582 position: NestPosition,
1583 args: Box<dyn PropertyArgs>,
1584 captured: bool,
1585 ) {
1586 let insert_idx = self.insert_idx;
1587 self.insert_idx = insert_idx.wrapping_add(1);
1588
1589 let property_id = args.id();
1590 if let Some(i) = self.p.property_index(property_id) {
1591 match &self.p.items[i].item {
1592 WidgetItem::Property { importance: imp, .. } => {
1593 if *imp <= importance {
1594 self.p.items[i] = WidgetItemPositioned {
1596 position,
1597 insert_idx,
1598 item: WidgetItem::Property {
1599 importance,
1600 args,
1601 captured,
1602 },
1603 };
1604 }
1605 }
1606 WidgetItem::Intrinsic { .. } => unreachable!(),
1607 }
1608 } else {
1609 if let Some(imp) = self.unset.get(&property_id)
1610 && *imp >= importance
1611 {
1612 return; }
1614 self.p.items.push(WidgetItemPositioned {
1615 position,
1616 insert_idx,
1617 item: WidgetItem::Property {
1618 importance,
1619 args,
1620 captured,
1621 },
1622 });
1623 }
1624 }
1625
1626 pub fn push_when(&mut self, importance: Importance, mut when: WhenInfo) {
1628 let insert_idx = self.when_insert_idx;
1629 self.when_insert_idx = insert_idx.wrapping_add(1);
1630
1631 when.assigns.retain(|a| {
1632 if let Some(imp) = self.unset.get(&a.id()) {
1633 *imp < importance
1634 } else {
1635 true
1636 }
1637 });
1638
1639 if !when.assigns.is_empty() {
1640 self.whens.push(WhenItemPositioned {
1641 importance,
1642 insert_idx,
1643 when,
1644 });
1645 }
1646 }
1647
1648 pub fn push_unset(&mut self, importance: Importance, property_id: PropertyId) {
1650 let check;
1651 match self.unset.entry(property_id) {
1652 hash_map::Entry::Occupied(mut e) => {
1653 let i = e.get_mut();
1654 check = *i < importance;
1655 *i = importance;
1656 }
1657 hash_map::Entry::Vacant(e) => {
1658 check = true;
1659 e.insert(importance);
1660 }
1661 }
1662
1663 if check {
1664 if let Some(i) = self.p.property_index(property_id) {
1665 match &self.p.items[i].item {
1666 WidgetItem::Property { importance: imp, .. } => {
1667 if *imp <= importance {
1668 self.p.items.swap_remove(i);
1669 }
1670 }
1671 WidgetItem::Intrinsic { .. } => unreachable!(),
1672 }
1673 }
1674
1675 self.whens.retain_mut(|w| {
1676 if w.importance <= importance {
1677 w.when.assigns.retain(|a| a.id() != property_id);
1678 !w.when.assigns.is_empty()
1679 } else {
1680 true
1681 }
1682 });
1683 }
1684 }
1685
1686 pub fn push_property_attribute(
1691 &mut self,
1692 property_id: PropertyId,
1693 attribute_name: &'static str,
1694 importance: Importance,
1695 input_actions: Vec<Box<dyn AnyPropertyAttribute>>,
1696 ) {
1697 match self.p_attributes.entry((property_id, attribute_name)) {
1698 hash_map::Entry::Occupied(mut e) => {
1699 if e.get().0 < importance {
1700 e.insert((importance, input_actions));
1701 }
1702 }
1703 hash_map::Entry::Vacant(e) => {
1704 if let Some(imp) = self.p_attributes_unset.get(&(property_id, attribute_name))
1705 && *imp >= importance
1706 {
1707 return;
1709 }
1710 e.insert((importance, input_actions));
1711 }
1712 }
1713 }
1714
1715 pub fn push_unset_property_attribute(&mut self, property_id: PropertyId, attribute_name: &'static str, importance: Importance) {
1719 let mut check = false;
1720 match self.p_attributes_unset.entry((property_id, attribute_name)) {
1721 hash_map::Entry::Occupied(mut e) => {
1722 if *e.get() < importance {
1723 e.insert(importance);
1724 check = true;
1725 }
1726 }
1727 hash_map::Entry::Vacant(e) => {
1728 e.insert(importance);
1729 check = true;
1730 }
1731 }
1732 if check {
1733 self.p_attributes.retain(|_, (imp, _)| *imp > importance);
1734 }
1735 }
1736
1737 pub fn clear_property_attributes(&mut self) {
1739 self.p_attributes.clear();
1740 }
1741
1742 pub fn push_build_action(&mut self, action: impl FnMut(&mut WidgetBuilding) + Send + 'static) {
1749 self.build_actions.push(Arc::new(Mutex::new(action)))
1750 }
1751
1752 pub fn push_pre_build_action(&mut self, action: impl FnMut(&mut WidgetBuilding) + Send + 'static) {
1762 self.pre_build_actions.push(Arc::new(Mutex::new(action)))
1763 }
1764
1765 pub fn clear_build_actions(&mut self) {
1767 self.build_actions.clear();
1768 self.pre_build_actions.clear();
1769 }
1770
1771 pub fn is_custom_build(&self) -> bool {
1773 self.custom_build.is_some()
1774 }
1775
1776 pub fn set_custom_build(&mut self, build: impl FnMut(WidgetBuilder) -> UiNode + Send + 'static) {
1783 self.custom_build = Some(Arc::new(Mutex::new(build)));
1784 }
1785
1786 pub fn clear_custom_build(&mut self) {
1788 self.custom_build = None;
1789 }
1790
1791 pub fn extend(&mut self, other: WidgetBuilder) {
1801 self.extend_important(other, Importance(0));
1802 }
1803 pub fn extend_important(&mut self, other: WidgetBuilder, min_importance: Importance) {
1807 for (id, imp) in other.unset {
1808 if imp >= min_importance {
1809 self.push_unset(imp, id);
1810 }
1811 }
1812
1813 for ((id, name), imp) in other.p_attributes_unset {
1814 if imp >= min_importance {
1815 self.push_unset_property_attribute(id, name, imp);
1816 }
1817 }
1818
1819 for WidgetItemPositioned { position, item, .. } in other.p.items {
1820 match item {
1821 WidgetItem::Property {
1822 importance,
1823 args,
1824 captured,
1825 } => {
1826 if importance >= min_importance {
1827 self.push_property_positioned_impl(importance, position, args, captured);
1828 }
1829 }
1830 WidgetItem::Intrinsic { .. } => unreachable!(),
1831 }
1832 }
1833
1834 for w in other.whens {
1835 if w.importance >= min_importance {
1836 self.push_when(w.importance, w.when);
1837 }
1838 }
1839
1840 for ((id, name), (imp, action)) in other.p_attributes {
1841 if imp >= min_importance {
1842 self.push_property_attribute(id, name, imp, action);
1843 }
1844 }
1845
1846 for act in other.pre_build_actions {
1847 self.pre_build_actions.push(act);
1848 }
1849
1850 for act in other.build_actions {
1851 self.build_actions.push(act);
1852 }
1853
1854 if let Some(c) = other.custom_build {
1855 self.custom_build = Some(c);
1856 }
1857 }
1858
1859 pub fn has_properties(&self) -> bool {
1861 !self.p.items.is_empty()
1862 }
1863
1864 pub fn has_unsets(&self) -> bool {
1866 !self.unset.is_empty()
1867 }
1868
1869 pub fn has_whens(&self) -> bool {
1871 !self.whens.is_empty()
1872 }
1873
1874 pub fn split_off(&mut self, properties: impl IntoIterator<Item = PropertyId>, out: &mut WidgetBuilder) {
1881 self.split_off_impl(properties.into_iter().collect(), out)
1882 }
1883 fn split_off_impl(&mut self, properties: IdSet<PropertyId>, out: &mut WidgetBuilder) {
1884 let mut found = 0;
1885
1886 let mut i = 0;
1888 while i < self.items.len() && found < properties.len() {
1889 match &self.items[i].item {
1890 WidgetItem::Property { args, .. } if properties.contains(&args.id()) => match self.items.swap_remove(i) {
1891 WidgetItemPositioned {
1892 position,
1893 item: WidgetItem::Property { importance, args, .. },
1894 ..
1895 } => {
1896 out.push_property_positioned(importance, position, args);
1897 found += 1;
1898 }
1899 _ => unreachable!(),
1900 },
1901 _ => {
1902 i += 1;
1903 continue;
1904 }
1905 }
1906 }
1907
1908 i = 0;
1909 while i < self.whens.len() {
1910 let mut ai = 0;
1912 let mut moved_assigns = vec![];
1913 while ai < self.whens[i].when.assigns.len() {
1914 if properties.contains(&self.whens[i].when.assigns[ai].id()) {
1915 let args = self.whens[i].when.assigns.remove(ai);
1916 moved_assigns.push(args);
1917 } else {
1918 ai += 1;
1919 }
1920 }
1921
1922 if !moved_assigns.is_empty() {
1923 let out_imp;
1924 let out_when;
1925 if self.whens[i].when.assigns.is_empty() {
1926 let WhenItemPositioned { importance, mut when, .. } = self.whens.remove(i);
1928 when.assigns = moved_assigns;
1929
1930 out_imp = importance;
1931 out_when = when;
1932 } else {
1933 let WhenItemPositioned { importance, when, .. } = &self.whens[i];
1935 out_imp = *importance;
1936 out_when = WhenInfo {
1937 inputs: when.inputs.clone(),
1938 state: when.state.clone(),
1939 assigns: moved_assigns,
1940 attributes_data: when.attributes_data.clone(),
1941 expr: when.expr,
1942 location: when.location,
1943 };
1944
1945 i += 1;
1946 };
1947
1948 for input in out_when.inputs.iter() {
1950 if let Some(i) = self.property_index(input.property) {
1951 match &self.items[i] {
1952 WidgetItemPositioned {
1953 position,
1954 item: WidgetItem::Property { importance, args, .. },
1955 ..
1956 } => {
1957 out.push_property_positioned(*importance, *position, args.clone());
1958 }
1959 _ => unreachable!(),
1960 }
1961 }
1962 }
1963
1964 out.push_when(out_imp, out_when);
1965 } else {
1966 i += 1;
1967 }
1968 }
1969
1970 for id in properties {
1972 if let Some(imp) = self.unset.remove(&id) {
1973 out.push_unset(imp, id);
1974 }
1975 }
1976 }
1977
1978 pub fn build(self) -> UiNode {
1984 if let Some(custom) = self.custom_build.clone() {
1985 match custom.try_lock() {
1986 Some(mut c) => c(self),
1987 None => self.default_build(),
1988 }
1989 } else {
1990 self.default_build()
1991 }
1992 }
1993
1994 pub fn default_build(self) -> UiNode {
1998 #[cfg(feature = "inspector")]
1999 let builder = self.clone();
2000
2001 let mut building = WidgetBuilding {
2002 #[cfg(feature = "inspector")]
2003 builder: Some(builder),
2004 #[cfg(feature = "trace_widget")]
2005 trace_widget: true,
2006 #[cfg(feature = "trace_wgt_item")]
2007 trace_wgt_item: true,
2008
2009 widget_type: self.widget_type,
2010 p: self.p,
2011 child: None,
2012 build_action_property: None,
2013 };
2014
2015 let mut p_attributes = self.p_attributes.into_iter().collect();
2016
2017 let mut when_init_context_handle = None;
2018
2019 if !self.whens.is_empty() {
2020 let handle = ContextInitHandle::new();
2021 building.build_whens(self.whens, handle.downgrade(), &mut p_attributes);
2022 when_init_context_handle = Some(handle);
2023 }
2024
2025 if !p_attributes.is_empty() {
2026 building.build_p_attributes(p_attributes);
2027 }
2028
2029 for action in self.pre_build_actions.into_iter().rev() {
2030 (action.lock())(&mut building);
2031 }
2032 for action in self.build_actions {
2033 (action.lock())(&mut building);
2034 }
2035
2036 building.run_build_action_properties();
2037
2038 building.build(when_init_context_handle)
2039 }
2040}
2041impl ops::Deref for WidgetBuilder {
2042 type Target = WidgetBuilderProperties;
2043
2044 fn deref(&self) -> &Self::Target {
2045 &self.p
2046 }
2047}
2048impl ops::DerefMut for WidgetBuilder {
2049 fn deref_mut(&mut self) -> &mut Self::Target {
2050 &mut self.p
2051 }
2052}
2053
2054pub struct WidgetBuilding {
2062 #[cfg(feature = "inspector")]
2063 builder: Option<WidgetBuilder>,
2064 #[cfg(feature = "trace_widget")]
2065 trace_widget: bool,
2066 #[cfg(feature = "trace_wgt_item")]
2067 trace_wgt_item: bool,
2068
2069 widget_type: WidgetType,
2070 p: WidgetBuilderProperties,
2071 child: Option<UiNode>,
2072
2073 build_action_property: Option<PropertyInfo>,
2074}
2075impl WidgetBuilding {
2076 pub fn widget_type(&self) -> WidgetType {
2078 self.widget_type
2079 }
2080
2081 pub fn has_child(&self) -> bool {
2085 self.child.is_some()
2086 }
2087
2088 pub fn set_child(&mut self, node: impl IntoUiNode) {
2090 self.child = Some(node.into_node());
2091 }
2092
2093 #[cfg(feature = "inspector")]
2097 pub fn disable_inspector(&mut self) {
2098 self.builder = None;
2099 }
2100
2101 #[cfg(feature = "trace_widget")]
2105 pub fn disable_trace_widget(&mut self) {
2106 self.trace_widget = false;
2107 }
2108
2109 #[cfg(feature = "trace_wgt_item")]
2113 pub fn disable_trace_wgt_item(&mut self) {
2114 self.trace_wgt_item = false;
2115 }
2116
2117 pub fn push_intrinsic(
2121 &mut self,
2122 group: NestGroup,
2123 name: &'static str,
2124 intrinsic: impl FnOnce(UiNode) -> UiNode + Send + Sync + 'static,
2125 ) {
2126 self.push_intrinsic_positioned(NestPosition::intrinsic(group), name, intrinsic)
2127 }
2128
2129 pub fn push_intrinsic_positioned(
2133 &mut self,
2134 position: NestPosition,
2135 name: &'static str,
2136 intrinsic: impl FnOnce(UiNode) -> UiNode + Send + Sync + 'static,
2137 ) {
2138 self.items.push(WidgetItemPositioned {
2139 position,
2140 insert_idx: u32::MAX,
2141 item: WidgetItem::Intrinsic {
2142 name,
2143 new: Box::new(intrinsic),
2144 },
2145 });
2146 }
2147
2148 pub fn remove_property(&mut self, property_id: PropertyId) -> Option<BuilderProperty> {
2152 if let Some(i) = self.property_index(property_id) {
2153 match self.items.swap_remove(i) {
2154 WidgetItemPositioned {
2155 position,
2156 item:
2157 WidgetItem::Property {
2158 importance,
2159 args,
2160 captured,
2161 },
2162 ..
2163 } => Some(BuilderProperty {
2164 importance,
2165 position,
2166 args,
2167 captured,
2168 }),
2169 _ => unreachable!(),
2170 }
2171 } else {
2172 None
2173 }
2174 }
2175
2176 pub fn capture_property(&mut self, property_id: PropertyId) -> Option<BuilderPropertyRef<'_>> {
2181 self.capture_property_impl(property_id)
2182 }
2183
2184 pub fn capture_var<T>(&mut self, property_id: PropertyId) -> Option<Var<T>>
2186 where
2187 T: VarValue,
2188 {
2189 let p = self.capture_property(property_id)?;
2190 let var = p.args.downcast_var::<T>(0).clone();
2191 Some(var)
2192 }
2193
2194 pub fn capture_var_or_else<T, F>(&mut self, property_id: PropertyId, or_else: impl FnOnce() -> F) -> Var<T>
2196 where
2197 T: VarValue,
2198 F: IntoVar<T>,
2199 {
2200 match self.capture_var::<T>(property_id) {
2201 Some(var) => var,
2202 None => or_else().into_var(),
2203 }
2204 }
2205
2206 pub fn capture_var_or_default<T>(&mut self, property_id: PropertyId) -> Var<T>
2208 where
2209 T: VarValue + Default,
2210 {
2211 self.capture_var_or_else(property_id, T::default)
2212 }
2213
2214 pub fn capture_ui_node(&mut self, property_id: PropertyId) -> Option<UiNode> {
2216 let p = self.capture_property(property_id)?;
2217 let node = p.args.ui_node(0).take_on_init();
2218 Some(node)
2219 }
2220
2221 pub fn capture_ui_node_or_else(&mut self, property_id: PropertyId, or_else: impl FnOnce() -> UiNode) -> UiNode {
2223 match self.capture_ui_node(property_id) {
2224 Some(u) => u,
2225 None => or_else(),
2226 }
2227 }
2228
2229 pub fn capture_ui_node_or_nil(&mut self, property_id: PropertyId) -> UiNode {
2231 self.capture_ui_node_or_else(property_id, UiNode::nil)
2232 }
2233
2234 pub fn capture_handler<A: Clone + 'static>(&mut self, property_id: PropertyId) -> Option<ArcHandler<A>> {
2236 let p = self.capture_property(property_id)?;
2237 let handler = p.args.downcast_handler::<A>(0).clone();
2238 Some(handler)
2239 }
2240
2241 pub fn build_action_property(&mut self) -> Option<&PropertyInfo> {
2243 self.build_action_property.as_ref()
2244 }
2245
2246 pub fn expect_property_capture(&mut self) {
2251 #[cfg(debug_assertions)]
2252 if let Some(p_name) = self.build_action_property().map(|p| p.name) {
2253 tracing::error!(
2254 "capture only property `{}` is not captured in `{}!`, it will have no effect",
2255 p_name,
2256 self.widget_type.name()
2257 );
2258 }
2259 }
2260
2261 fn build_whens(
2262 &mut self,
2263 mut whens: Vec<WhenItemPositioned>,
2264 when_init_context_id: WeakContextInitHandle,
2265 attributes: &mut PropertyAttributesVec,
2266 ) {
2267 whens.sort_unstable_by_key(|w| w.sort_key());
2268
2269 struct Input<'a> {
2270 input: &'a WhenInput,
2271 item_idx: usize,
2272 }
2273 let mut inputs = vec![];
2274
2275 struct Assign {
2276 item_idx: usize,
2277 builder: Vec<Box<dyn Any>>,
2278 when_count: usize,
2279 attrs_data: HashMap<&'static str, (Vec<Option<PropertyAttributeWhenData>>, Option<PropertyAttributeWhenDefault>)>,
2282 }
2283 let mut assigns = IdMap::default();
2284
2285 'when: for WhenItemPositioned { when, .. } in whens.iter().rev() {
2287 let valid_inputs = inputs.len();
2289 let valid_items = self.p.items.len();
2290 for input in when.inputs.iter() {
2291 if let Some(i) = self.property_index(input.property) {
2292 inputs.push(Input { input, item_idx: i })
2293 } else if let Some(default) = input.property_default {
2294 let args = default();
2295 self.p.items.push(WidgetItemPositioned {
2296 position: NestPosition::property(args.property().group),
2297 insert_idx: u32::MAX,
2298 item: WidgetItem::Property {
2299 importance: Importance::WIDGET,
2300 args,
2301 captured: false,
2302 },
2303 });
2304 inputs.push(Input {
2305 input,
2306 item_idx: self.p.items.len() - 1,
2307 });
2308 } else {
2309 inputs.truncate(valid_inputs);
2310 self.p.items.truncate(valid_items);
2311 continue 'when;
2312 }
2313 }
2314
2315 let mut any_assign = false;
2316 'assign: for assign in when.assigns.iter() {
2318 let id = assign.id();
2319 let assign_info;
2320 let i;
2321 if let Some(idx) = self.property_index(id) {
2322 assign_info = assign.property();
2323 i = idx;
2324 } else if let Some(default) = assign.property().default {
2325 let args = default();
2326 assign_info = args.property();
2327 i = self.p.items.len();
2328 self.p.items.push(WidgetItemPositioned {
2329 position: NestPosition::property(args.property().group),
2330 insert_idx: u32::MAX,
2331 item: WidgetItem::Property {
2332 importance: Importance::WIDGET,
2333 args,
2334 captured: false,
2335 },
2336 });
2337 } else {
2338 tracing::warn!(
2339 "property `{}` ignored, it is only set in `when` block and has no default value",
2340 assign.property().name
2341 );
2342 continue;
2343 }
2344
2345 any_assign = true;
2346
2347 let default_args = match &self.items[i].item {
2348 WidgetItem::Property { args, .. } => args,
2349 WidgetItem::Intrinsic { .. } => unreachable!(),
2350 };
2351 let info = default_args.property();
2352
2353 for (default_info, assign_info) in info.inputs.iter().zip(assign_info.inputs.iter()) {
2354 if default_info.ty != assign_info.ty {
2355 continue 'assign;
2357 }
2358 }
2359
2360 let entry = match assigns.entry(id) {
2361 IdEntry::Occupied(e) => e.into_mut(),
2362 IdEntry::Vacant(e) => e.insert(Assign {
2363 item_idx: i,
2364 builder: info
2365 .inputs
2366 .iter()
2367 .enumerate()
2368 .map(|(i, input)| match input.kind {
2369 InputKind::Var => Box::new(AnyWhenVarBuilder::new(default_args.var(i).clone())) as _,
2370 InputKind::UiNode => Box::new(WhenUiNodeBuilder::new(default_args.ui_node(i).take_on_init())) as _,
2371 InputKind::Handler => Box::new(AnyWhenArcHandlerBuilder::new(default_args.handler(i).clone_boxed())) as _,
2372 InputKind::Value => panic!("can only assign vars in when blocks"),
2373 })
2374 .collect(),
2375 when_count: 0,
2376 attrs_data: Default::default(),
2377 }),
2378 };
2379 entry.when_count += 1;
2380
2381 for (i, (input, entry)) in info.inputs.iter().zip(entry.builder.iter_mut()).enumerate() {
2382 match input.kind {
2383 InputKind::Var => {
2384 let entry = entry.downcast_mut::<AnyWhenVarBuilder>().unwrap();
2385 let value = assign.var(i).clone();
2386 entry.push(when.state.clone(), value);
2387 }
2388 InputKind::UiNode => {
2389 let entry = entry.downcast_mut::<WhenUiNodeBuilder>().unwrap();
2390 let node = assign.ui_node(i).take_on_init();
2391 entry.push(when.state.clone(), node);
2392 }
2393 InputKind::Handler => {
2394 let entry = entry.downcast_mut::<AnyWhenArcHandlerBuilder>().unwrap();
2395 let handler = assign.handler(i).clone_boxed();
2396 entry.push(when.state.clone(), handler);
2397 }
2398 InputKind::Value => panic!("cannot assign `Value` in when blocks"),
2399 }
2400 }
2401
2402 for ((property_id, attr_key), action) in &when.attributes_data {
2403 if *property_id == id {
2404 match entry.attrs_data.entry(*attr_key) {
2405 hash_map::Entry::Occupied(mut e) => {
2406 let e = e.get_mut();
2407 for _ in e.0.len()..(entry.when_count - 1) {
2408 e.0.push(None);
2409 }
2410 e.0.push(Some(action.data.clone()));
2411 if action.default.is_some() && e.1.is_none() {
2412 e.1.clone_from(&action.default);
2413 }
2414 }
2415 hash_map::Entry::Vacant(e) => {
2416 let mut a = Vec::with_capacity(entry.when_count);
2417 for _ in 0..(entry.when_count - 1) {
2418 a.push(None);
2419 }
2420 a.push(Some(action.data.clone()));
2421 e.insert((a, action.default.clone()));
2422 }
2423 }
2424 }
2425 }
2426 }
2427
2428 if !any_assign {
2429 inputs.truncate(valid_inputs);
2430 self.p.items.truncate(valid_items);
2431 }
2432 }
2433
2434 for Input { input, item_idx } in inputs {
2435 let args = match &self.items[item_idx].item {
2436 WidgetItem::Property { args, .. } => args,
2437 WidgetItem::Intrinsic { .. } => unreachable!(),
2438 };
2439 let info = args.property();
2440
2441 let member_i = match input.member {
2442 WhenInputMember::Named(name) => info.input_idx(name).expect("when ref named input not found"),
2443 WhenInputMember::Index(i) => i,
2444 };
2445
2446 let actual = match info.inputs[member_i].kind {
2447 InputKind::Var => args.var(member_i).clone(),
2448 InputKind::Value => any_const_var(args.value(member_i).clone_boxed()),
2449 _ => panic!("can only ref var or values in when expr"),
2450 };
2451 input.var.set(when_init_context_id.clone(), actual);
2452 }
2453
2454 for (
2455 _,
2456 Assign {
2457 item_idx,
2458 builder,
2459 when_count,
2460 attrs_data: mut actions_data,
2461 },
2462 ) in assigns
2463 {
2464 let args = match &mut self.items[item_idx].item {
2465 WidgetItem::Property { args, .. } => args,
2466 WidgetItem::Intrinsic { .. } => unreachable!(),
2467 };
2468
2469 let mut attrs = vec![];
2470 let mut attrs_data = vec![];
2471 if !attributes.is_empty() {
2472 let p_id = args.id();
2473 while let Some(i) = attributes.iter().position(|((id, _), _)| *id == p_id) {
2474 let ((_, action_key), (_, a)) = attributes.swap_remove(i);
2475 attrs.push(a);
2476
2477 if let Some(data) = actions_data.remove(action_key) {
2478 let mut data = data.clone();
2479 for _ in data.0.len()..when_count {
2480 data.0.push(None);
2481 }
2482 attrs_data.push(data.0);
2483 }
2484 }
2485 }
2486
2487 for (_, (mut data, default)) in actions_data {
2488 if let Some(default) = default {
2489 let action = default();
2490 for _ in data.len()..when_count {
2491 data.push(None);
2492 }
2493
2494 attrs.push(action);
2495 attrs_data.push(data);
2496 }
2497 }
2498
2499 *args = (args.property().new)(PropertyNewArgs {
2500 args: builder,
2501 attributes: attrs,
2502 attributes_when_data: attrs_data,
2503 });
2504 }
2505 }
2506
2507 fn build_p_attributes(&mut self, mut attributes: PropertyAttributesVec) {
2508 while !attributes.is_empty() {
2509 let ((p_id, _), (_, a)) = attributes.swap_remove(0);
2510 let mut attrs = vec![a];
2511
2512 while let Some(i) = attributes.iter().position(|((id, _), _)| *id == p_id) {
2513 let (_, (_, a)) = attributes.swap_remove(i);
2514 attrs.push(a);
2515 }
2516
2517 if let Some(i) = self.property_index(p_id) {
2518 match &mut self.items[i].item {
2519 WidgetItem::Property { args, .. } => *args = args.new_build(attrs, vec![]),
2520 WidgetItem::Intrinsic { .. } => unreachable!(),
2521 }
2522 }
2523 }
2524 }
2525
2526 fn run_build_action_properties(&mut self) {
2527 if self.items.is_empty() {
2528 return;
2529 }
2530
2531 self.items.sort_unstable_by_key(|b| b.sort_key());
2533
2534 let mut any = false;
2535 let mut i = self.items.len();
2536 loop {
2537 i -= 1;
2538 if let WidgetItem::Property { args, captured, .. } = &self.p.items[i].item
2539 && !captured
2540 {
2541 let p = args.property();
2542 if p.build_action {
2543 any = true;
2544 self.build_action_property = Some(p);
2545 args.clone_boxed().build_action(self);
2546 self.build_action_property = None;
2547 }
2548 }
2549 if i == 0 {
2550 break;
2551 }
2552 }
2553
2554 if any {
2555 self.items.sort_unstable_by_key(|b| b.sort_key());
2556 }
2557 }
2558
2559 fn build(mut self, when_init_context_handle: Option<ContextInitHandle>) -> UiNode {
2560 #[cfg(feature = "inspector")]
2561 let mut inspector_items = Vec::with_capacity(self.p.items.len());
2562
2563 let mut node = self.child.take().unwrap_or_else(|| FillUiNode.into_node());
2564 for WidgetItemPositioned { position, item, .. } in self.p.items.into_iter().rev() {
2565 match item {
2566 WidgetItem::Property { args, captured, .. } => {
2567 if !captured {
2568 node = args.instantiate(node);
2569
2570 #[cfg(feature = "trace_wgt_item")]
2571 if self.trace_wgt_item {
2572 let name = args.property().name;
2573 node = node.trace(move |mtd| crate::update::UpdatesTrace::property_span(name, mtd.mtd_name()));
2574 }
2575 }
2576
2577 #[cfg(feature = "inspector")]
2578 {
2579 if args.property().inputs.iter().any(|i| matches!(i.kind, InputKind::Var)) {
2580 node = crate::widget::inspector::actualize_var_info(node, args.id());
2581 }
2582
2583 inspector_items.push(crate::widget::inspector::InstanceItem::Property { args, captured });
2584 }
2585 }
2586 #[allow(unused_variables)] WidgetItem::Intrinsic { new, name } => {
2588 node = new(node);
2589 #[cfg(feature = "trace_wgt_item")]
2590 if self.trace_wgt_item {
2591 node = node.trace(move |mtd| crate::update::UpdatesTrace::intrinsic_span(name, mtd.mtd_name()));
2592 }
2593
2594 #[cfg(feature = "inspector")]
2595 inspector_items.push(crate::widget::inspector::InstanceItem::Intrinsic {
2596 group: position.group,
2597 name,
2598 });
2599
2600 #[cfg(not(feature = "inspector"))]
2601 let _ = position;
2602 }
2603 }
2604 }
2605
2606 #[cfg(feature = "inspector")]
2607 if let Some(builder) = self.builder {
2608 node = crate::widget::inspector::insert_widget_builder_info(
2609 node,
2610 crate::widget::inspector::InspectorInfo {
2611 builder,
2612 items: inspector_items.into_boxed_slice(),
2613 actual_vars: crate::widget::inspector::InspectorActualVars::default(),
2614 },
2615 );
2616 }
2617
2618 #[cfg(feature = "trace_widget")]
2619 if self.trace_widget {
2620 let name = self.widget_type.name();
2621 node = node.trace(move |op| crate::update::UpdatesTrace::widget_span(crate::widget::WIDGET.id(), name, op.mtd_name()));
2622 }
2623
2624 node = with_new_context_init_id(node);
2626
2627 if let Some(handle) = when_init_context_handle {
2628 let mut handle = Some(Arc::new(handle));
2630 node = crate::widget::node::match_node(node, move |c, op| {
2631 WHEN_INPUT_CONTEXT_INIT_ID.with_context(&mut handle, || c.op(op));
2632 });
2633 }
2634
2635 node
2636 }
2637}
2638impl ops::Deref for WidgetBuilding {
2639 type Target = WidgetBuilderProperties;
2640
2641 fn deref(&self) -> &Self::Target {
2642 &self.p
2643 }
2644}
2645impl ops::DerefMut for WidgetBuilding {
2646 fn deref_mut(&mut self) -> &mut Self::Target {
2647 &mut self.p
2648 }
2649}
2650
2651#[derive(Debug)]
2653#[non_exhaustive]
2654pub struct BuilderProperty {
2655 pub importance: Importance,
2657 pub position: NestPosition,
2659 pub args: Box<dyn PropertyArgs>,
2661 pub captured: bool,
2663}
2664
2665#[derive(Debug)]
2667#[non_exhaustive]
2668pub struct BuilderPropertyRef<'a> {
2669 pub importance: Importance,
2671 pub position: NestPosition,
2673 pub args: &'a dyn PropertyArgs,
2675 pub captured: bool,
2679}
2680
2681#[derive(Debug)]
2683#[non_exhaustive]
2684pub struct BuilderPropertyMut<'a> {
2685 pub importance: &'a mut Importance,
2687 pub position: &'a mut NestPosition,
2689 pub args: &'a mut Box<dyn PropertyArgs>,
2691 pub captured: &'a mut bool,
2695}
2696
2697pub struct WidgetBuilderProperties {
2699 items: Vec<WidgetItemPositioned>,
2700}
2701impl WidgetBuilderProperties {
2702 pub fn property(&self, property_id: PropertyId) -> Option<BuilderPropertyRef<'_>> {
2704 match self.property_index(property_id) {
2705 Some(i) => match &self.items[i].item {
2706 WidgetItem::Property {
2707 importance,
2708 args,
2709 captured,
2710 } => Some(BuilderPropertyRef {
2711 importance: *importance,
2712 position: self.items[i].position,
2713 args: &**args,
2714 captured: *captured,
2715 }),
2716 WidgetItem::Intrinsic { .. } => unreachable!(),
2717 },
2718 None => None,
2719 }
2720 }
2721
2722 pub fn property_mut(&mut self, property_id: PropertyId) -> Option<BuilderPropertyMut<'_>> {
2724 match self.property_index(property_id) {
2725 Some(i) => match &mut self.items[i] {
2726 WidgetItemPositioned {
2727 position,
2728 item:
2729 WidgetItem::Property {
2730 importance,
2731 args,
2732 captured,
2733 },
2734 ..
2735 } => Some(BuilderPropertyMut {
2736 importance,
2737 position,
2738 args,
2739 captured,
2740 }),
2741 _ => unreachable!(),
2742 },
2743 None => None,
2744 }
2745 }
2746
2747 pub fn properties(&self) -> impl Iterator<Item = BuilderPropertyRef<'_>> {
2751 self.items.iter().filter_map(|it| match &it.item {
2752 WidgetItem::Intrinsic { .. } => None,
2753 WidgetItem::Property {
2754 importance,
2755 args,
2756 captured,
2757 } => Some(BuilderPropertyRef {
2758 importance: *importance,
2759 position: it.position,
2760 args: &**args,
2761 captured: *captured,
2762 }),
2763 })
2764 }
2765
2766 pub fn properties_mut(&mut self) -> impl Iterator<Item = BuilderPropertyMut<'_>> {
2768 self.items.iter_mut().filter_map(|it| match &mut it.item {
2769 WidgetItem::Intrinsic { .. } => None,
2770 WidgetItem::Property {
2771 importance,
2772 args,
2773 captured,
2774 } => Some(BuilderPropertyMut {
2775 importance,
2776 position: &mut it.position,
2777 args,
2778 captured,
2779 }),
2780 })
2781 }
2782
2783 pub fn capture_value<T>(&mut self, property_id: PropertyId) -> Option<T>
2791 where
2792 T: VarValue,
2793 {
2794 let p = self.capture_property_impl(property_id)?;
2795 let value = p.args.downcast_value::<T>(0).clone();
2796 Some(value)
2797 }
2798
2799 pub fn capture_value_or_else<T>(&mut self, property_id: PropertyId, or_else: impl FnOnce() -> T) -> T
2801 where
2802 T: VarValue,
2803 {
2804 match self.capture_value(property_id) {
2805 Some(v) => v,
2806 None => or_else(),
2807 }
2808 }
2809
2810 pub fn capture_value_or_default<T>(&mut self, property_id: PropertyId) -> T
2812 where
2813 T: VarValue + Default,
2814 {
2815 self.capture_value_or_else(property_id, T::default)
2816 }
2817
2818 fn capture_property_impl(&mut self, property_id: PropertyId) -> Option<BuilderPropertyRef<'_>> {
2819 if let Some(i) = self.property_index(property_id) {
2820 match &mut self.items[i] {
2821 WidgetItemPositioned {
2822 position,
2823 item:
2824 WidgetItem::Property {
2825 importance,
2826 args,
2827 captured,
2828 },
2829 ..
2830 } => {
2831 *captured = true;
2832 Some(BuilderPropertyRef {
2833 importance: *importance,
2834 position: *position,
2835 args: &**args,
2836 captured: *captured,
2837 })
2838 }
2839 _ => unreachable!(),
2840 }
2841 } else {
2842 None
2843 }
2844 }
2845
2846 fn property_index(&self, property_id: PropertyId) -> Option<usize> {
2847 self.items.iter().position(|it| match &it.item {
2848 WidgetItem::Property { args, .. } => args.id() == property_id,
2849 WidgetItem::Intrinsic { .. } => false,
2850 })
2851 }
2852}
2853
2854pub trait AnyPropertyAttribute: crate::private::Sealed + Any + Send + Sync {
2856 fn as_any(&self) -> &dyn Any;
2858
2859 fn clone_boxed(&self) -> Box<dyn AnyPropertyAttribute>;
2861}
2862
2863#[non_exhaustive]
2865pub struct PropertyAttributeArgs<'a, I: Any + Send> {
2866 pub input: I,
2868 pub when_conditions_data: &'a [Option<PropertyAttributeWhenData>],
2872}
2873
2874pub struct PropertyAttribute<I: Any + Send>(Arc<Mutex<dyn FnMut(PropertyAttributeArgs<I>) -> I + Send>>);
2892impl<I: Any + Send> crate::private::Sealed for PropertyAttribute<I> {}
2893impl<I: Any + Send> Clone for PropertyAttribute<I> {
2894 fn clone(&self) -> Self {
2895 Self(self.0.clone())
2896 }
2897}
2898impl<I: Any + Send> AnyPropertyAttribute for PropertyAttribute<I> {
2899 fn clone_boxed(&self) -> Box<dyn AnyPropertyAttribute> {
2900 Box::new(self.clone())
2901 }
2902
2903 fn as_any(&self) -> &dyn Any {
2904 self
2905 }
2906}
2907impl<I: Any + Send> PropertyAttribute<I> {
2908 pub fn new(build: impl FnMut(PropertyAttributeArgs<I>) -> I + Send + 'static) -> Self {
2910 Self(Arc::new(Mutex::new(build)))
2911 }
2912
2913 pub fn no_op() -> Self {
2915 Self::new(|i| i.input)
2916 }
2917
2918 pub fn build(&self, args: PropertyAttributeArgs<I>) -> I {
2920 (self.0.lock())(args)
2921 }
2922}
2923impl Clone for Box<dyn AnyPropertyAttribute> {
2924 fn clone(&self) -> Self {
2925 self.clone_boxed()
2926 }
2927}
2928
2929pub struct PropertyInputTypes<Tuple>(std::marker::PhantomData<Tuple>);
2990impl<Tuple> PropertyInputTypes<Tuple> {
2991 pub const fn unit() -> Self {
2993 Self(std::marker::PhantomData)
2994 }
2995}
2996impl<Tuple> Clone for PropertyInputTypes<Tuple> {
2997 fn clone(&self) -> Self {
2998 *self
2999 }
3000}
3001impl<Tuple> Copy for PropertyInputTypes<Tuple> {}
3002unsafe impl<Tuple> Send for PropertyInputTypes<Tuple> {}
3004unsafe impl<Tuple> Sync for PropertyInputTypes<Tuple> {}