فهرست منبع

feat: open in running window by default; add new-window url action

Junyi Du 3 سال پیش
والد
کامیت
c7df6fc969
3فایلهای تغییر یافته به همراه26 افزوده شده و 11 حذف شده
  1. 2 4
      src/electron/electron/core.cljs
  2. 18 7
      src/electron/electron/url.cljs
  3. 6 0
      src/electron/electron/window.cljs

+ 2 - 4
src/electron/electron/core.cljs

@@ -213,10 +213,8 @@
 
       (.on app "second-instance"
            (fn [_event _commandLine _workingDirectory]
-             (when-let [win @*win]
-               (when (.isMinimized ^object win)
-                 (.restore win))
-               (.focus win))))
+             (when-let [window @*win]
+               (win/switch-to-window! window))))
 
       (.on app "window-all-closed" (fn []
                                      (try

+ 18 - 7
src/electron/electron/url.cljs

@@ -1,7 +1,8 @@
 (ns electron.url
   (:require [electron.handler :as handler]
             [electron.state :as state]
-            [electron.utils :refer [send-to-renderer]]
+            [electron.window :as win]
+            [electron.utils :refer [send-to-renderer] :as utils]
             [clojure.string :as string]
             [promesa.core :as p]))
 
@@ -31,21 +32,28 @@
   "Given a URL with `graph identifier` as path, `page` (optional) and `block-id` 
    (optional) as parameters, open the local graphs accordingly.
    `graph identifier` is the name of the graph to open, e.g. `lambda`"
-  [^js win parsed-url]
+  [^js win parsed-url force-new-window?]
   (let [graph-identifier (decode (string/replace (.-pathname parsed-url) "/" ""))
         [page-name block-id] (get-URL-decoded-params parsed-url ["page" "block-id"])
         graph-name (when graph-identifier (handler/get-graph-name graph-identifier))]
     (if graph-name
-      (p/let [_ (handler/broadcast-persist-graph! graph-name)]
+      (p/let [window-on-graph (first (win/get-graph-all-windows (utils/get-graph-dir graph-name)))
+              open-new-window? (or force-new-window? (not window-on-graph))
+              _ (when (and force-new-window? window-on-graph)
+                  (handler/broadcast-persist-graph! graph-name))]
           ;; TODO: call open new window on new graph without renderer (remove the reliance on local storage)
           ;; TODO: allow open new window on specific page, without waiting for `graph ready` ipc then redirect to that page
         (when (or page-name block-id)
-          (let [then-f (fn [win' graph-name']
+          (let [redirect-f (fn [win' graph-name']
                          (when (= graph-name graph-name')
                            (send-to-renderer win' "redirectWhenExists" {:page-name page-name
                                                                         :block-id block-id})))]
-            (state/set-state! :window/once-graph-ready then-f)))
-        (send-to-renderer win "openNewWindowOfGraph" graph-name))
+            (if open-new-window?
+              (state/set-state! :window/once-graph-ready redirect-f)
+              (do (win/switch-to-window! window-on-graph)
+                  (redirect-f window-on-graph graph-name)))))
+        (when open-new-window?
+          (send-to-renderer win "openNewWindowOfGraph" graph-name)))
       (graph-identifier-error-handler graph-identifier))))
 
 (defn logseq-url-handler
@@ -57,7 +65,10 @@
 
       ;; identifier of graph in local
       (= "graph" url-host)
-      (local-url-handler win parsed-url)
+      (local-url-handler win parsed-url false)
+
+      (= "new-window" url-host)
+      (local-url-handler win parsed-url true)
 
       :else
       (send-to-renderer "notification" {:type "error"

+ 6 - 0
src/electron/electron/window.cljs

@@ -84,6 +84,12 @@
                          (when @*quitting?
                            (async/put! state/persistent-dbs-chan true)))))))
 
+(defn switch-to-window!
+  [^js win]
+  (when (.isMinimized ^object win)
+    (.restore win))
+  (.focus win))
+
 (defn get-all-windows
   []
   (.getAllWindows BrowserWindow))