Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-17 16:48:57 +08:00
parent 7d22263df0
commit f700b07e5c
31 changed files with 565 additions and 323 deletions

View File

@ -33,7 +33,7 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
struct QtHelper struct QtHelper
{ {
QMutex mutex = {}; QMutex mutex = {};
QWindowList acceptableWindows = {}; QHash<QWindow *, FramelessHelperQt *> qtFramelessHelpers = {};
explicit QtHelper() = default; explicit QtHelper() = default;
~QtHelper() = default; ~QtHelper() = default;
@ -55,14 +55,16 @@ void FramelessHelperQt::addWindow(QWindow *window)
return; return;
} }
g_qtHelper()->mutex.lock(); g_qtHelper()->mutex.lock();
if (g_qtHelper()->acceptableWindows.contains(window)) { if (g_qtHelper()->qtFramelessHelpers.contains(window)) {
g_qtHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
return; return;
} }
g_qtHelper()->acceptableWindows.append(window); // Give it a parent so that it can be deleted even if we forget to do so.
const auto qtFramelessHelper = new FramelessHelperQt(window);
g_qtHelper()->qtFramelessHelpers.insert(window, qtFramelessHelper);
g_qtHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
window->setFlags(window->flags() | Qt::FramelessWindowHint); window->setFlags(window->flags() | Qt::FramelessWindowHint);
window->installEventFilter(this); window->installEventFilter(qtFramelessHelper);
} }
void FramelessHelperQt::removeWindow(QWindow *window) void FramelessHelperQt::removeWindow(QWindow *window)
@ -72,13 +74,16 @@ void FramelessHelperQt::removeWindow(QWindow *window)
return; return;
} }
g_qtHelper()->mutex.lock(); g_qtHelper()->mutex.lock();
if (!g_qtHelper()->acceptableWindows.contains(window)) { if (!g_qtHelper()->qtFramelessHelpers.contains(window)) {
g_qtHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
return; return;
} }
g_qtHelper()->acceptableWindows.removeAll(window); FramelessHelperQt *qtFramelessHelper = g_qtHelper()->qtFramelessHelpers.value(window);
g_qtHelper()->qtFramelessHelpers.remove(window);
g_qtHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
window->removeEventFilter(this); window->removeEventFilter(qtFramelessHelper);
delete qtFramelessHelper;
qtFramelessHelper = nullptr;
window->setFlags(window->flags() & ~Qt::FramelessWindowHint); window->setFlags(window->flags() & ~Qt::FramelessWindowHint);
} }
@ -100,7 +105,7 @@ bool FramelessHelperQt::eventFilter(QObject *object, QEvent *event)
} }
const auto window = qobject_cast<QWindow *>(object); const auto window = qobject_cast<QWindow *>(object);
g_qtHelper()->mutex.lock(); g_qtHelper()->mutex.lock();
if (!g_qtHelper()->acceptableWindows.contains(window)) { if (!g_qtHelper()->qtFramelessHelpers.contains(window)) {
g_qtHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
return false; return false;
} }

View File

@ -42,8 +42,8 @@ public:
explicit FramelessHelperQt(QObject *parent = nullptr); explicit FramelessHelperQt(QObject *parent = nullptr);
~FramelessHelperQt() override; ~FramelessHelperQt() override;
void addWindow(QWindow *window); static void addWindow(QWindow *window);
void removeWindow(QWindow *window); static void removeWindow(QWindow *window);
protected: protected:
Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override; Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override;

View File

@ -39,7 +39,7 @@ struct Win32Helper
{ {
QMutex mutex = {}; QMutex mutex = {};
QScopedPointer<FramelessHelperWin> nativeEventFilter; QScopedPointer<FramelessHelperWin> nativeEventFilter;
QWindowList acceptableWindows = {}; QWindowList framelessWindows = {};
QHash<WId, QWindow *> windowMapping = {}; QHash<WId, QWindow *> windowMapping = {};
QHash<HWND, WNDPROC> qtWindowProcs = {}; QHash<HWND, WNDPROC> qtWindowProcs = {};
@ -187,11 +187,11 @@ void FramelessHelperWin::addWindow(QWindow *window)
return; return;
} }
g_win32Helper()->mutex.lock(); g_win32Helper()->mutex.lock();
if (g_win32Helper()->acceptableWindows.contains(window)) { if (g_win32Helper()->framelessWindows.contains(window)) {
g_win32Helper()->mutex.unlock(); g_win32Helper()->mutex.unlock();
return; return;
} }
g_win32Helper()->acceptableWindows.append(window); g_win32Helper()->framelessWindows.append(window);
const WId winId = window->winId(); const WId winId = window->winId();
g_win32Helper()->windowMapping.insert(winId, window); g_win32Helper()->windowMapping.insert(winId, window);
if (g_win32Helper()->nativeEventFilter.isNull()) { if (g_win32Helper()->nativeEventFilter.isNull()) {
@ -216,11 +216,11 @@ void FramelessHelperWin::removeWindow(QWindow *window)
return; return;
} }
g_win32Helper()->mutex.lock(); g_win32Helper()->mutex.lock();
if (!g_win32Helper()->acceptableWindows.contains(window)) { if (!g_win32Helper()->framelessWindows.contains(window)) {
g_win32Helper()->mutex.unlock(); g_win32Helper()->mutex.unlock();
return; return;
} }
g_win32Helper()->acceptableWindows.removeAll(window); g_win32Helper()->framelessWindows.removeAll(window);
const WId winId = window->winId(); const WId winId = window->winId();
g_win32Helper()->windowMapping.remove(winId); g_win32Helper()->windowMapping.remove(winId);
g_win32Helper()->mutex.unlock(); g_win32Helper()->mutex.unlock();

View File

