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
use std::ffi::{CStr, CString};
use std::mem::MaybeUninit;
use crate::geo::{Point, Size};
use crate::{bind, Result, SdlError};
use super::{Window, WindowCoord};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Position {
pub x: WindowCoord,
pub y: WindowCoord,
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Opacity {
opacity: f32,
}
impl Opacity {
#[must_use]
pub fn new(opacity: f32) -> Option<Self> {
if (0.0..=1.0).contains(&opacity) {
return None;
}
Some(Self { opacity })
}
#[must_use]
pub fn with_clamped(opacity: f32) -> Self {
Self {
opacity: opacity.clamp(0.0, 1.0),
}
}
#[must_use]
pub fn as_f32(&self) -> f32 {
self.opacity
}
}
pub trait ConfigExt {
fn max_size(&self) -> Size;
fn min_size(&self) -> Size;
fn size(&self) -> Size;
fn opacity(&self) -> Opacity;
fn pos(&self) -> Point;
fn title(&self) -> &str;
fn set_max_size(&self, max_size: Size);
fn set_min_size(&self, min_size: Size);
fn set_size(&self, size: Size);
fn set_opacity(&self, opacity: Opacity) -> Result<()>;
fn set_pos(&self, pos: Position);
fn set_title(&self, title: &str);
fn set_resizable(&self, resizable: bool);
fn add_frame(&self);
fn remove_frame(&self);
}
impl ConfigExt for Window<'_> {
fn max_size(&self) -> Size {
let (mut width, mut height) = (MaybeUninit::uninit(), MaybeUninit::uninit());
unsafe {
bind::SDL_GetWindowMaximumSize(self.as_ptr(), width.as_mut_ptr(), height.as_mut_ptr());
}
Size {
width: unsafe { width.assume_init() } as _,
height: unsafe { height.assume_init() } as _,
}
}
fn min_size(&self) -> Size {
let (mut width, mut height) = (MaybeUninit::uninit(), MaybeUninit::uninit());
unsafe {
bind::SDL_GetWindowMinimumSize(self.as_ptr(), width.as_mut_ptr(), height.as_mut_ptr());
}
Size {
width: unsafe { width.assume_init() } as _,
height: unsafe { height.assume_init() } as _,
}
}
fn size(&self) -> Size {
let (mut width, mut height) = (MaybeUninit::uninit(), MaybeUninit::uninit());
unsafe { bind::SDL_GetWindowSize(self.as_ptr(), width.as_mut_ptr(), height.as_mut_ptr()) }
Size {
width: unsafe { width.assume_init() } as _,
height: unsafe { height.assume_init() } as _,
}
}
fn opacity(&self) -> Opacity {
let mut opacity = MaybeUninit::uninit();
let ret = unsafe { bind::SDL_GetWindowOpacity(self.as_ptr(), opacity.as_mut_ptr()) };
let opacity = if ret == 0 {
unsafe { opacity.assume_init() }
} else {
1.0
};
Opacity { opacity }
}
fn pos(&self) -> Point {
let mut point = Point::default();
unsafe { bind::SDL_GetWindowPosition(self.as_ptr(), &mut point.x, &mut point.y) }
point
}
fn title(&self) -> &str {
let raw = unsafe { bind::SDL_GetWindowTitle(self.as_ptr()) };
unsafe { CStr::from_ptr(raw) }
.to_str()
.expect("Getting title failed")
}
fn set_max_size(&self, Size { width, height }: Size) {
unsafe { bind::SDL_SetWindowMaximumSize(self.as_ptr(), width as _, height as _) }
}
fn set_min_size(&self, Size { width, height }: Size) {
unsafe { bind::SDL_SetWindowMinimumSize(self.as_ptr(), width as _, height as _) }
}
fn set_size(&self, Size { width, height }: Size) {
unsafe { bind::SDL_SetWindowSize(self.as_ptr(), width as _, height as _) }
}
fn set_opacity(&self, opacity: Opacity) -> Result<()> {
let ret = unsafe { bind::SDL_SetWindowOpacity(self.as_ptr(), opacity.opacity) };
if ret != 0 {
return Err(SdlError::UnsupportedFeature);
}
Ok(())
}
fn set_pos(&self, Position { x, y }: Position) {
unsafe { bind::SDL_SetWindowPosition(self.as_ptr(), x.into_arg(), y.into_arg()) }
}
fn set_title(&self, title: &str) {
let cstr = CString::new(title).unwrap_or_default();
unsafe { bind::SDL_SetWindowTitle(self.as_ptr(), cstr.as_ptr()) }
}
fn set_resizable(&self, resizable: bool) {
unsafe { bind::SDL_SetWindowResizable(self.as_ptr(), if resizable { 1 } else { 0 }) }
}
fn add_frame(&self) {
unsafe { bind::SDL_SetWindowBordered(self.as_ptr(), 1) }
}
fn remove_frame(&self) {
unsafe { bind::SDL_SetWindowBordered(self.as_ptr(), 0) }
}
}