Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 4c63ce7

Browse files
author
bors-servo
authored
Auto merge of #160 - oluseyi:bitflag-window-masks, r=jdm
Implement NSWindowStyleMask as bitflags RE #159 `NSWindowStyleMask`, as an enumeration in Objective-C, degrades to `NSUInteger` and is therefore bitwise-combinable, though it loses type information. Instead of implementing `NSWindowStyleMask` as a packed-data object with a `u64` representational form, we ought to employ bit flags to generate a strong type that remains combinable in the manner Cocoa programmers are accustomed to. Also easier than manually implementing `std::ops::BitOr` for the type. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/cocoa-rs/160) <!-- Reviewable:end -->
2 parents b351692 + e773703 commit 4c63ce7

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

examples/hello_world.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() {
3737
let window = NSWindow::alloc(nil)
3838
.initWithContentRect_styleMask_backing_defer_(NSRect::new(NSPoint::new(0., 0.),
3939
NSSize::new(200., 200.)),
40-
NSTitledWindowMask as NSUInteger,
40+
NSTitledWindowMask,
4141
NSBackingStoreBuffered,
4242
NO)
4343
.autorelease();

examples/tab_view.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ unsafe fn create_app(title: id, content: id) -> id {
6767
// create Window
6868
let window = NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
6969
NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)),
70-
NSTitledWindowMask as NSUInteger |
71-
NSClosableWindowMask as NSUInteger |
72-
NSResizableWindowMask as NSUInteger |
73-
NSMiniaturizableWindowMask as NSUInteger |
74-
NSUnifiedTitleAndToolbarWindowMask as NSUInteger,
70+
NSTitledWindowMask |
71+
NSClosableWindowMask |
72+
NSResizableWindowMask |
73+
NSMiniaturizableWindowMask |
74+
NSUnifiedTitleAndToolbarWindowMask,
7575
NSBackingStoreBuffered,
7676
NO
7777
).autorelease();

src/appkit.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub use core_graphics::geometry::CGPoint;
2020

2121
pub use self::NSApplicationActivationPolicy::*;
2222
pub use self::NSApplicationActivationOptions::*;
23-
pub use self::NSWindowMask::*;
2423
pub use self::NSBackingStoreType::*;
2524
pub use self::NSOpenGLPixelFormatAttribute::*;
2625
pub use self::NSOpenGLPFAOpenGLProfiles::*;
@@ -141,22 +140,22 @@ pub enum NSApplicationTerminateReply {
141140
NSTerminateLater = 2,
142141
}
143142

