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>
This commit is contained in:
Yuhang Zhao 2021-03-25 15:12:47 +08:00
parent 3e09c0f9b8
commit 4cba3a4dd4
7 changed files with 79 additions and 12 deletions

View File

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

View File

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

View File

@ -23,7 +23,6 @@
*/
#include "widget.h"
#include <QtGui/qscreen.h>
#include <QtWidgets/qlayout.h>
#include <QtWidgets/qlabel.h>
#include <QtCore/qdatetime.h>
@ -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 {

View File

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

View File

@ -44,7 +44,7 @@ enum class DesktopWallpaperAspectStyle
IgnoreRatioFit, // Stretch
KeepRatioFit, // Fit
KeepRatioByExpanding, // Fill
Span // Span
Span
};
// Common

25
utilities_linux.cpp Normal file
View File

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

25
utilities_macos.mm Normal file
View File

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