From 5f47b89671cdf84133eed6c908cb06d66a8cc2ff Mon Sep 17 00:00:00 2001 From: Iceyer Date: Mon, 27 Nov 2017 21:19:12 +0800 Subject: [PATCH] feat: add bordless window on mac Change-Id: Iafc1439bc51297a420d5cb0b7c064b86661d2847 --- src/platforms/mac/osxwindow.h | 20 +++++++++++++++++ src/platforms/mac/osxwindow.mm | 41 ++++++++++++++++++++++++++++++++++ src/widgets/dmainwindow.cpp | 20 ++++++++++++++++- src/widgets/dmainwindow.h | 5 +++++ src/widgets/dtitlebar.cpp | 8 ------- src/widgets/widgets.pri | 12 ++++++++++ 6 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 src/platforms/mac/osxwindow.h create mode 100644 src/platforms/mac/osxwindow.mm diff --git a/src/platforms/mac/osxwindow.h b/src/platforms/mac/osxwindow.h new file mode 100644 index 000000000..17646ca44 --- /dev/null +++ b/src/platforms/mac/osxwindow.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2015 ~ 2017 Deepin Technology Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace OSX { + void HideWindowTitlebar(long winid); +} diff --git a/src/platforms/mac/osxwindow.mm b/src/platforms/mac/osxwindow.mm new file mode 100644 index 000000000..56ac9f186 --- /dev/null +++ b/src/platforms/mac/osxwindow.mm @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2015 ~ 2017 Deepin Technology Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "osxwindow.h" + +#include + +namespace OSX { + +void HideWindowTitlebar(long winId) +{ + NSView *view = reinterpret_cast(winId); + NSWindow* window = [view window]; + + [window setStyleMask: [window styleMask] | NSFullSizeContentViewWindowMask | NSWindowTitleHidden]; + + [window setTitlebarAppearsTransparent:YES]; + [window setMovableByWindowBackground:YES]; + + [[window standardWindowButton:NSWindowCloseButton] setHidden:YES]; + [[window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES]; + [[window standardWindowButton:NSWindowZoomButton] setHidden:YES]; + + window.titleVisibility = NSWindowTitleHidden; +} + +} diff --git a/src/widgets/dmainwindow.cpp b/src/widgets/dmainwindow.cpp index aa00f9734..17cdaa479 100644 --- a/src/widgets/dmainwindow.cpp +++ b/src/widgets/dmainwindow.cpp @@ -28,6 +28,10 @@ #include #include +#ifdef Q_OS_MAC +#include "osxwindow.h" +#endif + /// shadow #define SHADOW_COLOR_NORMAL QColor(0, 0, 0, 255 * 35/100) #define SHADOW_COLOR_ACTIVE QColor(0, 0, 0, 255 * 60/100) @@ -42,8 +46,12 @@ DMainWindowPrivate::DMainWindowPrivate(DMainWindow *qq) handle = new DPlatformWindowHandle(qq, qq); qq->setMenuWidget(titlebar); } else { + qq->setMenuWidget(titlebar); +#ifdef Q_OS_MAC + OSX::HideWindowTitlebar(qq->winId()); +#else titlebar->setEmbedMode(true); - qq->setContentsMargins(0, titlebar->height(), 0, 0); +#endif } } @@ -96,6 +104,8 @@ void DMainWindowPrivate::init() } }); } + + } /*! @@ -517,6 +527,14 @@ void DMainWindow::setAutoInputMaskByClipPath(bool autoInputMaskByClipPath) d->handle->setAutoInputMaskByClipPath(autoInputMaskByClipPath); } +#ifdef Q_OS_MAC +void DMainWindow::setWindowFlags(Qt::WindowFlags type) +{ + QMainWindow::setWindowFlags(type); + OSX::HideWindowTitlebar(winId()); +} +#endif + DMainWindow::DMainWindow(DMainWindowPrivate &dd, QWidget *parent) : QMainWindow(parent) , DObject(dd) diff --git a/src/widgets/dmainwindow.h b/src/widgets/dmainwindow.h index ebe98e460..8c7516288 100644 --- a/src/widgets/dmainwindow.h +++ b/src/widgets/dmainwindow.h @@ -92,6 +92,11 @@ public Q_SLOTS: void setEnableBlurWindow(bool enableBlurWindow); void setAutoInputMaskByClipPath(bool autoInputMaskByClipPath); + // TODO: remove it if there is an batter sulotion +#ifdef Q_OS_MAC + void setWindowFlags(Qt::WindowFlags type); +#endif + Q_SIGNALS: void windowRadiusChanged(); void borderWidthChanged(); diff --git a/src/widgets/dtitlebar.cpp b/src/widgets/dtitlebar.cpp index d6dcda532..fb45acd93 100644 --- a/src/widgets/dtitlebar.cpp +++ b/src/widgets/dtitlebar.cpp @@ -543,14 +543,6 @@ bool DTitlebar::eventFilter(QObject *obj, QEvent *event) d->maxButton->setMaximized(d->parentWindow->windowState() == Qt::WindowMaximized); // } break; - case QEvent::Resize: - case QEvent::Show: - if (d->embedMode) { - const auto margins = d->parentWindow->contentsMargins(); - auto horizontalOffset = margins.left() + margins.right(); - setFixedWidth(d->parentWindow->width() - horizontalOffset); - } - break; case QEvent::ShowToParent: d->updateButtonsFunc(); break; diff --git a/src/widgets/widgets.pri b/src/widgets/widgets.pri index 6b9d0157a..4b55217bb 100644 --- a/src/widgets/widgets.pri +++ b/src/widgets/widgets.pri @@ -20,6 +20,18 @@ linux{ } +mac{ + HEADERS +=\ + $$PWD/../platforms/mac/osxwindow.h + + OBJECTIVE_SOURCES += \ + $$PWD/../platforms/mac/osxwindow.mm + + INCLUDEPATH += $$PWD/../platforms/mac + + LIBS += -framework Foundation -framework Cocoa +} + HEADERS += $$PWD/dslider.h\ $$PWD/dthememanager.h \ $$PWD/dapplication.h \