From a10ac4e32d3737d7bfb9dc734bb66427865f03ee Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sun, 8 May 2022 18:24:09 +0800 Subject: [PATCH] demos: simplify some code Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/mainwindow/main.cpp | 5 +++ examples/mainwindow/mainwindow.cpp | 10 ----- examples/mainwindow/mainwindow.h | 3 -- examples/openglwidget/main.cpp | 7 ++++ examples/openglwidget/mainwindow.cpp | 10 ----- examples/openglwidget/mainwindow.h | 3 -- examples/quick/MainWindow.qml | 1 - examples/quick/main.cpp | 5 +++ examples/widget/main.cpp | 5 +++ examples/widget/widget.cpp | 10 ----- examples/widget/widget.h | 1 - .../Core/framelesshelpercore_global.h | 3 +- src/core/framelessconfig.cpp | 14 +++++-- src/quick/framelessquickhelper.cpp | 41 ++++++++++++------- src/widgets/framelesswidgetshelper.cpp | 14 +++++++ 15 files changed, 75 insertions(+), 57 deletions(-) diff --git a/examples/mainwindow/main.cpp b/examples/mainwindow/main.cpp index 410935c..e2f6e88 100644 --- a/examples/mainwindow/main.cpp +++ b/examples/mainwindow/main.cpp @@ -23,10 +23,13 @@ */ #include +#include #include "mainwindow.h" FRAMELESSHELPER_USE_NAMESPACE +using namespace Global; + int main(int argc, char *argv[]) { // Not necessary, but better call this function, before the construction @@ -35,6 +38,8 @@ int main(int argc, char *argv[]) QApplication application(argc, argv); + FramelessConfig::instance()->set(Option::CenterWindowBeforeShow); + MainWindow mainWindow; mainWindow.show(); diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index b6a831e..42ae850 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -41,16 +41,6 @@ MainWindow::MainWindow(QWidget *parent, const Qt::WindowFlags flags) : Frameless MainWindow::~MainWindow() = default; -void MainWindow::showEvent(QShowEvent *event) -{ - FramelessMainWindow::showEvent(event); - static bool exposed = false; - if (!exposed) { - exposed = true; - FramelessWidgetsHelper::get(this)->moveWindowToDesktopCenter(); - } -} - void MainWindow::initialize() { m_titleBar.reset(new StandardTitleBar(this)); diff --git a/examples/mainwindow/mainwindow.h b/examples/mainwindow/mainwindow.h index 8efbf01..22e377a 100644 --- a/examples/mainwindow/mainwindow.h +++ b/examples/mainwindow/mainwindow.h @@ -44,9 +44,6 @@ public: explicit MainWindow(QWidget *parent = nullptr, const Qt::WindowFlags flags = {}); ~MainWindow() override; -protected: - void showEvent(QShowEvent *event) override; - private: void initialize(); diff --git a/examples/openglwidget/main.cpp b/examples/openglwidget/main.cpp index 6777860..51739c6 100644 --- a/examples/openglwidget/main.cpp +++ b/examples/openglwidget/main.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include "mainwindow.h" // This example demonstrates easy, cross-platform usage of OpenGL ES 3.0 functions via @@ -65,12 +66,18 @@ FRAMELESSHELPER_USE_NAMESPACE +using namespace Global; + int main(int argc, char *argv[]) { + // Not necessary, but better call this function, before the construction + // of any Q(Core|Gui)Application instances. FramelessHelper::Core::initialize(); QApplication application(argc, argv); + FramelessConfig::instance()->set(Option::CenterWindowBeforeShow); + QSurfaceFormat fmt = {}; fmt.setDepthBufferSize(24); diff --git a/examples/openglwidget/mainwindow.cpp b/examples/openglwidget/mainwindow.cpp index bf17d36..907621d 100644 --- a/examples/openglwidget/mainwindow.cpp +++ b/examples/openglwidget/mainwindow.cpp @@ -40,16 +40,6 @@ MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent) MainWindow::~MainWindow() = default; -void MainWindow::showEvent(QShowEvent *event) -{ - FramelessWidget::showEvent(event); - static bool exposed = false; - if (!exposed) { - exposed = true; - FramelessWidgetsHelper::get(this)->moveWindowToDesktopCenter(); - } -} - void MainWindow::initialize() { resize(800, 600); diff --git a/examples/openglwidget/mainwindow.h b/examples/openglwidget/mainwindow.h index 4c53a8d..cdbabe6 100644 --- a/examples/openglwidget/mainwindow.h +++ b/examples/openglwidget/mainwindow.h @@ -41,9 +41,6 @@ public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow() override; -protected: - void showEvent(QShowEvent *event) override; - private: void initialize(); diff --git a/examples/quick/MainWindow.qml b/examples/quick/MainWindow.qml index 19d2200..7761948 100644 --- a/examples/quick/MainWindow.qml +++ b/examples/quick/MainWindow.qml @@ -29,7 +29,6 @@ import org.wangwenx190.FramelessHelper 1.0 FramelessWindow { id: window - visible: true // Default is false, so won't be visible unless explicitly set to true. width: 800 height: 600 title: qsTr("FramelessHelper demo application - Qt Quick") diff --git a/examples/quick/main.cpp b/examples/quick/main.cpp index 6604ae8..d86ae51 100644 --- a/examples/quick/main.cpp +++ b/examples/quick/main.cpp @@ -27,9 +27,12 @@ #include #include #include +#include FRAMELESSHELPER_USE_NAMESPACE +using namespace Global; + int main(int argc, char *argv[]) { // Not necessary, but better call this function, before the construction @@ -38,6 +41,8 @@ int main(int argc, char *argv[]) QGuiApplication application(argc, argv); + FramelessConfig::instance()->set(Option::CenterWindowBeforeShow); + // Allow testing other RHI backends through environment variable. if (!qEnvironmentVariableIsSet("QSG_RHI_BACKEND")) { // This line is not relevant to FramelessHelper, we change diff --git a/examples/widget/main.cpp b/examples/widget/main.cpp index 49c3d96..57033da 100644 --- a/examples/widget/main.cpp +++ b/examples/widget/main.cpp @@ -23,10 +23,13 @@ */ #include +#include #include "widget.h" FRAMELESSHELPER_USE_NAMESPACE +using namespace Global; + int main(int argc, char *argv[]) { // Not necessary, but better call this function, before the construction @@ -35,6 +38,8 @@ int main(int argc, char *argv[]) QApplication application(argc, argv); + FramelessConfig::instance()->set(Option::CenterWindowBeforeShow); + Widget widget; widget.show(); diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index 489a086..f37953f 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -53,16 +53,6 @@ void Widget::timerEvent(QTimerEvent *event) } } -void Widget::showEvent(QShowEvent *event) -{ - FramelessWidget::showEvent(event); - static bool exposed = false; - if (!exposed) { - exposed = true; - FramelessWidgetsHelper::get(this)->moveWindowToDesktopCenter(); - } -} - void Widget::initialize() { setWindowTitle(tr("FramelessHelper demo application - Qt Widgets")); diff --git a/examples/widget/widget.h b/examples/widget/widget.h index 7454c88..4002a7e 100644 --- a/examples/widget/widget.h +++ b/examples/widget/widget.h @@ -45,7 +45,6 @@ public: protected: void timerEvent(QTimerEvent *event) override; - void showEvent(QShowEvent *event) override; private: void initialize(); diff --git a/include/FramelessHelper/Core/framelesshelpercore_global.h b/include/FramelessHelper/Core/framelesshelpercore_global.h index ef2034d..27500f3 100644 --- a/include/FramelessHelper/Core/framelesshelpercore_global.h +++ b/include/FramelessHelper/Core/framelesshelpercore_global.h @@ -189,7 +189,8 @@ enum class Option ForceHideWindowFrameBorder = 1, ForceShowWindowFrameBorder = 2, DisableWindowsSnapLayouts = 3, - WindowUseRoundCorners = 4 + WindowUseRoundCorners = 4, + CenterWindowBeforeShow = 5 }; Q_ENUM_NS(Option) diff --git a/src/core/framelessconfig.cpp b/src/core/framelessconfig.cpp index e9b12fa..91dbfb9 100644 --- a/src/core/framelessconfig.cpp +++ b/src/core/framelessconfig.cpp @@ -43,7 +43,8 @@ static constexpr const struct {"FRAMELESSHELPER_FORCE_HIDE_WINDOW_FRAME_BORDER", "Options/ForceHideWindowFrameBorder"}, {"FRAMELESSHELPER_FORCE_SHOW_WINDOW_FRAME_BORDER", "Options/ForceShowWindowFrameBorder"}, {"FRAMELESSHELPER_DISABLE_WINDOWS_SNAP_LAYOUTS", "Options/DisableWindowsSnapLayouts"}, - {"FRAMELESSHELPER_WINDOW_USE_ROUND_CORNERS", "Options/WindowUseRoundCorners"} + {"FRAMELESSHELPER_WINDOW_USE_ROUND_CORNERS", "Options/WindowUseRoundCorners"}, + {"FRAMELESSHELPER_CENTER_WINDOW_BEFORE_SHOW", "Options/CenterWindowBeforeShow"} }; static constexpr const auto OptionCount = std::size(OptionsTable); @@ -77,11 +78,16 @@ void FramelessConfig::reload(const bool force) if (g_data()->loaded && !force) { return; } - const QDir appDir(QCoreApplication::applicationDirPath()); - const QSettings configFile(appDir.filePath(kConfigFileName), QSettings::IniFormat); + const QScopedPointer configFile([]() -> QSettings * { + if (!QCoreApplication::instance()) { + return nullptr; + } + const QDir appDir(QCoreApplication::applicationDirPath()); + return new QSettings(appDir.filePath(kConfigFileName), QSettings::IniFormat); + }()); for (int i = 0; i != OptionCount; ++i) { const bool on = (qEnvironmentVariableIsSet(OptionsTable[i].env) && (qEnvironmentVariableIntValue(OptionsTable[i].env) > 0)) - || (configFile.value(QUtf8String(OptionsTable[i].ini), false).toBool()); + || (!configFile.isNull() && configFile->value(QUtf8String(OptionsTable[i].ini), false).toBool()); g_data()->options[i] = on; } g_data()->loaded = true; diff --git a/src/quick/framelessquickhelper.cpp b/src/quick/framelessquickhelper.cpp index 130b0f8..08b6a06 100644 --- a/src/quick/framelessquickhelper.cpp +++ b/src/quick/framelessquickhelper.cpp @@ -25,6 +25,7 @@ #include "framelessquickhelper.h" #include "framelessquickhelper_p.h" #include +#include #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) # include // For QWINDOWSIZE_MAX #else @@ -33,6 +34,7 @@ #include #include #include +#include #include FRAMELESSHELPER_BEGIN_NAMESPACE @@ -173,21 +175,21 @@ void FramelessQuickHelperPrivate::attachToWindow() } SystemParameters params = {}; - params.getWindowId = [window]() -> WId { return window->winId(); }; - params.getWindowFlags = [window]() -> Qt::WindowFlags { return window->flags(); }; - params.setWindowFlags = [window](const Qt::WindowFlags flags) -> void { window->setFlags(flags); }; - params.getWindowSize = [window]() -> QSize { return window->size(); }; - params.setWindowSize = [window](const QSize &size) -> void { window->resize(size); }; - params.getWindowPosition = [window]() -> QPoint { return window->position(); }; - params.setWindowPosition = [window](const QPoint &pos) -> void { window->setX(pos.x()); window->setY(pos.y()); }; - params.getWindowScreen = [window]() -> QScreen * { return window->screen(); }; + params.getWindowId = [q]() -> WId { return q->window()->winId(); }; + params.getWindowFlags = [q]() -> Qt::WindowFlags { return q->window()->flags(); }; + params.setWindowFlags = [q](const Qt::WindowFlags flags) -> void { q->window()->setFlags(flags); }; + params.getWindowSize = [q]() -> QSize { return q->window()->size(); }; + params.setWindowSize = [q](const QSize &size) -> void { q->window()->resize(size); }; + params.getWindowPosition = [q]() -> QPoint { return q->window()->position(); }; + params.setWindowPosition = [q](const QPoint &pos) -> void { q->window()->setX(pos.x()); q->window()->setY(pos.y()); }; + params.getWindowScreen = [q]() -> QScreen * { return q->window()->screen(); }; params.isWindowFixedSize = [this]() -> bool { return isWindowFixedSize(); }; params.setWindowFixedSize = [this](const bool value) -> void { setWindowFixedSize(value); }; - params.getWindowState = [window]() -> Qt::WindowState { return window->windowState(); }; - params.setWindowState = [window](const Qt::WindowState state) -> void { window->setWindowState(state); }; + params.getWindowState = [q]() -> Qt::WindowState { return q->window()->windowState(); }; + params.setWindowState = [q](const Qt::WindowState state) -> void { q->window()->setWindowState(state); }; params.getWindowHandle = [q]() -> QWindow * { return q->window(); }; - params.windowToScreen = [window](const QPoint &pos) -> QPoint { return window->mapToGlobal(pos); }; - params.screenToWindow = [window](const QPoint &pos) -> QPoint { return window->mapFromGlobal(pos); }; + params.windowToScreen = [q](const QPoint &pos) -> QPoint { return q->window()->mapToGlobal(pos); }; + params.screenToWindow = [q](const QPoint &pos) -> QPoint { return q->window()->mapFromGlobal(pos); }; params.isInsideSystemButtons = [this](const QPoint &pos, SystemButtonType *button) -> bool { QuickGlobal::SystemButtonType button2 = QuickGlobal::SystemButtonType::Unknown; const bool result = isInSystemButtons(pos, &button2); @@ -195,7 +197,7 @@ void FramelessQuickHelperPrivate::attachToWindow() return result; }; params.isInsideTitleBarDraggableArea = [this](const QPoint &pos) -> bool { return isInTitleBarDraggableArea(pos); }; - params.getWindowDevicePixelRatio = [window]() -> qreal { return window->effectiveDevicePixelRatio(); }; + params.getWindowDevicePixelRatio = [q]() -> qreal { return q->window()->effectiveDevicePixelRatio(); }; params.setSystemButtonState = [this](const SystemButtonType button, const ButtonState state) -> void { setSystemButtonState(FRAMELESSHELPER_ENUM_CORE_TO_QUICK(SystemButtonType, button), FRAMELESSHELPER_ENUM_CORE_TO_QUICK(ButtonState, state)); @@ -209,6 +211,18 @@ void FramelessQuickHelperPrivate::attachToWindow() g_quickHelper()->mutex.unlock(); FramelessManager::instance()->addWindow(params); + + // We have to wait for a little time before moving the top level window + // , because the platform window may not finish initializing by the time + // we reach here, and all the modifications from the Qt side will be lost + // due to QPA will reset the position and size of the window during it's + // initialization process. + QTimer::singleShot(0, this, [this, window](){ + if (FramelessConfig::instance()->isSet(Option::CenterWindowBeforeShow)) { + moveWindowToDesktopCenter(); + } + window->setVisible(true); + }); } void FramelessQuickHelperPrivate::setSystemButton(QQuickItem *item, const QuickGlobal::SystemButtonType buttonType) @@ -726,7 +740,6 @@ void FramelessQuickHelper::itemChange(const ItemChange change, const ItemChangeD } Q_D(FramelessQuickHelper); d->attachToWindow(); - d->moveWindowToDesktopCenter(); // Temp hack } } diff --git a/src/widgets/framelesswidgetshelper.cpp b/src/widgets/framelesswidgetshelper.cpp index 8d1bc3a..7b96a68 100644 --- a/src/widgets/framelesswidgetshelper.cpp +++ b/src/widgets/framelesswidgetshelper.cpp @@ -27,9 +27,11 @@ #include #include #include +#include #include #include #include +#include #include FRAMELESSHELPER_BEGIN_NAMESPACE @@ -238,6 +240,18 @@ void FramelessWidgetsHelperPrivate::attachToWindow() g_widgetsHelper()->mutex.unlock(); FramelessManager::instance()->addWindow(params); + + // We have to wait for a little time before moving the top level window + // , because the platform window may not finish initializing by the time + // we reach here, and all the modifications from the Qt side will be lost + // due to QPA will reset the position and size of the window during it's + // initialization process. + QTimer::singleShot(0, this, [this, window](){ + if (FramelessConfig::instance()->isSet(Option::CenterWindowBeforeShow)) { + moveWindowToDesktopCenter(); + } + window->setVisible(true); + }); } QWidget *FramelessWidgetsHelperPrivate::getWindow() const