@ -40,32 +40,19 @@ static const bool g_usePureQtImplementation = (qEnvironmentVariableIntValue("FRA
static constexpr const bool g_usePureQtImplementation = true; static constexpr const bool g_usePureQtImplementation = true;
#endif #endif
Q_GLOBAL_STATIC(FramelessManagerPrivate, g_managerPrivate) Q_GLOBAL_STATIC(FramelessWindowsManager, g_manager)
FramelessManagerPrivate::FramelessManagerPrivate() = default; FramelessWindowsManagerPrivate::FramelessWindowsManagerPrivate(FramelessWindowsManager *q)
FramelessManagerPrivate::~FramelessManagerPrivate()
{ {
QMutexLocker locker(&mutex); Q_ASSERT(q);
if (!qtFramelessHelpers.isEmpty()) { if (q) {
auto it = qtFramelessHelpers.constBegin(); q_ptr = q;
while (it != qtFramelessHelpers.constEnd()) {
const auto helper = it.value();
if (helper) {
delete helper;
}
++it;
}
qtFramelessHelpers.clear();
} }
} }
FramelessManagerPrivate *FramelessManagerPrivate::instance() FramelessWindowsManagerPrivate::~FramelessWindowsManagerPrivate() = default;
{
return g_managerPrivate();
}
QUuid FramelessManagerPrivate::findIdByWindow(QWindow *value) const QUuid FramelessWindowsManagerPrivate::findIdByWindow(QWindow *value) const
{ {
Q_ASSERT(value); Q_ASSERT(value);
if (!value) { if (!value) {
@ -81,7 +68,7 @@ QUuid FramelessManagerPrivate::findIdByWindow(QWindow *value) const
return windowMapping.value(value); return windowMapping.value(value);
} }
QUuid FramelessManagerPrivate::findIdByWinId(const WId value) const QUuid FramelessWindowsManagerPrivate::findIdByWinId(const WId value) const
{ {
Q_ASSERT(value); Q_ASSERT(value);
if (!value) { if (!value) {
@ -97,7 +84,7 @@ QUuid FramelessManagerPrivate::findIdByWinId(const WId value) const
return winIdMapping.value(value); return winIdMapping.value(value);
} }
QWindow *FramelessManagerPrivate::findWindowById(const QUuid &value) const QWindow *FramelessWindowsManagerPrivate::findWindowById(const QUuid &value) const
{ {
Q_ASSERT(!value.isNull()); Q_ASSERT(!value.isNull());
if (value.isNull()) { if (value.isNull()) {
@ -117,7 +104,7 @@ QWindow *FramelessManagerPrivate::findWindowById(const QUuid &value) const
return nullptr; return nullptr;
} }
WId FramelessManagerPrivate::findWinIdById(const QUuid &value) const WId FramelessWindowsManagerPrivate::findWinIdById(const QUuid &value) const
{ {
Q_ASSERT(!value.isNull()); Q_ASSERT(!value.isNull());
if (value.isNull()) { if (value.isNull()) {
@ -127,15 +114,16 @@ WId FramelessManagerPrivate::findWinIdById(const QUuid &value) const
return (window ? window->winId() : 0); return (window ? window->winId() : 0);
} }
Q_GLOBAL_STATIC(FramelessWindowsManager, g_managerPublic) FramelessWindowsManager::FramelessWindowsManager(QObject *parent) : QObject(parent)
{
FramelessWindowsManager::FramelessWindowsManager(QObject *parent) : QObject(parent) {} d_ptr.reset(new FramelessWindowsManagerPrivate(this));
}
FramelessWindowsManager::~FramelessWindowsManager() = default; FramelessWindowsManager::~FramelessWindowsManager() = default;
FramelessWindowsManager *FramelessWindowsManager::instance() FramelessWindowsManager *FramelessWindowsManager::instance()
{ {
return g_managerPublic(); return g_manager();
} }
void FramelessWindowsManager::addWindow(QWindow *window) void FramelessWindowsManager::addWindow(QWindow *window)
@ -144,20 +132,15 @@ void FramelessWindowsManager::addWindow(QWindow *window)
if (!window) { if (!window) {
return; return;
} }
g_managerPrivate()->mutex.lock(); Q_D(FramelessWindowsManager);
if (g_managerPrivate()->windowMapping.contains(window)) { d->mutex.lock();
g_managerPrivate()->mutex.unlock(); if (d->windowMapping.contains(window)) {
d->mutex.unlock();
return; return;
} }
const QUuid uuid = QUuid::createUuid(); const QUuid uuid = QUuid::createUuid();
g_managerPrivate()->windowMapping.insert(window, uuid); d->windowMapping.insert(window, uuid);
g_managerPrivate()->winIdMapping.insert(window->winId(), uuid); d->winIdMapping.insert(window->winId(), uuid);
FramelessHelperQt *qtFramelessHelper = nullptr;
if (g_usePureQtImplementation) {
// Give it a parent so that it can be deleted even if we forget to do.
qtFramelessHelper = new FramelessHelperQt(window);
g_managerPrivate()->qtFramelessHelpers.insert(uuid, qtFramelessHelper);
}
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
if (!g_usePureQtImplementation) { if (!g_usePureQtImplementation) {
// Work-around Win32 multi-monitor artifacts. // Work-around Win32 multi-monitor artifacts.
@ -173,15 +156,12 @@ void FramelessWindowsManager::addWindow(QWindow *window)
// observed disappeared indeed, amazingly. // observed disappeared indeed, amazingly.
window->resize(window->size()); window->resize(window->size());
}); });
g_managerPrivate()->win32WorkaroundConnections.insert(uuid, workaroundConnection); d->win32WorkaroundConnections.insert(uuid, workaroundConnection);
} }
#endif #endif
g_managerPrivate()->mutex.unlock(); d->mutex.unlock();
if (g_usePureQtImplementation) { if (g_usePureQtImplementation) {
Q_ASSERT(qtFramelessHelper); FramelessHelperQt::addWindow(window);
if (qtFramelessHelper) {
qtFramelessHelper->addWindow(window);
}
} }
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
if (!g_usePureQtImplementation) { if (!g_usePureQtImplementation) {
@ -196,32 +176,30 @@ void FramelessWindowsManager::removeWindow(QWindow *window)
if (!window) { if (!window) {
return; return;
} }
QMutexLocker locker(&g_managerPrivate()->mutex); Q_D(FramelessWindowsManager);
if (!g_managerPrivate()->windowMapping.contains(window)) { QMutexLocker locker(&d->mutex);
if (!d->windowMapping.contains(window)) {
return; return;
} }
const QUuid uuid = g_managerPrivate()->windowMapping.value(window); const QUuid uuid = d->windowMapping.value(window);
Q_ASSERT(!uuid.isNull()); Q_ASSERT(!uuid.isNull());
if (uuid.isNull()) { if (uuid.isNull()) {
return; return;
} }
if (g_managerPrivate()->qtFramelessHelpers.contains(uuid)) { if (g_usePureQtImplementation) {
const auto helper = g_managerPrivate()->qtFramelessHelpers.value(uuid); FramelessHelperQt::removeWindow(window);
Q_ASSERT(helper);
if (helper) {
helper->removeWindow(window);
delete helper;
}
g_managerPrivate()->qtFramelessHelpers.remove(uuid);
} }
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
if (g_managerPrivate()->win32WorkaroundConnections.contains(uuid)) { if (!g_usePureQtImplementation) {
disconnect(g_managerPrivate()->win32WorkaroundConnections.value(uuid)); FramelessHelperWin::removeWindow(window);
g_managerPrivate()->win32WorkaroundConnections.remove(uuid); }
if (d->win32WorkaroundConnections.contains(uuid)) {
disconnect(d->win32WorkaroundConnections.value(uuid));
d->win32WorkaroundConnections.remove(uuid);
} }
#endif #endif
g_managerPrivate()->windowMapping.remove(window); d->windowMapping.remove(window);
g_managerPrivate()->winIdMapping.remove(window->winId()); d->winIdMapping.remove(window->winId());
} }
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -33,9 +33,12 @@ QT_END_NAMESPACE
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
class FramelessWindowsManagerPrivate;
class FRAMELESSHELPER_CORE_API FramelessWindowsManager : public QObject class FRAMELESSHELPER_CORE_API FramelessWindowsManager : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DECLARE_PRIVATE(FramelessWindowsManager)
Q_DISABLE_COPY_MOVE(FramelessWindowsManager) Q_DISABLE_COPY_MOVE(FramelessWindowsManager)
public: public:
@ -44,12 +47,15 @@ public:
Q_NODISCARD static FramelessWindowsManager *instance(); Q_NODISCARD static FramelessWindowsManager *instance();
static void addWindow(QWindow *window); void addWindow(QWindow *window);
static void removeWindow(QWindow *window); void removeWindow(QWindow *window);
Q_SIGNALS: Q_SIGNALS:
void systemThemeChanged(); void systemThemeChanged();
void systemMenuRequested(const QPointF &); void systemMenuRequested(const QPointF &);
private:
QScopedPointer<FramelessWindowsManagerPrivate> d_ptr;
}; };
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -32,19 +32,16 @@
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
class FramelessHelperQt; class FramelessWindowsManager;
class FRAMELESSHELPER_CORE_API FramelessManagerPrivate class FRAMELESSHELPER_CORE_API FramelessWindowsManagerPrivate
{ {
Q_DISABLE_COPY_MOVE(FramelessManagerPrivate) Q_DECLARE_PUBLIC(FramelessWindowsManager)
Q_DISABLE_COPY_MOVE(FramelessWindowsManagerPrivate)
friend class FramelessWindowsManager;
public: public:
explicit FramelessManagerPrivate(); explicit FramelessWindowsManagerPrivate(FramelessWindowsManager *q);
~FramelessManagerPrivate(); ~FramelessWindowsManagerPrivate();
[[nodiscard]] static FramelessManagerPrivate *instance();
[[nodiscard]] QUuid findIdByWindow(QWindow *value) const; [[nodiscard]] QUuid findIdByWindow(QWindow *value) const;
[[nodiscard]] QUuid findIdByWinId(const WId value) const; [[nodiscard]] QUuid findIdByWinId(const WId value) const;
@ -53,10 +50,10 @@ public:
[[nodiscard]] WId findWinIdById(const QUuid &value) const; [[nodiscard]] WId findWinIdById(const QUuid &value) const;
private: private:
FramelessWindowsManager *q_ptr = nullptr;
mutable QMutex mutex = {}; mutable QMutex mutex = {};
QHash<QWindow *, QUuid> windowMapping = {}; QHash<QWindow *, QUuid> windowMapping = {};
QHash<WId, QUuid> winIdMapping = {}; QHash<WId, QUuid> winIdMapping = {};
QHash<QUuid, FramelessHelperQt *> qtFramelessHelpers = {};
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
QHash<QUuid, QMetaObject::Connection> win32WorkaroundConnections = {}; QHash<QUuid, QMetaObject::Connection> win32WorkaroundConnections = {};
#endif #endif

View File

@ -23,6 +23,7 @@
*/ */
#include "utils.h" #include "utils.h"
#include <QtCore/qvariant.h>
#include <QtGui/qwindow.h> #include <QtGui/qwindow.h>
// The "Q_INIT_RESOURCE()" macro can't be used within a namespace, // The "Q_INIT_RESOURCE()" macro can't be used within a namespace,
@ -36,7 +37,7 @@ static inline void initResource()
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
static const QString kResourcePrefix = QStringLiteral(":/org.wangwenx190.FramelessHelper/images"); static const QString kImageResourcePrefix = QStringLiteral(":/org.wangwenx190.FramelessHelper/images");
Qt::CursorShape Utils::calculateCursorShape(const QWindow *window, const QPointF &pos) Qt::CursorShape Utils::calculateCursorShape(const QWindow *window, const QPointF &pos)
{ {
@ -103,11 +104,12 @@ bool Utils::isWindowFixedSize(const QWindow *window)
return (!minSize.isEmpty() && !maxSize.isEmpty() && (minSize == maxSize)); return (!minSize.isEmpty() && !maxSize.isEmpty() && (minSize == maxSize));
} }
QIcon Utils::getSystemButtonIcon(const SystemButtonType type, const SystemTheme theme) QVariant Utils::getSystemButtonIconResource
(const SystemButtonType button, const SystemTheme theme, const ResourceType type)
{ {
const QString resourcePath = [type, theme]() -> QString { const QString resourceUri = [button, theme]() -> QString {
const QString szType = [type]() -> QString { const QString szButton = [button]() -> QString {
switch (type) { switch (button) {
case SystemButtonType::WindowIcon: case SystemButtonType::WindowIcon:
break; break;
case SystemButtonType::Minimize: case SystemButtonType::Minimize:
@ -134,10 +136,18 @@ QIcon Utils::getSystemButtonIcon(const SystemButtonType type, const SystemTheme
} }
return {}; return {};
}(); }();
return QStringLiteral("%1/%2/chrome-%3.svg").arg(kResourcePrefix, szTheme, szType); return QStringLiteral("%1/%2/chrome-%3.svg").arg(kImageResourcePrefix, szTheme, szButton);
}(); }();
initResource(); initResource();
return QIcon(resourcePath); switch (type) {
case ResourceType::Image:
return QImage(resourceUri);
case ResourceType::Pixmap:
return QPixmap(resourceUri);
case ResourceType::Icon:
return QIcon(resourceUri);
}
return {};
} }
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -48,6 +48,14 @@ enum class SystemButtonType : int
}; };
Q_ENUM_NS(SystemButtonType) Q_ENUM_NS(SystemButtonType)
enum class ResourceType : int
{
Image = 0,
Pixmap = 1,
Icon = 2
};
Q_ENUM_NS(ResourceType)
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
enum class DwmColorizationArea : int enum class DwmColorizationArea : int
{ {
@ -67,7 +75,8 @@ namespace Utils
FRAMELESSHELPER_CORE_API void startSystemMove(QWindow *window); FRAMELESSHELPER_CORE_API void startSystemMove(QWindow *window);
FRAMELESSHELPER_CORE_API void startSystemResize(QWindow *window, const Qt::Edges edges); FRAMELESSHELPER_CORE_API void startSystemResize(QWindow *window, const Qt::Edges edges);
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowFixedSize(const QWindow *window); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowFixedSize(const QWindow *window);
[[nodiscard]] FRAMELESSHELPER_CORE_API QIcon getSystemButtonIcon(const SystemButtonType type, const SystemTheme theme); [[nodiscard]] FRAMELESSHELPER_CORE_API QVariant getSystemButtonIconResource
(const SystemButtonType button, const SystemTheme theme, const ResourceType type);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWin8OrGreater(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWin8OrGreater();

View File

@ -1,8 +1,8 @@
if(TARGET Qt${QT_VERSION_MAJOR}::Widgets) if(TARGET Qt${QT_VERSION_MAJOR}::Widgets)
add_subdirectory(widget) add_subdirectory(widget)
#add_subdirectory(mainwindow) add_subdirectory(mainwindow)
endif() endif()
if(TARGET Qt${QT_VERSION_MAJOR}::Quick) if(TARGET Qt${QT_VERSION_MAJOR}::Quick)
#add_subdirectory(quick) add_subdirectory(quick)
endif() endif()

View File

@ -15,6 +15,7 @@ add_executable(MainWindow WIN32 ${SOURCES})
target_link_libraries(MainWindow PRIVATE target_link_libraries(MainWindow PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Widgets
FramelessHelperCore
FramelessHelperWidgets FramelessHelperWidgets
) )

View File

@ -13,10 +13,6 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Hello, World!</string> <string>Hello, World!</string>
</property> </property>
<property name="windowIcon">
<iconset>
<normaloff>../example.ico</normaloff>../example.ico</iconset>
</property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>

View File

@ -179,10 +179,6 @@
<property name="toolTip"> <property name="toolTip">
<string>Minimize</string> <string>Minimize</string>
</property> </property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/dark/chrome-minimize.svg</normaloff>:/images/dark/chrome-minimize.svg</iconset>
</property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>16</width> <width>16</width>
@ -220,11 +216,6 @@
<property name="toolTip"> <property name="toolTip">
<string>Maximize</string> <string>Maximize</string>
</property> </property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/dark/chrome-maximize.svg</normaloff>
<normalon>:/images/dark/chrome-restore.svg</normalon>:/images/dark/chrome-maximize.svg</iconset>
</property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>16</width> <width>16</width>
@ -265,10 +256,6 @@
<property name="toolTip"> <property name="toolTip">
<string>Close</string> <string>Close</string>
</property> </property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/dark/chrome-close.svg</normaloff>:/images/dark/chrome-close.svg</iconset>
</property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>16</width> <width>16</width>
@ -280,8 +267,6 @@
</layout> </layout>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources> <resources/>
<include location="../images.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>

View File

@ -23,171 +23,61 @@
*/ */
#include "mainwindow.h" #include "mainwindow.h"
#include <QtGui/qpainter.h> #include "ui_MainWindow.h"
#include <QtGui/qevent.h> #include "ui_TitleBar.h"
#include <framelesswindowsmanager.h>
#include <utilities.h>
FRAMELESSHELPER_USE_NAMESPACE MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : FramelessMainWindow(parent, flags)
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{ {
setAttribute(Qt::WA_DontCreateNativeAncestors);
createWinId();
setupUi(); setupUi();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
if (titleBarWidget) { if (titleBar) {
delete titleBarWidget; delete titleBar;
titleBarWidget = nullptr; titleBar = nullptr;
} }
if (appMainWindow) { if (mainWindow) {
delete appMainWindow; delete mainWindow;
appMainWindow = nullptr; mainWindow = nullptr;
}
}
void MainWindow::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
initFramelessHelperOnce();
}
void MainWindow::changeEvent(QEvent *event)
{
QMainWindow::changeEvent(event);
bool shouldUpdate = false;
if (event->type() == QEvent::WindowStateChange) {
#ifdef Q_OS_WINDOWS
if (Utilities::isWindowFrameBorderVisible()) {
if (isMaximized() || isFullScreen()) {
setContentsMargins(0, 0, 0, 0);
} else if (!isMinimized()) {
resetContentsMargins();
}
}
#endif
shouldUpdate = true;
Q_EMIT windowStateChanged();
} else if (event->type() == QEvent::ActivationChange) {
shouldUpdate = true;
}
if (shouldUpdate) {
update();
}
}
void MainWindow::initFramelessHelperOnce()
{
if (m_inited) {
return;
}
m_inited = true;
FramelessWindowsManager::addWindow(windowHandle());
}
void MainWindow::resetContentsMargins()
{
#ifdef Q_OS_WINDOWS
if (Utilities::isWindowFrameBorderVisible()) {
setContentsMargins(0, 1, 0, 0);
}
#endif
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QMainWindow::paintEvent(event);
#ifdef Q_OS_WINDOWS
if ((windowState() == Qt::WindowNoState) && Utilities::isWindowFrameBorderVisible() && !Utilities::isWin11OrGreater()) {
QPainter painter(this);
painter.save();
QPen pen = {};
pen.setColor(Utilities::getFrameBorderColor(isActiveWindow()));
pen.setWidth(1);
painter.setPen(pen);
painter.drawLine(0, 0, width(), 0);
painter.restore();
}
#endif
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
QMainWindow::mousePressEvent(event);
const Qt::MouseButton button = event->button();
if ((button != Qt::LeftButton) && (button != Qt::RightButton)) {
return;
}
if (isInTitleBarDraggableArea(event->pos())) {
if (button == Qt::LeftButton) {
Utilities::startSystemMove(windowHandle());
} else {
#ifdef Q_OS_WINDOWS
# if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
const QPointF globalPos = event->globalPosition();
# else
const QPointF globalPos = event->globalPos();
# endif
const QPointF pos = globalPos * devicePixelRatioF();
Utilities::showSystemMenu(winId(), pos);
#endif
}
}
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
QMainWindow::mouseDoubleClickEvent(event);
if (event->button() != Qt::LeftButton) {
return;
}
if (isInTitleBarDraggableArea(event->pos())) {
titleBarWidget->maximizeButton->click();
} }
} }
void MainWindow::setupUi() void MainWindow::setupUi()
{ {
appMainWindow = new Ui::MainWindow; mainWindow = new Ui::MainWindow;
appMainWindow->setupUi(this); mainWindow->setupUi(this);
const auto widget = new QWidget(this); const auto titleBarWidget = new QWidget(this);
titleBarWidget = new Ui::TitleBar; titleBar = new Ui::TitleBar;
titleBarWidget->setupUi(widget); titleBar->setupUi(titleBarWidget);
QMenuBar *mb = menuBar(); QMenuBar *mb = menuBar();
titleBarWidget->horizontalLayout->insertWidget(1, mb); titleBar->horizontalLayout->insertWidget(1, mb);
setMenuWidget(widget); setMenuWidget(titleBarWidget);
resetContentsMargins(); setTitleBarWidget(titleBarWidget);
connect(this, &MainWindow::windowIconChanged, titleBarWidget->iconButton, &QPushButton::setIcon); setHitTestVisible(titleBar->iconButton, true);
connect(this, &MainWindow::windowTitleChanged, titleBarWidget->titleLabel, &QLabel::setText); setHitTestVisible(titleBar->minimizeButton, true);
connect(titleBarWidget->closeButton, &QPushButton::clicked, this, &MainWindow::close); setHitTestVisible(titleBar->maximizeButton, true);
connect(titleBarWidget->minimizeButton, &QPushButton::clicked, this, &MainWindow::showMinimized); setHitTestVisible(titleBar->closeButton, true);
connect(titleBarWidget->maximizeButton, &QPushButton::clicked, this, [this](){
if (isMaximized() || isFullScreen()) { connect(titleBar->minimizeButton, &QPushButton::clicked, this, &MainWindow::showMinimized);
connect(titleBar->maximizeButton, &QPushButton::clicked, this, [this](){
if (isZoomed()) {
showNormal(); showNormal();
} else { } else {
showMaximized(); showMaximized();
} }
}); });
connect(titleBar->closeButton, &QPushButton::clicked, this, &MainWindow::close);
connect(this, &MainWindow::windowIconChanged, titleBar->iconButton, &QPushButton::setIcon);
connect(this, &MainWindow::windowTitleChanged, titleBar->titleLabel, &QLabel::setText);
connect(this, &MainWindow::windowStateChanged, this, [this](){ connect(this, &MainWindow::windowStateChanged, this, [this](){
const bool check = (isMaximized() || isFullScreen()); const bool zoomed = isZoomed();
titleBarWidget->maximizeButton->setChecked(check); titleBar->maximizeButton->setChecked(zoomed);
titleBarWidget->maximizeButton->setToolTip(check ? tr("Restore") : tr("Maximize")); titleBar->maximizeButton->setToolTip(zoomed ? tr("Restore") : tr("Maximize"));
}); });
} }
bool MainWindow::isInTitleBarDraggableArea(const QPoint &pos) const
{
QRegion draggableArea = {0, 0, menuWidget()->width(), menuWidget()->height()};
draggableArea -= titleBarWidget->minimizeButton->geometry();
draggableArea -= titleBarWidget->maximizeButton->geometry();
draggableArea -= titleBarWidget->closeButton->geometry();
return draggableArea.contains(pos);
}

View File

@ -24,38 +24,30 @@
#pragma once #pragma once
#include <QtWidgets/qmainwindow.h> #include <framelessmainwindow.h>
#include "ui_MainWindow.h"
#include "ui_TitleBar.h"
class MainWindow : public QMainWindow namespace Ui
{
class TitleBar;
class MainWindow;
}
class MainWindow : public FRAMELESSHELPER_PREPEND_NAMESPACE(FramelessMainWindow)
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(MainWindow)
public: public:
explicit MainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = {}); explicit MainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = {});
~MainWindow() override; ~MainWindow() override;
protected:
void showEvent(QShowEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void changeEvent(QEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
private: private:
void setupUi(); void setupUi();
void initFramelessHelperOnce();
bool isInTitleBarDraggableArea(const QPoint &pos) const;
private Q_SLOTS:
void resetContentsMargins();
Q_SIGNALS: Q_SIGNALS:
void windowStateChanged(); void windowStateChanged();
private: private:
bool m_inited = false; Ui::TitleBar *titleBar = nullptr;
Ui::TitleBar *titleBarWidget = nullptr; Ui::MainWindow *mainWindow = nullptr;
Ui::MainWindow *appMainWindow = nullptr;
}; };

View File

@ -16,6 +16,7 @@ add_executable(Quick WIN32 ${SOURCES})
target_link_libraries(Quick PRIVATE target_link_libraries(Quick PRIVATE
Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::QuickControls2 Qt${QT_VERSION_MAJOR}::QuickControls2
FramelessHelperCore
FramelessHelperQuick FramelessHelperQuick
) )
@ -26,4 +27,5 @@ target_compile_definitions(Quick PRIVATE
QT_USE_QSTRINGBUILDER QT_USE_QSTRINGBUILDER
QT_DEPRECATED_WARNINGS QT_DEPRECATED_WARNINGS
QT_DISABLE_DEPRECATED_BEFORE=0x060400 QT_DISABLE_DEPRECATED_BEFORE=0x060400
$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>
) )

View File

@ -30,8 +30,6 @@
FRAMELESSHELPER_USE_NAMESPACE FRAMELESSHELPER_USE_NAMESPACE
static constexpr const char FRAMELESSHELPER_QUICK_URI[] = "org.wangwenx190.FramelessHelper";
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
@ -54,9 +52,6 @@ int main(int argc, char *argv[])
} }
#endif #endif
QScopedPointer<FramelessQuickHelper> framelessHelper(new FramelessQuickHelper);
QScopedPointer<FramelessQuickUtils> framelessUtils(new FramelessQuickUtils);
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@ -65,28 +60,23 @@ int main(int argc, char *argv[])
QQuickStyle::setStyle(QStringLiteral("Default")); QQuickStyle::setStyle(QStringLiteral("Default"));
#endif #endif
qmlRegisterSingletonInstance(FRAMELESSHELPER_QUICK_URI, 1, 0, "FramelessHelper", framelessHelper.data()); FramelessQuickHelper::registerTypes(&engine);
qmlRegisterSingletonInstance(FRAMELESSHELPER_QUICK_URI, 1, 0, "FramelessUtils", framelessUtils.data());
const QUrl mainQmlUrl(QStringLiteral("qrc:///qml/MainWindow.qml")); const QUrl homepageUrl(QStringLiteral("qrc:///qml/MainWindow.qml"));
const QMetaObject::Connection connection = QObject::connect( const QMetaObject::Connection connection = QObject::connect(
&engine, &engine, &QQmlApplicationEngine::objectCreated, &application,
&QQmlApplicationEngine::objectCreated, [&homepageUrl, &connection](QObject *object, const QUrl &url) {
&application, if (url != homepageUrl) {
[&mainQmlUrl, &connection](QObject *object, const QUrl &url) {
if (url != mainQmlUrl) {
return; return;
} }
if (object) { if (object) {
QObject::disconnect(connection); QObject::disconnect(connection);
} else { } else {
QCoreApplication::exit(-1); QCoreApplication::exit(-1);
} }
}, }, Qt::QueuedConnection);
Qt::QueuedConnection);
engine.load(mainQmlUrl); engine.load(homepageUrl);
return QCoreApplication::exec(); return QCoreApplication::exec();
} }

