demos: simplify some code

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-05-08 18:24:09 +08:00
parent c853cc63c6
commit a10ac4e32d
15 changed files with 75 additions and 57 deletions

View File

@ -23,10 +23,13 @@
*/
#include <QtWidgets/qapplication.h>
#include <framelessconfig_p.h>
#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();

View File

@ -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));

View File

@ -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();

View File

@ -53,6 +53,7 @@
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <framelesshelpercore_global.h>
#include <framelessconfig_p.h>
#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);

View File

@ -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);

View File

@ -41,9 +41,6 @@ public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
protected:
void showEvent(QShowEvent *event) override;
private:
void initialize();

View File

@ -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")

View File

@ -27,9 +27,12 @@
#include <QtQuick/qquickwindow.h>
#include <QtQuickControls2/qquickstyle.h>
#include <framelessquickmodule.h>
#include <framelessconfig_p.h>
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

View File

@ -23,10 +23,13 @@
*/
#include <QtWidgets/qapplication.h>
#include <framelessconfig_p.h>
#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();

View File

@ -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"));

View File

@ -45,7 +45,6 @@ public:
protected:
void timerEvent(QTimerEvent *event) override;
void showEvent(QShowEvent *event) override;
private:
void initialize();

View File

@ -189,7 +189,8 @@ enum class Option
ForceHideWindowFrameBorder = 1,
ForceShowWindowFrameBorder = 2,
DisableWindowsSnapLayouts = 3,
WindowUseRoundCorners = 4
WindowUseRoundCorners = 4,
CenterWindowBeforeShow = 5
};
Q_ENUM_NS(Option)

View File

@ -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<QSettings> 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;

View File

@ -25,6 +25,7 @@
#include "framelessquickhelper.h"
#include "framelessquickhelper_p.h"
#include <QtCore/qmutex.h>
#include <QtCore/qtimer.h>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
# include <QtGui/qpa/qplatformwindow.h> // For QWINDOWSIZE_MAX
#else
@ -33,6 +34,7 @@
#include <QtQuick/qquickwindow.h>
#include <QtQuickTemplates2/private/qquickabstractbutton_p.h>
#include <framelessmanager.h>
#include <framelessconfig_p.h>
#include <utils.h>
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
}
}

View File

@ -27,9 +27,11 @@
#include <QtCore/qmutex.h>
#include <QtCore/qhash.h>
#include <QtCore/qpointer.h>
#include <QtCore/qtimer.h>
#include <QtGui/qwindow.h>
#include <QtWidgets/qwidget.h>
#include <framelessmanager.h>
#include <framelessconfig_p.h>
#include <utils.h>
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