demo: add shortcut to switch between fullscreen state

Can also help use better debugging the old style window frame
issue on Windows.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-08-21 15:38:16 +08:00
parent 1bce57445b
commit 6c060b3946
6 changed files with 90 additions and 1 deletions

View File

@ -28,6 +28,17 @@ import org.wangwenx190.FramelessHelper
import Demo
FramelessWindow {
property int __savedWindowState: Window.Windowed
function toggleFullScreen() {
if (window.visibility === Window.FullScreen) {
window.visibility = window.__savedWindowState;
} else {
window.__savedWindowState = window.visibility;
window.showFullScreen();
}
}
id: window
visible: false // Hide the window before we sets up it's correct size and position.
width: 800
@ -60,6 +71,22 @@ FramelessWindow {
window.visible = true;
}
Shortcut {
sequences: [ StandardKey.Cancel, StandardKey.Close, StandardKey.Quit ]
onActivated: {
if (window.visibility === Window.FullScreen) {
window.toggleFullScreen();
} else {
window.close();
}
}
}
Shortcut {
sequences: [ StandardKey.FullScreen, "ALT+RETURN" ]
onActivated: window.toggleFullScreen()
}
Timer {
interval: 500
running: true

View File

@ -28,6 +28,7 @@
#include <QtCore/qcoreapplication.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#include <QtGui/qshortcut.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qboxlayout.h>
#include <FramelessManager>
@ -100,6 +101,26 @@ void Widget::initialize()
setLayout(mainLayout);
updateStyleSheet();
m_cancelShortcut.reset(new QShortcut(this));
m_cancelShortcut->setKey(FRAMELESSHELPER_STRING_LITERAL("ESC"));
connect(m_cancelShortcut.data(), &QShortcut::activated, this, [this](){
if (isFullScreen()) {
Q_EMIT m_fullScreenShortcut->activated();
} else {
close();
}
});
m_fullScreenShortcut.reset(new QShortcut(this));
m_fullScreenShortcut->setKey(FRAMELESSHELPER_STRING_LITERAL("ALT+RETURN"));
connect(m_fullScreenShortcut.data(), &QShortcut::activated, this, [this](){
if (isFullScreen()) {
setWindowState(windowState() & ~Qt::WindowFullScreen);
} else {
showFullScreen();
}
});
FramelessWidgetsHelper *helper = FramelessWidgetsHelper::get(this);
helper->setTitleBarWidget(m_titleBar.data());
helper->setSystemButton(m_titleBar->minimizeButton(), SystemButtonType::Minimize);

View File

@ -28,6 +28,7 @@
QT_BEGIN_NAMESPACE
class QLabel;
class QShortcut;
QT_END_NAMESPACE
FRAMELESSHELPER_BEGIN_NAMESPACE
@ -56,4 +57,6 @@ private Q_SLOTS:
private:
QScopedPointer<QLabel> m_clockLabel;
QScopedPointer<FRAMELESSHELPER_PREPEND_NAMESPACE(StandardTitleBar)> m_titleBar;
QScopedPointer<QShortcut> m_fullScreenShortcut;
QScopedPointer<QShortcut> m_cancelShortcut;
};

View File

@ -71,6 +71,9 @@ public:
Q_NODISCARD bool isBlurBehindWindowEnabled() const;
void setBlurBehindWindowEnabled(const bool value, const QColor &color);
protected:
Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override;
private:
Q_NODISCARD QRect mapItemGeometryToScene(const QQuickItem * const item) const;
Q_NODISCARD bool isInSystemButtons(const QPoint &pos, QuickGlobal::SystemButtonType *button) const;

View File

@ -163,6 +163,8 @@ void FramelessQuickHelperPrivate::attachToWindow()
return;
}
window->installEventFilter(this);
SystemParameters params = {};
params.getWindowId = [window]() -> WId { return window->winId(); };
params.getWindowFlags = [window]() -> Qt::WindowFlags { return window->flags(); };
@ -335,7 +337,11 @@ void FramelessQuickHelperPrivate::bringWindowToFront()
window->show();
}
if (window->visibility() == QQuickWindow::Minimized) {
window->showNormal(); // ### FIXME: we should not show normal, we should restore the previous state.
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
window->setWindowStates(window->windowStates() & ~Qt::WindowMinimized);
#else
window->showNormal();
#endif
}
window->raise();
window->requestActivate();
@ -448,6 +454,29 @@ void FramelessQuickHelperPrivate::setBlurBehindWindowEnabled(const bool value, c
}
}
bool FramelessQuickHelperPrivate::eventFilter(QObject *object, QEvent *event)
{
Q_ASSERT(object);
Q_ASSERT(event);
if (!object || !event) {
return false;
}
#ifdef Q_OS_WINDOWS
if (!object->isWindowType()) {
return QObject::eventFilter(object, event);
}
if (event->type() != QEvent::WindowStateChange) {
return QObject::eventFilter(object, event);
}
const auto changeEvent = static_cast<QWindowStateChangeEvent *>(event);
if (Utils::windowStatesToWindowState(changeEvent->oldState()) == Qt::WindowFullScreen) {
const auto window = qobject_cast<QQuickWindow *>(object);
Utils::fixupQtInternals(window->winId());
}
#endif
return QObject::eventFilter(object, event);
}
QRect FramelessQuickHelperPrivate::mapItemGeometryToScene(const QQuickItem * const item) const
{
Q_ASSERT(item);

View File

@ -148,6 +148,12 @@ void WidgetsSharedHelper::changeEventHandler(QEvent *event)
QMetaObject::invokeMethod(m_targetWidget, "hiddenChanged");
QMetaObject::invokeMethod(m_targetWidget, "normalChanged");
QMetaObject::invokeMethod(m_targetWidget, "zoomedChanged");
#ifdef Q_OS_WINDOWS
const auto changeEvent = static_cast<QWindowStateChangeEvent *>(event);
if (Utils::windowStatesToWindowState(changeEvent->oldState()) == Qt::WindowFullScreen) {
Utils::fixupQtInternals(m_targetWidget->winId());
}
#endif
}
void WidgetsSharedHelper::paintEventHandler(QPaintEvent *event)