Skip to content

Commit 35ea595

Browse files
authored
fix(dialog): Create dialogs on main thread (#1073)
fixes tauri-apps/tauri#6301
1 parent 14c8583 commit 35ea595

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

.changes/dialog-main-thread.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
dialog: patch
3+
---
4+
5+
Fixed an issue where the dialog apis panicked when they were called with no application windows open.

examples/api/src-tauri/gen/schemas/desktop-schema.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@
23622362
"type": "object",
23632363
"required": [
23642364
"args",
2365-
"command",
2365+
"cmd",
23662366
"name",
23672367
"sidecar"
23682368
],
@@ -2375,7 +2375,7 @@
23752375
}
23762376
]
23772377
},
2378-
"command": {
2378+
"cmd": {
23792379
"description": "The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`, `$APPCONFIG`, `$APPDATA`, `$APPLOCALDATA`, `$APPCACHE`, `$APPLOG`.",
23802380
"type": "string"
23812381
},
@@ -2397,7 +2397,7 @@
23972397
"type": "object",
23982398
"required": [
23992399
"args",
2400-
"command",
2400+
"cmd",
24012401
"name",
24022402
"sidecar"
24032403
],
@@ -2410,7 +2410,7 @@
24102410
}
24112411
]
24122412
},
2413-
"command": {
2413+
"cmd": {
24142414
"description": "The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`, `$APPCONFIG`, `$APPDATA`, `$APPLOCALDATA`, `$APPCACHE`, `$APPLOG`.",
24152415
"type": "string"
24162416
},

plugins/dialog/src/desktop.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ impl<R: Runtime> Dialog<R> {
4242
}
4343
}
4444

45-
macro_rules! run_dialog {
46-
($e:expr, $h: expr) => {{
47-
std::thread::spawn(move || $h(tauri::async_runtime::block_on($e)));
48-
}};
49-
}
50-
51-
macro_rules! run_file_dialog {
52-
($e:expr, $h: ident) => {{
53-
std::thread::spawn(move || $h(tauri::async_runtime::block_on($e)));
54-
}};
55-
}
56-
5745
impl From<MessageDialogKind> for rfd::MessageLevel {
5846
fn from(kind: MessageDialogKind) -> Self {
5947
match kind {
@@ -132,7 +120,11 @@ pub fn pick_file<R: Runtime, F: FnOnce(Option<PathBuf>) + Send + 'static>(
132120
f: F,
133121
) {
134122
let f = |path: Option<rfd::FileHandle>| f(path.map(|p| p.path().to_path_buf()));
135-
run_file_dialog!(AsyncFileDialog::from(dialog).pick_file(), f)
123+
let handle = dialog.dialog.app_handle().to_owned();
124+
let _ = handle.run_on_main_thread(move || {
125+
let dialog = AsyncFileDialog::from(dialog).pick_file();
126+
std::thread::spawn(move || f(tauri::async_runtime::block_on(dialog)));
127+
});
136128
}
137129

138130
pub fn pick_files<R: Runtime, F: FnOnce(Option<Vec<PathBuf>>) + Send + 'static>(
@@ -142,15 +134,23 @@ pub fn pick_files<R: Runtime, F: FnOnce(Option<Vec<PathBuf>>) + Send + 'static>(
142134
let f = |paths: Option<Vec<rfd::FileHandle>>| {
143135
f(paths.map(|list| list.into_iter().map(|p| p.path().to_path_buf()).collect()))
144136
};
145-
run_file_dialog!(AsyncFileDialog::from(dialog).pick_files(), f)
137+
let handle = dialog.dialog.app_handle().to_owned();
138+
let _ = handle.run_on_main_thread(move || {
139+
let dialog = AsyncFileDialog::from(dialog).pick_files();
140+
std::thread::spawn(move || f(tauri::async_runtime::block_on(dialog)));
141+
});
146142
}
147143

148144
pub fn pick_folder<R: Runtime, F: FnOnce(Option<PathBuf>) + Send + 'static>(
149145
dialog: FileDialogBuilder<R>,
150146
f: F,
151147
) {
152148
let f = |path: Option<rfd::FileHandle>| f(path.map(|p| p.path().to_path_buf()));
153-
run_file_dialog!(AsyncFileDialog::from(dialog).pick_folder(), f)
149+
let handle = dialog.dialog.app_handle().to_owned();
150+
let _ = handle.run_on_main_thread(move || {
151+
let dialog = AsyncFileDialog::from(dialog).pick_folder();
152+
std::thread::spawn(move || f(tauri::async_runtime::block_on(dialog)));
153+
});
154154
}
155155

156156
pub fn pick_folders<R: Runtime, F: FnOnce(Option<Vec<PathBuf>>) + Send + 'static>(
@@ -160,15 +160,23 @@ pub fn pick_folders<R: Runtime, F: FnOnce(Option<Vec<PathBuf>>) + Send + 'static
160160
let f = |paths: Option<Vec<rfd::FileHandle>>| {
161161
f(paths.map(|list| list.into_iter().map(|p| p.path().to_path_buf()).collect()))
162162
};
163-
run_file_dialog!(AsyncFileDialog::from(dialog).pick_folders(), f)
163+
let handle = dialog.dialog.app_handle().to_owned();
164+
let _ = handle.run_on_main_thread(move || {
165+
let dialog = AsyncFileDialog::from(dialog).pick_folders();
166+
std::thread::spawn(move || f(tauri::async_runtime::block_on(dialog)));
167+
});
164168
}
165169

166170
pub fn save_file<R: Runtime, F: FnOnce(Option<PathBuf>) + Send + 'static>(
167171
dialog: FileDialogBuilder<R>,
168172
f: F,
169173
) {
170174
let f = |path: Option<rfd::FileHandle>| f(path.map(|p| p.path().to_path_buf()));
171-
run_file_dialog!(AsyncFileDialog::from(dialog).save_file(), f)
175+
let handle = dialog.dialog.app_handle().to_owned();
176+
let _ = handle.run_on_main_thread(move || {
177+
let dialog = AsyncFileDialog::from(dialog).save_file();
178+
std::thread::spawn(move || f(tauri::async_runtime::block_on(dialog)));
179+
});
172180
}
173181

174182
/// Shows a message dialog
@@ -187,5 +195,9 @@ pub fn show_message_dialog<R: Runtime, F: FnOnce(bool) + Send + 'static>(
187195
});
188196
};
189197

190-
run_dialog!(AsyncMessageDialog::from(dialog).show(), f);
198+
let handle = dialog.dialog.app_handle().to_owned();
199+
let _ = handle.run_on_main_thread(move || {
200+
let dialog = AsyncMessageDialog::from(dialog).show();
201+
std::thread::spawn(move || f(tauri::async_runtime::block_on(dialog)));
202+
});
191203
}

plugins/updater/permissions/schemas/schema.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,14 @@
139139
},
140140
"platforms": {
141141
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
142-
"type": [
143-
"array",
144-
"null"
142+
"default": [
143+
"linux",
144+
"macOS",
145+
"windows",
146+
"android",
147+
"iOS"
145148
],
149+
"type": "array",
146150
"items": {
147151
"$ref": "#/definitions/Target"
148152
}

0 commit comments

Comments
 (0)