-
Notifications
You must be signed in to change notification settings - Fork 6
/
macwindow.mm
255 lines (205 loc) · 5.82 KB
/
macwindow.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "macwindow.h"
#import <AppKit/AppKit.h>
// An NSTitlebarAccessoryViewController that controls a programatically created view
@interface ProgramaticViewController : NSTitlebarAccessoryViewController
@end
@implementation ProgramaticViewController
- (id)initWithView: (NSView *)aView
{
self = [self init];
self.view = aView;
return self;
}
@end
MacWindow::MacWindow(QWindow *window, QWindow *accessoryWindow)
:m_window(window)
,m_accessoryWindow(accessoryWindow)
{
}
void MacWindow::setContentWindow(QWindow *contentWindow)
{
m_window = contentWindow;
scheduleRecreateNSWindow();
}
void MacWindow::setLeftAcccessoryWindow(QWindow *leftAccessoryWindow)
{
m_accessoryWindow = leftAccessoryWindow;
scheduleRecreateNSWindow();
}
void MacWindow::setRightAcccessoryWindow(QWindow *rightAccessoryWindow)
{
m_rightAccessoryWindow = rightAccessoryWindow;
scheduleRecreateNSWindow();
}
void MacWindow::createNSWindow()
{
qDebug() << "createNSWindow";
if (m_nsWindow)
return;
auto styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
if (m_fullSizeContentView)
styleMask |= NSFullSizeContentViewWindowMask;
NSRect frame = NSMakeRect(200, 200, 320, 200);
m_nsWindow =
[[NSWindow alloc] initWithContentRect:frame
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
m_nsWindow.titleVisibility = m_titleVisibility ? NSWindowTitleVisible : NSWindowTitleHidden;
m_nsWindow.title = m_titleText.toNSString();
m_nsWindow.titlebarAppearsTransparent = m_titlebarAppearsTransparent;
m_nsWindow.movableByWindowBackground = m_movableByWindowBackground;
if (m_toolbarEnabled) {
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"main"];
toolbar.showsBaselineSeparator = m_showsBaselineSeparator;
m_nsWindow.toolbar = toolbar;
}
m_nsWindow.contentView = (__bridge NSView *)reinterpret_cast<void *>(m_window->winId());
// Accessory View
if (m_accessoryView) {
{
NSView *view = (__bridge NSView *)reinterpret_cast<void *>(m_accessoryWindow->winId());
ProgramaticViewController *viewController = [[ProgramaticViewController alloc] initWithView:view];
viewController.layoutAttribute = NSLayoutAttributeLeft;
[m_nsWindow addTitlebarAccessoryViewController:viewController];
}
{
NSView *view = (__bridge NSView *)reinterpret_cast<void *>(m_rightAccessoryWindow->winId());
ProgramaticViewController *viewController = [[ProgramaticViewController alloc] initWithView:view];
viewController.layoutAttribute = NSLayoutAttributeRight;
[m_nsWindow addTitlebarAccessoryViewController:viewController];
}
}
}
void MacWindow::destroyNSWindow()
{
@autoreleasepool {
[m_nsWindow close];
m_nsWindow = nil;
}
}
void MacWindow::recreateNSWindow()
{
if (!m_nsWindow)
return;
destroyNSWindow();
createNSWindow();
if (m_visible)
[m_nsWindow makeKeyAndOrderFront:nil];
}
void MacWindow::scheduleRecreateNSWindow()
{
QTimer::singleShot(0, [this](){
recreateNSWindow();
});
}
void MacWindow::setFullSizeContentView(bool enable)
{
if (m_fullSizeContentView == enable)
return;
m_fullSizeContentView = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::fullSizeContentView() const
{
return m_fullSizeContentView;
}
void MacWindow::setTitlebarAppearsTransparent(bool enable)
{
if (m_titlebarAppearsTransparent == enable)
return;
m_titlebarAppearsTransparent = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::titlebarAppearsTransparent() const
{
return m_titlebarAppearsTransparent;
}
void MacWindow::setMovableByWindowBackgropund(bool enable)
{
if (m_movableByWindowBackground == enable)
return;
m_movableByWindowBackground = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::movableByWindowBackgropund() const
{
return m_movableByWindowBackground;
}
void MacWindow::setToolbarEnabled(bool enable)
{
if (m_toolbarEnabled == enable)
return;
m_toolbarEnabled = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::toolbarEnabled() const
{
return m_toolbarEnabled;
}
void MacWindow::setShowsBaselineSeparator(bool enable)
{
if (m_showsBaselineSeparator == enable)
return;
m_showsBaselineSeparator = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::showsBaselineSeparator() const
{
return m_showsBaselineSeparator;
}
void MacWindow::setTitleVisibility(bool enable)
{
if (m_titleVisibility == enable)
return;
m_titleVisibility = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::titleVisibility() const
{
return m_titleVisibility;
}
void MacWindow::setTitleText(const QString &text)
{
if (m_titleText == text)
return;
m_titleText = text;
scheduleRecreateNSWindow();
}
QString MacWindow::titleText() const
{
return m_titleText;
}
void MacWindow::setAccessoryViewsEnabled(bool enable)
{
if (m_accessoryView == enable)
return;
m_accessoryView = enable;
scheduleRecreateNSWindow();
}
bool MacWindow::acceoryViews() const
{
return m_accessoryView;
}
void MacWindow::setGeometry(QRect geometry)
{
}
QRect MacWindow::geometry() const
{
return QRect();
}
void MacWindow::setVisible(bool visible)
{
qDebug() << "setVisible" << visible;
m_visible = visible;
if (visible) {
createNSWindow();
[m_nsWindow makeKeyAndOrderFront:nil];
} else {
}
}
void MacWindow::show()
{
setVisible(true);
}