ferroui_base/data/
relative_source.rs1use crate::TypeInfo;
2use std::cell::Cell;
3use std::rc::Rc;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub enum RelativeSourceMode {
8 DataContext,
10 TemplatedParent,
12 SelfMode,
14 FindAncestor,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
21pub enum TreeType {
22 Visual,
24 Logical,
26}
27
28#[derive(Debug)]
37pub struct RelativeSource {
38 ancestor_level: Cell<i32>,
39 ancestor_type: Cell<Option<&'static TypeInfo>>,
40 mode: Cell<RelativeSourceMode>,
41 tree: Cell<TreeType>,
42}
43
44impl Default for RelativeSource {
45 fn default() -> Self {
47 Self {
48 ancestor_level: Cell::new(1),
49 ancestor_type: Cell::new(None),
50 mode: Cell::new(RelativeSourceMode::FindAncestor),
51 tree: Cell::new(TreeType::Visual),
52 }
53 }
54}
55
56impl PartialEq for RelativeSource {
59 #[inline]
60 fn eq(&self, other: &Self) -> bool {
61 std::ptr::eq(self, other)
62 }
63}
64
65impl RelativeSource {
66 pub fn empty() -> Rc<Self> {
69 Rc::new(Self::default())
70 }
71
72 pub fn new(mode: RelativeSourceMode) -> Rc<Self> {
74 Self::empty().with_mode(mode)
75 }
76
77 pub fn ancestor_level(&self) -> i32 {
81 self.ancestor_level.get()
82 }
83
84 pub fn set_ancestor_level(&self, value: i32) {
86 if value <= 0 {
87 panic!("AncestorLevel may not be set to less than 1.");
88 }
89 self.ancestor_level.set(value);
90 }
91
92 pub fn with_ancestor_level(self: Rc<Self>, value: i32) -> Rc<Self> {
94 self.set_ancestor_level(value);
95 self
96 }
97
98 pub fn ancestor_type(&self) -> Option<&'static TypeInfo> {
101 self.ancestor_type.get()
102 }
103
104 pub fn set_ancestor_type(&self, value: Option<&'static TypeInfo>) {
105 self.ancestor_type.set(value)
106 }
107
108 pub fn with_ancestor_type(self: Rc<Self>, value: Option<&'static TypeInfo>) -> Rc<Self> {
110 self.set_ancestor_type(value);
111 self
112 }
113
114 pub fn mode(&self) -> RelativeSourceMode {
116 self.mode.get()
117 }
118
119 pub fn set_mode(&self, value: RelativeSourceMode) {
120 self.mode.set(value)
121 }
122
123 pub fn with_mode(self: Rc<Self>, value: RelativeSourceMode) -> Rc<Self> {
125 self.set_mode(value);
126 self
127 }
128
129 pub fn tree(&self) -> TreeType {
131 self.tree.get()
132 }
133
134 pub fn set_tree(&self, value: TreeType) {
135 self.tree.set(value)
136 }
137
138 pub fn with_tree(self: Rc<Self>, value: TreeType) -> Rc<Self> {
140 self.set_tree(value);
141 self
142 }
143}