servo / cocoa-rs

DEPRECATED - Cocoa/Objective-C bindings for the Rust programming language
https://github.com/servo/core-foundation-rs/
Other
286 stars 72 forks source link

Elegant combining of multiple NSWindowStyleMasks #159

Closed oluseyi closed 7 years ago

oluseyi commented 7 years ago

Getting started with cocoa-rs as an experienced Objective-C Cocoa developer, I tried to modify the example at https://github.com/servo/cocoa-rs/blob/master/examples/hello_world.rs with multiple window style masks, converting the combined value to NSUInteger:

// create Window
let windowStyleMask = NSTitledWindowMask|
                        NSClosableWindowMask|
                        NSMiniaturizableWindowMask|
                        NSResizableWindowMask|
                        NSUnifiedTitleAndToolbarWindowMask|
                        NSFullSizeContentViewWindowMask as NSUInteger;

let window = NSWindow::alloc(nil)
    .initWithContentRect_styleMask_backing_defer_(NSRect::new(NSPoint::new(0., 0.),
                                                                NSSize::new(200., 200.)),
                                                    windowStyleMask,
                                                    NSBackingStoreBuffered,
                                                    NO)

Of course, this didn't work. I feel that cocoa-rs should strive to offer idiomatic interfaces wherever possible, so I looked into the implementation and feel that an elegant approach that preserves strong type information is possible.

oluseyi commented 7 years ago

The final interface doesn't require the user ever cast to NSUInteger—that is entirely hidden as an implementation detail:

let windowStyleMask = NSTitledWindowMask|
                      NSClosableWindowMask|
                      NSMiniaturizableWindowMask|
                      NSResizableWindowMask|
                      NSUnifiedTitleAndToolbarWindowMask|
                      NSFullSizeContentViewWindowMask;
let window = NSWindow::alloc(nil)
    .initWithContentRect_styleMask_backing_defer_(windowFrame,
                                                    windowStyleMask,
                                                    NSBackingStoreBuffered,
                                                    NO)
    .autorelease();