Skip to content

Commit 6b57ec2

Browse files
authored
Persist enhancements (#86)
* initial commit * remove system language from db * fix native theme effect * reintroduce fcus on pointer down * enhance error handling * various fixes * refactor * enhance performance * fix tests * add to-fixed tests * fix translations * fix naming and remove lang dir from db * fix dir
1 parent ed79bba commit 6b57ec2

30 files changed

+351
-284
lines changed

deps.edn

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{:paths ["src"]
2-
:deps {akiroz.re-frame/storage {:mvn/version "0.1.4"}
3-
binaryage/devtools {:mvn/version "1.0.7"}
2+
:deps {binaryage/devtools {:mvn/version "1.0.7"}
43
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.3"}
54
clj-kdtree/clj-kdtree {:git/url "https://github.com/abscondment/clj-kdtree.git"
65
:sha "5ec321c5e8006db00fa8b45a8ed9eb0b8f3dd56d"

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"karma-cljs-test": "0.1.0",
105105
"karma-electron": "7.3.0",
106106
"karma-junit-reporter": "2.0.1",
107+
"localforage": "1.10.0",
107108
"mdn-data": "2.21.0",
108109
"npm-run-all": "4.1.5",
109110
"opentype.js": "1.3.4",

src/config.cljs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
(def ext "rps")
88

9-
(def app-key :repath-studio)
10-
119
(def app-name "Repath Studio")
1210

1311
(def mime-type "application/x-repath-studio")

src/electron/main.cljs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
(when (and (secure-url? url-parsed) (allowed-url? url-parsed))
4545
(.openExternal shell url-parsed.href))))
4646

47-
(defn register-ipc-on-events!
48-
[]
47+
(defn register-ipc-on-events! []
4948
(doseq
5049
[[e f]
51-
[["relaunch" #(do (.relaunch app) (.exit app))]
50+
[["initialized" #(.close ^js @loading-window)]
51+
["relaunch" #(doto app (.relaunch) (.exit))]
5252
["open-remote-url" open-external!]
5353
["open-directory" #(.showItemInFolder shell %)]
5454
["window-minimize" #(.minimize ^js @main-window)]
@@ -59,8 +59,7 @@
5959
(.maximize ^js @main-window))]]]
6060
(.on ipcMain e #(f %2))))
6161

62-
(defn register-ipc-handle-events!
63-
[]
62+
(defn register-ipc-handle-events! []
6463
(doseq
6564
[[e f]
6665
[["open-documents" file/open!]
@@ -70,8 +69,7 @@
7069
["print" file/print!]]]
7170
(.handle ipcMain e #(f %2))))
7271

73-
(defn register-window-events!
74-
[]
72+
(defn register-window-events! []
7573
(doseq
7674
[[window-event f]
7775
[["maximize" #(send-to-renderer! "window-maximized")]
@@ -87,31 +85,29 @@
8785
"window-unmaximized"))]]]
8886
(.on ^js @main-window window-event f)))
8987

90-
(defn register-web-contents-events!
91-
[]
92-
(doseq
93-
[[web-contents-event f]
94-
[["will-frame-navigate" #(.preventDefault %)]
95-
["closed" #(reset! main-window nil)]]]
96-
(.on (.-webContents ^js @main-window) web-contents-event f)))
88+
(defn register-web-contents-events! []
89+
(let [web-contents (.-webContents ^js @main-window)]
90+
(doseq
91+
[[web-contents-event f]
92+
[["will-frame-navigate" #(.preventDefault %)]
93+
["closed" #(reset! main-window nil)]]]
94+
(.on web-contents web-contents-event f))))
9795

98-
(defn handle-on-ready-to-show!
96+
(defn on-ready-to-show!
9997
[^js window]
10098
(doseq
10199
[action
102100
[(if (.isMaximized window) "window-maximized" "window-unmaximized")
103101
(if (.isFullScreen window) "window-entered-fullscreen" "window-leaved-fullscreen")
104-
(if (.isFocused window) "window-focused" "window-blurred")
105-
"window-loaded"]]
102+
(if (.isFocused window) "window-focused" "window-blurred")]]
106103
(send-to-renderer! action)))
107104

108105
(defn resource-path
109106
[s]
110107
(url/format #js {:pathname (.join path js/__dirname s)
111108
:protocol "file"}))
112109

113-
(defn init-main-window!
114-
[]
110+
(defn init-main-window! []
115111
(let [win-state (window-state-keeper #js {:defaultWidth 1920
116112
:defaultHeight 1080})]
117113
(reset! main-window
@@ -120,12 +116,12 @@
120116
:y (.-y win-state)
121117
:width (.-width win-state)
122118
:height (.-height win-state)
123-
:backgroundColor "#313131"
124119
:titleBarStyle (when (= (.platform os) "darwin") "hidden")
125120
:trafficLightPosition #js {:x 8 :y 10}
126121
:icon (.join path js/__dirname "/public/img/icon.png")
127122
:frame false
128123
:show false
124+
:transparent true
129125
:webPreferences
130126
#js {:sandbox false
131127
:preload (.join path js/__dirname "preload.js")}}))
@@ -134,11 +130,9 @@
134130
"ready-to-show"
135131
(fn []
136132
(.show ^js @main-window)
137-
(.manage win-state ^js @main-window)
138-
(.hide ^js @loading-window)
139-
(.close ^js @loading-window)))
133+
(.manage win-state ^js @main-window)))
140134

141-
(.on ^js @main-window "ready-to-show" #(handle-on-ready-to-show! @main-window))
135+
(.on ^js @main-window "ready-to-show" #(on-ready-to-show! @main-window))
142136

143137
(.loadURL ^js @main-window
144138
(if config/debug?
@@ -156,9 +150,10 @@
156150
(BrowserWindow.
157151
#js {:width 720
158152
:height 576
159-
:backgroundColor "#313131"
160153
:icon (.join path js/__dirname "/public/img/icon.png")
161154
:show false
155+
:alwaysOnTop true
156+
:transparent true
162157
:frame false}))
163158
(.once ^js @loading-window "show" init-main-window!)
164159
(.loadURL ^js @loading-window (resource-path "/public/loading.html"))

src/lang/ar-EG.edn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
:source-code "الكود المصدري"
1515
:changelog "سجل التغييرات"}
1616

17+
:renderer.app.events
18+
{:create-doc "إنشاء مستند"}
19+
1720
:renderer.dialog.views
1821
{:search-command "البحث عن أمر"
1922
:no-results "لا توجد نتائج."
@@ -502,7 +505,6 @@
502505
:renderer.document.events
503506
{:save-changes "هل تريد حفظ التغييرات؟"
504507
:create-doc "إنشاء مستند"
505-
:init-doc "تهيئة مستند"
506508
:create-doc-from-template "إنشاء مستند من قالب"
507509
:load-doc "تحميل مستند"
508510
:error-loading "خطأ أثناء تحميل %1"

src/lang/el-GR.edn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
:source-code "Πηγαίος κώδικας"
1515
:changelog "Αρχείο αλλαγών"}
1616

17+
:renderer.app.events
18+
{:create-doc "Δημιουργία εγγράφου"}
19+
1720
:renderer.dialog.views
1821
{:search-command "Αναζήτηση εντολής"
1922
:no-results "Δεν βρέθηκαν αποτελέσματα."
@@ -546,7 +549,6 @@
546549
:renderer.document.events
547550
{:save-changes "Θέλετε να αποθηκεύσετε τις αλλαγές σας;"
548551
:create-doc "Δημιουργία εγγράφου"
549-
:init-doc "Αρχικοποίηση εγγράφου"
550552
:create-doc-from-template "Δημιουργία εγγράφου από πρότυπο"
551553
:load-doc "Φόρτωση εγγράφου"
552554
:error-loading "Σφάλμα κατα τη φόρτωση %1"

src/lang/en-US.edn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
:source-code "Source Code"
1515
:changelog "Changelog"}
1616

17+
:renderer.app.events
18+
{:create-doc "Create document"}
19+
1720
:renderer.dialog.views
1821
{:search-command "Search for a command"
1922
:no-results "No results found."
@@ -518,7 +521,6 @@
518521
:renderer.document.events
519522
{:save-changes "Do you want to save your changes?"
520523
:create-doc "Create document"
521-
:init-doc "Init document"
522524
:create-doc-from-template "Create document from template"
523525
:load-doc "Load document"
524526
:error-loading "Error while loading %1"

src/renderer/app/db.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@
6565
[:notifications {:default []} [:* Notification]]
6666
[:debug-info {:default false} boolean?]
6767
[:help-bar {:default true} boolean?]
68+
[:loading {:default true} boolean?]
6869
[:pen-mode {:default false} boolean?]
6970
[:backdrop {:default false} boolean?]
7071
[:lang {:optional true :persist true} Lang]
71-
[:dir {:optional true :persist true} string?]
72-
[:system-lang {:optional true} string?]
7372
[:platform {:optional true} Platform]
7473
[:versions {:optional true} [:maybe map?]]
7574
[:env {:optional true} [:maybe map?]]

src/renderer/app/effects.cljs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(ns renderer.app.effects
22
(:require
3-
[akiroz.re-frame.storage :as rf.storage]
3+
["localforage" :as localforage]
4+
[cognitect.transit :as transit]
45
[config :as config]
56
[re-frame.core :as rf]
67
[re-frame.db :as rf.db]
78
[renderer.app.db :as app.db]
8-
[renderer.history.handlers :as history.handlers]))
9-
10-
(rf.storage/reg-co-fx! config/app-key {:cofx :store})
9+
[renderer.history.handlers :as history.handlers]
10+
[renderer.notification.events :as-alias notification.events]))
1111

1212
(rf/reg-cofx
1313
::platform
@@ -36,29 +36,42 @@
3636
(assoc coeffects :user-agent (.-userAgent js/navigator))))
3737

3838
(rf/reg-cofx
39-
::system-language
39+
::language
4040
(fn [coeffects _]
41-
(assoc coeffects :system-language (.-language js/navigator))))
41+
(assoc coeffects :language (.-language js/navigator))))
4242

4343
(rf/reg-fx
4444
::query-local-fonts
4545
(fn [{:keys [on-success on-error formatter]}]
4646
(when-not (undefined? js/window.queryLocalFonts)
4747
(-> (.queryLocalFonts js/window)
48-
(.then #(when on-success (rf/dispatch (conj on-success
49-
(cond-> %
50-
formatter formatter)))))
48+
(.then #(when on-success (rf/dispatch (conj on-success (cond-> %
49+
formatter formatter)))))
5150
(.catch #(when on-error (rf/dispatch (conj on-error %))))))))
5251

52+
(defn json->clj
53+
[json]
54+
(transit/read (transit/reader :json) json))
55+
56+
(rf/reg-fx
57+
::get-local-db
58+
(fn [{:keys [on-success on-error on-finally]}]
59+
(-> (localforage/getItem config/app-name)
60+
(.then #(when on-success (rf/dispatch (conj on-success (json->clj %)))))
61+
(.catch #(when on-error (rf/dispatch (conj on-error %))))
62+
(.finally #(when on-finally (rf/dispatch on-finally))))))
63+
5364
(rf/reg-fx
5465
::persist
5566
(fn []
5667
(let [db @rf.db/app-db
5768
db (cond-> db
5869
(:active-document db)
5970
history.handlers/drop-rest)]
60-
(->> (select-keys db app.db/persisted-keys)
61-
(rf.storage/->store config/app-key)))))
71+
(.catch (->> (select-keys db app.db/persisted-keys)
72+
(transit/write (transit/writer :json))
73+
(localforage/setItem config/app-name))
74+
#(rf/dispatch [::notification.events/show-exception %])))))
6275

6376
(rf/reg-fx
6477
::validate
@@ -70,4 +83,4 @@
7083
(rf/reg-fx
7184
::clear-local-storage
7285
(fn []
73-
(rf.storage/->store config/app-key {})))
86+
(localforage/clear)))

0 commit comments

Comments
 (0)