better fullscreen toggle and minor cleanup

macos-click-through
Keith Simmons 5 years ago
parent 3da11e09d8
commit 91c29c5898

@ -55,7 +55,7 @@ struct WindowWrapper {
previous_dpis: (f32, f32), previous_dpis: (f32, f32),
transparency: f32, transparency: f32,
fullscreen: bool, fullscreen: bool,
cached_size: (i32, i32), cached_size: (u32, u32),
cached_position: (i32, i32), cached_position: (i32, i32),
} }
@ -176,46 +176,44 @@ impl WindowWrapper {
} }
pub fn toggle_fullscreen(&mut self) { pub fn toggle_fullscreen(&mut self) {
unsafe {
let raw_handle = self.window.raw(); let raw_handle = self.window.raw();
let display_index = sdl2::sys::SDL_GetWindowDisplayIndex(raw_handle);
if let Ok(rect) = self.window.subsystem().display_bounds(display_index) {
if self.fullscreen { if self.fullscreen {
if cfg!(not(target_os = "macos")) {
unsafe {
// Set window back to resizable // Set window back to resizable
sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_TRUE); sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_TRUE);
}
}
// Use cached size and position // Use cached size and position
self.window self.window
.set_size(self.cached_size.0 as u32, self.cached_size.1 as u32) .set_size(self.cached_size.0, self.cached_size.1)
.unwrap(); .unwrap();
self.window.set_position( self.window.set_position(
sdl2::video::WindowPos::Positioned(self.cached_position.0), sdl2::video::WindowPos::Positioned(self.cached_position.0),
sdl2::video::WindowPos::Positioned(self.cached_position.1), sdl2::video::WindowPos::Positioned(self.cached_position.1),
); );
self.window.set_bordered(true);
} else { } else {
// Cache the size and position self.cached_size = self.window.size();
sdl2::sys::SDL_GetWindowSize( self.cached_position = self.window.position();
raw_handle,
&mut self.cached_size.0,
&mut self.cached_size.1,
);
sdl2::sys::SDL_GetWindowPosition(
raw_handle,
&mut self.cached_position.0,
&mut self.cached_position.1,
);
sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_FALSE);
if cfg!(not(target_os = "macos")) {
let raw_handle = self.window.raw();
let display_index = self.window.display_index().unwrap();
if let Ok(rect) = self.window.subsystem().display_bounds(display_index) {
unsafe {
sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_FALSE);
}
// Set window to fullscreen // Set window to fullscreen
self.window.set_size(rect.width(), rect.height()).unwrap(); self.window.set_size(rect.width(), rect.height()).unwrap();
self.window.set_position( self.window.set_position(
sdl2::video::WindowPos::Positioned(rect.x()), sdl2::video::WindowPos::Positioned(rect.x()),
sdl2::video::WindowPos::Positioned(rect.y()), sdl2::video::WindowPos::Positioned(rect.y()),
); );
self.window.set_bordered(true);
} }
} else {
self.window.maximize();
} }
} }
self.fullscreen = !self.fullscreen; self.fullscreen = !self.fullscreen;

Loading…
Cancel
Save