Merge pull request #256 from Kethku/macos-fullscreen-fix

better fullscreen toggle and minor cleanup
macos-click-through
Keith Simmons 5 years ago committed by GitHub
commit ce63064b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,7 +6,7 @@ use log::{debug, error, info, trace};
use skulpin::sdl2; use skulpin::sdl2;
use skulpin::sdl2::event::{Event, WindowEvent}; use skulpin::sdl2::event::{Event, WindowEvent};
use skulpin::sdl2::keyboard::Keycode; use skulpin::sdl2::keyboard::Keycode;
use skulpin::sdl2::video::Window; use skulpin::sdl2::video::{FullscreenType, Window};
use skulpin::sdl2::Sdl; use skulpin::sdl2::Sdl;
use skulpin::{dpis, CoordinateSystem, PresentMode, Renderer as SkulpinRenderer, RendererBuilder}; use skulpin::{dpis, CoordinateSystem, PresentMode, Renderer as SkulpinRenderer, RendererBuilder};
use skulpin::{LogicalSize, PhysicalSize}; use skulpin::{LogicalSize, PhysicalSize};
@ -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),
} }
@ -177,46 +177,51 @@ impl WindowWrapper {
} }
pub fn toggle_fullscreen(&mut self) { pub fn toggle_fullscreen(&mut self) {
if self.fullscreen {
if cfg!(target_os = "windows") {
unsafe { 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 {
// 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);
}
} else {
self.window.set_fullscreen(FullscreenType::Off).ok();
}
// 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, if cfg!(target_os = "windows") {
&mut self.cached_size.1, let video_subsystem = self.window.subsystem();
); if let Ok(rect) = self
sdl2::sys::SDL_GetWindowPosition( .window
.display_index()
.and_then(|index| video_subsystem.display_bounds(index))
{
// Set window to fullscreen
unsafe {
let raw_handle = self.window.raw();
sdl2::sys::SDL_SetWindowResizable(
raw_handle, raw_handle,
&mut self.cached_position.0, sdl2::sys::SDL_bool::SDL_FALSE,
&mut self.cached_position.1,
); );
sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_FALSE); }
// 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.set_fullscreen(FullscreenType::Desktop).ok();
} }
} }

Loading…
Cancel
Save