From e9ed05c3baf2296fbd0389f9f199867081aecb78 Mon Sep 17 00:00:00 2001
From: vladest <vladstelmahovsky@gmail.com>
Date: Tue, 15 Aug 2017 15:55:36 +0200
Subject: [PATCH] Reimplement snapshot dir browser for async

---
 .../scripting/WindowScriptingInterface.cpp    | 21 ++++++++++++------
 .../src/scripting/WindowScriptingInterface.h  |  4 ++--
 scripts/system/snapshot.js                    | 22 +++++++++++--------
 3 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/interface/src/scripting/WindowScriptingInterface.cpp b/interface/src/scripting/WindowScriptingInterface.cpp
index 36381b3626..2e6867c30b 100644
--- a/interface/src/scripting/WindowScriptingInterface.cpp
+++ b/interface/src/scripting/WindowScriptingInterface.cpp
@@ -174,8 +174,7 @@ void WindowScriptingInterface::ensureReticleVisible() const {
 /// \param const QString& title title of the window
 /// \param const QString& directory directory to start the file browser at
 /// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog`
-/// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue`
-QScriptValue WindowScriptingInterface::browseDir(const QString& title, const QString& directory) {
+void WindowScriptingInterface::browseDir(const QString& title, const QString& directory) {
     ensureReticleVisible();
     QString path = directory;
     if (path.isEmpty()) {
@@ -184,11 +183,19 @@ QScriptValue WindowScriptingInterface::browseDir(const QString& title, const QSt
 #ifndef Q_OS_WIN
     path = fixupPathForMac(directory);
 #endif
-    QString result = OffscreenUi::getExistingDirectory(nullptr, title, path);
-    if (!result.isEmpty()) {
-        setPreviousBrowseLocation(QFileInfo(result).absolutePath());
-    }
-    return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result);
+    auto offscreenUi = DependencyManager::get<OffscreenUi>();
+    connect(offscreenUi.data(), &OffscreenUi::fileDialogResponse,
+            this, [=] (QString result) {
+        auto offscreenUi = DependencyManager::get<OffscreenUi>();
+        disconnect(offscreenUi.data(), &OffscreenUi::fileDialogResponse,
+                this, nullptr);
+        if (!result.isEmpty()) {
+            setPreviousBrowseLocation(QFileInfo(result).absolutePath());
+        }
+        emit browseDirChanged(result);
+    });
+
+    OffscreenUi::getExistingDirectoryAsync(nullptr, title, path);
 }
 
 /// Display an open file dialog.  If `directory` is an invalid file or directory the browser will start at the current
diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h
index f8ed20f42f..3de5ec43a3 100644
--- a/interface/src/scripting/WindowScriptingInterface.h
+++ b/interface/src/scripting/WindowScriptingInterface.h
@@ -53,7 +53,7 @@ public slots:
     QScriptValue confirm(const QString& message = "");
     QScriptValue prompt(const QString& message = "", const QString& defaultText = "");
     CustomPromptResult customPrompt(const QVariant& config);
-    QScriptValue browseDir(const QString& title = "", const QString& directory = "");
+    void browseDir(const QString& title = "", const QString& directory = "");
     QScriptValue browse(const QString& title = "", const QString& directory = "",  const QString& nameFilter = "");
     QScriptValue save(const QString& title = "", const QString& directory = "",  const QString& nameFilter = "");
     QScriptValue browseAssets(const QString& title = "", const QString& directory = "", const QString& nameFilter = "");
@@ -87,7 +87,7 @@ signals:
     void announcement(const QString& message);
 
     void messageBoxClosed(int id, int button);
-
+    void browseDirChanged(QString browseDir);
     // triggered when window size or position changes
     void geometryChanged(QRect geometry);
 
diff --git a/scripts/system/snapshot.js b/scripts/system/snapshot.js
index 37618253ee..08614c2030 100644
--- a/scripts/system/snapshot.js
+++ b/scripts/system/snapshot.js
@@ -120,15 +120,8 @@ function onMessage(message) {
             openLoginWindow();
             break;
         case 'chooseSnapshotLocation':
-            var snapshotPath = Window.browseDir("Choose Snapshots Directory", "", "");
-
-            if (snapshotPath) { // not cancelled
-                Snapshot.setSnapshotsLocation(snapshotPath);
-                tablet.emitScriptEvent(JSON.stringify({
-                    type: "snapshot",
-                    action: "snapshotLocationChosen"
-                }));
-            }
+            Window.browseDirChanged.connect(snapshotDirChanged);
+            Window.browseDir("Choose Snapshots Directory", "", "");
             break;
         case 'openSettings':
             if ((HMD.active && Settings.getValue("hmdTabletBecomesToolbar", false))
@@ -579,6 +572,17 @@ function stillSnapshotTaken(pathStillSnapshot, notify) {
     });
 }
 
+function snapshotDirChanged(snapshotPath) {
+    Window.browseDirChanged.disconnect(snapshotDirChanged);
+    if (snapshotPath !== "") { // not cancelled
+        Snapshot.setSnapshotsLocation(snapshotPath);
+        tablet.emitScriptEvent(JSON.stringify({
+            type: "snapshot",
+            action: "snapshotLocationChosen"
+        }));
+    }
+}
+
 function processingGifStarted(pathStillSnapshot) {
     Window.processingGifStarted.disconnect(processingGifStarted);
     Window.processingGifCompleted.connect(processingGifCompleted);