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
use egui::{Id, Pos2, Rect, Vec2};
/// The state of a [`Surface::Window`](crate::Surface::Window).
///
/// Doubles as a handle for the surface, allowing the user to set its size and position.
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(default)
)]
pub struct WindowState {
/// The [`Rect`] that this window was last taking up.
screen_rect: Option<Rect>,
/// Was this window dragged in the last frame?
dragged: bool,
/// The next position this window should be set to next frame.
next_position: Option<Pos2>,
/// The next size this window should be set to next frame.
next_size: Option<Vec2>,
/// The height of the window before it was fully collapsed
expanded_height: Option<f32>,
/// True the first frame this window is drawn.
/// handles expanding after being fully collapsed, etc.
new: bool,
/// True if the window is minimized
minimized: bool,
}
impl Default for WindowState {
fn default() -> Self {
Self {
screen_rect: None,
dragged: false,
next_position: None,
next_size: None,
expanded_height: None,
new: true,
minimized: false,
}
}
}
impl WindowState {
/// Create a default window state.
pub(crate) fn new() -> Self {
Self::default()
}
/// Set the position for this window in screen coordinates.
pub fn set_position(&mut self, position: Pos2) -> &mut Self {
self.next_position = Some(position);
self
}
/// Set the size of this window in egui points.
pub fn set_size(&mut self, size: Vec2) -> &mut Self {
self.next_size = Some(size);
self
}
/// Get the [`Rect`] which this window occupied when last shown, in screen coordinates.
/// If this window hasn't been shown before, this will be [`Rect::NOTHING`].
pub fn rect(&self) -> Rect {
// The reason why we're unwrapping an Option with a default value instead of
// just storing Rect::NOTHING for the None variant is that deserializing Rect::NOTHING
// with serde_json causes a panic, because f32::INFINITY serializes into null in JSON.
self.screen_rect.unwrap_or(Rect::NOTHING)
}
/// Returns whether this window was being dragged when last shown.
pub fn dragged(&self) -> bool {
self.dragged
}
/// Records where the window was shown this frame and whether it is dragged.
#[inline(always)]
pub(crate) fn set_shown(&mut self, rect: Rect, dragged: bool) {
self.screen_rect = Some(rect);
self.dragged = dragged;
}
/// Set the height of this window when it is expanded.
#[inline(always)]
pub(crate) fn set_expanded_height(&mut self, height: f32) -> &mut Self {
self.expanded_height = Some(height);
self
}
#[inline(always)]
pub(crate) fn set_new(&mut self, new: bool) -> &mut Self {
self.new = new;
self
}
#[inline(always)]
pub(crate) fn next_position(&mut self) -> Option<Pos2> {
self.next_position.take()
}
#[inline(always)]
pub(crate) fn next_size(&mut self) -> Option<Vec2> {
self.next_size.take()
}
#[inline(always)]
pub(crate) fn expanded_height(&mut self) -> Option<f32> {
self.expanded_height.take()
}
#[inline(always)]
pub(crate) fn toggle_minimized(&mut self) {
self.minimized = !self.minimized;
}
#[inline(always)]
pub(crate) fn is_minimized(&self) -> bool {
self.minimized
}
/// Builds the window, and returns with it the outer height it gets back this frame, if it
/// is unfolded or restored: the caller sets it as both its least and largest height after
/// its own size constraints, which would otherwise override it.
///
/// The `'static` means the `open` field is always `None`. `chrome` is the height the
/// window's frame adds to its content: egui sizes a window by its outer size, and the
/// expanded height kept is the content's.
pub(crate) fn create_window(
&mut self,
id: Id,
bounds: Rect,
chrome: f32,
) -> (egui::Window<'static>, Option<f32>) {
let new = self.new;
let mut window_constructor = egui::Window::new("")
.id(id)
.constrain_to(bounds)
.title_bar(false);
if let Some(position) = self.next_position() {
window_constructor = window_constructor.current_pos(position);
}
if let Some(size) = self.next_size() {
window_constructor = window_constructor.fixed_size(size);
}
// The height the window had before it was folded or minimized, now it is open again.
let restored_height = if new {
self.expanded_height().map(|height| height + chrome)
} else {
None
};
self.new = false;
(window_constructor, restored_height)
}
}