-
Notifications
You must be signed in to change notification settings - Fork 119
/
decorator.rs
82 lines (70 loc) · 2.48 KB
/
decorator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use glazier::kurbo::{Point, Rect};
use leptos_reactive::create_effect;
use crate::{
app_handle::{AppContext, StyleSelector},
event::{Event, EventListner},
style::Style,
view::View,
};
pub trait Decorators: View + Sized {
fn style(self, cx: AppContext, style: impl Fn() -> Style + 'static) -> Self {
let id = self.id();
create_effect(cx.scope, move |_| {
let style = style();
AppContext::update_style(id, style);
});
self
}
/// The visual style to apply when the mouse hovers over the element
fn hover_style(self, cx: AppContext, style: impl Fn() -> Style + 'static) -> Self {
let id = self.id();
create_effect(cx.scope, move |_| {
let style = style();
AppContext::update_style_selector(id, style, StyleSelector::Hover);
});
self
}
fn focus_style(self, cx: AppContext, style: impl Fn() -> Style + 'static) -> Self {
let id = self.id();
create_effect(cx.scope, move |_| {
let style = style();
AppContext::update_style_selector(id, style, StyleSelector::Focus);
});
self
}
fn active_style(self, cx: AppContext, style: impl Fn() -> Style + 'static) -> Self {
let id = self.id();
create_effect(cx.scope, move |_| {
let style = style();
AppContext::update_style_selector(id, style, StyleSelector::Active);
});
self
}
fn disabled_style(self, cx: AppContext, style: impl Fn() -> Style + 'static) -> Self {
let id = self.id();
create_effect(cx.scope, move |_| {
let style = style();
AppContext::update_style_selector(id, style, StyleSelector::Disabled);
});
self
}
fn disabled(self, cx: AppContext, disabled_fn: impl Fn() -> bool + 'static) -> Self {
let id = self.id();
create_effect(cx.scope, move |_| {
let is_disabled = disabled_fn();
AppContext::update_disabled(id, is_disabled);
});
self
}
fn on_event(self, listener: EventListner, action: impl Fn(&Event) -> bool + 'static) -> Self {
let id = self.id();
AppContext::update_event_listner(id, listener, Box::new(action));
self
}
fn on_resize(self, action: impl Fn(Point, Rect) + 'static) -> Self {
let id = self.id();
AppContext::update_resize_listner(id, Box::new(action));
self
}
}
impl<V: View> Decorators for V {}