From be6d2ed3b9f94c9189aaae6acf57f1a45400bdaa Mon Sep 17 00:00:00 2001 From: Joe Moon Date: Mon, 2 Apr 2018 16:12:38 -0700 Subject: [PATCH] subclass windows in macos so they can be made resizable even with no decorations (#408) * make windows without decorations resizable and movable in macos fixes #368 The subclassing logic was copied from servo's fork of glutin: https://github.com/servo/glutin/blob/63026a0f4c8ba8bdc40787f8305ab008e5eb45e3/src/api/cocoa/mod.rs#L418 * remove `isMovableByWindowBackground` and `mouseDownCanMoveWindow` * revert example changes * remove resizable mask from decoration: false * avoid duplicate class declarations * update changelog * fix changelog * changelog whitespace --- CHANGELOG.md | 2 ++ src/platform/macos/window.rs | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaaca49c..d2bc6e40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- Added subclass to macos windows so they can be made resizable even with no decorations. + # Version 0.11.3 (2018-03-28) - Added `set_min_dimensions` and `set_max_dimensions` methods to `Window`, and implemented on Windows, X11, Wayland, and OSX. diff --git a/src/platform/macos/window.rs b/src/platform/macos/window.rs index c2476744..5937dee1 100644 --- a/src/platform/macos/window.rs +++ b/src/platform/macos/window.rs @@ -414,10 +414,7 @@ impl Window2 { NSWindowStyleMask::NSTitledWindowMask } else if !attrs.decorations { // Window2 without a titlebar - NSWindowStyleMask::NSClosableWindowMask | - NSWindowStyleMask::NSMiniaturizableWindowMask | - NSWindowStyleMask::NSResizableWindowMask | - NSWindowStyleMask::NSFullSizeContentViewWindowMask + NSWindowStyleMask::NSBorderlessWindowMask } else if pl_attrs.titlebar_hidden { NSWindowStyleMask::NSBorderlessWindowMask | NSWindowStyleMask::NSResizableWindowMask @@ -442,7 +439,18 @@ impl Window2 { NSWindowStyleMask::NSTitledWindowMask }; - let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_( + let winit_window = Class::get("WinitWindow").unwrap_or_else(|| { + let window_superclass = Class::get("NSWindow").unwrap(); + let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap(); + decl.add_method(sel!(canBecomeMainWindow), yes as extern fn(&Object, Sel) -> BOOL); + decl.add_method(sel!(canBecomeKeyWindow), yes as extern fn(&Object, Sel) -> BOOL); + decl.register(); + Class::get("WinitWindow").unwrap() + }); + + let window: id = msg_send![winit_window, alloc]; + + let window = IdRef::new(window.initWithContentRect_styleMask_backing_defer_( frame, masks, appkit::NSBackingStoreBuffered, @@ -796,3 +804,7 @@ impl Clone for IdRef { IdRef(self.0) } } + +extern fn yes(_: &Object, _: Sel) -> BOOL { + YES +}