Skip to content

Commit

Permalink
On macOS, resize simple fullscreen on window move
Browse files Browse the repository at this point in the history
Fixes #1118.
  • Loading branch information
dylanscott authored and kchibisov committed Mar 2, 2023
1 parent 339d57b commit 68ed564
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 macOS, fix wrong focused state on startup.
- On Windows, fix crash on setting taskbar when using Visual Studio debugger.
- On macOS, resize simple fullscreen windows on windowDidChangeScreen events.

# 0.28.1

Expand Down
9 changes: 9 additions & 0 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEve
use winit::event_loop::EventLoop;
use winit::window::{Fullscreen, WindowBuilder};

#[cfg(target_os = "macos")]
use winit::platform::macos::WindowExtMacOS;

fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
Expand Down Expand Up @@ -32,6 +35,8 @@ fn main() {
println!("- Esc\tExit");
println!("- F\tToggle exclusive fullscreen mode");
println!("- B\tToggle borderless mode");
#[cfg(target_os = "macos")]
println!("- C\tToggle simple fullscreen mode");
println!("- S\tNext screen");
println!("- M\tNext mode for this screen");
println!("- D\tToggle window decorations");
Expand Down Expand Up @@ -67,6 +72,10 @@ fn main() {
println!("Setting mode: {fullscreen:?}");
window.set_fullscreen(fullscreen);
}
#[cfg(target_os = "macos")]
VirtualKeyCode::C => {
window.set_simple_fullscreen(!window.simple_fullscreen());
}
VirtualKeyCode::S => {
monitor_index += 1;
if let Some(mon) = elwt.available_monitors().nth(monitor_index) {
Expand Down
16 changes: 13 additions & 3 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub struct SharedState {
pub(crate) target_fullscreen: Option<Option<Fullscreen>>,
pub maximized: bool,
pub standard_frame: Option<NSRect>,
is_simple_fullscreen: bool,
pub(crate) is_simple_fullscreen: bool,
pub saved_style: Option<NSWindowStyleMask>,
/// Presentation options saved before entering `set_simple_fullscreen`, and
/// restored upon exiting it. Also used when transitioning from Borderless to
Expand Down Expand Up @@ -1317,6 +1317,10 @@ impl WindowExtMacOS for WinitWindow {
// Tell our window's state that we're in fullscreen
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
let presentation_options =
NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock
Expand All @@ -1341,11 +1345,17 @@ impl WindowExtMacOS for WinitWindow {
self.set_style_mask_sync(new_mask);
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);
}

let frame = shared_state_lock.saved_standard_frame();
self.setFrame_display(frame, true);
self.setMovable(true);

Expand Down
14 changes: 14 additions & 0 deletions src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ declare_class!(
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);
}
}
}
}
);

Expand Down

0 comments on commit 68ed564

Please sign in to comment.