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
252
//! Resources shared by every window of an application.
use super::Context;
use crate::{
images::{ImageMetrics, SharedImages, TextureIds},
material::MaterialRegistry,
text::GlyphStore,
FontFamily, ImageDecoder, ImageLimits, Style, Theme,
};
use std::{
fmt,
sync::{
atomic::{AtomicU64, Ordering},
Arc, Mutex,
},
};
/// Heavy, window-independent state: the font family, the glyph atlas, the image cache and
/// its limits, and the application appearance.
///
/// One `Context` belongs to one native window and owns everything that is per window: input,
/// focus, hover, pointer capture, IME, popups, tooltips, modals, scrolling, drag, viewport and
/// all retained widget state keyed by [`Id`](crate::Id). Contexts built from clones of one
/// `SharedResources` use one glyph atlas and one image cache, so a glyph or an image is
/// rasterized, decoded and uploaded once however many windows show it. The desktop runner
/// creates every window's context this way.
///
/// Cloning is cheap and yields a handle to the same resources.
///
/// ```
/// use zaxis::{Context, SharedResources, Theme};
/// let resources = SharedResources::new();
/// let mut first = Context::with_shared(&resources);
/// let second = Context::with_shared(&resources);
/// // One call re-themes every window; each applies it on its next pass.
/// resources.set_theme(Theme::light());
/// first.run(|_| {});
/// assert_eq!(first.style(), &Theme::light().resolve());
/// # let _ = second;
/// ```
#[derive(Clone)]
pub struct SharedResources {
inner: Arc<Inner>,
}
struct Inner {
family: FontFamily,
/// `None` leaves monospace text to the system's generic monospace font.
monospace: Option<FontFamily>,
glyphs: Arc<Mutex<GlyphStore>>,
images: SharedImages,
appearance: Mutex<Appearance>,
appearance_revision: AtomicU64,
materials: Mutex<MaterialRegistry>,
}
#[derive(Default)]
struct Appearance {
theme: Option<Theme>,
style: Option<Style>,
}
impl Default for SharedResources {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for SharedResources {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SharedResources")
.field("appearance_revision", &self.appearance_revision())
.finish_non_exhaustive()
}
}
impl SharedResources {
/// Resources with the bundled Inter family and no appearance broadcast yet: every
/// context keeps its own dark default until [`set_theme`](Self::set_theme) is called.
pub fn new() -> Self {
Self::with_fonts(FontFamily::inter())
}
/// Replace the font family for every window. System fonts still supply scripts the
/// family lacks, followed by color emoji.
pub fn with_fonts(family: FontFamily) -> Self {
Self::with_font_families(family, FontFamily::default_monospace())
}
/// Replace both families for every window: the main one and the one that
/// `TextFamily::Monospace` text, `Typography::code` and `Text::monospace` use.
///
/// `None` for `monospace` uses the system's generic monospace font, which differs
/// between platforms; the default is the bundled JetBrains Mono (`bundled-monospace`
/// feature). A system font is never chosen over a configured or bundled family.
pub fn with_font_families(family: FontFamily, monospace: Option<FontFamily>) -> Self {
let ids = TextureIds::default();
Self {
inner: Arc::new(Inner {
family,
monospace,
glyphs: Arc::new(Mutex::new(GlyphStore::new(ids.clone()))),
images: SharedImages::new(ids),
appearance: Mutex::default(),
appearance_revision: AtomicU64::new(0),
materials: Mutex::default(),
}),
}
}
/// Install a theme for every window. Each context applies it at the start of its next
/// pass and is asked to repaint, so the change reaches visible windows in their next
/// frame and hidden ones when they are shown again. Windows created later start with it.
/// A theme installed directly with [`Context::set_theme`] is replaced by the next call.
pub fn set_theme(&self, theme: Theme) {
self.broadcast(Appearance {
theme: Some(theme),
style: None,
});
}
/// Install a resolved style for every window; see [`set_theme`](Self::set_theme).
pub fn set_style(&self, style: Style) {
self.broadcast(Appearance {
theme: None,
style: Some(style),
});
}
/// The theme last installed with [`set_theme`](Self::set_theme), if any.
pub fn theme(&self) -> Option<Theme> {
self.lock_appearance().theme.clone()
}
pub fn image_limits(&self) -> ImageLimits {
self.inner.images.lock().limits.clone()
}
/// Replace the image limits of every window. Decoded images are dropped and reloaded.
pub fn set_image_limits(&self, limits: ImageLimits) {
let mut images = self.inner.images.lock();
images.limits = limits;
images.clear_decoded();
}
/// Register a decoder for every window; see [`Context::add_image_decoder`].
pub fn add_image_decoder(&self, decoder: Arc<dyn ImageDecoder>) {
self.inner.images.lock().add_decoder(decoder);
}
/// Wake callback for finished image jobs; see [`Context::set_image_waker`].
pub fn set_image_waker(&self, waker: impl Fn() + Send + Sync + 'static) {
self.inner.images.lock().set_waker(Some(Arc::new(waker)));
}
/// Decode images on the drawing thread within a per-frame budget; see
/// [`Context::decode_images_inline`].
pub fn decode_images_inline(&self, frame_budget: Option<std::time::Duration>) {
self.inner.images.lock().set_inline_decoding(frame_budget);
}
/// Image cache counters summed over every window.
pub fn image_metrics(&self) -> ImageMetrics {
self.inner.images.lock().metrics()
}
pub fn font_family(&self) -> &FontFamily {
&self.inner.family
}
/// The configured monospace family, `None` for the system's generic monospace font.
pub fn monospace_family(&self) -> Option<&FontFamily> {
self.inner.monospace.as_ref()
}
/// Whether `other` is a handle to the same resources.
pub fn same_as(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
pub(crate) fn glyphs(&self) -> Arc<Mutex<GlyphStore>> {
Arc::clone(&self.inner.glyphs)
}
pub(crate) fn images(&self) -> &SharedImages {
&self.inner.images
}
pub(crate) fn appearance_revision(&self) -> u64 {
self.inner.appearance_revision.load(Ordering::Acquire)
}
/// The registry of materials; see `material::registry`.
pub(crate) fn materials(&self) -> std::sync::MutexGuard<'_, MaterialRegistry> {
self.inner
.materials
.lock()
.expect("material registry mutex")
}
fn lock_appearance(&self) -> std::sync::MutexGuard<'_, Appearance> {
self.inner.appearance.lock().expect("appearance mutex")
}
fn broadcast(&self, appearance: Appearance) {
*self.lock_appearance() = appearance;
self.inner
.appearance_revision
.fetch_add(1, Ordering::Release);
}
}
impl Context {
/// A context for one window that uses `resources`: its glyph atlas, image cache, font
/// family and broadcast appearance. Create one context per native window.
pub fn with_shared(resources: &SharedResources) -> Self {
Self::build(resources.clone())
}
/// The resources this context shares with the other windows of its application.
pub fn shared_resources(&self) -> &SharedResources {
&self.shared
}
/// Apply a pending appearance broadcast. Runs at the start of a pass.
pub(super) fn sync_appearance(&mut self) {
let revision = self.shared.appearance_revision();
if revision == self.appearance_seen {
return;
}
self.appearance_seen = revision;
let (theme, style) = {
let appearance = self.shared.lock_appearance();
(appearance.theme.clone(), appearance.style.clone())
};
if let Some(theme) = theme {
self.set_theme(theme);
} else if let Some(style) = style {
self.set_style(style);
}
}
/// Whether another window changed what this one shows: a new appearance, or finished
/// image work for an image it draws. Hidden-from-images windows are not woken.
pub(super) fn shared_state_changed(&self) -> bool {
self.shared.appearance_revision() != self.appearance_seen
|| (self.shows_images && {
let images = self.images.lock();
images.has_results() || images.epoch() != self.images_epoch
})
}
}