View File

@ -45,7 +45,7 @@ Button {
Image { Image {
anchors.centerIn: parent anchors.centerIn: parent
source: FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible source: FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible
? "qrc:/images/light/chrome-close.svg" : "qrc:/images/dark/chrome-close.svg" ? "image://framelesshelper/dark/close" : "image://framelesshelper/light/close"
} }
} }

View File

@ -32,7 +32,7 @@ Window {
visible: true visible: true
width: 800 width: 800
height: 600 height: 600
title: qsTr("Hello, World!") title: qsTr("Hello, World! - Qt Quick")
color: FramelessUtils.darkModeEnabled ? "#202020" : "#f0f0f0" color: FramelessUtils.darkModeEnabled ? "#202020" : "#f0f0f0"
Timer { Timer {

View File

@ -48,9 +48,9 @@ Button {
anchors.centerIn: parent anchors.centerIn: parent
source: button.maximized ? source: button.maximized ?
(FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible (FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible
? "qrc:/images/light/chrome-restore.svg" : "qrc:/images/dark/chrome-restore.svg") : ? "image://framelesshelper/dark/restore" : "image://framelesshelper/light/restore") :
(FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible (FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible
? "qrc:/images/light/chrome-maximize.svg" : "qrc:/images/dark/chrome-maximize.svg") ? "image://framelesshelper/dark/maximize" : "image://framelesshelper/light/maximize")
} }
} }

