From 91c29c5898c0c25e6e76f7fd999b2a3cd44d7c9c Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 20 Apr 2020 12:29:08 -0700 Subject: [PATCH 1/4] better fullscreen toggle and minor cleanup --- src/window.rs | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/window.rs b/src/window.rs index a69becb..60c5dbf 100644 --- a/src/window.rs +++ b/src/window.rs @@ -55,7 +55,7 @@ struct WindowWrapper { previous_dpis: (f32, f32), transparency: f32, fullscreen: bool, - cached_size: (i32, i32), + cached_size: (u32, u32), cached_position: (i32, i32), } @@ -176,46 +176,44 @@ impl WindowWrapper { } pub fn toggle_fullscreen(&mut self) { - unsafe { - let raw_handle = self.window.raw(); - let display_index = sdl2::sys::SDL_GetWindowDisplayIndex(raw_handle); + let raw_handle = self.window.raw(); - 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 sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_TRUE); + } + } - // Use cached size and position - self.window - .set_size(self.cached_size.0 as u32, self.cached_size.1 as u32) - .unwrap(); - self.window.set_position( - sdl2::video::WindowPos::Positioned(self.cached_position.0), - sdl2::video::WindowPos::Positioned(self.cached_position.1), - ); - self.window.set_bordered(true); - } else { - // Cache the size and position - sdl2::sys::SDL_GetWindowSize( - 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); - + // Use cached size and position + self.window + .set_size(self.cached_size.0, self.cached_size.1) + .unwrap(); + self.window.set_position( + sdl2::video::WindowPos::Positioned(self.cached_position.0), + sdl2::video::WindowPos::Positioned(self.cached_position.1), + ); + } else { + self.cached_size = self.window.size(); + self.cached_position = self.window.position(); + + 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 self.window.set_size(rect.width(), rect.height()).unwrap(); self.window.set_position( sdl2::video::WindowPos::Positioned(rect.x()), sdl2::video::WindowPos::Positioned(rect.y()), ); - self.window.set_bordered(true); } + } else { + self.window.maximize(); } } self.fullscreen = !self.fullscreen; From 65ab4987d727891808eb572b94431dbd050b41d2 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 20 Apr 2020 12:39:27 -0700 Subject: [PATCH 2/4] remove unwrap --- src/window.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/window.rs b/src/window.rs index 60c5dbf..0bebb38 100644 --- a/src/window.rs +++ b/src/window.rs @@ -176,12 +176,10 @@ impl WindowWrapper { } pub fn toggle_fullscreen(&mut self) { - let raw_handle = self.window.raw(); - if self.fullscreen { if cfg!(not(target_os = "macos")) { unsafe { - // Set window back to resizable + let raw_handle = self.window.raw(); sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_TRUE); } } @@ -199,13 +197,20 @@ impl WindowWrapper { self.cached_position = self.window.position(); 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) { + let video_subsystem = self.window.subsystem(); + if let Ok(rect) = self + .window + .display_index() + .and_then(|index| video_subsystem.display_bounds(index)) + { + // Set window to fullscreen unsafe { - sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_FALSE); + let raw_handle = self.window.raw(); + 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_position( sdl2::video::WindowPos::Positioned(rect.x()), From 1c06f6140820b903bca8972a619bba0d3e9a9be3 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 20 Apr 2020 13:18:25 -0700 Subject: [PATCH 3/4] more tweeks to get fullscreen working --- src/window.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/window.rs b/src/window.rs index 0bebb38..db68035 100644 --- a/src/window.rs +++ b/src/window.rs @@ -6,7 +6,7 @@ use log::{debug, error, info, trace}; use skulpin::sdl2; use skulpin::sdl2::event::{Event, WindowEvent}; use skulpin::sdl2::keyboard::Keycode; -use skulpin::sdl2::video::Window; +use skulpin::sdl2::video::{FullscreenType, Window}; use skulpin::sdl2::Sdl; use skulpin::{dpis, CoordinateSystem, PresentMode, Renderer as SkulpinRenderer, RendererBuilder}; use skulpin::{LogicalSize, PhysicalSize}; @@ -177,11 +177,13 @@ impl WindowWrapper { pub fn toggle_fullscreen(&mut self) { if self.fullscreen { - if cfg!(not(target_os = "macos")) { + if cfg!(target_os = "windows") { unsafe { let raw_handle = self.window.raw(); 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 @@ -196,20 +198,13 @@ impl WindowWrapper { self.cached_size = self.window.size(); self.cached_position = self.window.position(); - if cfg!(not(target_os = "macos")) { + if cfg!(target_os = "windows") { let video_subsystem = self.window.subsystem(); - if let Ok(rect) = self - .window - .display_index() - .and_then(|index| video_subsystem.display_bounds(index)) - { + if let Ok(rect) = self.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, - sdl2::sys::SDL_bool::SDL_FALSE, - ); + sdl2::sys::SDL_SetWindowResizable(raw_handle, sdl2::sys::SDL_bool::SDL_FALSE); } self.window.set_size(rect.width(), rect.height()).unwrap(); self.window.set_position( @@ -218,7 +213,7 @@ impl WindowWrapper { ); } } else { - self.window.maximize(); + self.window.set_fullscreen(FullscreenType::Desktop).ok(); } } self.fullscreen = !self.fullscreen; From f77da92f5fe6598d41ce2c6ed2beb0d45272d9a4 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 20 Apr 2020 13:22:05 -0700 Subject: [PATCH 4/4] ft --- src/window.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/window.rs b/src/window.rs index db68035..ba62d8a 100644 --- a/src/window.rs +++ b/src/window.rs @@ -200,11 +200,18 @@ impl WindowWrapper { if cfg!(target_os = "windows") { let video_subsystem = self.window.subsystem(); - if let Ok(rect) = self.window.display_index().and_then(|index| video_subsystem.display_bounds(index)) { + if let Ok(rect) = self + .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, sdl2::sys::SDL_bool::SDL_FALSE); + sdl2::sys::SDL_SetWindowResizable( + raw_handle, + sdl2::sys::SDL_bool::SDL_FALSE, + ); } self.window.set_size(rect.width(), rect.height()).unwrap(); self.window.set_position(