aylis_core/
event_value.rs1use crate::component::ComponentId;
7use crate::event::{EventArgs, GestureEvent, KeyEvent, PointerEvent, SwipeEvent, WheelEvent};
8use crate::prop::{PropEnum, PropKind, PropValue};
9use crate::ui::Ui;
10use crate::{Color, Date, DateTime, Time};
11
12#[derive(Clone, Copy, Debug, PartialEq)]
14pub enum EventArg {
15 Unknown,
17 None,
19 Pointer,
20 Wheel,
21 Gesture,
22 Swipe,
23 Key,
24 DateRange,
26 Value(PropKind),
28}
29
30pub trait EventValue: Sized + 'static {
32 const ARG: EventArg;
34 fn from_args(args: &EventArgs) -> Option<Self>;
36}
37
38fn text_of(args: &EventArgs) -> Option<&str> {
40 match args {
41 EventArgs::Text(t) => Some(t),
42 EventArgs::Value(PropValue::String(t) | PropValue::Enum(t)) => Some(t),
43 _ => None,
44 }
45}
46
47impl EventValue for () {
48 const ARG: EventArg = EventArg::None;
49 fn from_args(_args: &EventArgs) -> Option<Self> {
50 Some(())
51 }
52}
53
54impl EventValue for bool {
55 const ARG: EventArg = EventArg::Value(PropKind::Bool);
56 fn from_args(args: &EventArgs) -> Option<Self> {
57 match args {
58 EventArgs::Value(v) => v.to_bool().ok(),
59 _ => None,
60 }
61 }
62}
63
64impl EventValue for i64 {
65 const ARG: EventArg = EventArg::Value(PropKind::Int);
66 fn from_args(args: &EventArgs) -> Option<Self> {
67 match args {
68 EventArgs::Value(v) => v.to_i64().ok(),
69 EventArgs::Text(t) => t.trim().parse().ok(),
70 _ => None,
71 }
72 }
73}
74
75impl EventValue for f64 {
76 const ARG: EventArg = EventArg::Value(PropKind::Float);
77 fn from_args(args: &EventArgs) -> Option<Self> {
78 match args {
79 EventArgs::Value(v) => v.to_f64().ok(),
80 EventArgs::Text(t) => t.trim().parse().ok(),
81 _ => None,
82 }
83 }
84}
85
86impl EventValue for String {
87 const ARG: EventArg = EventArg::Value(PropKind::String);
88 fn from_args(args: &EventArgs) -> Option<Self> {
89 text_of(args).map(str::to_string)
90 }
91}
92
93impl EventValue for Color {
94 const ARG: EventArg = EventArg::Value(PropKind::Color);
95 fn from_args(args: &EventArgs) -> Option<Self> {
96 match args {
97 EventArgs::Value(PropValue::Color(c)) => Some(*c),
98 _ => text_of(args).and_then(Color::from_hex),
99 }
100 }
101}
102
103impl EventValue for Option<Date> {
104 const ARG: EventArg = EventArg::Value(PropKind::Date);
105 fn from_args(args: &EventArgs) -> Option<Self> {
106 text_of(args).map(Date::parse_iso)
107 }
108}
109
110impl EventValue for Option<Time> {
111 const ARG: EventArg = EventArg::Value(PropKind::Time);
112 fn from_args(args: &EventArgs) -> Option<Self> {
113 text_of(args).map(Time::parse_iso)
114 }
115}
116
117impl EventValue for Option<DateTime> {
118 const ARG: EventArg = EventArg::Value(PropKind::DateTime);
119 fn from_args(args: &EventArgs) -> Option<Self> {
120 text_of(args).map(DateTime::parse_iso)
121 }
122}
123
124impl EventValue for Option<crate::Shadow> {
125 const ARG: EventArg = EventArg::Value(PropKind::Shadow);
126 fn from_args(args: &EventArgs) -> Option<Self> {
127 Some(text_of(args).and_then(crate::Shadow::parse))
128 }
129}
130
131impl EventValue for crate::Easing {
132 const ARG: EventArg = EventArg::Value(PropKind::Easing);
133 fn from_args(args: &EventArgs) -> Option<Self> {
134 Some(text_of(args).and_then(crate::Easing::parse).unwrap_or_default())
135 }
136}
137
138impl EventValue for Vec<String> {
139 const ARG: EventArg = EventArg::Value(PropKind::List);
140 fn from_args(args: &EventArgs) -> Option<Self> {
141 if let EventArgs::List(items) = args {
144 return Some(items.clone());
145 }
146 text_of(args).map(|t| t.split(';').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect())
147 }
148}
149
150impl EventValue for Option<ComponentId> {
151 const ARG: EventArg = EventArg::Value(PropKind::Component);
152 fn from_args(args: &EventArgs) -> Option<Self> {
153 match args {
154 EventArgs::Value(v) => v.to_component().ok(),
155 EventArgs::None => Some(None),
156 _ => None,
157 }
158 }
159}
160
161impl EventValue for PointerEvent {
162 const ARG: EventArg = EventArg::Pointer;
163 fn from_args(args: &EventArgs) -> Option<Self> {
164 match args {
165 EventArgs::Pointer(p) => Some(*p),
166 _ => None,
167 }
168 }
169}
170
171impl EventValue for WheelEvent {
172 const ARG: EventArg = EventArg::Wheel;
173 fn from_args(args: &EventArgs) -> Option<Self> {
174 match args {
175 EventArgs::Wheel(w) => Some(*w),
176 _ => None,
177 }
178 }
179}
180
181impl EventValue for GestureEvent {
182 const ARG: EventArg = EventArg::Gesture;
183 fn from_args(args: &EventArgs) -> Option<Self> {
184 match args {
185 EventArgs::Gesture(g) => Some(*g),
186 _ => None,
187 }
188 }
189}
190
191impl EventValue for SwipeEvent {
192 const ARG: EventArg = EventArg::Swipe;
193 fn from_args(args: &EventArgs) -> Option<Self> {
194 match args {
195 EventArgs::Swipe(s) => Some(*s),
196 _ => None,
197 }
198 }
199}
200
201impl EventValue for KeyEvent {
202 const ARG: EventArg = EventArg::Key;
203 fn from_args(args: &EventArgs) -> Option<Self> {
204 match args {
205 EventArgs::Key(k) => Some(*k),
206 _ => None,
207 }
208 }
209}
210
211impl EventValue for DateRange {
212 const ARG: EventArg = EventArg::DateRange;
213 fn from_args(args: &EventArgs) -> Option<Self> {
214 let text = text_of(args)?;
215 let (start, end) = text.split_once(';').unwrap_or((text, ""));
216 Some(DateRange { start: Date::parse_iso(start), end: Date::parse_iso(end) })
217 }
218}
219
220impl<E: PropEnum> EventValue for E {
221 const ARG: EventArg = EventArg::Value(PropKind::Enum(E::NAMES));
222 fn from_args(args: &EventArgs) -> Option<Self> {
223 text_of(args).and_then(E::from_name)
224 }
225}
226
227#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
230pub struct DateRange {
231 pub start: Option<Date>,
232 pub end: Option<Date>,
233}
234
235pub trait FromSender: Sized {
238 fn from_sender(ui: &Ui, id: ComponentId) -> Option<Self>;
239}
240
241impl FromSender for ComponentId {
242 fn from_sender(_ui: &Ui, id: ComponentId) -> Option<Self> {
243 Some(id)
244 }
245}