On macOS, resize simple fullscreen on window move

Fixes #1118.
This commit is contained in:
Dylan Scott 2023-03-01 14:47:45 -08:00 committed by GitHub
parent b870a11a99
commit 79ac236721
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 3 deletions

View file

@ -15,6 +15,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Wayland, fix rounding issues when doing resize. - On Wayland, fix rounding issues when doing resize.
- On macOS, fix wrong focused state on startup. - On macOS, fix wrong focused state on startup.
- On Windows, fix crash on setting taskbar when using Visual Studio debugger. - On Windows, fix crash on setting taskbar when using Visual Studio debugger.
- On macOS, resize simple fullscreen windows on windowDidChangeScreen events.
# 0.28.1 # 0.28.1

View file

@ -5,6 +5,9 @@ use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEve
use winit::event_loop::EventLoop; use winit::event_loop::EventLoop;
use winit::window::{Fullscreen, WindowBuilder}; use winit::window::{Fullscreen, WindowBuilder};
#[cfg(target_os = "macos")]
use winit::platform::macos::WindowExtMacOS;
fn main() { fn main() {
SimpleLogger::new().init().unwrap(); SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
@ -32,6 +35,8 @@ fn main() {
println!("- Esc\tExit"); println!("- Esc\tExit");
println!("- F\tToggle exclusive fullscreen mode"); println!("- F\tToggle exclusive fullscreen mode");
println!("- B\tToggle borderless mode"); println!("- B\tToggle borderless mode");
#[cfg(target_os = "macos")]
println!("- C\tToggle simple fullscreen mode");
println!("- S\tNext screen"); println!("- S\tNext screen");
println!("- M\tNext mode for this screen"); println!("- M\tNext mode for this screen");
println!("- D\tToggle window decorations"); println!("- D\tToggle window decorations");
@ -67,6 +72,10 @@ fn main() {
println!("Setting mode: {fullscreen:?}"); println!("Setting mode: {fullscreen:?}");
window.set_fullscreen(fullscreen); window.set_fullscreen(fullscreen);
} }
#[cfg(target_os = "macos")]
VirtualKeyCode::C => {
window.set_simple_fullscreen(!window.simple_fullscreen());
}
VirtualKeyCode::S => { VirtualKeyCode::S => {
monitor_index += 1; monitor_index += 1;
if let Some(mon) = elwt.available_monitors().nth(monitor_index) { if let Some(mon) = elwt.available_monitors().nth(monitor_index) {

View file

@ -150,7 +150,7 @@ pub struct SharedState {
pub(crate) target_fullscreen: Option<Option<Fullscreen>>, pub(crate) target_fullscreen: Option<Option<Fullscreen>>,
pub maximized: bool, pub maximized: bool,
pub standard_frame: Option<NSRect>, pub standard_frame: Option<NSRect>,
is_simple_fullscreen: bool, pub(crate) is_simple_fullscreen: bool,
pub saved_style: Option<NSWindowStyleMask>, pub saved_style: Option<NSWindowStyleMask>,
/// Presentation options saved before entering `set_simple_fullscreen`, and /// Presentation options saved before entering `set_simple_fullscreen`, and
/// restored upon exiting it. Also used when transitioning from Borderless to /// restored upon exiting it. Also used when transitioning from Borderless to
@ -1317,6 +1317,10 @@ impl WindowExtMacOS for WinitWindow {
// Tell our window's state that we're in fullscreen // Tell our window's state that we're in fullscreen
shared_state_lock.is_simple_fullscreen = true; shared_state_lock.is_simple_fullscreen = true;
// Drop shared state lock before calling app.setPresentationOptions, because
// it will call our windowDidChangeScreen listener which reacquires the lock
drop(shared_state_lock);
// Simulate pre-Lion fullscreen by hiding the dock and menu bar // Simulate pre-Lion fullscreen by hiding the dock and menu bar
let presentation_options = let presentation_options =
NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock
@ -1341,11 +1345,17 @@ impl WindowExtMacOS for WinitWindow {
self.set_style_mask_sync(new_mask); self.set_style_mask_sync(new_mask);
shared_state_lock.is_simple_fullscreen = false; shared_state_lock.is_simple_fullscreen = false;
if let Some(presentation_opts) = shared_state_lock.save_presentation_opts { let save_presentation_opts = shared_state_lock.save_presentation_opts;
let frame = shared_state_lock.saved_standard_frame();
// Drop shared state lock before calling app.setPresentationOptions, because
// it will call our windowDidChangeScreen listener which reacquires the lock
drop(shared_state_lock);
if let Some(presentation_opts) = save_presentation_opts {
app.setPresentationOptions(presentation_opts); app.setPresentationOptions(presentation_opts);
} }
let frame = shared_state_lock.saved_standard_frame();
self.setFrame_display(frame, true); self.setFrame_display(frame, true);
self.setMovable(true); self.setMovable(true);

View file

@ -409,6 +409,20 @@ declare_class!(
self.queue_event(WindowEvent::ThemeChanged(theme)); self.queue_event(WindowEvent::ThemeChanged(theme));
} }
} }
#[sel(windowDidChangeScreen:)]
fn window_did_change_screen(&self, _: Option<&Object>) {
trace_scope!("windowDidChangeScreen:");
let is_simple_fullscreen = self
.window
.lock_shared_state("window_did_change_screen")
.is_simple_fullscreen;
if is_simple_fullscreen {
if let Some(screen) = self.window.screen() {
self.window.setFrame_display(screen.frame(), true);
}
}
}
} }
); );