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
|
use std::borrow::Cow;
use crate::data_types::dimens::Dimens;
use crate::ecs::Component;
pub mod platform;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id
{
inner: winit::window::WindowId,
}
impl Id
{
pub(crate) fn from_inner(inner: winit::window::WindowId) -> Self
{
Self { inner }
}
}
macro_rules! impl_creation_attributes_field_fns {
($field: ident, ($($getter_ret_pre: tt)?), $getter_ret_type: ty, $field_type: ty) => {
impl CreationAttributes
{
pub fn $field(&self) -> $getter_ret_type
{
$($getter_ret_pre)? self.attrs.$field
}
paste::paste! {
pub fn [<with_ $field>](mut self, $field: impl Into<$field_type>) -> Self {
self.attrs.$field = $field.into();
self
}
}
}
};
}
#[derive(Debug, Component, Clone)]
#[non_exhaustive]
pub struct CreationAttributes
{
attrs: winit::window::WindowAttributes,
}
impl_creation_attributes_field_fns!(title, (&), &str, String);
impl_creation_attributes_field_fns!(transparent, (), bool, bool);
impl_creation_attributes_field_fns!(maximized, (), bool, bool);
impl CreationAttributes
{
pub fn fullscreen(&self) -> Option<Fullscreen>
{
match self.attrs.fullscreen.as_ref()? {
winit::window::Fullscreen::Borderless(_) => Some(Fullscreen::Borderless),
winit::window::Fullscreen::Exclusive(_) => unreachable!(),
}
}
pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self
{
self.attrs.fullscreen = fullscreen.map(|fullscreen| match fullscreen {
Fullscreen::Borderless => winit::window::Fullscreen::Borderless(None),
});
self
}
}
impl CreationAttributes
{
#[cfg(target_os = "linux")]
pub fn with_x11_visual(mut self, visual_id: XVisualID) -> Self
{
use winit::platform::x11::WindowAttributesExtX11;
self.attrs = self.attrs.with_x11_visual(visual_id);
self
}
}
impl CreationAttributes
{
pub(crate) fn into_inner(self) -> winit::window::WindowAttributes
{
self.attrs
}
}
impl Default for CreationAttributes
{
fn default() -> Self
{
CreationAttributes {
attrs: winit::window::WindowAttributes::default().with_title("Application"),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Fullscreen
{
Borderless,
}
#[derive(Debug, Component, Clone, Copy)]
pub struct CreationReady;
#[derive(Debug, Component)]
#[non_exhaustive]
pub struct Window
{
pub title: Cow<'static, str>,
pub cursor_visible: bool,
pub cursor_grab_mode: CursorGrabMode,
wid: Id,
inner_size: Dimens<u32>,
scale_factor: f64,
}
impl Window
{
pub fn wid(&self) -> Id
{
self.wid
}
pub fn inner_size(&self) -> &Dimens<u32>
{
&self.inner_size
}
pub fn scale_factor(&self) -> f64
{
self.scale_factor
}
pub(crate) fn new(
winit_window: &winit::window::Window,
creation_attrs: &CreationAttributes,
) -> Self
{
Self {
title: creation_attrs.title().to_string().into(),
cursor_visible: true,
cursor_grab_mode: CursorGrabMode::None,
wid: Id::from_inner(winit_window.id()),
inner_size: winit_window.inner_size().into(),
scale_factor: winit_window.scale_factor(),
}
}
pub(crate) fn apply(&self, winit_window: &winit::window::Window)
{
winit_window.set_title(&self.title);
winit_window.set_cursor_visible(self.cursor_visible);
}
pub(crate) fn set_inner_size(&mut self, inner_size: Dimens<u32>)
{
self.inner_size = inner_size;
}
pub(crate) fn set_scale_factor(&mut self, scale_factor: f64)
{
self.scale_factor = scale_factor;
}
}
#[derive(Debug, Component)]
pub struct Closed;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CursorGrabMode
{
#[default]
None,
/// The cursor is locked to a specific position in the window.
Locked,
}
/// A unique identifier for an X11 visual.
pub type XVisualID = u32;
impl<P> From<winit::dpi::PhysicalSize<P>> for Dimens<P>
{
fn from(size: winit::dpi::PhysicalSize<P>) -> Self
{
Self {
width: size.width,
height: size.height,
}
}
}
impl<P> From<Dimens<P>> for winit::dpi::PhysicalSize<P>
{
fn from(dimens: Dimens<P>) -> Self
{
Self {
width: dimens.width,
height: dimens.height,
}
}
}
|