From 4cba3a4dd41f503b2bc194ebc57cdc5eecc3207c Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Thu, 25 Mar 2021 15:12:47 +0800 Subject: [PATCH] Minor improvements 1. Update margins when maximized for the examples 2. Prepare for the UNIX version of utility functions Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- CMakeLists.txt | 5 +++++ examples/quick/qml/main.qml | 10 ++++++---- examples/widget/widget.cpp | 23 ++++++++++++++++------- examples/widget/widget.h | 1 + utilities.h | 2 +- utilities_linux.cpp | 25 +++++++++++++++++++++++++ utilities_macos.mm | 25 +++++++++++++++++++++++++ 7 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 utilities_linux.cpp create mode 100644 utilities_macos.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index 36b5159..af8a7bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,11 @@ else() framelesshelper.h framelesshelper.cpp ) + if(MACOS) + list(APPEND SOURCES utilities_macos.mm) + else() + list(APPEND SOURCES utilities_linux.cpp) + endif() endif() if(WIN32 AND BUILD_SHARED_LIBS) diff --git a/examples/quick/qml/main.qml b/examples/quick/qml/main.qml index 1b91619..6ea054f 100644 --- a/examples/quick/qml/main.qml +++ b/examples/quick/qml/main.qml @@ -35,6 +35,8 @@ Window { title: qsTr("Hello, World!") color: "transparent" + property int _flh_margin: ((window.visibility === Window.Maximized) || (window.visibility === Window.FullScreen)) ? 0 : (1 / Screen.devicePixelRatio) + FramelessHelper { id: framelessHelper } @@ -60,11 +62,11 @@ Window { color: "transparent" anchors { top: parent.top - topMargin: 1 + topMargin: window._flh_margin left: parent.left - leftMargin: 1 + leftMargin: window._flh_margin right: parent.right - rightMargin: 1 + rightMargin: window._flh_margin } Text { @@ -89,7 +91,7 @@ Window { MaximizeButton { id: maximizeButton - maximized: window.visibility === Window.Maximized + maximized: ((window.visibility === Window.Maximized) || (window.visibility === Window.FullScreen)) onClicked: { if (maximized) { window.showNormal() diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index 8495ca7..b748bd7 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -23,7 +23,6 @@ */ #include "widget.h" -#include #include #include #include @@ -43,11 +42,7 @@ Widget::~Widget() = default; void Widget::moveToDesktopCenter() { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - const QSize ss = screen()->size(); -#else - const QSize ss = QGuiApplication::primaryScreen()->size(); -#endif + const QSize ss = Utilities::getScreenGeometry(nullptr).size(); const int newX = (ss.width() - width()) / 2; const int newY = (ss.height() - height()) / 2; move(newX, newY); @@ -70,6 +65,20 @@ void Widget::timerEvent(QTimerEvent *event) m_label->setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss"))); } +void Widget::changeEvent(QEvent *event) +{ + QtAcrylicWidget::changeEvent(event); + if (event->type() == QEvent::WindowStateChange) { + if (isMaximized() || isFullScreen()) { + layout()->setContentsMargins(0, 0, 0, 0); + m_maximizeButton->setIcon(QIcon{QStringLiteral(":/images/button_restore_black.svg")}); + } else if (!isMinimized()) { + layout()->setContentsMargins(1, 1, 1, 1); + m_maximizeButton->setIcon(QIcon{QStringLiteral(":/images/button_maximize_black.svg")}); + } + } +} + void Widget::setupUi() { const QWindow *win = windowHandle(); @@ -87,7 +96,7 @@ void Widget::setupUi() m_maximizeButton->setIcon(QIcon{QStringLiteral(":/images/button_maximize_black.svg")}); m_maximizeButton->setIconSize(systemButtonSize); connect(m_maximizeButton, &QPushButton::clicked, this, [this](){ - if (isMaximized()) { + if (isMaximized() || isFullScreen()) { showNormal(); m_maximizeButton->setIcon(QIcon{QStringLiteral(":/images/button_maximize_black.svg")}); } else { diff --git a/examples/widget/widget.h b/examples/widget/widget.h index 575caee..c0f98d8 100644 --- a/examples/widget/widget.h +++ b/examples/widget/widget.h @@ -45,6 +45,7 @@ public: protected: void showEvent(QShowEvent *event) override; void timerEvent(QTimerEvent *event) override; + void changeEvent(QEvent *event) override; private: void setupUi(); diff --git a/utilities.h b/utilities.h index 000205b..d28a8b9 100644 --- a/utilities.h +++ b/utilities.h @@ -44,7 +44,7 @@ enum class DesktopWallpaperAspectStyle IgnoreRatioFit, // Stretch KeepRatioFit, // Fit KeepRatioByExpanding, // Fill - Span // Span + Span }; // Common diff --git a/utilities_linux.cpp b/utilities_linux.cpp new file mode 100644 index 0000000..53fb5a7 --- /dev/null +++ b/utilities_linux.cpp @@ -0,0 +1,25 @@ +/* + * MIT License + * + * Copyright (C) 2021 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 "utilities.h" diff --git a/utilities_macos.mm b/utilities_macos.mm new file mode 100644 index 0000000..53fb5a7 --- /dev/null +++ b/utilities_macos.mm @@ -0,0 +1,25 @@ +/* + * MIT License + * + * Copyright (C) 2021 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 "utilities.h"