From f1b52fc5eeea841ef182a0d14076b34128666d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AD=90=E6=A5=9A=5Czhuzi?= Date: Mon, 27 Feb 2023 23:04:52 +0800 Subject: [PATCH] update --- example/MainPage.qml | 23 ++++++++++++++--- example/SettingPage.qml | 1 + example/T_Controls.qml | 3 +-- example/main.cpp | 2 +- src/FluApp.cpp | 41 +++++++++++++------------------ src/FluApp.h | 6 ++++- src/FramelessView.h | 1 + src/FramelessView_win.cpp | 12 +++++++++ src/WindowHelper.cpp | 21 ++++------------ src/WindowHelper.h | 11 +++++---- src/build-preset/plugins.qmltypes | 5 ++++ src/controls/FluToggleSwitch.qml | 24 +++++++++++++++--- src/controls/FluWindow.qml | 24 +++++++++++++++++- 13 files changed, 116 insertions(+), 58 deletions(-) diff --git a/example/MainPage.qml b/example/MainPage.qml index b1cbefe9..6b84803c 100644 --- a/example/MainPage.qml +++ b/example/MainPage.qml @@ -9,6 +9,7 @@ FluWindow { id:rootwindow width: 800 height: 600 + title: "FluentUI" FluAppBar{ id:appbar @@ -43,10 +44,23 @@ FluWindow { Rectangle{ color: { - if(nav_list.currentIndex === index){ - return "#EAEAEB" + if(FluApp.isDark){ + if(item_mouse.containsMouse){ + return "#292929" + } + if(nav_list.currentIndex === index){ + return "#2D2D2D" + } + return "#00000000" + }else{ + if(item_mouse.containsMouse){ + return "#EDEDED" + } + if(nav_list.currentIndex === index){ + return "#EAEAEA" + } + return "#00000000" } - return item_mouse.containsMouse? "#EAEAEA" : "#00000000" } radius: 4 anchors{ @@ -70,9 +84,10 @@ FluWindow { } } - Text{ + FluText{ text:model.text anchors.centerIn: parent + fontStyle: FluText.Caption } } diff --git a/example/SettingPage.qml b/example/SettingPage.qml index 7c626351..93e435f2 100644 --- a/example/SettingPage.qml +++ b/example/SettingPage.qml @@ -5,6 +5,7 @@ FluWindow { width: 500 height: 500 + title:"设置" FluAppBar{ id:appbar diff --git a/example/T_Controls.qml b/example/T_Controls.qml index c728b497..99c23368 100644 --- a/example/T_Controls.qml +++ b/example/T_Controls.qml @@ -17,8 +17,7 @@ Item { FluButton{ Layout.topMargin: 20 onClicked: { - FluApp.getWIdByWindow(Window.window) -// FluApp.navigate("/Setting") + } } diff --git a/example/main.cpp b/example/main.cpp index dd62784a..47ed906f 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { -// qputenv("QSG_RENDER_LOOP","basic"); + qputenv("QSG_RENDER_LOOP","basic"); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; diff --git a/src/FluApp.cpp b/src/FluApp.cpp index c5ca1f71..3b1c27b2 100644 --- a/src/FluApp.cpp +++ b/src/FluApp.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "FramelessView.h" @@ -30,26 +31,7 @@ void FluApp::setAppWindow(QWindow *window){ } void FluApp::run(){ - if(!routes().contains(initialRoute())){ - qErrnoWarning("没有找到当前路由"); - return; - } - FramelessView *view = new FramelessView(); - view->engine()->rootContext()->setContextProperty("FluApp",FluApp::getInstance()); - view->setColor(QColor(255,0,0,1)); - const QUrl url(routes().value(initialRoute()).toString()); - QObject::connect(view, &QQuickView::statusChanged, view, [&](QQuickView::Status status) { - if (status == QQuickView::Status::Ready) { - - qDebug()<<"-----------winId:"<winId(); - } - }); - QObject::connect(view->engine(), &QQmlEngine::quit, qApp, &QCoreApplication::quit); - QObject::connect(qApp, &QGuiApplication::aboutToQuit, qApp, [&view](){view->setSource({});}); -// view->setTitle("FluentUI"); - view->setSource(url); - view->moveToScreenCenter(); - view->show(); + navigate(initialRoute()); } void FluApp::navigate(const QString& route){ @@ -57,16 +39,27 @@ void FluApp::navigate(const QString& route){ qErrnoWarning("没有找到当前路由"); return; } + + bool isAppWindow = route==initialRoute(); FramelessView *view = new FramelessView(); view->engine()->rootContext()->setContextProperty("FluApp",FluApp::getInstance()); view->setColor(isDark() ? QColor(0,0,0,1) : QColor(255, 255, 255, 1)); + QObject::connect(view, &QQuickView::statusChanged, view, [&](QQuickView::Status status) { + if (status == QQuickView::Status::Ready) { + Q_EMIT windowReady(view); + } + }); view->setSource((routes().value(route).toString())); - view->closeDeleteLater(); + if(isAppWindow){ + QObject::connect(view->engine(), &QQmlEngine::quit, qApp, &QCoreApplication::quit); + QObject::connect(qApp, &QGuiApplication::aboutToQuit, qApp, [&view](){view->setSource({});}); + }else{ + view->closeDeleteLater(); + } view->moveToScreenCenter(); view->show(); } -void FluApp::getWIdByWindow(QWindow *window){ - qDebug()<< window->winId(); - window->winId(); +bool FluApp::equalsWindow(FramelessView *view,QWindow *window){ + return view->winId() == window->winId(); } diff --git a/src/FluApp.h b/src/FluApp.h index b460ae2a..bfc04195 100644 --- a/src/FluApp.h +++ b/src/FluApp.h @@ -4,6 +4,7 @@ #include #include #include +#include "FramelessView.h" #include "stdafx.h" class FluApp : public QObject @@ -12,6 +13,7 @@ class FluApp : public QObject Q_PROPERTY_AUTO(QString,initialRoute); Q_PROPERTY_AUTO(bool,isDark); Q_PROPERTY_AUTO(QJsonObject,routes); + public: static FluApp *getInstance(); @@ -24,7 +26,9 @@ public: Q_INVOKABLE void setAppWindow(QWindow *window); - Q_INVOKABLE void getWIdByWindow(QWindow *window); + Q_SIGNAL void windowReady(FramelessView *view); + + Q_INVOKABLE bool equalsWindow(FramelessView *view,QWindow *window); private: static FluApp* m_instance; diff --git a/src/FramelessView.h b/src/FramelessView.h index d373b73f..f129e2fa 100644 --- a/src/FramelessView.h +++ b/src/FramelessView.h @@ -23,6 +23,7 @@ public: bool isFull() const; QQuickItem *titleItem() const; + static QMap *windowCache; static QRect calcCenterGeo(const QRect &screenGeo, const QSize &normalSize); public slots: void setIsMax(bool isMax); diff --git a/src/FramelessView_win.cpp b/src/FramelessView_win.cpp index 1226915c..b117ce63 100644 --- a/src/FramelessView_win.cpp +++ b/src/FramelessView_win.cpp @@ -20,6 +20,10 @@ // WS_SYSMENU: enables the context menu with the move, close, maximize, minize... commands (shift + right-click on the task bar item) // WS_CAPTION: enables aero minimize animation/transition // WS_MAXIMIZEBOX, WS_MINIMIZEBOX: enable minimize/maximize + +QMap* FramelessView::windowCache = new QMap; + + enum class Style : DWORD { windowed = WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, @@ -149,6 +153,13 @@ FramelessView::FramelessView(QWindow* parent) setIsMax(windowState() == Qt::WindowMaximized); setIsFull(windowState() == Qt::WindowFullScreen); }); + + QObject::connect(this, &QQuickView::statusChanged, this, [&](QQuickView::Status status) { + if (status == QQuickView::Status::Ready) { + FramelessView::windowCache->insert(this->winId(),this); + } + }); + } void FramelessView::showEvent(QShowEvent* e) { @@ -181,6 +192,7 @@ FramelessView::~FramelessView() { ::DestroyMenu(d->mMenuHandler); } + FramelessView::windowCache->remove(this->winId()); delete d; } diff --git a/src/WindowHelper.cpp b/src/WindowHelper.cpp index b185906a..dabade55 100644 --- a/src/WindowHelper.cpp +++ b/src/WindowHelper.cpp @@ -6,22 +6,11 @@ WindowHelper::WindowHelper(QObject *parent) } -void WindowHelper::classBegin() -{ -} - -void WindowHelper::componentComplete() -{ - - auto rootItem = qobject_cast(parent()); - if (auto window = rootItem->window()) - { - this->window = window; - this->window->setTitle("FluentUI"); - qDebug()<<"--------->--------->"; - } -} - void WindowHelper::setTitle(const QString& text){ window->setTitle(text); } + + +void WindowHelper::initWindow(FramelessView* window){ + this->window = window; +} diff --git a/src/WindowHelper.h b/src/WindowHelper.h index a9f77c7e..678ea5bb 100644 --- a/src/WindowHelper.h +++ b/src/WindowHelper.h @@ -4,20 +4,21 @@ #include #include #include +#include +#include "FramelessView.h" -class WindowHelper : public QObject, public QQmlParserStatus +class WindowHelper : public QObject { Q_OBJECT public: explicit WindowHelper(QObject *parent = nullptr); - void classBegin() override; - void componentComplete() override; + Q_INVOKABLE void initWindow(FramelessView* window); Q_INVOKABLE void setTitle(const QString& text); -private: - QQuickWindow* window; +private: + FramelessView* window; }; diff --git a/src/build-preset/plugins.qmltypes b/src/build-preset/plugins.qmltypes index 3cbfa743..06845e5d 100644 --- a/src/build-preset/plugins.qmltypes +++ b/src/build-preset/plugins.qmltypes @@ -809,6 +809,10 @@ Module { prototype: "QObject" exports: ["FluentUI/WindowHelper 1.0"] exportMetaObjectRevisions: [0] + Method { + name: "initWindow" + Parameter { name: "window"; type: "FramelessView"; isPointer: true } + } Method { name: "setTitle" Parameter { name: "text"; type: "string" } @@ -1044,5 +1048,6 @@ Module { defaultProperty: "data" Property { name: "isMax"; type: "bool" } Property { name: "title"; type: "string" } + Property { name: "window"; type: "QVariant" } } } diff --git a/src/controls/FluToggleSwitch.qml b/src/controls/FluToggleSwitch.qml index 845c7dca..e366a084 100644 --- a/src/controls/FluToggleSwitch.qml +++ b/src/controls/FluToggleSwitch.qml @@ -14,15 +14,31 @@ Switch { width: root.width height: root.height radius: height / 2 - color: root.checked ? checkedColor : "white" + color: { + if(FluApp.isDark){ + if(root.checked){ + return checkedColor + } + if(switch_mouse.containsMouse){ + return "#3E3E3C" + } + return "#323232" + }else{ + if(switch_mouse.containsMouse){ + return "#F4F4F4" + } + return root.checked ? checkedColor : "white" + } + } border.width: 1 border.color: root.checked ? checkedColor : "#666666" Rectangle { - x: root.checked ? parent.width - width - 4 : 4 - width: root.checked ? parent.height - 8 : parent.height - 8 - height: width + x: root.checked ? root.implicitWidth - width - 4 : 4 + width: root.height - 8 + height: root.height - 8 radius: width / 2 + scale: switch_mouse.containsMouse ? 1.2 : 1.0 anchors.verticalCenter: parent.verticalCenter color: root.checked ? "#FFFFFF" : "#666666" // border.color: "#D5D5D5" diff --git a/src/controls/FluWindow.qml b/src/controls/FluWindow.qml index c76aca1e..a2682363 100644 --- a/src/controls/FluWindow.qml +++ b/src/controls/FluWindow.qml @@ -13,9 +13,21 @@ Rectangle { } property string title: "FluentUI" + Behavior on opacity{ + NumberAnimation{ + duration: 100 + } + } + + property var window : { + if(Window.window == null) + return null + return Window.window + } + onIsMaxChanged: { if(isMax){ - root.anchors.margins = 4 + root.anchors.margins = 8 root.anchors.fill = parent }else{ root.anchors.margins = 0 @@ -26,7 +38,17 @@ Rectangle { color : FluApp.isDark ? "#202020" : "#F3F3F3" Component.onCompleted: { + console.debug("onCompleted") + } + Connections{ + target: FluApp + function onWindowReady(view){ + if(FluApp.equalsWindow(view,window)){ + helper.initWindow(view); + helper.setTitle(title); + } + } } WindowHelper{