Skip to content

Commit

Permalink
subclass windows in macos so they can be made resizable even with no …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sodiumjoe authored and francesca64 committed Apr 2, 2018
1 parent 0b922ad commit be6d2ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
22 changes: 17 additions & 5 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -796,3 +804,7 @@ impl Clone for IdRef {
IdRef(self.0)
}
}

extern fn yes(_: &Object, _: Sel) -> BOOL {
YES
}

0 comments on commit be6d2ed

Please sign in to comment.