View File

@ -45,7 +45,7 @@ Button {
Image { Image {
anchors.centerIn: parent anchors.centerIn: parent
source: FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible source: FramelessUtils.darkModeEnabled || FramelessUtils.titleBarColorVisible
? "qrc:/images/light/chrome-minimize.svg" : "qrc:/images/dark/chrome-minimize.svg" ? "image://framelesshelper/dark/minimize" : "image://framelesshelper/light/minimize"
} }
} }

View File

@ -42,14 +42,15 @@ Widget::~Widget() = default;
void Widget::timerEvent(QTimerEvent *event) void Widget::timerEvent(QTimerEvent *event)
{ {
FramelessWidget::timerEvent(event); FramelessWidget::timerEvent(event);
if (m_clockLabel) { if (!m_clockLabel) {
m_clockLabel->setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss"))); return;
} }
m_clockLabel->setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss")));
} }
void Widget::setupUi() void Widget::setupUi()
{ {
setWindowTitle(tr("Hello, World!")); setWindowTitle(tr("Hello, World! - Qt Widgets"));
resize(800, 600); resize(800, 600);
m_clockLabel = new QLabel(this); m_clockLabel = new QLabel(this);
m_clockLabel->setFrameShape(QFrame::NoFrame); m_clockLabel->setFrameShape(QFrame::NoFrame);

View File

@ -6,6 +6,8 @@ set(SOURCES
framelessquickhelper.cpp framelessquickhelper.cpp
framelessquickutils.h framelessquickutils.h
framelessquickutils.cpp framelessquickutils.cpp
framelesshelperimageprovider.h
framelesshelperimageprovider.cpp
) )
if(WIN32 AND NOT FRAMELESSHELPER_BUILD_STATIC) if(WIN32 AND NOT FRAMELESSHELPER_BUILD_STATIC)

View File

@ -0,0 +1,112 @@
/*
* MIT License
*
* Copyright (C) 2022 by wangwenx190 (Yuhang Zhao)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "framelesshelperimageprovider.h"
#include <utils.h>
FRAMELESSHELPER_BEGIN_NAMESPACE
static constexpr const QSize kDefaultSystemButtonIconSize = {16, 16};
[[nodiscard]] static inline SystemTheme strToTheme(const QString &str)
{
Q_ASSERT(!str.isEmpty());
if (str.isEmpty()) {
return SystemTheme::Light;
}
if (str.compare(QStringLiteral("light"), Qt::CaseInsensitive) == 0) {
return SystemTheme::Light;
}
if (str.compare(QStringLiteral("dark"), Qt::CaseInsensitive) == 0) {
return SystemTheme::Dark;
}
if (str.compare(QStringLiteral("hc-light"), Qt::CaseInsensitive) == 0) {
return SystemTheme::HighContrastLight;
}
if (str.compare(QStringLiteral("hc-dark"), Qt::CaseInsensitive) == 0) {
return SystemTheme::HighContrastDark;
}
return SystemTheme::Light;
}
[[nodiscard]] static inline SystemButtonType strToButton(const QString &str)
{
Q_ASSERT(!str.isEmpty());
if (str.isEmpty()) {
return SystemButtonType::WindowIcon;
}
if (str.compare(QStringLiteral("minimize"), Qt::CaseInsensitive) == 0) {
return SystemButtonType::Minimize;
}
if (str.compare(QStringLiteral("maximize"), Qt::CaseInsensitive) == 0) {
return SystemButtonType::Maximize;
}
if (str.compare(QStringLiteral("restore"), Qt::CaseInsensitive) == 0) {
return SystemButtonType::Restore;
}
if (str.compare(QStringLiteral("close"), Qt::CaseInsensitive) == 0) {
return SystemButtonType::Close;
}
return SystemButtonType::WindowIcon;
}
FramelessHelperImageProvider::FramelessHelperImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {}
FramelessHelperImageProvider::~FramelessHelperImageProvider() = default;
QPixmap FramelessHelperImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
Q_ASSERT(!id.isEmpty());
if (id.isEmpty()) {
return {};
}
const QStringList params = id.split(u'/', Qt::SkipEmptyParts, Qt::CaseInsensitive);
Q_ASSERT(!params.isEmpty());
if (params.isEmpty()) {
return {};
}
Q_ASSERT(params.count() >= 2);
if (params.count() < 2) {
return {};
}
const SystemTheme theme = strToTheme(params.at(0));
const SystemButtonType button = strToButton(params.at(1));
const QVariant pixmapVar = Utils::getSystemButtonIconResource(button, theme, ResourceType::Pixmap);
if (!pixmapVar.isValid()) {
return {};
}
if (static_cast<QMetaType::Type>(pixmapVar.typeId()) != QMetaType::QPixmap) {
return {};
}
if (size) {
*size = kDefaultSystemButtonIconSize;
}
const auto pixmap = qvariant_cast<QPixmap>(pixmapVar);
if (!requestedSize.isEmpty() && (pixmap.size() != requestedSize)) {
return pixmap.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
return pixmap;
}
FRAMELESSHELPER_END_NAMESPACE

View File

@ -0,0 +1,44 @@
/*
* MIT License
*
* Copyright (C) 2022 by wangwenx190 (Yuhang Zhao)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "framelesshelperquick_global.h"
#include <QtQuick/qquickimageprovider.h>
FRAMELESSHELPER_BEGIN_NAMESPACE
class FRAMELESSHELPER_QUICK_API FramelessHelperImageProvider : public QQuickImageProvider
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(FramelessHelperImageProvider)
public:
explicit FramelessHelperImageProvider();
~FramelessHelperImageProvider() override;
Q_NODISCARD QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize) override;
};
FRAMELESSHELPER_END_NAMESPACE

View File

@ -23,21 +23,47 @@
*/ */
#include "framelessquickhelper.h" #include "framelessquickhelper.h"
#include <QtQml/qqmlengine.h>
#include "framelesshelperimageprovider.h"
#include "framelessquickutils.h"
#include <framelesswindowsmanager.h> #include <framelesswindowsmanager.h>
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
static constexpr const char FRAMELESSHELPER_QUICK_URI[] = "org.wangwenx190.FramelessHelper";
FramelessQuickHelper::FramelessQuickHelper(QObject *parent) : QObject(parent) {} FramelessQuickHelper::FramelessQuickHelper(QObject *parent) : QObject(parent) {}
FramelessQuickHelper::~FramelessQuickHelper() = default; FramelessQuickHelper::~FramelessQuickHelper() = default;
void FramelessQuickHelper::registerTypes(QQmlEngine *engine)
{
Q_ASSERT(engine);
if (!engine) {
return;
}
engine->addImageProvider(QStringLiteral("framelesshelper"), new FramelessHelperImageProvider);
qmlRegisterSingletonType<FramelessQuickHelper>(FRAMELESSHELPER_QUICK_URI, 1, 0, "FramelessHelper", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
const auto framelessHelper = new FramelessQuickHelper;
return framelessHelper;
});
qmlRegisterSingletonType<FramelessQuickUtils>(FRAMELESSHELPER_QUICK_URI, 1, 0, "FramelessUtils", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
const auto framelessUtils = new FramelessQuickUtils;
return framelessUtils;
});
}
void FramelessQuickHelper::addWindow(QWindow *window) void FramelessQuickHelper::addWindow(QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
if (!window) { if (!window) {
return; return;
} }
FramelessWindowsManager::addWindow(window); FramelessWindowsManager::instance()->addWindow(window);
} }
void FramelessQuickHelper::removeWindow(QWindow *window) void FramelessQuickHelper::removeWindow(QWindow *window)
@ -46,7 +72,7 @@ void FramelessQuickHelper::removeWindow(QWindow *window)
if (!window) { if (!window) {
return; return;
} }
FramelessWindowsManager::removeWindow(window); FramelessWindowsManager::instance()->removeWindow(window);
} }
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -29,6 +29,7 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QWindow; class QWindow;
class QQmlEngine;
QT_END_NAMESPACE QT_END_NAMESPACE
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
@ -48,6 +49,8 @@ public:
explicit FramelessQuickHelper(QObject *parent = nullptr); explicit FramelessQuickHelper(QObject *parent = nullptr);
~FramelessQuickHelper() override; ~FramelessQuickHelper() override;
Q_INVOKABLE static void registerTypes(QQmlEngine *engine);
Q_INVOKABLE static void addWindow(QWindow *window); Q_INVOKABLE static void addWindow(QWindow *window);
Q_INVOKABLE static void removeWindow(QWindow *window); Q_INVOKABLE static void removeWindow(QWindow *window);
}; };

