Skip to content

Commit 87fc4fb

Browse files
committed
Bit of UI code tidying.
Use JSON.parse for dispatch, save the JS parser some work. SettingsReset -> Settings, closer to its actual meaning. Partially move settings bit into GuiWrapper.
1 parent e6e37e2 commit 87fc4fb

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/gui.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum GuiResponse {
4848
date: String,
4949
version: String,
5050
},
51-
SettingsReset {
51+
Settings {
5252
decimal: bool,
5353
compression: String,
5454
excludes: String,
@@ -76,13 +76,16 @@ impl<T> GuiWrapper<T> {
7676
pub fn new(handle: Handle<T>) -> Self {
7777
let gui = Self(handle);
7878
gui.version();
79+
gui.settings();
7980
gui
8081
}
8182

8283
pub fn send(&self, msg: &GuiResponse) {
8384
let js = format!(
84-
"Response.dispatch({})",
85-
serde_json::to_string(msg).expect("serialize")
85+
"Response.dispatch(JSON.parse({}))",
86+
serde_json::to_string(msg)
87+
.and_then(|s| serde_json::to_string(&s))
88+
.expect("serialize")
8689
);
8790
self.0.dispatch(move |wv| wv.eval(&js)).ok(); // let errors bubble through via messages
8891
}
@@ -95,6 +98,15 @@ impl<T> GuiWrapper<T> {
9598
self.send(&version);
9699
}
97100

101+
pub fn settings(&self) {
102+
let s = settings::get();
103+
self.send(&GuiResponse::Settings {
104+
decimal: s.decimal,
105+
compression: s.compression.to_string(),
106+
excludes: s.excludes.join("\n"),
107+
});
108+
}
109+
98110
pub fn summary(&self, info: FolderSummary) {
99111
self.send(&GuiResponse::FolderSummary { info });
100112
}
@@ -178,7 +190,7 @@ pub fn spawn_gui() {
178190
.invoke_handler(move |mut webview, arg| {
179191
match serde_json::from_str::<GuiRequest>(arg) {
180192
Ok(GuiRequest::OpenUrl { url }) => {
181-
open_url(url);
193+
let _ = open::that(url);
182194
}
183195
Ok(GuiRequest::SaveSettings {
184196
decimal,
@@ -196,7 +208,7 @@ pub fn spawn_gui() {
196208
} else {
197209
message_dispatch(
198210
&mut webview,
199-
&GuiResponse::SettingsReset {
211+
&GuiResponse::Settings {
200212
decimal: s.decimal,
201213
compression: s.compression.to_string(),
202214
excludes: s.excludes.join("\n"),
@@ -215,7 +227,7 @@ pub fn spawn_gui() {
215227

216228
message_dispatch(
217229
&mut webview,
218-
&GuiResponse::SettingsReset {
230+
&GuiResponse::Settings {
219231
decimal: s.decimal,
220232
compression: s.compression.to_string(),
221233
excludes: s.excludes.join("\n"),
@@ -238,16 +250,6 @@ pub fn spawn_gui() {
238250
.expect("WebView");
239251

240252
persistence::load();
241-
let s = settings::get();
242-
243-
message_dispatch(
244-
&mut webview,
245-
&GuiResponse::SettingsReset {
246-
decimal: s.decimal,
247-
compression: s.compression.to_string(),
248-
excludes: s.excludes.join("\n"),
249-
},
250-
);
251253

252254
if !system_supports_compression().unwrap_or_default() {
253255
webview
@@ -299,10 +301,6 @@ fn message_dispatch<T>(wv: &mut web_view::WebView<'_, T>, msg: &GuiResponse) {
299301
wv.eval(&js).ok();
300302
}
301303

302-
fn open_url<U: AsRef<str>>(url: U) {
303-
let _ = open::that(url.as_ref());
304-
}
305-
306304
fn set_dpi_aware() {
307305
use winapi::um::shellscalingapi::{SetProcessDpiAwareness, PROCESS_SYSTEM_DPI_AWARE};
308306

src/ui/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ var Response = (function() {
201201
return {
202202
dispatch: function(msg) {
203203
switch(msg.type) {
204-
case "SettingsReset":
204+
case "Settings":
205205
Gui.set_decimal(msg.decimal);
206206
Gui.set_compression(msg.compression);
207207
Gui.set_excludes(msg.excludes);

0 commit comments

Comments
 (0)