FluentUI/src/FluFramelessHelper.cpp

350 lines
11 KiB
C++
Raw Normal View History

2023-12-19 20:02:15 +08:00
#include "FluFramelessHelper.h"
2023-12-11 23:47:03 +08:00
#include <QGuiApplication>
2023-12-22 12:39:04 +08:00
#include "FluTools.h"
2023-12-13 21:28:21 +08:00
#ifdef Q_OS_WIN
2023-12-19 20:02:15 +08:00
#pragma comment (lib,"user32.lib")
#pragma comment (lib,"dwmapi.lib")
2023-12-13 21:28:21 +08:00
#include <windows.h>
2023-12-19 20:02:15 +08:00
#include <dwmapi.h>
2023-12-19 18:01:49 +08:00
2023-12-13 21:28:21 +08:00
static inline QByteArray qtNativeEventType()
{
static const auto result = "windows_generic_MSG";
return result;
}
2023-12-22 01:30:25 +08:00
2023-12-18 21:29:38 +08:00
static inline bool isCompositionEnabled(){
typedef HRESULT (WINAPI* DwmIsCompositionEnabledPtr)(BOOL *pfEnabled);
HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module)
{
BOOL composition_enabled = false;
DwmIsCompositionEnabledPtr dwm_is_composition_enabled;
dwm_is_composition_enabled= reinterpret_cast<DwmIsCompositionEnabledPtr>(GetProcAddress(module, "DwmIsCompositionEnabled"));
if (dwm_is_composition_enabled)
{
dwm_is_composition_enabled(&composition_enabled);
}
return composition_enabled;
}
return false;
}
2023-12-19 18:01:49 +08:00
2023-12-19 20:02:15 +08:00
static inline void showShadow(HWND hwnd){
if(isCompositionEnabled()){
2023-12-28 15:07:04 +08:00
const MARGINS shadow = { 1, 0, 0, 0 };
2023-12-19 20:02:15 +08:00
typedef HRESULT (WINAPI* DwmExtendFrameIntoClientAreaPtr)(HWND hWnd, const MARGINS *pMarInset);
HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module)
{
DwmExtendFrameIntoClientAreaPtr dwm_extendframe_into_client_area_;
dwm_extendframe_into_client_area_= reinterpret_cast<DwmExtendFrameIntoClientAreaPtr>(GetProcAddress(module, "DwmExtendFrameIntoClientArea"));
if (dwm_extendframe_into_client_area_)
{
dwm_extendframe_into_client_area_(hwnd, &shadow);
}
}
}else{
ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW;
SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle);
}
}
2023-12-18 21:32:56 +08:00
#endif
2023-12-18 21:29:38 +08:00
2023-12-22 01:30:25 +08:00
FramelessEventFilter::FramelessEventFilter(FluFramelessHelper* helper){
_helper = helper;
_current = _helper->window->winId();
2023-12-13 21:28:21 +08:00
}
bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, QT_NATIVE_EVENT_RESULT_TYPE *result){
#ifdef Q_OS_WIN
2023-12-22 01:30:25 +08:00
if ((eventType != qtNativeEventType()) || !message || _helper.isNull() || _helper->window.isNull()) {
2023-12-13 21:28:21 +08:00
return false;
}
const auto msg = static_cast<const MSG *>(message);
2023-12-18 21:29:38 +08:00
const HWND hwnd = msg->hwnd;
if (!hwnd) {
2023-12-13 21:28:21 +08:00
return false;
}
2023-12-18 21:29:38 +08:00
const qint64 wid = reinterpret_cast<qint64>(hwnd);
2023-12-13 21:28:21 +08:00
if(wid != _current){
return false;
}
const UINT uMsg = msg->message;
2023-12-18 21:29:38 +08:00
if (!msg || !hwnd)
2023-12-13 21:28:21 +08:00
{
return false;
}
2023-12-22 18:07:46 +08:00
if(uMsg == WM_NCCALCSIZE){
2023-12-15 11:25:54 +08:00
*result = WVR_REDRAW;
return true;
2023-12-16 15:25:37 +08:00
}else if(uMsg == WM_NCPAINT){
2023-12-18 21:29:38 +08:00
if(!isCompositionEnabled()){
*result = WVR_REDRAW;
return true;
}
2023-12-16 15:25:37 +08:00
return false;
2023-12-18 21:29:38 +08:00
}else if(uMsg == WM_NCACTIVATE){
2023-12-18 23:56:28 +08:00
if(!isCompositionEnabled()){
*result = 1;
return true;
2023-12-18 21:29:38 +08:00
}
2023-12-18 23:56:28 +08:00
return false;
2023-12-22 01:30:25 +08:00
}else if(uMsg == WM_NCHITTEST){
2023-12-22 12:39:04 +08:00
if(FluTools::getInstance()->isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){
2023-12-22 02:09:47 +08:00
if (*result == HTNOWHERE) {
*result = HTZOOM;
}
2023-12-22 01:30:25 +08:00
return true;
}
return false;
}else if(uMsg == WM_NCLBUTTONDBLCLK || uMsg == WM_NCLBUTTONDOWN){
2023-12-22 12:39:04 +08:00
if(FluTools::getInstance()->isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){
2023-12-22 01:30:25 +08:00
QMouseEvent event = QMouseEvent(QEvent::MouseButtonPress, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::sendEvent(_helper->maximizeButton(),&event);
return true;
}
return false;
}else if(uMsg == WM_NCLBUTTONUP || uMsg == WM_NCRBUTTONUP){
2023-12-22 12:39:04 +08:00
if(FluTools::getInstance()->isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){
2023-12-22 01:30:25 +08:00
QMouseEvent event = QMouseEvent(QEvent::MouseButtonRelease, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::sendEvent(_helper->maximizeButton(),&event);
}
return false;
2023-12-13 21:28:21 +08:00
}
return false;
#endif
return false;
}
2023-12-19 20:02:15 +08:00
FluFramelessHelper::FluFramelessHelper(QObject *parent)
2023-12-11 23:47:03 +08:00
: QObject{parent}
{
2023-12-22 01:30:25 +08:00
2023-12-11 23:47:03 +08:00
}
2023-12-19 20:02:15 +08:00
void FluFramelessHelper::classBegin(){
2023-12-11 23:47:03 +08:00
}
2023-12-22 01:30:25 +08:00
void FluFramelessHelper::_updateCursor(int edges){
2023-12-11 23:47:03 +08:00
switch (edges) {
case 0:
2023-12-22 01:30:25 +08:00
window->setCursor(Qt::ArrowCursor);
2023-12-11 23:47:03 +08:00
break;
case Qt::LeftEdge:
case Qt::RightEdge:
2023-12-22 01:30:25 +08:00
window->setCursor(Qt::SizeHorCursor);
2023-12-11 23:47:03 +08:00
break;
case Qt::TopEdge:
case Qt::BottomEdge:
2023-12-22 01:30:25 +08:00
window->setCursor(Qt::SizeVerCursor);
2023-12-11 23:47:03 +08:00
break;
case Qt::LeftEdge | Qt::TopEdge:
case Qt::RightEdge | Qt::BottomEdge:
2023-12-22 01:30:25 +08:00
window->setCursor(Qt::SizeFDiagCursor);
2023-12-11 23:47:03 +08:00
break;
case Qt::RightEdge | Qt::TopEdge:
case Qt::LeftEdge | Qt::BottomEdge:
2023-12-22 01:30:25 +08:00
window->setCursor(Qt::SizeBDiagCursor);
2023-12-11 23:47:03 +08:00
break;
}
}
2023-12-19 20:02:15 +08:00
bool FluFramelessHelper::eventFilter(QObject *obj, QEvent *ev){
2023-12-28 20:47:36 +08:00
if (!window.isNull() && window->flags()) {
2023-12-16 15:25:37 +08:00
2023-12-13 16:20:09 +08:00
static int edges = 0;
2023-12-11 23:47:03 +08:00
const int margin = 8;
switch (ev->type()) {
case QEvent::MouseButtonPress:
2023-12-13 16:20:09 +08:00
if(edges!=0){
2023-12-22 01:30:25 +08:00
QMouseEvent *event = static_cast<QMouseEvent*>(ev);
if(event->button() == Qt::LeftButton){
_updateCursor(edges);
window->startSystemResize(Qt::Edges(edges));
}
2023-12-13 16:20:09 +08:00
}
2023-12-11 23:47:03 +08:00
break;
case QEvent::MouseButtonRelease:
2023-12-13 21:28:21 +08:00
edges = 0;
2023-12-22 01:30:25 +08:00
_updateCursor(edges);
2023-12-11 23:47:03 +08:00
break;
case QEvent::MouseMove: {
2023-12-22 01:30:25 +08:00
if(_maximized() || _fullScreen()){
2023-12-13 16:20:09 +08:00
break;
}
2023-12-22 01:30:25 +08:00
if(!resizeable()){
2023-12-13 16:20:09 +08:00
break;
}
2023-12-11 23:47:03 +08:00
QMouseEvent *event = static_cast<QMouseEvent*>(ev);
QPoint p =
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
event->pos();
#else
event->position().toPoint();
#endif
2023-12-22 01:30:25 +08:00
if(p.x() >= margin && p.x() <= (window->width() - margin) && p.y() >= margin && p.y() <= (window->height() - margin)){
2023-12-13 23:43:01 +08:00
if(edges != 0){
edges = 0;
2023-12-22 01:30:25 +08:00
_updateCursor(edges);
2023-12-13 23:43:01 +08:00
}
break;
}
edges = 0;
2023-12-11 23:47:03 +08:00
if ( p.x() < margin ) {
edges |= Qt::LeftEdge;
}
2023-12-22 01:30:25 +08:00
if ( p.x() > (window->width() - margin) ) {
2023-12-11 23:47:03 +08:00
edges |= Qt::RightEdge;
}
if ( p.y() < margin ) {
edges |= Qt::TopEdge;
}
2023-12-22 01:30:25 +08:00
if ( p.y() > (window->height() - margin) ) {
2023-12-11 23:47:03 +08:00
edges |= Qt::BottomEdge;
}
2023-12-22 01:30:25 +08:00
_updateCursor(edges);
2023-12-11 23:47:03 +08:00
break;
}
default:
break;
}
}
return QObject::eventFilter(obj, ev);
}
2023-12-19 20:02:15 +08:00
void FluFramelessHelper::componentComplete(){
2023-12-11 23:47:03 +08:00
auto o = parent();
while (nullptr != o) {
2023-12-22 01:30:25 +08:00
window = (QQuickWindow*)o;
2023-12-11 23:47:03 +08:00
o = o->parent();
}
2023-12-22 01:30:25 +08:00
if(!window.isNull()){
2023-12-13 21:28:21 +08:00
#ifdef Q_OS_WIN
2023-12-28 20:47:36 +08:00
#if (QT_VERSION == QT_VERSION_CHECK(6, 5, 3))
2023-12-28 21:20:47 +08:00
window->setFlag(Qt::FramelessWindowHint,true);
2023-12-28 20:47:36 +08:00
#endif
2023-12-28 21:20:47 +08:00
window->setFlag(Qt::WindowMinimizeButtonHint,true);
2023-12-22 01:30:25 +08:00
_nativeEvent =new FramelessEventFilter(this);
2023-12-13 21:28:21 +08:00
qApp->installNativeEventFilter(_nativeEvent);
2023-12-22 01:30:25 +08:00
HWND hwnd = reinterpret_cast<HWND>(window->winId());
2023-12-28 12:42:22 +08:00
DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
if(resizeable()){
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CAPTION);
}else{
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION);
}
2023-12-19 20:02:15 +08:00
showShadow(hwnd);
2023-12-28 20:47:36 +08:00
SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
2023-12-28 12:42:22 +08:00
#else
window->setFlags((window->flags() & (~Qt::WindowMinMaxButtonsHint) & (~Qt::Dialog)) | Qt::FramelessWindowHint | Qt::Window);
2023-12-13 21:28:21 +08:00
#endif
2023-12-22 01:30:25 +08:00
_stayTop = QQmlProperty(window,"stayTop");
2023-12-28 12:42:22 +08:00
_screen = QQmlProperty(window,"screen");
2023-12-19 20:28:14 +08:00
_onStayTopChange();
2023-12-19 18:01:49 +08:00
_stayTop.connectNotifySignal(this,SLOT(_onStayTopChange()));
_screen.connectNotifySignal(this,SLOT(_onScreenChanged()));
2023-12-22 01:30:25 +08:00
window->installEventFilter(this);
2023-12-11 23:47:03 +08:00
}
}
2023-12-19 20:02:15 +08:00
void FluFramelessHelper::_onScreenChanged(){
2023-12-20 18:01:09 +08:00
#ifdef Q_OS_WIN
2023-12-22 01:30:25 +08:00
HWND hwnd = reinterpret_cast<HWND>(window->winId());
2023-12-21 10:44:46 +08:00
SetWindowPos(hwnd,0,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOOWNERZORDER);
RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
2023-12-20 18:01:09 +08:00
#endif
2023-12-19 18:01:49 +08:00
}
2023-12-22 01:30:25 +08:00
void FluFramelessHelper::showSystemMenu(){
#ifdef Q_OS_WIN
QPoint point = QCursor::pos();
HWND hwnd = reinterpret_cast<HWND>(window->winId());
DWORD style = GetWindowLongPtr(hwnd,GWL_STYLE);
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX);
const HMENU hMenu = ::GetSystemMenu(hwnd, FALSE);
DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND);
DeleteMenu(hMenu, SC_SIZE, MF_BYCOMMAND);
if(_maximized() || _fullScreen()){
EnableMenuItem(hMenu,SC_RESTORE,MFS_ENABLED);
}else{
EnableMenuItem(hMenu,SC_RESTORE,MFS_DISABLED);
}
if(resizeable() && !_maximized() && !_fullScreen()){
EnableMenuItem(hMenu,SC_MAXIMIZE,MFS_ENABLED);
}else{
EnableMenuItem(hMenu,SC_MAXIMIZE,MFS_DISABLED);
}
2023-12-22 12:39:04 +08:00
const int result = TrackPopupMenu(hMenu, (TPM_RETURNCMD | (QGuiApplication::isRightToLeft() ? TPM_RIGHTALIGN : TPM_LEFTALIGN)), point.x()*window->devicePixelRatio(), point.y()*window->devicePixelRatio(), 0, hwnd, nullptr);
2023-12-22 01:30:25 +08:00
if (result != FALSE) {
PostMessageW(hwnd, WM_SYSCOMMAND, result, 0);
}
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION &~ WS_SYSMENU);
#endif
}
2023-12-19 20:02:15 +08:00
void FluFramelessHelper::_onStayTopChange(){
2023-12-19 18:01:49 +08:00
bool isStayTop = _stayTop.read().toBool();
2023-12-18 22:24:24 +08:00
#ifdef Q_OS_WIN
2023-12-22 01:30:25 +08:00
HWND hwnd = reinterpret_cast<HWND>(window->winId());
2023-12-19 18:01:49 +08:00
if(isStayTop){
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}else{
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
#else
2023-12-22 01:30:25 +08:00
window->setFlag(Qt::WindowStaysOnTopHint,isStayTop);
2023-12-18 22:24:24 +08:00
#endif
}
2023-12-19 20:02:15 +08:00
FluFramelessHelper::~FluFramelessHelper(){
2023-12-22 01:30:25 +08:00
if (!window.isNull()) {
window->setFlags(Qt::Window);
2023-12-13 21:28:21 +08:00
#ifdef Q_OS_WIN
qApp->removeNativeEventFilter(_nativeEvent);
2023-12-22 01:30:25 +08:00
delete _nativeEvent;
2023-12-13 21:28:21 +08:00
#endif
2023-12-22 01:30:25 +08:00
window->removeEventFilter(this);
}
}
bool FluFramelessHelper::hoverMaxBtn(){
QVariant appBar = window->property("appBar");
if(appBar.isNull()){
return false;
2023-12-11 23:47:03 +08:00
}
2023-12-22 01:30:25 +08:00
QVariant var;
QMetaObject::invokeMethod(appBar.value<QObject*>(), "maximizeButtonHover",Q_RETURN_ARG(QVariant, var));
if(var.isNull()){
return false;
}
return var.toBool();
}
QObject* FluFramelessHelper::maximizeButton(){
QVariant appBar = window->property("appBar");
if(appBar.isNull()){
return nullptr;
}
QVariant var;
QMetaObject::invokeMethod(appBar.value<QObject*>(), "maximizeButton",Q_RETURN_ARG(QVariant, var));
if(var.isNull()){
return nullptr;
}
return var.value<QObject*>();
}
bool FluFramelessHelper::resizeable(){
return !(window->width() == window->maximumWidth() && window->width() == window->minimumWidth() && window->height() == window->maximumHeight() && window->height() == window->minimumHeight());
}
bool FluFramelessHelper::_maximized(){
return window->visibility() == QWindow::Maximized;
}
bool FluFramelessHelper::_fullScreen(){
return window->visibility() == QWindow::FullScreen;
2023-12-11 23:47:03 +08:00
}