/* * MIT License * * Copyright (C) 2022 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 "framelessquickhelper.h" #if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 1)) # include # include #endif #include "framelesswindowsmanager.h" #include "utilities.h" #ifdef Q_OS_WINDOWS # include "framelesshelper_windows.h" #endif FRAMELESSHELPER_BEGIN_NAMESPACE FramelessQuickHelper::FramelessQuickHelper(QObject *parent) : QObject(parent) {} FramelessQuickHelper::~FramelessQuickHelper() = default; void FramelessQuickHelper::addWindow(QWindow *window) { Q_ASSERT(window); if (!window) { return; } FramelessWindowsManager::addWindow(window); } void FramelessQuickHelper::removeWindow(QWindow *window) { Q_ASSERT(window); if (!window) { return; } FramelessWindowsManager::removeWindow(window); } FramelessQuickUtils::FramelessQuickUtils(QObject *parent) : QObject(parent) { connect(FramelessWindowsManager::instance(), &FramelessWindowsManager::themeChanged, this, [this](){ Q_EMIT frameBorderActiveColorChanged(); Q_EMIT frameBorderInactiveColorChanged(); Q_EMIT darkModeEnabledChanged(); Q_EMIT systemAccentColorChanged(); Q_EMIT titleBarColorVisibleChanged(); }); } FramelessQuickUtils::~FramelessQuickUtils() = default; qreal FramelessQuickUtils::titleBarHeight() { return 30; } bool FramelessQuickUtils::frameBorderVisible() { #ifdef Q_OS_WINDOWS return (Utilities::isWindowFrameBorderVisible() && !Utilities::isWin11OrGreater()); #else return false; #endif } qreal FramelessQuickUtils::frameBorderThickness() { return 1; } QColor FramelessQuickUtils::frameBorderActiveColor() { #ifdef Q_OS_WINDOWS return Utilities::getFrameBorderColor(true); #else return {}; #endif } QColor FramelessQuickUtils::frameBorderInactiveColor() { #ifdef Q_OS_WINDOWS return Utilities::getFrameBorderColor(false); #else return {}; #endif } bool FramelessQuickUtils::darkModeEnabled() { #if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 1)) if (const QPlatformTheme * const theme = QGuiApplicationPrivate::platformTheme()) { return (theme->appearance() == QPlatformTheme::Appearance::Dark); } return false; #else # ifdef Q_OS_WINDOWS return Utilities::shouldAppsUseDarkMode(); # else return false; # endif #endif } QColor FramelessQuickUtils::systemAccentColor() { #ifdef Q_OS_WINDOWS return Utilities::getDwmColorizationColor(); #else return {}; #endif } bool FramelessQuickUtils::titleBarColorVisible() { #ifdef Q_OS_WINDOWS if (!Utilities::isWin10OrGreater()) { return false; } const DwmColorizationArea area = Utilities::getDwmColorizationArea(); return ((area == DwmColorizationArea::TitleBar_WindowBorder) || (area == DwmColorizationArea::All)); #else return false; #endif } void FramelessQuickUtils::showMinimized2(QWindow *window) { Q_ASSERT(window); if (!window) { return; } #ifdef Q_OS_WINDOWS // Work-around a QtQuick bug: https://bugreports.qt.io/browse/QTBUG-69711 // Don't use "SW_SHOWMINIMIZED" because it will activate the current window // instead of the next window in the Z order, which is not the default behavior // of native Win32 applications. ShowWindow(reinterpret_cast(window->winId()), SW_MINIMIZE); #else window->showMinimized(); #endif } void FramelessQuickUtils::showSystemMenu(QWindow *window, const QPointF &pos) { Q_ASSERT(window); if (!window) { return; } #ifdef Q_OS_WINDOWS # if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) const QPointF globalPos = window->mapToGlobal(pos) * window->devicePixelRatio(); # else const QPointF globalPos = QPointF(window->mapToGlobal(pos.toPoint())) * window->devicePixelRatio(); # endif Utilities::showSystemMenu(window->winId(), globalPos); #endif } void FramelessQuickUtils::startSystemMove2(QWindow *window) { Q_ASSERT(window); if (!window) { return; } #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) window->startSystemMove(); #else Utilities::startSystemMove(window); #endif } void FramelessQuickUtils::startSystemResize2(QWindow *window, const Qt::Edges edges) { Q_ASSERT(window); if (!window) { return; } if (edges == Qt::Edges{}) { return; } #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) window->startSystemResize(edges); #else Utilities::startSystemResize(window, edges); #endif } FRAMELESSHELPER_END_NAMESPACE