144-
#[repr(u64)]
145-
#[derive(Clone, Copy, Debug, PartialEq)]
146-
pub enum NSWindowMask {
147-
NSBorderlessWindowMask = 0,
148-
NSTitledWindowMask = 1 << 0,
149-
NSClosableWindowMask = 1 << 1,
150-
NSMiniaturizableWindowMask = 1 << 2,
151-
NSResizableWindowMask = 1 << 3,
143+
bitflags! {
144+
pub flags NSWindowStyleMask: NSUInteger {
145+
const NSBorderlessWindowMask = 0,
146+
const NSTitledWindowMask = 1 << 0,
147+
const NSClosableWindowMask = 1 << 1,
148+
const NSMiniaturizableWindowMask = 1 << 2,
149+
const NSResizableWindowMask = 1 << 3,
152150

153-
NSTexturedBackgroundWindowMask = 1 << 8,
151+
const NSTexturedBackgroundWindowMask = 1 << 8,
154152

155-
NSUnifiedTitleAndToolbarWindowMask = 1 << 12,
153+
const NSUnifiedTitleAndToolbarWindowMask = 1 << 12,
156154

157-
NSFullScreenWindowMask = 1 << 14,
155+
const NSFullScreenWindowMask = 1 << 14,
158156

159-
NSFullSizeContentViewWindowMask = 1 << 15
157+
const NSFullSizeContentViewWindowMask = 1 << 15
158+
}
160159
}
161160

162161
#[repr(u64)]
@@ -799,19 +798,19 @@ pub trait NSWindow {
799798
// Creating Windows
800799
unsafe fn initWithContentRect_styleMask_backing_defer_(self,
801800
rect: NSRect,
802-
style: NSUInteger,
801+
style: NSWindowStyleMask,
803802
backing: NSBackingStoreType,
804803
defer: BOOL) -> id;
805804
unsafe fn initWithContentRect_styleMask_backing_defer_screen_(self,
806805
rect: NSRect,
807-
style: NSUInteger,
806+
style: NSWindowStyleMask,
808807
backing: NSBackingStoreType,
809808
defer: BOOL,
810809
screen: id) -> id;
811810

812811
// Configuring Windows
813-
unsafe fn styleMask(self) -> NSUInteger;
814-
unsafe fn setStyleMask_(self, styleMask: NSUInteger);
812+
unsafe fn styleMask(self) -> NSWindowStyleMask;
813+
unsafe fn setStyleMask_(self, styleMask: NSWindowStyleMask);
815814
unsafe fn toggleFullScreen_(self, sender: id);
816815
unsafe fn worksWhenModal(self) -> BOOL;
817816
unsafe fn alphaValue(self) -> CGFloat;
@@ -846,9 +845,9 @@ pub trait NSWindow {
846845
// TODO: Accessing Window Information
847846

848847
// Getting Layout Information
849-
unsafe fn contentRectForFrameRect_styleMask_(self, windowFrame: NSRect, windowStyle: NSUInteger) -> NSRect;
850-
unsafe fn frameRectForContentRect_styleMask_(self, windowContentRect: NSRect, windowStyle: NSUInteger) -> NSRect;
851-
unsafe fn minFrameWidthWithTitle_styleMask_(self, windowTitle: id, windowStyle: NSUInteger) -> CGFloat;
848+
unsafe fn contentRectForFrameRect_styleMask_(self, windowFrame: NSRect, windowStyle: NSWindowStyleMask) -> NSRect;
849+
unsafe fn frameRectForContentRect_styleMask_(self, windowContentRect: NSRect, windowStyle: NSWindowStyleMask) -> NSRect;
850+
unsafe fn minFrameWidthWithTitle_styleMask_(self, windowTitle: id, windowStyle: NSWindowStyleMask) -> CGFloat;
852851
unsafe fn contentRectForFrameRect_(self, windowFrame: NSRect) -> NSRect;
853852
unsafe fn frameRectForContentRect_(self, windowContent: NSRect) -> NSRect;
854853

@@ -1025,36 +1024,37 @@ impl NSWindow for id {
10251024

10261025
unsafe fn initWithContentRect_styleMask_backing_defer_(self,
10271026
rect: NSRect,
1028-
style: NSUInteger,
1027+
style: NSWindowStyleMask,
10291028
backing: NSBackingStoreType,
10301029
defer: BOOL) -> id {
10311030
msg_send![self, initWithContentRect:rect
1032-
styleMask:style
1031+
styleMask:style.bits
10331032
backing:backing as NSUInteger
10341033
defer:defer]
10351034
}
10361035

10371036
unsafe fn initWithContentRect_styleMask_backing_defer_screen_(self,
10381037
rect: NSRect,
1039-
style: NSUInteger,
1038+
style: NSWindowStyleMask,
10401039
backing: NSBackingStoreType,
10411040
defer: BOOL,
10421041
screen: id) -> id {
10431042
msg_send![self, initWithContentRect:rect
1044-
styleMask:style
1043+
styleMask:style.bits
10451044
backing:backing as NSUInteger
10461045
defer:defer
10471046
screen:screen]
10481047
}
10491048

10501049
// Configuring Windows
10511050

1052-
unsafe fn styleMask(self) -> NSUInteger {
1053-
msg_send![self, styleMask]
1051+
unsafe fn styleMask(self) -> NSWindowStyleMask {
1052+
let styleMask = NSWindowStyleMask::from_bits_truncate(msg_send![self, styleMask]);
1053+
styleMask
10541054
}
10551055

1056-
unsafe fn setStyleMask_(self, styleMask: NSUInteger) {
1057-
msg_send![self, setStyleMask:styleMask]
1056+
unsafe fn setStyleMask_(self, styleMask: NSWindowStyleMask) {
1057+
msg_send![self, setStyleMask:styleMask.bits]
10581058
}
10591059

10601060
unsafe fn toggleFullScreen_(self, sender: id) {
@@ -1176,16 +1176,16 @@ impl NSWindow for id {
11761176

11771177
// Getting Layout Information
11781178

1179-
unsafe fn contentRectForFrameRect_styleMask_(self, windowFrame: NSRect, windowStyle: NSUInteger) -> NSRect {
1180-
msg_send![self, contentRectForFrameRect:windowFrame styleMask:windowStyle]
1179+
unsafe fn contentRectForFrameRect_styleMask_(self, windowFrame: NSRect, windowStyle: NSWindowStyleMask) -> NSRect {
1180+
msg_send![self, contentRectForFrameRect:windowFrame styleMask:windowStyle.bits]
11811181
}
11821182

1183-
unsafe fn frameRectForContentRect_styleMask_(self, windowContentRect: NSRect, windowStyle: NSUInteger) -> NSRect {
1184-
msg_send![self, frameRectForContentRect:windowContentRect styleMask:windowStyle]
1183+
unsafe fn frameRectForContentRect_styleMask_(self, windowContentRect: NSRect, windowStyle: NSWindowStyleMask) -> NSRect {
1184+
msg_send![self, frameRectForContentRect:windowContentRect styleMask:windowStyle.bits]
11851185
}
11861186

1187-
unsafe fn minFrameWidthWithTitle_styleMask_(self, windowTitle: id, windowStyle: NSUInteger) -> CGFloat {
1188-
msg_send![self, minFrameWidthWithTitle:windowTitle styleMask:windowStyle]
1187+
unsafe fn minFrameWidthWithTitle_styleMask_(self, windowTitle: id, windowStyle: NSWindowStyleMask) -> CGFloat {
1188+
msg_send![self, minFrameWidthWithTitle:windowTitle styleMask:windowStyle.bits]
11891189
}
11901190

11911191
unsafe fn contentRectForFrameRect_(self, windowFrame: NSRect) -> NSRect {

0 commit comments

Comments
 (0)