FluentUI/src/FluFrameless.cpp

522 lines
18 KiB
C++
Raw Normal View History

2024-03-28 19:18:56 +08:00
#include "FluFrameless.h"
#include <QQuickWindow>
#include <QGuiApplication>
#include <QScreen>
2024-04-03 13:24:30 +08:00
#include <QDateTime>
2024-04-23 13:41:26 +08:00
#include "FluTools.h"
2024-03-28 19:18:56 +08:00
#ifdef Q_OS_WIN
2024-04-11 14:51:43 +08:00
#pragma comment (lib, "user32.lib")
#pragma comment (lib, "dwmapi.lib")
2024-03-28 19:18:56 +08:00
#include <windows.h>
#include <windowsx.h>
#include <dwmapi.h>
2024-04-11 14:51:43 +08:00
2024-04-23 13:41:26 +08:00
2024-04-11 14:51:43 +08:00
static inline QByteArray qtNativeEventType() {
2024-03-28 19:18:56 +08:00
static const auto result = "windows_generic_MSG";
return result;
}
2024-04-11 14:51:43 +08:00
static inline bool isCompositionEnabled() {
typedef HRESULT (WINAPI *DwmIsCompositionEnabledPtr)(BOOL *pfEnabled);
2024-03-28 19:18:56 +08:00
HMODULE module = ::LoadLibraryW(L"dwmapi.dll");
2024-04-11 14:51:43 +08:00
if (module) {
2024-03-28 19:18:56 +08:00
BOOL composition_enabled = false;
DwmIsCompositionEnabledPtr dwm_is_composition_enabled;
2024-04-11 14:51:43 +08:00
dwm_is_composition_enabled = reinterpret_cast<DwmIsCompositionEnabledPtr>(::GetProcAddress(module, "DwmIsCompositionEnabled"));
if (dwm_is_composition_enabled) {
2024-03-28 19:18:56 +08:00
dwm_is_composition_enabled(&composition_enabled);
}
return composition_enabled;
}
return false;
}
2024-04-11 14:51:43 +08:00
2024-03-28 19:18:56 +08:00
#endif
2024-04-11 14:51:43 +08:00
bool containsCursorToItem(QQuickItem *item) {
if (!item || !item->isVisible()) {
return false;
}
auto point = QCursor::pos();
auto rect = QRectF(item->mapToGlobal(QPoint(0, 0)), item->size());
if (point.x() > rect.x() && point.x() < (rect.x() + rect.width()) && point.y() > rect.y() && point.y() < (rect.y() + rect.height())) {
return true;
}
return false;
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
FluFrameless::FluFrameless(QQuickItem *parent) : QQuickItem{parent} {
_fixSize = false;
_appbar = nullptr;
_maximizeButton = nullptr;
_minimizedButton = nullptr;
_closeButton = nullptr;
_topmost = false;
_disabled = false;
2024-04-23 13:41:26 +08:00
_isWindows11OrGreater = FluTools::getInstance()->isWindows11OrGreater();
2024-03-31 21:52:06 +08:00
}
2024-04-11 14:51:43 +08:00
FluFrameless::~FluFrameless() = default;
[[maybe_unused]] void FluFrameless::onDestruction() {
QGuiApplication::instance()->removeNativeEventFilter(this);
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
void FluFrameless::componentComplete() {
if (_disabled) {
2024-03-28 19:18:56 +08:00
return;
}
2024-04-17 15:11:14 +08:00
int w = window()->width();
int h = window()->height();
2024-03-28 19:18:56 +08:00
_current = window()->winId();
2024-04-11 14:51:43 +08:00
window()->setFlags((window()->flags()) | Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint | Qt::FramelessWindowHint);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (QQuickWindow::sceneGraphBackend() == "software") {
window()->setFlag(Qt::FramelessWindowHint, false);
2024-04-04 17:43:58 +08:00
}
#endif
2024-04-11 14:51:43 +08:00
if (!_fixSize) {
2024-03-28 19:18:56 +08:00
window()->setFlag(Qt::WindowMaximizeButtonHint);
}
window()->installEventFilter(this);
2024-04-11 14:51:43 +08:00
QGuiApplication::instance()->installNativeEventFilter(this);
if (_maximizeButton) {
2024-03-28 19:18:56 +08:00
setHitTestVisible(_maximizeButton);
}
2024-04-11 14:51:43 +08:00
if (_minimizedButton) {
2024-03-28 19:18:56 +08:00
setHitTestVisible(_minimizedButton);
}
2024-04-11 14:51:43 +08:00
if (_closeButton) {
2024-03-28 19:18:56 +08:00
setHitTestVisible(_closeButton);
}
#ifdef Q_OS_WIN
HWND hwnd = reinterpret_cast<HWND>(window()->winId());
DWORD style = ::GetWindowLongPtr(hwnd, GWL_STYLE);
2024-04-11 14:51:43 +08:00
if (_fixSize) {
2024-04-04 02:01:52 +08:00
::SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME);
2024-04-11 14:51:43 +08:00
for (int i = 0; i <= QGuiApplication::screens().count() - 1; ++i) {
connect(QGuiApplication::screens().at(i), &QScreen::logicalDotsPerInchChanged, this, [=] {
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_FRAMECHANGED);
2024-03-28 19:18:56 +08:00
});
}
2024-04-11 14:51:43 +08:00
} else {
2024-03-28 19:18:56 +08:00
::SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME);
}
2024-04-11 14:51:43 +08:00
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
connect(window(), &QQuickWindow::screenChanged, this, [hwnd] {
::SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOOWNERZORDER);
::RedrawWindow(hwnd, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW);
2024-03-28 19:18:56 +08:00
});
#endif
2024-04-17 15:11:14 +08:00
h = h + _appbar->height();
2024-04-23 13:41:26 +08:00
if (_fixSize) {
window()->setMaximumSize(QSize(w, h));
window()->setMinimumSize(QSize(w, h));
2024-04-17 15:11:14 +08:00
}
2024-04-23 13:41:26 +08:00
window()->resize(QSize(w, h));
2024-04-11 14:51:43 +08:00
connect(this, &FluFrameless::topmostChanged, this, [this] {
2024-03-28 19:18:56 +08:00
_setWindowTopmost(topmost());
});
_setWindowTopmost(topmost());
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] bool FluFrameless::nativeEventFilter(const QByteArray &eventType, void *message, QT_NATIVE_EVENT_RESULT_TYPE *result) {
2024-03-28 19:18:56 +08:00
#ifdef Q_OS_WIN
if ((eventType != qtNativeEventType()) || !message) {
return false;
}
const auto msg = static_cast<const MSG *>(message);
2024-04-11 14:51:43 +08:00
auto hwnd = msg->hwnd;
if (!hwnd) {
2024-03-28 19:18:56 +08:00
return false;
}
2024-04-11 14:51:43 +08:00
const quint64 wid = reinterpret_cast<qint64>(hwnd);
if (wid != _current) {
2024-03-28 19:18:56 +08:00
return false;
}
2024-04-11 14:51:43 +08:00
const auto uMsg = msg->message;
const auto wParam = msg->wParam;
const auto lParam = msg->lParam;
2024-03-28 19:18:56 +08:00
static QPoint offsetXY;
2024-04-11 14:51:43 +08:00
if (uMsg == WM_WINDOWPOSCHANGING) {
auto *wp = reinterpret_cast<WINDOWPOS *>(lParam);
if (wp != nullptr && (wp->flags & SWP_NOSIZE) == 0) {
2024-03-28 19:18:56 +08:00
wp->flags |= SWP_NOCOPYBITS;
*result = ::DefWindowProcW(hwnd, uMsg, wParam, lParam);
return true;
}
return false;
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_NCCALCSIZE) {
2024-03-28 19:18:56 +08:00
const auto clientRect = ((wParam == FALSE) ? reinterpret_cast<LPRECT>(lParam) : &(reinterpret_cast<LPNCCALCSIZE_PARAMS>(lParam))->rgrc[0]);
const LONG originalTop = clientRect->top;
const LONG originalLeft = clientRect->left;
const LONG originalRight = clientRect->right;
const LONG originalBottom = clientRect->bottom;
const LRESULT hitTestResult = ::DefWindowProcW(hwnd, WM_NCCALCSIZE, wParam, lParam);
if ((hitTestResult != HTERROR) && (hitTestResult != HTNOWHERE)) {
*result = hitTestResult;
return true;
}
2024-04-11 14:51:43 +08:00
int offsetSize;
2024-03-28 19:18:56 +08:00
bool isMaximum = ::IsZoomed(hwnd);
2024-04-23 13:41:26 +08:00
auto _offsetXY = QPoint(abs(clientRect->left - originalLeft), abs(clientRect->top - originalTop));
if (_offsetXY.x() != 0) {
offsetXY = _offsetXY;
}
2024-04-11 14:51:43 +08:00
if (isMaximum || _isFullScreen()) {
2024-03-28 19:18:56 +08:00
offsetSize = 0;
2024-04-11 14:51:43 +08:00
} else {
2024-03-28 19:18:56 +08:00
offsetSize = 1;
}
2024-04-11 14:51:43 +08:00
if (!isCompositionEnabled()) {
2024-03-28 19:18:56 +08:00
offsetSize = 0;
}
2024-04-16 12:36:41 +08:00
if (!isMaximum || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) {
2024-03-28 19:18:56 +08:00
clientRect->top = originalTop + offsetSize;
2024-04-16 12:36:41 +08:00
clientRect->bottom = originalBottom - offsetSize;
clientRect->left = originalLeft + offsetSize;
clientRect->right = originalRight - offsetSize;
2024-03-28 19:18:56 +08:00
}
2024-03-31 21:52:06 +08:00
_setMaximizeHovered(false);
2024-03-28 19:18:56 +08:00
*result = WVR_REDRAW;
return true;
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_NCHITTEST) {
2024-04-23 13:41:26 +08:00
if (_isWindows11OrGreater) {
if (_hitMaximizeButton()) {
if (*result == HTNOWHERE) {
*result = HTZOOM;
}
_setMaximizeHovered(true);
return true;
2024-03-28 19:18:56 +08:00
}
2024-04-23 13:41:26 +08:00
_setMaximizeHovered(false);
_setMaximizePressed(false);
2024-03-28 19:18:56 +08:00
}
*result = 0;
POINT nativeGlobalPos{GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
POINT nativeLocalPos = nativeGlobalPos;
::ScreenToClient(hwnd, &nativeLocalPos);
RECT clientRect{0, 0, 0, 0};
::GetClientRect(hwnd, &clientRect);
2024-04-11 14:51:43 +08:00
auto clientWidth = clientRect.right - clientRect.left;
auto clientHeight = clientRect.bottom - clientRect.top;
2024-03-28 19:18:56 +08:00
bool left = nativeLocalPos.x < _margins;
bool right = nativeLocalPos.x > clientWidth - _margins;
bool top = nativeLocalPos.y < _margins;
bool bottom = nativeLocalPos.y > clientHeight - _margins;
*result = 0;
if (!_fixSize && !_isFullScreen() && !_isMaximized()) {
if (left && top) {
*result = HTTOPLEFT;
} else if (left && bottom) {
*result = HTBOTTOMLEFT;
} else if (right && top) {
*result = HTTOPRIGHT;
} else if (right && bottom) {
*result = HTBOTTOMRIGHT;
} else if (left) {
*result = HTLEFT;
} else if (right) {
*result = HTRIGHT;
} else if (top) {
*result = HTTOP;
} else if (bottom) {
*result = HTBOTTOM;
}
}
if (0 != *result) {
return true;
}
2024-04-11 14:51:43 +08:00
if (_hitAppBar()) {
2024-03-28 19:18:56 +08:00
*result = HTCAPTION;
return true;
}
*result = HTCLIENT;
return true;
2024-04-23 13:41:26 +08:00
} else if (_isWindows11OrGreater && (uMsg == WM_NCLBUTTONDBLCLK || uMsg == WM_NCLBUTTONDOWN)) {
2024-04-11 14:51:43 +08:00
if (_hitMaximizeButton()) {
2024-03-28 19:18:56 +08:00
QMouseEvent event = QMouseEvent(QEvent::MouseButtonPress, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
2024-04-11 14:51:43 +08:00
QGuiApplication::sendEvent(_maximizeButton, &event);
2024-03-31 10:59:15 +08:00
_setMaximizePressed(true);
2024-03-28 19:18:56 +08:00
return true;
}
2024-04-23 13:41:26 +08:00
} else if (_isWindows11OrGreater && (uMsg == WM_NCLBUTTONUP || uMsg == WM_NCRBUTTONUP)) {
2024-04-11 14:51:43 +08:00
if (_hitMaximizeButton()) {
2024-03-28 19:18:56 +08:00
QMouseEvent event = QMouseEvent(QEvent::MouseButtonRelease, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
2024-04-11 14:51:43 +08:00
QGuiApplication::sendEvent(_maximizeButton, &event);
2024-03-31 10:59:15 +08:00
_setMaximizePressed(false);
2024-03-28 19:18:56 +08:00
return true;
}
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_NCPAINT) {
2024-03-28 19:18:56 +08:00
*result = FALSE;
return true;
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_NCACTIVATE) {
2024-03-28 19:18:56 +08:00
*result = ::DefWindowProcW(hwnd, WM_NCACTIVATE, wParam, -1);
return true;
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_GETMINMAXINFO) {
auto *minmaxInfo = reinterpret_cast<MINMAXINFO *>(lParam);
2024-04-23 13:41:26 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
2024-04-16 12:36:41 +08:00
minmaxInfo->ptMaxPosition.x = 0;
minmaxInfo->ptMaxPosition.y = 0;
minmaxInfo->ptMaxSize.x = 0;
minmaxInfo->ptMaxSize.y = 0;
2024-04-23 13:41:26 +08:00
return false;
2024-04-16 12:36:41 +08:00
#else
2024-03-28 19:18:56 +08:00
auto pixelRatio = window()->devicePixelRatio();
auto geometry = window()->screen()->availableGeometry();
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
2024-04-23 22:38:39 +08:00
if(!_fixSize){
minmaxInfo->ptMinTrackSize.x = window()->minimumWidth() * pixelRatio + offsetXY.x();
minmaxInfo->ptMinTrackSize.y = window()->minimumHeight() * pixelRatio + offsetXY.y() + _appbar->height() * pixelRatio;
}
2024-03-28 19:18:56 +08:00
minmaxInfo->ptMaxPosition.x = rect.left - offsetXY.x();
minmaxInfo->ptMaxPosition.y = rect.top - offsetXY.x();
2024-04-11 14:51:43 +08:00
minmaxInfo->ptMaxSize.x = qRound(geometry.width() * pixelRatio) + offsetXY.x() * 2;
minmaxInfo->ptMaxSize.y = qRound(geometry.height() * pixelRatio) + offsetXY.y() * 2;
2024-04-23 13:41:26 +08:00
return true;
2024-04-16 12:36:41 +08:00
#endif
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_NCRBUTTONDOWN) {
2024-03-28 19:18:56 +08:00
if (wParam == HTCAPTION) {
_showSystemMenu(QCursor::pos());
}
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN) {
2024-03-28 19:18:56 +08:00
const bool altPressed = ((wParam == VK_MENU) || (::GetKeyState(VK_MENU) < 0));
const bool spacePressed = ((wParam == VK_SPACE) || (::GetKeyState(VK_SPACE) < 0));
if (altPressed && spacePressed) {
auto pos = window()->position();
2024-04-11 14:51:43 +08:00
_showSystemMenu(QPoint(pos.x(), qRound(pos.y() + _appbar->height())));
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
} else if (uMsg == WM_SYSCOMMAND) {
if (wParam == SC_MINIMIZE) {
if (window()->transientParent()) {
2024-04-23 13:41:26 +08:00
HWND hwnd = reinterpret_cast<HWND>(window()->transientParent()->winId());
::ShowWindow(hwnd, 2);
2024-04-11 14:51:43 +08:00
} else {
2024-04-23 13:41:26 +08:00
HWND hwnd = reinterpret_cast<HWND>(window()->winId());
::ShowWindow(hwnd, 2);
2024-03-28 19:18:56 +08:00
}
return true;
}
return false;
}
return false;
2024-04-11 14:51:43 +08:00
#else
2024-03-28 19:18:56 +08:00
return false;
2024-04-11 14:51:43 +08:00
#endif
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
bool FluFrameless::_isMaximized() {
2024-03-28 19:18:56 +08:00
return window()->visibility() == QWindow::Maximized;
}
2024-04-11 14:51:43 +08:00
bool FluFrameless::_isFullScreen() {
2024-03-28 19:18:56 +08:00
return window()->visibility() == QWindow::FullScreen;
}
2024-04-11 14:51:43 +08:00
void FluFrameless::_showSystemMenu(QPoint point) {
2024-03-28 19:18:56 +08:00
#ifdef Q_OS_WIN
HWND hwnd = reinterpret_cast<HWND>(window()->winId());
2024-04-11 14:51:43 +08:00
DWORD style = ::GetWindowLongPtr(hwnd, GWL_STYLE);
2024-03-28 19:18:56 +08:00
::SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_SYSMENU);
2024-04-11 14:51:43 +08:00
auto hMenu = ::GetSystemMenu(hwnd, FALSE);
if (_isMaximized() || _isFullScreen()) {
::EnableMenuItem(hMenu, SC_MOVE, MFS_DISABLED);
::EnableMenuItem(hMenu, SC_RESTORE, MFS_ENABLED);
} else {
::EnableMenuItem(hMenu, SC_MOVE, MFS_ENABLED);
::EnableMenuItem(hMenu, SC_RESTORE, MFS_DISABLED);
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
if (!_fixSize && !_isMaximized() && !_isFullScreen()) {
::EnableMenuItem(hMenu, SC_SIZE, MFS_ENABLED);
::EnableMenuItem(hMenu, SC_MAXIMIZE, MFS_ENABLED);
} else {
::EnableMenuItem(hMenu, SC_SIZE, MFS_DISABLED);
::EnableMenuItem(hMenu, SC_MAXIMIZE, MFS_DISABLED);
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
const int result = ::TrackPopupMenu(hMenu, (TPM_RETURNCMD | (QGuiApplication::isRightToLeft() ? TPM_RIGHTALIGN : TPM_LEFTALIGN)), qRound(point.x() * window()->devicePixelRatio()),
qRound(point.y() * window()->devicePixelRatio()), 0, hwnd, nullptr);
2024-03-28 19:18:56 +08:00
if (result != FALSE) {
::PostMessageW(hwnd, WM_SYSCOMMAND, result, 0);
}
2024-04-11 14:51:43 +08:00
::SetWindowLongPtr(hwnd, GWL_STYLE, style & ~WS_SYSMENU);
2024-03-28 19:18:56 +08:00
#endif
}
2024-04-11 14:51:43 +08:00
bool FluFrameless::_hitAppBar() {
for (int i = 0; i <= _hitTestList.size() - 1; ++i) {
auto item = _hitTestList.at(i);
if (containsCursorToItem(item)) {
2024-03-28 19:18:56 +08:00
return false;
}
}
2024-04-11 14:51:43 +08:00
if (containsCursorToItem(_appbar)) {
2024-03-28 19:18:56 +08:00
return true;
}
return false;
}
2024-04-11 14:51:43 +08:00
bool FluFrameless::_hitMaximizeButton() {
if (containsCursorToItem(_maximizeButton)) {
2024-03-28 19:18:56 +08:00
return true;
}
return false;
}
2024-04-11 14:51:43 +08:00
void FluFrameless::_setMaximizePressed(bool val) {
_maximizeButton->setProperty("down", val);
2024-03-31 10:59:15 +08:00
}
2024-04-11 14:51:43 +08:00
void FluFrameless::_setMaximizeHovered(bool val) {
_maximizeButton->setProperty("hover", val);
2024-03-31 10:59:15 +08:00
}
2024-04-11 14:51:43 +08:00
void FluFrameless::_updateCursor(int edges) {
2024-03-28 19:18:56 +08:00
switch (edges) {
2024-04-11 14:51:43 +08:00
case 0:
window()->setCursor(Qt::ArrowCursor);
break;
case Qt::LeftEdge:
case Qt::RightEdge:
window()->setCursor(Qt::SizeHorCursor);
break;
case Qt::TopEdge:
case Qt::BottomEdge:
window()->setCursor(Qt::SizeVerCursor);
break;
case Qt::LeftEdge | Qt::TopEdge:
case Qt::RightEdge | Qt::BottomEdge:
window()->setCursor(Qt::SizeFDiagCursor);
break;
case Qt::RightEdge | Qt::TopEdge:
case Qt::LeftEdge | Qt::BottomEdge:
window()->setCursor(Qt::SizeBDiagCursor);
break;
default:
break;
2024-03-28 19:18:56 +08:00
}
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void FluFrameless::showFullScreen() {
2024-03-28 19:18:56 +08:00
window()->showFullScreen();
}
2024-04-11 14:51:43 +08:00
void FluFrameless::showMaximized() {
2024-03-28 19:18:56 +08:00
#ifdef Q_OS_WIN
HWND hwnd = reinterpret_cast<HWND>(window()->winId());
2024-04-11 14:51:43 +08:00
::ShowWindow(hwnd, 3);
2024-03-28 19:18:56 +08:00
#else
2024-04-23 13:41:26 +08:00
window()->setVisibility(QQuickWindow::Maximized);
2024-03-28 19:18:56 +08:00
#endif
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void FluFrameless::showMinimized() {
2024-04-23 13:41:26 +08:00
#ifdef Q_OS_WIN
HWND hwnd = reinterpret_cast<HWND>(window()->winId());
::ShowWindow(hwnd, 2);
#else
window()->setVisibility(QQuickWindow::Minimized);
#endif
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
void FluFrameless::showNormal() {
2024-04-23 13:41:26 +08:00
window()->setVisibility(QQuickWindow::Windowed);
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
void FluFrameless::setHitTestVisible(QQuickItem *val) {
if (!_hitTestList.contains(val)) {
2024-03-31 10:59:15 +08:00
_hitTestList.append(val);
}
2024-03-28 19:18:56 +08:00
}
2024-04-11 14:51:43 +08:00
void FluFrameless::_setWindowTopmost(bool topmost) {
2024-03-28 19:18:56 +08:00
#ifdef Q_OS_WIN
HWND hwnd = reinterpret_cast<HWND>(window()->winId());
2024-04-11 14:51:43 +08:00
if (topmost) {
2024-03-28 19:18:56 +08:00
::SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
2024-04-11 14:51:43 +08:00
} else {
2024-03-28 19:18:56 +08:00
::SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
#else
window()->setFlag(Qt::WindowStaysOnTopHint,topmost);
#endif
}
2024-04-11 14:51:43 +08:00
bool FluFrameless::eventFilter(QObject *obj, QEvent *ev) {
2024-03-28 19:18:56 +08:00
#ifndef Q_OS_WIN
switch (ev->type()) {
case QEvent::MouseButtonPress:
if(_edges!=0){
QMouseEvent *event = static_cast<QMouseEvent*>(ev);
if(event->button() == Qt::LeftButton){
_updateCursor(_edges);
window()->startSystemResize(Qt::Edges(_edges));
}
}else{
if(_hitAppBar()){
2024-04-03 13:24:30 +08:00
qint64 clickTimer = QDateTime::currentMSecsSinceEpoch();
qint64 offset = clickTimer - this->_clickTimer;
this->_clickTimer = clickTimer;
if(offset<300){
if(_isMaximized()){
showNormal();
}else{
showMaximized();
}
}else{
window()->startSystemMove();
}
2024-03-28 19:18:56 +08:00
}
}
break;
case QEvent::MouseButtonRelease:
_edges = 0;
break;
case QEvent::MouseMove: {
if(_isMaximized() || _isFullScreen()){
break;
}
if(_fixSize){
break;
}
QMouseEvent *event = static_cast<QMouseEvent*>(ev);
QPoint p =
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
event->pos();
#else
event->position().toPoint();
#endif
if(p.x() >= _margins && p.x() <= (window()->width() - _margins) && p.y() >= _margins && p.y() <= (window()->height() - _margins)){
if(_edges != 0){
_edges = 0;
_updateCursor(_edges);
}
break;
}
_edges = 0;
if ( p.x() < _margins ) {
_edges |= Qt::LeftEdge;
}
if ( p.x() > (window()->width() - _margins) ) {
_edges |= Qt::RightEdge;
}
if ( p.y() < _margins ) {
_edges |= Qt::TopEdge;
}
if ( p.y() > (window()->height() - _margins) ) {
_edges |= Qt::BottomEdge;
}
_updateCursor(_edges);
break;
}
default:
break;
}
#endif
return QObject::eventFilter(obj, ev);
}