-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathframelesswindow.mm
366 lines (325 loc) · 10.9 KB
/
framelesswindow.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "framelesswindow.h"
#ifdef Q_OS_MAC
# include <QDebug>
# include <Cocoa/Cocoa.h>
CFramelessWindow::CFramelessWindow(QWidget *parent)
: QMainWindow(parent),
m_draggableHeight(0),
m_bWinMoving(false),
m_bMousePressed(false),
m_bCloseBtnQuit(true),
m_bNativeSystemBtn(false),
m_bIsCloseBtnEnabled(true),
m_bIsMinBtnEnabled(true),
m_bIsZoomBtnEnabled(true),
m_bTitleBarVisible(false)
{
initUI();
}
@interface AppObserver : NSObject
@property (readwrite) CFramelessWindow *window;
- (void)appDidHide:(NSNotification *)notification;
- (void)appDidUnhide:(NSNotification *)notification;
@end
@implementation AppObserver
- (void)appDidHide:(NSNotification *)notification
{
self.window->hide();
}
- (void)appDidUnhide:(NSNotification *)notification
{
self.window->show();
}
@end
// 此类用于支持重载系统按钮的行为
// this Objective-c class is used to override the action of sysytem close button and zoom button
// https://stackoverflow.com/questions/27643659/setting-c-function-as-selector-for-nsbutton-produces-no-results
@interface ButtonPasser : NSObject {
}
@property (readwrite) CFramelessWindow *window;
+ (void)closeButtonAction:(id)sender;
- (void)zoomButtonAction:(id)sender;
@end
@implementation ButtonPasser {
}
+ (void)closeButtonAction:(id)sender
{
Q_UNUSED(sender);
[NSApp hide:nil];
}
- (void)zoomButtonAction:(id)sender
{
Q_UNUSED(sender);
if (0 == self.window)
return;
if (self.window->isFullScreen() || self.window->isMaximized()) {
self.window->showNormal();
emit self.window->toggleFullScreen(false);
} else {
NSView *view = sender;
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
[window toggleFullScreen:window];
emit self.window->toggleFullScreen(true);
}
}
@end
void CFramelessWindow::initUI()
{
m_bNativeSystemBtn = false;
// 如果当前osx版本老于10.9,则后续代码不可用。转为使用定制的系统按钮,不支持自由缩放窗口及窗口阴影
// if (QSysInfo::MV_None == QSysInfo::macVersion())
// {
// if (QSysInfo::MV_None == QSysInfo::MacintoshVersion)
// {setWindowFlags(Qt::FramelessWindowHint); return;}
// }
// if (QSysInfo::MV_10_9 >= QSysInfo::MacintoshVersion)
// {setWindowFlags(Qt::FramelessWindowHint); return;}
NSView *view = (NSView *)winId();
if (0 == view) {
setWindowFlags(Qt::FramelessWindowHint);
return;
}
NSWindow *window = view.window;
if (0 == window) {
setWindowFlags(Qt::FramelessWindowHint);
return;
}
AppObserver *observer = [[AppObserver alloc] init];
if (observer) {
observer.window = this;
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:observer
selector:@selector(appDidHide:)
name:NSWorkspaceDidHideApplicationNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:observer
selector:@selector(appDidUnhide:)
name:NSWorkspaceDidUnhideApplicationNotification
object:nil];
} else {
qWarning() << "Failed to set up Notification Observer!";
}
// 设置标题文字和图标为不可见
window.titleVisibility = NSWindowTitleHidden; // MAC_10_10及以上版本支持
// 设置标题栏为透明
window.titlebarAppearsTransparent = YES; // MAC_10_10及以上版本支持
// 设置不可由标题栏拖动,避免与自定义拖动冲突
[window setMovable:NO]; // MAC_10_6及以上版本支持
// window.movableByWindowBackground = YES;
// 设置view扩展到标题栏
window.styleMask |= NSWindowStyleMaskFullSizeContentView; // MAC_10_10及以上版本支持
m_bNativeSystemBtn = true;
ButtonPasser *passer = [[ButtonPasser alloc] init];
passer.window = this;
// 重载全屏按钮的行为
// override the action of fullscreen button
NSButton *zoomButton = [window standardWindowButton:NSWindowZoomButton];
[zoomButton setTarget:passer];
[zoomButton setAction:@selector(zoomButtonAction:)];
// TODO: move window buttons 2 pixels to the right to align with searchEdit and notesList
// Currently, doesn't work
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton];
NSButton *minimizeButton = [window standardWindowButton:NSWindowMiniaturizeButton];
closeButton.frame = NSMakeRect(closeButton.frame.origin.x + 10, closeButton.frame.origin.y,
closeButton.frame.size.width, closeButton.frame.size.height);
minimizeButton.frame =
NSMakeRect(minimizeButton.frame.origin.x + 10, minimizeButton.frame.origin.y,
minimizeButton.frame.size.width, minimizeButton.frame.size.height);
zoomButton.frame = NSMakeRect(zoomButton.frame.origin.x + 10, zoomButton.frame.origin.y,
zoomButton.frame.size.width, zoomButton.frame.size.height);
}
void CFramelessWindow::setCloseBtnQuit(bool bQuit)
{
if (bQuit || !m_bNativeSystemBtn)
return;
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
// 重载关闭按钮的行为
// override the action of close button
// https://stackoverflow.com/questions/27643659/setting-c-function-as-selector-for-nsbutton-produces-no-results
// https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/Target-Action/Target-Action.html
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton];
[closeButton setTarget:[ButtonPasser class]];
[closeButton setAction:@selector(closeButtonAction:)];
}
void CFramelessWindow::setCloseBtnEnabled(bool bEnable)
{
if (!m_bNativeSystemBtn)
return;
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
m_bIsCloseBtnEnabled = bEnable;
if (bEnable) {
[[window standardWindowButton:NSWindowCloseButton] setEnabled:YES];
} else {
[[window standardWindowButton:NSWindowCloseButton] setEnabled:NO];
}
}
void CFramelessWindow::setMinBtnEnabled(bool bEnable)
{
if (!m_bNativeSystemBtn)
return;
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
m_bIsMinBtnEnabled = bEnable;
if (bEnable) {
[[window standardWindowButton:NSWindowMiniaturizeButton] setEnabled:YES];
} else {
[[window standardWindowButton:NSWindowMiniaturizeButton] setEnabled:NO];
}
}
void CFramelessWindow::setZoomBtnEnabled(bool bEnable)
{
if (!m_bNativeSystemBtn)
return;
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
m_bIsZoomBtnEnabled = bEnable;
if (bEnable) {
[[window standardWindowButton:NSWindowZoomButton] setEnabled:YES];
} else {
[[window standardWindowButton:NSWindowZoomButton] setEnabled:NO];
}
}
void CFramelessWindow::setDraggableAreaHeight(int height)
{
if (height < 0)
height = 0;
m_draggableHeight = height;
}
void CFramelessWindow::hideEvent(QHideEvent *event)
{
// FIXME: Figure out why this fails to trigger our AppObserver listener sometimes. :/
[NSApp hide:nil];
QMainWindow::hideEvent(event);
}
void CFramelessWindow::showEvent(QShowEvent *event)
{
// FIXME: Figure out why this fails to trigger our AppObserver listener sometimes. :/
[NSApp unhide:nil];
QMainWindow::showEvent(event);
}
void CFramelessWindow::mousePressEvent(QMouseEvent *event)
{
if ((event->button() != Qt::LeftButton) || isMaximized()) {
return QMainWindow::mousePressEvent(event);
}
int height = size().height();
if (m_draggableHeight > 0)
height = m_draggableHeight;
QRect rc;
rc.setRect(0, 0, size().width(), height);
if (rc.contains(this->mapFromGlobal(QCursor::pos())) == true) // 如果按下的位置
{
m_WindowPos = this->pos();
m_MousePos = event->globalPos();
m_bMousePressed = true;
}
return QMainWindow::mousePressEvent(event);
}
void CFramelessWindow::mouseReleaseEvent(QMouseEvent *event)
{
m_bWinMoving = false;
if ((event->button() == Qt::LeftButton)) {
m_bMousePressed = false;
}
return QMainWindow::mouseReleaseEvent(event);
}
void CFramelessWindow::mouseMoveEvent(QMouseEvent *event)
{
if (!m_bMousePressed)
return QMainWindow::mouseMoveEvent(event);
m_bWinMoving = true;
this->move(m_WindowPos + (event->globalPos() - m_MousePos));
return QMainWindow::mouseMoveEvent(event);
}
void CFramelessWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
// TODO
// if (!isFullScreen())
// {
// emit restoreFromFullScreen();
// }
}
void CFramelessWindow::onRestoreFromFullScreen()
{
setTitlebarVisible(false);
}
void CFramelessWindow::setTitlebarVisible(bool bTitlebarVisible)
{
if (!m_bNativeSystemBtn)
return;
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
m_bTitleBarVisible = bTitlebarVisible;
if (bTitlebarVisible) {
window.styleMask ^= NSWindowStyleMaskFullSizeContentView; // MAC_10_10及以上版本支持
} else {
window.styleMask |= NSWindowStyleMaskFullSizeContentView; // MAC_10_10及以上版本支持
}
}
void CFramelessWindow::maximizeWindowMac()
{
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
[window zoom:window];
}
void CFramelessWindow::setWindowAlwaysOnTopMac(bool isAlwaysOnTop)
{
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
if (isAlwaysOnTop)
[window setLevel:NSFloatingWindowLevel];
else
[window setLevel:NSNormalWindowLevel];
}
void CFramelessWindow::setStandardWindowButtonsMacVisibility(bool isVisible)
{
NSView *view = (NSView *)winId();
if (0 == view)
return;
NSWindow *window = view.window;
if (0 == window)
return;
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton];
NSButton *minimizeButton = [window standardWindowButton:NSWindowMiniaturizeButton];
NSButton *zoomButton = [window standardWindowButton:NSWindowZoomButton];
[closeButton setHidden:!isVisible];
[minimizeButton setHidden:!isVisible];
[zoomButton setHidden:!isVisible];
}
#endif // Q_OS_MAC