From 7290b98fdba61953b072a721bc4649644abab4d3 Mon Sep 17 00:00:00 2001
From: zhuzichu <zhuzichu520@gmail.com>
Date: Tue, 11 Jul 2023 16:43:28 +0800
Subject: [PATCH] update

---
 example/qml/App.qml                           |  1 +
 example/qml/global/ItemsOriginal.qml          |  6 ++
 example/qml/window/HotloadWindow.qml          | 85 +++++++++++++++++++
 example/src/component/FileWatcher.cpp         | 26 ++++++
 example/src/component/FileWatcher.h           | 21 +++++
 example/src/main.cpp                          |  4 +-
 .../FluentUI/Controls/FluRemoteLoader.qml     |  3 +
 7 files changed, 145 insertions(+), 1 deletion(-)
 create mode 100644 example/qml/window/HotloadWindow.qml
 create mode 100644 example/src/component/FileWatcher.cpp
 create mode 100644 example/src/component/FileWatcher.h

diff --git a/example/qml/App.qml b/example/qml/App.qml
index eefb1a74..954ea120 100644
--- a/example/qml/App.qml
+++ b/example/qml/App.qml
@@ -15,6 +15,7 @@ Window {
             "/":"qrc:/example/qml/window/MainWindow.qml",
             "/about":"qrc:/example/qml/window/AboutWindow.qml",
             "/login":"qrc:/example/qml/window/LoginWindow.qml",
+            "/hotload":"qrc:/example/qml/window/HotloadWindow.qml",
             "/singleTaskWindow":"qrc:/example/qml/window/SingleTaskWindow.qml",
             "/standardWindow":"qrc:/example/qml/window/StandardWindow.qml",
             "/singleInstanceWindow":"qrc:/example/qml/window/SingleInstanceWindow.qml"
diff --git a/example/qml/global/ItemsOriginal.qml b/example/qml/global/ItemsOriginal.qml
index aa8852ca..1c4d8b7b 100644
--- a/example/qml/global/ItemsOriginal.qml
+++ b/example/qml/global/ItemsOriginal.qml
@@ -333,6 +333,12 @@ FluObject{
                 navigationView.push("qrc:/example/qml/page/T_RemoteLoader.qml")
             }
         }
