|
2 | 2 |
|
3 | 3 | #import <AppKit/AppKit.h> // NSWindowController
|
4 | 4 |
|
5 |
| -@interface WindowController : NSWindowController |
6 |
| -- (id)initWithNewWindow; |
7 |
| -@end |
8 |
| - |
9 |
| -@implementation WindowController |
10 |
| -- (id)initWithNewWindow { |
11 |
| - // The style of the window, which determines different aspects of the |
12 |
| - // window's appearance and behavior. For more information, see |
13 |
| - // https://developer.apple.com/documentation/appkit/nswindowstylemask?language=objc |
14 |
| - NSWindowStyleMask windowStyleMask = |
15 |
| - NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | |
16 |
| - NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable; |
17 |
| - |
18 |
| - NSWindow *window = [[NSWindow alloc] |
19 |
| - initWithContentRect:NSMakeRect(0, 0, 0, 0) |
20 |
| - styleMask:windowStyleMask |
21 |
| - // You should use this mode. |
22 |
| - // It supports hardware acceleration, Quartz drawing, |
23 |
| - // and takes advantage of the GPU when possible. |
24 |
| - backing:NSBackingStoreBuffered |
25 |
| - // When defer is set to YES, the window server postpones the |
26 |
| - // creation of the window device (the low-level resources |
27 |
| - // for rendering) until the window is moved onscreen. This |
28 |
| - // can optimize performance by delaying the allocation of |
29 |
| - // graphics resources until they are needed. |
30 |
| - defer:YES]; |
31 |
| - |
32 |
| - // By default, we don't want the title to be visible |
33 |
| - [window setTitleVisibility:NSWindowTitleHidden]; |
34 |
| - |
35 |
| - // By default, we don't want the title bar to be transparent |
36 |
| - // as we hide the title. It lets the window's content extend to the |
37 |
| - // top of the window. |
38 |
| - [window setTitlebarAppearsTransparent:YES]; |
39 |
| - |
40 |
| - [window.contentView setAutoresizesSubviews:YES]; |
41 |
| - |
42 |
| - return [super initWithWindow:window]; |
43 |
| -} |
44 |
| -@end |
45 |
| - |
46 | 5 | namespace sourcemeta::native {
|
47 |
| -Window::Window() { |
48 |
| - internal_ = |
49 |
| - static_cast<void *>([[WindowController alloc] initWithNewWindow].window); |
50 |
| -} |
51 | 6 |
|
52 |
| -Window::~Window() { |
53 |
| - NSWindow *window = static_cast<NSWindow *>(internal_); |
54 |
| - [[window windowController] close]; |
| 7 | +class Window::Internal { |
| 8 | +public: |
| 9 | + Internal() { |
| 10 | + // The style of the window, which determines different aspects of the |
| 11 | + // window's appearance and behavior. For more information, see |
| 12 | + // https://developer.apple.com/documentation/appkit/nswindowstylemask?language=objc |
| 13 | + NSWindowStyleMask windowStyleMask = |
| 14 | + NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | |
| 15 | + NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable; |
| 16 | + |
| 17 | + NSWindow *window = [[NSWindow alloc] |
| 18 | + initWithContentRect:NSMakeRect(0, 0, 0, 0) |
| 19 | + styleMask:windowStyleMask |
| 20 | + // You should use this mode. |
| 21 | + // It supports hardware acceleration, Quartz drawing, |
| 22 | + // and takes advantage of the GPU when possible. |
| 23 | + backing:NSBackingStoreBuffered |
| 24 | + // When defer is set to YES, the window server postpones |
| 25 | + // the creation of the window device (the low-level |
| 26 | + // resources for rendering) until the window is moved |
| 27 | + // onscreen. This can optimize performance by delaying the |
| 28 | + // allocation of graphics resources until they are needed. |
| 29 | + defer:YES]; |
| 30 | + |
| 31 | + // By default, we don't want the title to be visible |
| 32 | + [window setTitleVisibility:NSWindowTitleHidden]; |
| 33 | + |
| 34 | + // By default, we don't want the title bar to be transparent |
| 35 | + // as we hide the title. It lets the window's content extend to the |
| 36 | + // top of the window. |
| 37 | + [window setTitlebarAppearsTransparent:YES]; |
| 38 | + |
| 39 | + [window.contentView setAutoresizesSubviews:YES]; |
| 40 | + |
| 41 | + window_ = window; |
| 42 | + } |
| 43 | + |
| 44 | + ~Internal() { [[window_ windowController] close]; } |
55 | 45 |
|
56 |
| - if (internal_) { |
57 |
| - CFBridgingRelease(internal_); |
| 46 | + auto show() -> void { |
| 47 | + NSWindowController *controller = [window_ windowController]; |
| 48 | + [controller showWindow:nil]; |
| 49 | + [window_ makeKeyAndOrderFront:controller]; |
58 | 50 | }
|
59 |
| -} |
60 | 51 |
|
61 |
| -auto Window::size(const unsigned int width, const unsigned int height) -> void { |
62 |
| - NSWindow *window = static_cast<NSWindow *>(internal_); |
63 |
| - NSRect frame = [window frame]; |
| 52 | + auto size(const unsigned int width, const unsigned int height) -> void { |
| 53 | + NSRect frame = [window_ frame]; |
64 | 54 |
|
65 |
| - frame.size.width = width; |
66 |
| - frame.size.height = height; |
| 55 | + frame.size.width = width; |
| 56 | + frame.size.height = height; |
67 | 57 |
|
68 |
| - [window setFrame:frame display:YES animate:YES]; |
69 |
| -} |
| 58 | + [window_ setFrame:frame display:YES animate:YES]; |
| 59 | + } |
70 | 60 |
|
71 |
| -auto Window::show() -> void { |
72 |
| - NSWindow *window = static_cast<NSWindow *>(internal_); |
73 |
| - WindowController *controller = [window windowController]; |
74 |
| - [controller showWindow:nil]; |
75 |
| - [window makeKeyAndOrderFront:controller]; |
| 61 | + auto handle() -> void * { return window_; } |
| 62 | + |
| 63 | +private: |
| 64 | + NSWindow *window_; |
| 65 | +}; |
| 66 | + |
| 67 | +Window::Window() { internal_ = new Window::Internal(); } |
| 68 | + |
| 69 | +Window::~Window() { delete internal_; } |
| 70 | + |
| 71 | +auto Window::size(const unsigned int width, const unsigned int height) -> void { |
| 72 | + if (width == 0 || height == 0) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + internal_->size(width, height); |
76 | 77 | }
|
77 | 78 |
|
78 |
| -auto Window::handle() -> void * { return internal_; } |
| 79 | +auto Window::show() -> void { internal_->show(); } |
| 80 | + |
| 81 | +auto Window::handle() -> void * { return internal_->handle(); } |
79 | 82 | } // namespace sourcemeta::native
|
0 commit comments