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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
use std::collections::{HashMap, HashSet};
use std::cell::RefCell;
use std::rc::Rc;
use std::path::{Path, PathBuf};
use std::time::Instant;
use serde::{Serialize, Deserialize};
use crate::{BuildOptions, Error, Point, Frame, MouseButton, Rect, frame::{RendGroup, RendGroupDef}};
use crate::{font::FontSummary, widget::Widget, image::ImageHandle, theme::ThemeSet, resource::ResourceSet};
use crate::theme_definition::{AnimState, AnimStateKey};
use crate::render::Renderer;
#[derive(Copy, Clone)]
pub(crate) struct PersistentStateData {
pub is_open: bool,
pub resize: Point,
pub moved: Point,
pub scroll: Point,
}
/**
The internal state stored by Thyme for a given Widget that
persists between frames.
Note that Thyme will generally be able to automatically generate
unique IDs for many widgets such as buttons. But, if you want to
access this data for a particular widget you will need to specify
a known ID for that widget.
# Example
```
fn reset_window_state(ui: &mut Frame, window_id: &str) {
ui.modify(window_id, |state| {
state.resize = Point::default();
state.moved = Point::default();
state.is_open = true;
});
}
```
*/
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PersistentState {
/// Whether the widget will be shown. Defaults to true.
pub is_open: bool,
/// Whether a tree or similar widget is expanded, showing all children, or not
pub expanded: bool,
/// An amount, in logical pixels that the widget has been resized by. Default to zero.
pub resize: Point,
/// An amount, in logical pizels that the widget has been moved by. Defaults to zero.
pub moved: Point,
/// An amount, in logical pixels that the internal content has been
/// scrolled by. Defaults to zero.
pub scroll: Point,
/// The "zero" time for timed images associated with this widget. Defaults to zero,
/// which is the internal [`Context`](struct.Context.html) init time.
pub base_time_millis: u32,
/// Any characters that have been sent to this widget from the keyboard. Defaults to
/// empty. Widgets should typically drain this list as they work with input.
pub characters: Vec<char>,
/// The text for this widget, overriding default text. Defaults to `None`.
pub text: Option<String>,
/// A timer in milliseconds, allowing the widget to easily store a particular relevant
/// time - for delayed actions, for example
pub timer: u32,
}
impl PersistentState {
pub(crate) fn copy_data(&self) -> PersistentStateData {
PersistentStateData {
is_open: self.is_open,
resize: self.resize,
moved: self.moved,
scroll: self.scroll,
}
}
}
impl Default for PersistentState {
fn default() -> Self {
PersistentState {
is_open: true,
expanded: true,
resize: Point::default(),
moved: Point::default(),
scroll: Point::default(),
base_time_millis: 0,
characters: Vec::default(),
text: None,
timer: 0,
}
}
}
/// The current state of the various keyboard modifier keys - Shift, Control, and Alt
/// You can get this using [`Frame.input_modiifers`](struct.Frame.html#method.input_modifiers)
#[derive(Default, Copy, Clone, Debug)]
pub struct InputModifiers {
/// whether the Shift key is pressed
pub shift: bool,
/// whether the Control key is pressed
pub ctrl: bool,
/// Whether the Alt key is pressed
pub alt: bool,
}
pub struct ContextInternal {
resources: ResourceSet,
options: BuildOptions,
themes: ThemeSet,
frame_active: bool,
mouse_taken_switch_time: u32,
mouse_taken_switch_position: Option<Point>,
mouse_taken_last_frame: Option<(String, RendGroup)>,
mouse_in_rend_group_last_frame: Option<RendGroup>,
top_rend_group: RendGroup,
check_set_top_rend_group: Option<String>,
mouse_pressed_outside: [bool; 3],
keyboard_focus_widget: Option<String>,
empty_persistent_state: PersistentState,
modal: Option<Modal>,
persistent_state: HashMap<String, PersistentState>,
input_modifiers: InputModifiers,
last_mouse_pos: Point,
mouse_pos: Point,
mouse_pressed: [bool; 3],
mouse_clicked: [bool; 3],
mouse_wheel: Point,
display_size: Point,
scale_factor: f32,
start_instant: Instant,
time_millis: u32,
errors: HashSet<String>,
}
impl ContextInternal {
pub(crate) fn log(&mut self, level: log::Level, error: String) {
if self.errors.contains(&error) { return; }
log::log!(level, "{}", error);
self.errors.insert(error);
}
pub(crate) fn mut_modal<F: FnOnce(&mut Modal)>(&mut self, f: F) {
if let Some(modal) = self.modal.as_mut() {
(f)(modal);
}
}
pub(crate) fn modal_id(&self) -> Option<&str> {
self.modal.as_ref().map(|modal| modal.id.as_ref())
}
pub(crate) fn has_modal(&self) -> bool {
self.modal.is_some()
}
pub(crate) fn clear_modal_if_match(&mut self, id: &str) {
if self.modal_id() == Some(id) {
self.modal.take();
}
}
pub(crate) fn set_modal(&mut self, id: String) {
self.modal = Some(Modal::new(id));
}
pub(crate) fn mouse_in_rend_group_last_frame(&self) -> Option<RendGroup> {
self.mouse_in_rend_group_last_frame
}
pub(crate) fn set_top_rend_group(&mut self, group: RendGroup) {
self.top_rend_group = group;
}
pub(crate) fn top_rend_group(&self) -> RendGroup { self.top_rend_group }
pub(crate) fn set_top_rend_group_id(&mut self, id: &str) {
self.check_set_top_rend_group = Some(id.to_string());
}
pub(crate) fn check_set_rend_group_top(&mut self, groups: &[RendGroupDef]) {
let id = match &self.check_set_top_rend_group {
None => return,
Some(id) => id,
};
for group in groups {
if group.id() == id {
self.top_rend_group = group.group();
self.check_set_top_rend_group = None;
break;
}
}
}
pub(crate) fn base_time_millis_for(&self, id: &str) -> u32 {
self.persistent_state.get(id).map_or(0, |state| state.base_time_millis)
}
pub(crate) fn time_millis(&self) -> u32 { self.time_millis }
pub(crate) fn mouse_pos(&self) -> Point { self.mouse_pos }
pub(crate) fn last_mouse_pos(&self) -> Point { self.last_mouse_pos }
pub(crate) fn mouse_pressed(&self, index: usize) -> bool { self.mouse_pressed[index] }
pub(crate) fn mouse_pressed_button(&self) -> Option<MouseButton> {
if self.mouse_pressed[0] { Some(MouseButton::Left) }
else if self.mouse_pressed[1] { Some(MouseButton::Right) }
else if self.mouse_pressed[2] { Some(MouseButton::Middle) }
else { None }
}
pub(crate) fn mouse_clicked_button(&self) -> Option<MouseButton> {
if self.mouse_clicked[0] { Some(MouseButton::Left) }
else if self.mouse_clicked[1] { Some(MouseButton::Right) }
else if self.mouse_clicked[2] { Some(MouseButton::Middle) }
else { None }
}
pub (crate) fn set_focus_keyboard(&mut self, id: String) {
self.keyboard_focus_widget = Some(id);
}
pub (crate) fn is_focus_keyboard(&self, id: &str) -> bool {
self.keyboard_focus_widget.as_deref() == Some(id)
}
pub(crate) fn take_mouse_wheel(&mut self) -> Point {
let result = self.mouse_wheel;
self.mouse_wheel = Point::default();
result
}
pub(crate) fn mouse_taken_last_frame_id(&self) -> Option<&str> {
self.mouse_taken_last_frame.as_ref().map(|(id, _)| id.as_ref())
}
pub(crate) fn scale_factor(&self) -> f32 { self.scale_factor }
pub(crate) fn display_size(&self) -> Point { self.display_size }
pub(crate) fn themes(&self) -> &ThemeSet { &self.themes }
pub(crate) fn init_state<T: Into<String>>(&mut self, id: T, open: bool, expanded: bool) {
self.persistent_state.entry(id.into()).or_insert(
PersistentState {
is_open: open,
expanded,
..Default::default()
}
);
}
pub(crate) fn clear_state(&mut self, id: &str) {
self.persistent_state.remove(id);
}
pub(crate) fn state(&self, id: &str) -> &PersistentState {
match self.persistent_state.get(id) {
None => &self.empty_persistent_state,
Some(state) => state,
}
}
pub(crate) fn state_mut<T: Into<String>>(&mut self, id: T) -> &mut PersistentState {
self.persistent_state.entry(id.into()).or_default()
}
pub(crate) fn mouse_pressed_outside(&self) -> bool {
for pressed in self.mouse_pressed_outside.iter() {
if *pressed { return true; }
}
false
}
pub(crate) fn input_modifiers(&self) -> InputModifiers {
self.input_modifiers
}
pub(crate) fn update_mouse_taken_switch_time(&mut self, taken: &Option<(String, RendGroup)>) {
if taken != &self.mouse_taken_last_frame {
self.mouse_taken_switch_time = self.time_millis;
self.mouse_taken_switch_position = None;
}
}
pub(crate) fn tooltip_ready(&mut self, mouse_rect: Rect) -> Option<Point> {
// do not allow tooltip to show while a mouse button is pressed
if self.mouse_pressed.iter().any(|p| *p) {
return None;
}
let ready = self.time_millis - self.mouse_taken_switch_time > self.options.tooltip_time;
if ready && self.mouse_taken_switch_position.is_none() {
self.mouse_taken_switch_position = Some(Point::new(mouse_rect.left(), mouse_rect.bot()));
}
self.mouse_taken_switch_position
}
pub(crate) fn next_frame(&mut self, mouse_taken: Option<(String, RendGroup)>, mouse_in_rend_group: Option<RendGroup>) {
let mut clear_modal = false;
if let Some(modal) = self.modal.as_mut() {
if modal.prevent_close {
modal.prevent_close = false;
} else if modal.close_on_click_outside && self.mouse_clicked[0] && !modal.bounds.is_inside(self.mouse_pos) {
clear_modal = true;
}
}
if clear_modal {
let modal = self.modal.take().unwrap();
self.state_mut(modal.id).is_open = false;
}
self.mouse_wheel = Point::default();
self.mouse_clicked = [false; 3];
self.mouse_taken_last_frame = mouse_taken;
self.last_mouse_pos = self.mouse_pos;
self.mouse_in_rend_group_last_frame = mouse_in_rend_group;
self.frame_active = false;
}
}
/**
The main Thyme Context that holds internal [`PersistentState`](struct.PersistentState.html)
and is responsible for creating [`Frames`](struct.Frame.html).
This is created by [`build`](struct.ContextBuilder.html#method.build) on
[`ContextBuilder`](struct.ContextBuilder.html) after resource registration is complete.
**/
pub struct Context {
internal: Rc<RefCell<ContextInternal>>,
}
impl Context {
pub(crate) fn new(
resources: ResourceSet,
options: BuildOptions,
themes: ThemeSet,
display_size: Point,
scale_factor: f32
) -> Context {
let internal = ContextInternal {
resources,
options,
display_size,
scale_factor,
themes,
persistent_state: HashMap::new(),
empty_persistent_state: PersistentState::default(),
mouse_pos: Point::default(),
last_mouse_pos: Point::default(),
input_modifiers: InputModifiers::default(),
mouse_pressed: [false; 3],
mouse_clicked: [false; 3],
mouse_wheel: Point::default(),
mouse_taken_switch_time: 0,
mouse_taken_switch_position: None,
mouse_taken_last_frame: None,
mouse_in_rend_group_last_frame: None,
top_rend_group: RendGroup::default(),
check_set_top_rend_group: None,
mouse_pressed_outside: [false; 3],
modal: None,
time_millis: 0,
start_instant: Instant::now(),
keyboard_focus_widget: None,
errors: HashSet::new(),
frame_active: false,
};
Context {
internal: Rc::new(RefCell::new(internal))
}
}
// Finds the specified font and appropriately logs any error in this context.
pub(crate) fn find_font(&self, id: &str) -> Option<FontSummary> {
let mut internal = self.internal.borrow_mut();
match internal.themes().find_font(Some(id)) {
None => {
internal.log(log::Level::Error, format!("Unable to find font '{}' for widget", id));
None
}, Some(handle) => Some(handle)
}
}
// Finds the specified image and appropriately logs any error in this context.
pub(crate) fn find_image(&self, id: &str) -> Option<ImageHandle> {
let mut internal = self.internal.borrow_mut();
match internal.themes().find_image(Some(id)) {
None => {
internal.log(log::Level::Error, format!("Unable to find image '{}' for widget", id));
None
}, Some(handle) => Some(handle),
}
}
/// Returns true if thyme wants to use the mouse in the current frame, generally
/// because the mouse is over a Thyme widget. If this returns true, you probably
/// want Thyme to handle input this frame, while if it returns false, your application
/// or game logic should handle input.
pub fn wants_mouse(&self) -> bool {
let internal = self.internal.borrow();
internal.mouse_taken_last_frame.is_some() || internal.modal.is_some()
}
/// Returns true if thyme wants to use keyboard input in the current frame, generally
/// because a widget that accepts text input is keyboard focused. If this returns true,
/// you probably don't want to handle keyboard events in your own application code.
pub fn wants_keyboard(&self) -> bool {
let internal = self.internal.borrow();
internal.modal.is_some() || internal.keyboard_focus_widget.is_some()
}
/// Returns the amount of time, in milliseconds, that the mouse has been hovering
/// (inside) of the widget that it is currently inside. If `hovered` is true
/// in a [`WidgetState`](struct.WidgetState.html), then the mouse has been hovering
/// that widget for this amount of time.
pub fn mouse_time_in_current_widget(&self) -> u32 {
let internal = self.internal.borrow();
internal.time_millis - internal.mouse_taken_switch_time
}
pub(crate) fn internal(&self) -> &Rc<RefCell<ContextInternal>> {
&self.internal
}
/// Sets the scale factor, sometimes referred to as HiDPI factor for the monitor.
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// the scale factor based on a scale factor changed event. User code should
/// not need to call this.
pub fn set_scale_factor(&mut self, scale: f32) {
let mut internal = self.internal.borrow_mut();
internal.scale_factor = scale;
}
/// Returns the current scale factor being used internally by Thyme. See
/// [`set_scale_factor`](#method.set_scale_factor)
pub fn scale_factor(&self) -> f32 {
let internal = self.internal.borrow();
internal.scale_factor
}
/// Set the display size in logical pixels (physical pixels divided by the scale factor).
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// this in response to a window resize event. User code should
/// not need to call this.
pub fn set_display_size(&mut self, size: Point) {
let mut internal = self.internal.borrow_mut();
internal.display_size = size;
}
/// Returns the current display size being used internally by Thyme. See
/// [`set_display_size`](#method.set_display_size)
pub fn display_size(&self) -> Point {
let internal = self.internal.borrow();
internal.display_size
}
/// Add mouse wheel event, with `delta` being the amount of device-dependant logical scrolling.
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// this in response to a window resize event. User code should
/// not need to call this.
pub fn add_mouse_wheel(&mut self, delta: Point) {
let mut internal = self.internal.borrow_mut();
internal.mouse_wheel = internal.mouse_wheel + delta * internal.options.line_scroll;
}
/// Set the input modifiers - the status of keys such as `ctrl` and `shift`.
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// this in response to a window resize event. User code should
/// not need to call this.
pub fn set_input_modifiers(&mut self, input_modifiers: InputModifiers) {
let mut internal = self.internal.borrow_mut();
internal.input_modifiers = input_modifiers;
}
/// Set the mouse pressed state for a given mouse button.
/// # Inputs:
/// - button `pressed` state
/// - index: 0 = LeftClick, 1 = Right Click, 2 = Middle Click
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// this in response to a window resize event. User code should
/// not need to call this.
pub fn set_mouse_pressed(&mut self, pressed: bool, index: usize) {
let mut internal = self.internal.borrow_mut();
if index >= internal.mouse_pressed.len() {
return;
}
// don't take a mouse press that started outside the GUI elements
if pressed && internal.mouse_taken_last_frame.is_none() {
internal.mouse_pressed_outside[index] = true;
}
if !pressed && internal.mouse_pressed_outside[index] {
internal.mouse_pressed_outside[index] = false;
}
if internal.mouse_pressed[index] && !pressed {
internal.mouse_clicked[index] = true;
internal.keyboard_focus_widget = None;
}
internal.mouse_pressed[index] = pressed;
// do not allow tooltip to show when mouse is pressed
internal.mouse_taken_switch_position = None;
internal.mouse_taken_switch_time = internal.time_millis;
}
/// Pushes a character (that was received from the keyboard) to thyme, to be
/// dispatched to the appropriate widget based on keyboard focus in the next Frame.
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// this in response to a window resize event. User code should
/// not need to call this.
pub fn push_character(&mut self, c: char) {
let mut internal = self.internal.borrow_mut();
let id = match &internal.keyboard_focus_widget {
Some(id) => id.to_string(),
None => return,
};
let state = internal.state_mut(id);
state.characters.push(c);
}
/// Returns the current mouse position, based on mouse cursor movement. The scale
/// factor must be taken into account to convert physical pixels to the logical pixels
/// used by this. This may be useful is you want to get Thyme's last mouse position
/// outside of a Thyme frame for the rest of your application to use.
pub fn mouse_pos(&self) -> Point {
let internal = self.internal.borrow();
internal.mouse_pos
}
/// Set mouse position, based on mouse cursor movement. The scale factor must
/// be taken into account to convert physical pixels to the logical pixels used by
/// this.
/// This is normally handled by the [`IO`](trait.IO.html) backend, which will set
/// this in response to a window resize event. User code should
/// not need to call this.
pub fn set_mouse_pos(&mut self, pos: Point) {
let mut internal = self.internal.borrow_mut();
internal.mouse_pos = pos;
}
/// Adds the specified path as a source file for the resources being used
/// by the theme for this context. This will only work if the theme was
/// set up to read source data from files, i.e. using
/// [`ContextBuilder#register_theme_from_files`](struct.ContextBuilder.html#method.register_theme_from_files)
/// This does not rebuild the theme; you will
/// need to call [`rebuild_all`](#method.rebuild_all) for that.
pub fn add_theme_file<P: Into<PathBuf>>(&mut self, path: P) {
let path = path.into();
let mut internal = self.internal.borrow_mut();
internal.resources.add_theme_file(path);
}
/// Removes the theme source file with the specified path from the resources
/// being used by the theme for this context, if it is present. If it is not
/// present, does nothing. This does not rebuild the theme; you will
/// need to call [`rebuild_all`](#method.rebuild_all) for that.
pub fn remove_theme_file<P: Into<PathBuf>>(&mut self, path: P) {
let path: &Path = &path.into();
let mut internal = self.internal.borrow_mut();
internal.resources.remove_theme_file(path);
}
/// Rebuilds this context, reloading all asset data. Notably, files on disk
/// that were used in [`building`](struct.ContextBuilder.html) the context
/// are re-read. If any errors are encountered in reading or parsing files, this
/// will return `Err` and no changes are made to the context.
pub fn rebuild_all<R: Renderer>(&mut self, renderer: &mut R) -> Result<(), Error> {
let mut internal = self.internal.borrow_mut();
internal.resources.clear_data_cache();
internal.resources.cache_data()?;
let scale_factor = internal.scale_factor;
let themes = internal.resources.build_assets(renderer, scale_factor)?;
internal.themes = themes;
Ok(())
}
/// Checks the internal live reload thread to see if any file notifications have occurred
/// since the last check. If so, will fully rebuild the theme. If any errors are encountered
/// in the process of rebuilding the theme, will return the `Err` and no changes are made to
/// the current theme. Note that if you built the context with live reload disabled
/// (see [`BuildOptions`](struct.BuildOptions.html)), this function will do nothing.
pub fn check_live_reload<R: Renderer>(&mut self, renderer: &mut R) -> Result<(), Error> {
let mut internal = self.internal.borrow_mut();
let scale_factor = internal.scale_factor;
let themes = internal.resources.check_live_reload(renderer, scale_factor)?;
if let Some(themes) = themes {
internal.themes = themes;
}
Ok(())
}
/// Generates a [`SavedContext`](struct.SavedContext.html) from the current
/// context state. This can be serialized to a file and restored later using
/// [`load`](struct.Context.html#load) to restore the Context state.
pub fn save(&self) -> SavedContext {
let internal = self.internal.borrow();
SavedContext {
modal: internal.modal.clone(),
persistent_state: internal.persistent_state.clone(),
}
}
/// Restores the specified [`SavedContext`](struct.SavedContext.html) to this
/// Context, restoring the overall UI state. The [`SavedContext`](struct.SavedContext.html)
/// passed in should be generated from [`save`](struct.Context.html#save).
pub fn load(&mut self, save: SavedContext) {
let mut internal = self.internal.borrow_mut();
internal.modal = save.modal;
internal.persistent_state = save.persistent_state;
}
/// Creates a [`Frame`](struct.Frame.html), the main object that should pass through
/// your UI building functions and is responsible for constructing the widget tree.
/// This method should be called each frame you want to draw / interact with the UI.
pub fn create_frame(&mut self) -> Frame {
let now = Instant::now();
let anim_state;
let display_size = {
let mut context = self.internal.borrow_mut();
if context.frame_active {
panic!("A Thyme Frame is already active but a new one has been requested.");
}
context.frame_active = true;
let elapsed = (now - context.start_instant).as_millis() as u32;
context.time_millis = elapsed;
if context.mouse_pressed[0] {
anim_state = AnimState::new(AnimStateKey::Pressed);
} else {
anim_state = AnimState::normal();
}
context.display_size() / context.scale_factor()
};
let context = Context { internal: Rc::clone(&self.internal) };
let root = Widget::root(display_size);
Frame::new(context, root, anim_state)
}
}
#[derive(Deserialize, Serialize, Clone, Debug)]
pub(crate) struct Modal {
pub(crate) id: String,
pub(crate) close_on_click_outside: bool,
pub(crate) bounds: Rect,
pub(crate) prevent_close: bool,
}
impl Modal {
fn new(id: String) -> Modal {
Modal {
id,
close_on_click_outside: false,
bounds: Rect::default(),
prevent_close: true,
}
}
}
/**
* The serializable data associated with a [`Context`](struct.Context.html). Created
* using [`Context.save`](struct.Context.html#save).
*/
#[derive(Deserialize, Serialize, Default, Debug)]
pub struct SavedContext {
modal: Option<Modal>,
persistent_state: HashMap<String, PersistentState>,
}