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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
pub mod hashids;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::cell::*;
use std::collections::*;
use crate::backend::css;
use crate::backend::browser;
use crate::backend::browser::{NodeApi, ElementApi, Element, Stylesheet, AdjacentPosition};
use crate::view_sys::shared::*;
pub(crate) fn upsert(styling: &Styling) -> StylingEnv {
CSS_REGISTRY.with({
move |reg| {
reg .borrow_mut()
.upsert(styling);
}
});
styling_env(&styling)
}
pub(crate) fn removed(styling: &Styling) -> StylingEnv {
styling_env(&styling)
}
pub struct StylingEnv {
pub css_id: u64,
pub animation_ids: Vec<u64>,
}
impl StylingEnv {
pub fn css_id(&self) -> String {
css_id_format(self.css_id)
}
pub fn animation_ids(&self) -> Vec<String> {
self.animation_ids
.iter()
.map(|aid| css_keyframe_id(self.css_id, aid.clone()))
.collect()
}
}
pub(crate) fn css_id_format(x: u64) -> String {
let hasher = hashids::HashIds::new();
let short_id = hasher.encode(&[x]);
format!("_{}", short_id)
}
fn css_keyframe_id(styling_hash: u64, animation_hash: u64) -> String {
let hasher = hashids::HashIds::new();
let short_id = hasher.encode(&[styling_hash, animation_hash]);
format!("aid-{}", short_id)
}
pub(crate) fn is_empty_hash(x: u64) -> bool {
let empty: Styling = Styling::default();
x == calculate_hash(&empty)
}
thread_local! {
pub(crate) static CSS_REGISTRY: RefCell<CssRegistry> = {
RefCell::new(CssRegistry::Pending)
};
}
pub enum CssRegistry {
Pending,
Live(LiveCssRegistry)
}
pub struct LiveCssRegistry {
mount: CssMount,
added: HashSet<u64>,
}
pub struct CssMount {
wrapper: browser::Element,
local: browser::Stylesheet,
state: browser::Stylesheet,
media: browser::Stylesheet,
}
pub(crate) fn register_defaults(element: &browser::Element) {
element.set_attribute("x", "");
}
fn add_global_css_defaults(node: &browser::Stylesheet) {
let default_id = "x";
node.push_declaration(css::Declaration {
selector: format!("[{}]", default_id),
properties: css::Properties(vec![
css::Property {
property: String::from("box-sizing"),
value: String::from("border-box"),
}
]),
});
}
impl CssRegistry {
fn init() -> Self {
let window = browser::window();
let wrapper = window.document.create_element("app-internals");
let local = window.document.create_element("style");
let state = window.document.create_element("style");
let media = window.document.create_element("style");
local.set_attribute("ss-defaults", "");
state.set_attribute("ss-state-and-animation", "");
media.set_attribute("ss-media", "");
wrapper.append_child(&local);
wrapper.append_child(&state);
wrapper.append_child(&media);
let local = browser::Stylesheet::from_element(local);
let state = browser::Stylesheet::from_element(state);
let media = browser::Stylesheet::from_element(media);
wrapper.set_attribute("subscript-cssom-interface", "");
wrapper.set_attribute("style", "display: none;");
wrapper.set_attribute("hidden", "true");
window.document.body.insert_adjacent_element(AdjacentPosition::AfterBegin, &wrapper);
add_global_css_defaults(&local);
CssRegistry::Live(LiveCssRegistry {
added: HashSet::new(),
mount: CssMount {wrapper,local,state,media},
})
}
fn get_live(&mut self) -> &mut LiveCssRegistry {
match self {
CssRegistry::Live(live) => live,
CssRegistry::Pending => {
*self = CssRegistry::init();
match self {
CssRegistry::Live(live) => live,
CssRegistry::Pending => panic!()
}
}
}
}
fn upsert(&mut self, styling: &Styling) {
let hash = calculate_hash(&styling);
let live = self.get_live();
let already_added = live.added.contains(&hash);
if !already_added {
console!("new stylesheet");
insert_styling(styling, hash, &live.mount);
live.added.insert(hash);
}
}
}
fn styling_env(styling: &Styling) -> StylingEnv {
let hash = calculate_hash(&styling);
let mut env = StylingEnv {
css_id: hash,
animation_ids: Vec::new(),
};
for animation in styling.animations.iter() {
let aid = calculate_hash(&animation);
env.animation_ids.push(aid);
}
env
}
fn insert_styling(styling: &Styling, hash: u64, mount: &CssMount) {
let to_properties = |xs: &Vec<Style>| -> css::Properties {
let xs = xs
.iter()
.map(|x| -> css::Property {
css::Property{
property: x.property.clone(),
value: x.value.clone(),
}
})
.collect::<Vec<_>>();
css::Properties(xs)
};
let mut default = css::Declaration {
selector: format!(".{}", css_id_format(hash)),
properties: to_properties(&styling.default.0),
};
if !styling.animations.is_empty() {
let styling_env = styling_env(styling);
let aids = styling_env.animation_ids();
default.properties.0.push(css::Property{
property: String::from("animation-name"),
value: aids.join(","),
});
}
mount.local.push_declaration(default);
for state in styling.state.iter() {
let state = css::Declaration {
selector: format!(".{}{}", css_id_format(hash), state.name.as_str()),
properties: to_properties(&state.body.0),
};
mount.state.push_declaration(state);
}
for animation in styling.animations.iter() {
let aid = calculate_hash(&animation);
let keyfrmaes = animation.0
.iter()
.map(|keyframe| -> css::KeyframeInterval {
css::KeyframeInterval {
value: keyframe.value.clone(),
style: to_properties(&keyframe.style.0),
}
})
.collect::<Vec<_>>();
let keyfrmaes = css::Keyframes {
name: css_keyframe_id(hash, aid),
keyframes: keyfrmaes,
};
mount.state.push_keyframes(keyfrmaes);
}
for media in styling.media.iter() {
let media = css::Media {
condition: to_properties(&media.condition.0).0,
declarations: vec![css::Declaration {
selector: format!(".{}", css_id_format(hash)),
properties: to_properties(&media.body.0),
}],
};
mount.media.push_media(media);
}
}
fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}