View File

@ -6,6 +6,8 @@ set(SOURCES
framelesswidgetshelper.cpp framelesswidgetshelper.cpp
framelesswidget.h framelesswidget.h
framelesswidget.cpp framelesswidget.cpp
framelessmainwindow.h
framelessmainwindow.cpp
) )
if(WIN32 AND NOT FRAMELESSHELPER_BUILD_STATIC) if(WIN32 AND NOT FRAMELESSHELPER_BUILD_STATIC)

View File

@ -0,0 +1,93 @@
/*
* MIT License
*
* Copyright (C) 2022 by wangwenx190 (Yuhang Zhao)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "framelessmainwindow.h"
#include "framelesswidgetshelper.h"
FRAMELESSHELPER_BEGIN_NAMESPACE
FramelessMainWindow::FramelessMainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{
m_helper.reset(new FramelessWidgetsHelper(this, this));
m_helper->initialize();
}
FramelessMainWindow::~FramelessMainWindow() = default;
bool FramelessMainWindow::isNormal() const
{
return m_helper->isNormal();
}
bool FramelessMainWindow::isZoomed() const
{
return m_helper->isZoomed();
}
void FramelessMainWindow::setTitleBarWidget(QWidget *widget)
{
m_helper->setTitleBarWidget(widget);
}
QWidget *FramelessMainWindow::titleBarWidget() const
{
return m_helper->titleBarWidget();
}
void FramelessMainWindow::setHitTestVisible(QWidget *widget, const bool visible)
{
m_helper->setHitTestVisible(widget, visible);
}
void FramelessMainWindow::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
m_helper->showEventHandler(event);
}
void FramelessMainWindow::changeEvent(QEvent *event)
{
QMainWindow::changeEvent(event);
m_helper->changeEventHandler(event);
}
void FramelessMainWindow::paintEvent(QPaintEvent *event)
{
QMainWindow::paintEvent(event);
m_helper->paintEventHandler(event);
}
void FramelessMainWindow::mousePressEvent(QMouseEvent *event)
{
QMainWindow::mousePressEvent(event);
m_helper->mousePressEventHandler(event);
}
void FramelessMainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
QMainWindow::mouseDoubleClickEvent(event);
m_helper->mouseDoubleClickEventHandler(event);
}
FRAMELESSHELPER_END_NAMESPACE

View File

@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (C) 2022 by wangwenx190 (Yuhang Zhao)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "framelesshelperwidgets_global.h"
#include <QtWidgets/qmainwindow.h>
FRAMELESSHELPER_BEGIN_NAMESPACE
class FramelessWidgetsHelper;
class FRAMELESSHELPER_WIDGETS_API FramelessMainWindow : public QMainWindow
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(FramelessMainWindow)
Q_PROPERTY(QWidget* titleBarWidget READ titleBarWidget WRITE setTitleBarWidget NOTIFY titleBarWidgetChanged FINAL)
public:
explicit FramelessMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = {});
~FramelessMainWindow() override;
Q_NODISCARD bool isNormal() const;
Q_NODISCARD bool isZoomed() const;
void setTitleBarWidget(QWidget *widget);
Q_NODISCARD QWidget *titleBarWidget() const;
Q_INVOKABLE void setHitTestVisible(QWidget *widget, const bool visible);
protected:
void showEvent(QShowEvent *event) override;
void changeEvent(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
Q_SIGNALS:
void titleBarWidgetChanged();
void systemThemeChanged();
void systemMenuRequested(const QPointF &);
private:
QScopedPointer<FramelessWidgetsHelper> m_helper;
};
FRAMELESSHELPER_END_NAMESPACE

View File

@ -34,6 +34,8 @@
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
static constexpr const char QT_MAINWINDOW_CLASS_NAME[] = "QMainWindow";
static const QString kSystemButtonStyleSheet = QStringLiteral(R"( static const QString kSystemButtonStyleSheet = QStringLiteral(R"(
QPushButton { QPushButton {
border-style: none; border-style: none;
@ -88,12 +90,16 @@ void FramelessWidgetsHelper::setTitleBarWidget(QWidget *widget)
if (m_systemTitleBarWidget && m_systemTitleBarWidget->isVisible()) { if (m_systemTitleBarWidget && m_systemTitleBarWidget->isVisible()) {
m_systemTitleBarWidget->hide(); m_systemTitleBarWidget->hide();
} }
if (m_userTitleBarWidget) { if (isMainWindow()) {
m_mainLayout->removeWidget(m_userTitleBarWidget); m_userTitleBarWidget = widget;
m_userTitleBarWidget = nullptr; } else {
if (m_userTitleBarWidget) {
m_mainLayout->removeWidget(m_userTitleBarWidget);
m_userTitleBarWidget = nullptr;
}
m_userTitleBarWidget = widget;
m_mainLayout->insertWidget(0, m_userTitleBarWidget);
} }
m_userTitleBarWidget = widget;
m_mainLayout->insertWidget(0, m_userTitleBarWidget);
QMetaObject::invokeMethod(q, "titleBarWidgetChanged"); QMetaObject::invokeMethod(q, "titleBarWidgetChanged");
} }
@ -108,6 +114,9 @@ void FramelessWidgetsHelper::setContentWidget(QWidget *widget)
if (!widget) { if (!widget) {
return; return;
} }
if (isMainWindow()) {
return;
}
if (m_userContentWidget == widget) { if (m_userContentWidget == widget) {
return; return;
} }
@ -122,6 +131,9 @@ void FramelessWidgetsHelper::setContentWidget(QWidget *widget)
QWidget *FramelessWidgetsHelper::contentWidget() const QWidget *FramelessWidgetsHelper::contentWidget() const
{ {
if (isMainWindow()) {
return nullptr;
}
return m_userContentWidget; return m_userContentWidget;
} }
@ -221,8 +233,8 @@ void FramelessWidgetsHelper::setupFramelessHelperOnce()
return; return;
} }
m_framelessHelperInited = true; m_framelessHelperInited = true;
FramelessWindowsManager::addWindow(q->windowHandle()); FramelessWindowsManager *manager = FramelessWindowsManager::instance();
const FramelessWindowsManager * const manager = FramelessWindowsManager::instance(); manager->addWindow(q->windowHandle());
connect(manager, &FramelessWindowsManager::systemThemeChanged, this, [this](){ connect(manager, &FramelessWindowsManager::systemThemeChanged, this, [this](){
updateSystemTitleBarStyleSheet(); updateSystemTitleBarStyleSheet();
updateSystemButtonsIcon(); updateSystemButtonsIcon();
@ -287,6 +299,9 @@ void FramelessWidgetsHelper::createSystemTitleBar()
void FramelessWidgetsHelper::createUserContentContainer() void FramelessWidgetsHelper::createUserContentContainer()
{ {
if (isMainWindow()) {
return;
}
m_userContentContainerWidget = new QWidget(q); m_userContentContainerWidget = new QWidget(q);
m_userContentContainerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_userContentContainerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_userContentContainerLayout = new QVBoxLayout(m_userContentContainerWidget); m_userContentContainerLayout = new QVBoxLayout(m_userContentContainerWidget);
@ -298,13 +313,16 @@ void FramelessWidgetsHelper::createUserContentContainer()
void FramelessWidgetsHelper::setupInitialUi() void FramelessWidgetsHelper::setupInitialUi()
{ {
createSystemTitleBar(); createSystemTitleBar();
createUserContentContainer(); if (isMainWindow()) {
m_mainLayout = new QVBoxLayout(q); } else {
m_mainLayout->setContentsMargins(0, 0, 0, 0); createUserContentContainer();
m_mainLayout->setSpacing(0); m_mainLayout = new QVBoxLayout(q);
m_mainLayout->addWidget(m_systemTitleBarWidget); m_mainLayout->setContentsMargins(0, 0, 0, 0);
m_mainLayout->addWidget(m_userContentContainerWidget); m_mainLayout->setSpacing(0);
q->setLayout(m_mainLayout); m_mainLayout->addWidget(m_systemTitleBarWidget);
m_mainLayout->addWidget(m_userContentContainerWidget);
q->setLayout(m_mainLayout);
}
updateSystemTitleBarStyleSheet(); updateSystemTitleBarStyleSheet();
updateContentsMargins(); updateContentsMargins();
} }
@ -316,7 +334,10 @@ bool FramelessWidgetsHelper::isInTitleBarDraggableArea(const QPoint &pos) const
QRegion region = {QRect(QPoint(0, 0), m_userTitleBarWidget->size())}; QRegion region = {QRect(QPoint(0, 0), m_userTitleBarWidget->size())};
if (!m_hitTestVisibleWidgets.isEmpty()) { if (!m_hitTestVisibleWidgets.isEmpty()) {
for (auto &&widget : qAsConst(m_hitTestVisibleWidgets)) { for (auto &&widget : qAsConst(m_hitTestVisibleWidgets)) {
region -= widget->geometry(); Q_ASSERT(widget);
if (widget) {
region -= widget->geometry();
}
} }
} }
return region; return region;
@ -340,6 +361,14 @@ bool FramelessWidgetsHelper::shouldDrawFrameBorder() const
#endif #endif
} }
bool FramelessWidgetsHelper::isMainWindow() const
{
if (!q) {
return false;
}
return q->inherits(QT_MAINWINDOW_CLASS_NAME);
}
void FramelessWidgetsHelper::updateContentsMargins() void FramelessWidgetsHelper::updateContentsMargins()
{ {
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
@ -383,13 +412,13 @@ void FramelessWidgetsHelper::updateSystemTitleBarStyleSheet()
void FramelessWidgetsHelper::updateSystemButtonsIcon() void FramelessWidgetsHelper::updateSystemButtonsIcon()
{ {
const SystemTheme theme = ((Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized()) ? SystemTheme::Dark : SystemTheme::Light); const SystemTheme theme = ((Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized()) ? SystemTheme::Dark : SystemTheme::Light);
m_systemMinimizeButton->setIcon(Utils::getSystemButtonIcon(SystemButtonType::Minimize, theme)); m_systemMinimizeButton->setIcon(qvariant_cast<QIcon>(Utils::getSystemButtonIconResource(SystemButtonType::Minimize, theme, ResourceType::Icon)));
if (isZoomed()) { if (isZoomed()) {
m_systemMaximizeButton->setIcon(Utils::getSystemButtonIcon(SystemButtonType::Restore, theme)); m_systemMaximizeButton->setIcon(qvariant_cast<QIcon>(Utils::getSystemButtonIconResource(SystemButtonType::Restore, theme, ResourceType::Icon)));
} else { } else {
m_systemMaximizeButton->setIcon(Utils::getSystemButtonIcon(SystemButtonType::Maximize, theme)); m_systemMaximizeButton->setIcon(qvariant_cast<QIcon>(Utils::getSystemButtonIconResource(SystemButtonType::Maximize, theme, ResourceType::Icon)));
} }
m_systemCloseButton->setIcon(Utils::getSystemButtonIcon(SystemButtonType::Close, theme)); m_systemCloseButton->setIcon(qvariant_cast<QIcon>(Utils::getSystemButtonIconResource(SystemButtonType::Close, theme, ResourceType::Icon)));
} }
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -74,6 +74,7 @@ private:
void setupInitialUi(); void setupInitialUi();
Q_NODISCARD bool isInTitleBarDraggableArea(const QPoint &pos) const; Q_NODISCARD bool isInTitleBarDraggableArea(const QPoint &pos) const;
Q_NODISCARD bool shouldDrawFrameBorder() const; Q_NODISCARD bool shouldDrawFrameBorder() const;
Q_NODISCARD bool isMainWindow() const;
private Q_SLOTS: private Q_SLOTS:
void updateContentsMargins(); void updateContentsMargins();