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
|
use std::borrow::Cow;
use ecs::Component;
use crate::data_types::dimens::Dimens;
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 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, Component, Clone, Copy)]
pub struct CreationReady;
#[derive(Debug, Component)]
#[non_exhaustive]
pub struct Window
{
wid: Id,
pub title: Cow<'static, str>,
pub cursor_visible: bool,
pub cursor_grab_mode: CursorGrabMode,
inner_size: Dimens<u32>,
}
impl Window
{
pub fn wid(&self) -> Id
{
self.wid
}
pub fn inner_size(&self) -> &Dimens<u32>
{
&self.inner_size
}
pub(crate) fn new(
winit_window: &winit::window::Window,
creation_attrs: &CreationAttributes,
) -> Self
{
Self {
wid: Id::from_inner(winit_window.id()),
title: creation_attrs.title().to_string().into(),
cursor_visible: true,
cursor_grab_mode: CursorGrabMode::None,
inner_size: winit_window.inner_size().into(),
}
}
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;
}
}
#[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,
}
}
}
|