+        FluPaneItem{
+            title:"HotLoader"
+            tapFunc:function(){
+                 FluApp.navigate("/hotload")
+            }
+        }
     }
 
     function getRecentlyAddedData(){
diff --git a/example/qml/window/HotloadWindow.qml b/example/qml/window/HotloadWindow.qml
new file mode 100644
index 00000000..5343d0dd
--- /dev/null
+++ b/example/qml/window/HotloadWindow.qml
@@ -0,0 +1,85 @@
+import QtQuick
+import QtQuick.Controls
+import QtQuick.Layouts
+import FluentUI
+import example
+import "qrc:///example/qml/component"
+
+CustomWindow {
+
+    id:window
+    title:"热加载"
+    width: 800
+    height: 600
+    minimumWidth: 520
+    minimumHeight: 200
+    launchMode: FluWindow.SingleTask
+    FileWatcher{
+        id:watcher
+        onFileChanged: {
+            loader.reload()
+        }
+    }
+    FluArea{
+        anchors.fill: parent
+        FluRemoteLoader{
+            id:loader
+            anchors.fill: parent
+            statusMode: FluStatusView.Success
+            lazy: true
+            errorItem: Item{
+                FluText{
+                    text:loader.itemLodaer().sourceComponent.errorString()
+                    color:"red"
+                    anchors.fill: parent
+                    wrapMode: Text.WrapAnywhere
+                    padding: 20
+                    verticalAlignment: Qt.AlignVCenter
+                    horizontalAlignment: Qt.AlignHCenter
+                }
+            }
+        }
+        FluText{
+            text:"拖入qml文件"
+            font.pixelSize: 26
+            anchors.centerIn: parent
+            visible: !loader.itemLodaer().item && loader.statusMode === FluStatusView.Success
+        }
+        Rectangle{
+            radius: 4
+            anchors.fill: parent
+            color: "#33333333"
+            visible: drop_area.containsDrag
+        }
+        DropArea{
+            id:drop_area
+            anchors.fill: parent
+            onEntered:
+                (event)=>{
+                    if(!event.hasUrls){
+                        event.accepted = false
+                        return
+                    }
+                    if (event.urls.length !== 1) {
+                        event.accepted = false
+                        return
+                    }
+                    var url = event.urls[0].toString()
+                    var fileExtension = url.substring(url.lastIndexOf(".") + 1)
+                    if (fileExtension !== "qml") {
+                        event.accepted = false
+                        return
+                    }
+                    return true
+                }
+            onDropped:
+                (event)=>{
+                    var path = event.urls[0].toString()
+                    loader.source = path
+                    watcher.path = path
+                    loader.reload()
+                }
+        }
+    }
+
+}
diff --git a/example/src/component/FileWatcher.cpp b/example/src/component/FileWatcher.cpp
new file mode 100644
index 00000000..d94c9cdf
--- /dev/null
+++ b/example/src/component/FileWatcher.cpp
@@ -0,0 +1,26 @@
+#include "FileWatcher.h"
+
+#include <qDebug>
+
+FileWatcher::FileWatcher(QObject *parent)
+    : QObject{parent}
+{
+    connect(&_watcher, &QFileSystemWatcher::fileChanged, this, [=](const QString &path){
+        Q_EMIT fileChanged();
+        clean();
+        _watcher.addPath(_path);
+    });
+    connect(this,&FileWatcher::pathChanged,this,[=](){
+        clean();
+        _watcher.addPath(_path.replace("file:///",""));
+    });
+    if(!_path.isEmpty()){
+        _watcher.addPath(_path);
+    }
+}
+
+void FileWatcher::clean(){
+    foreach (const QString &item,  _watcher.files()) {
+        _watcher.removePath(item);
+    }
+}
diff --git a/example/src/component/FileWatcher.h b/example/src/component/FileWatcher.h
new file mode 100644
index 00000000..84919492
--- /dev/null
+++ b/example/src/component/FileWatcher.h
@@ -0,0 +1,21 @@
+#ifndef FILEWATCHER_H
+#define FILEWATCHER_H
+
+#include <QObject>
+#include <QFileSystemWatcher>
+#include "src/stdafx.h"
+
+class FileWatcher : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY_AUTO(QString,path);
+public:
+    explicit FileWatcher(QObject *parent = nullptr);
+    Q_SIGNAL void fileChanged();
+private:
+    void clean();
+private:
+    QFileSystemWatcher _watcher;
+};
+
+#endif // FILEWATCHER_H
diff --git a/example/src/main.cpp b/example/src/main.cpp
index 2ac8c945..f7714296 100644
--- a/example/src/main.cpp
+++ b/example/src/main.cpp
@@ -7,11 +7,12 @@
 #include <FramelessHelper/Quick/framelessquickmodule.h>
 #include <FramelessHelper/Core/private/framelessconfig_p.h>
 #include "src/component/CircularReveal.h"
+#include "src/component/FileWatcher.h"
 #include "AppInfo.h"
 
 FRAMELESSHELPER_USE_NAMESPACE
 
-int main(int argc, char *argv[])
+    int main(int argc, char *argv[])
 {
     //将样式设置为Basic,不然会导致组件显示异常
     qputenv("QT_QUICK_CONTROLS_STYLE","Basic");
@@ -41,6 +42,7 @@ int main(int argc, char *argv[])
     engine.addImportPath("qrc:/"); // 让静态资源可以被QML引擎搜索到
 #endif
     qmlRegisterType<CircularReveal>("example", 1, 0, "CircularReveal");
+    qmlRegisterType<FileWatcher>("example", 1, 0, "FileWatcher");
     appInfo->init(&engine);
     const QUrl url(QStringLiteral("qrc:/example/qml/App.qml"));
     QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
diff --git a/src/imports/FluentUI/Controls/FluRemoteLoader.qml b/src/imports/FluentUI/Controls/FluRemoteLoader.qml
index 759d78f7..0a1794e5 100644
--- a/src/imports/FluentUI/Controls/FluRemoteLoader.qml
+++ b/src/imports/FluentUI/Controls/FluRemoteLoader.qml
@@ -33,4 +33,7 @@ FluStatusView {
         var timestamp = Date.now();
         loader.source = control.source+"?"+timestamp
     }
+    function itemLodaer(){
+        return loader
+    }
 }