Procházet zdrojové kódy

refactor(core): use tinyfiledialogs-rs for message/confirmation dialogs (#1255)

Lucas Fernandes Nogueira před 4 roky
rodič
revize
6eee355a12

+ 5 - 0
.changes/dialog-api.md

@@ -0,0 +1,5 @@
+---
+"tauri-api": minor
+---
+
+The `message` and `ask` dialogs now use `tinyfiledialogs-rs` instead of `tauri-dialog-rs`.

+ 1 - 1
tauri-api/Cargo.toml

@@ -28,7 +28,7 @@ flate2 = "1.0"
 thiserror = "1.0.23"
 rand = "0.8"
 nfd = "0.0.4"
-tauri-dialog = "0.1.0"
+tinyfiledialogs = "3.3"
 reqwest = { version = "0.11", features = [ "json", "multipart" ] }
 bytes = { version = "1", features = ["serde"] }
 http = "0.2"

+ 28 - 23
tauri-api/src/dialog.rs

@@ -2,8 +2,34 @@ use std::path::Path;
 
 pub use nfd::Response;
 use nfd::{open_dialog, DialogType};
-pub use tauri_dialog::DialogSelection;
-use tauri_dialog::{DialogBuilder, DialogButtons, DialogStyle};
+
+use tinyfiledialogs::{message_box_ok, message_box_yes_no, MessageBoxIcon, YesNo};
+
+/// Response for the ask dialog
+pub enum AskResponse {
+  /// User confirmed.
+  Yes,
+  /// User denied.
+  No,
+}
+
+/// Displays a dialog with a message and an optional title with a "yes" and a "no" button
+pub fn ask(title: impl AsRef<str>, message: impl AsRef<str>) -> AskResponse {
+  match message_box_yes_no(
+    title.as_ref(),
+    message.as_ref(),
+    MessageBoxIcon::Question,
+    YesNo::No,
+  ) {
+    YesNo::Yes => AskResponse::Yes,
+    YesNo::No => AskResponse::No,
+  }
+}
+
+/// Displays a message dialog
+pub fn message(title: impl AsRef<str>, message: impl AsRef<str>) {
+  message_box_ok(title.as_ref(), message.as_ref(), MessageBoxIcon::Info);
+}
 
 fn open_dialog_internal(
   dialog_type: DialogType,
@@ -24,27 +50,6 @@ fn open_dialog_internal(
   }
 }
 
-/// Displays a dialog with a message and an optional title with a "yes" and a "no" button
-pub fn ask(message: impl AsRef<str>, title: impl AsRef<str>) -> DialogSelection {
-  DialogBuilder::new()
-    .message(message.as_ref())
-    .title(title.as_ref())
-    .style(DialogStyle::Question)
-    .buttons(DialogButtons::YesNo)
-    .build()
-    .show()
-}
-
-/// Displays a message dialog
-pub fn message(message: impl AsRef<str>, title: impl AsRef<str>) {
-  DialogBuilder::new()
-    .message(message.as_ref())
-    .title(title.as_ref())
-    .style(DialogStyle::Info)
-    .build()
-    .show();
-}
-
 /// Open single select file dialog
 pub fn select(
   filter_list: Option<impl AsRef<str>>,

+ 11 - 20
tauri/examples/api/src-tauri/Cargo.lock

@@ -2760,33 +2760,14 @@ dependencies = [
  "serde_json",
  "serde_repr",
  "tar",
- "tauri-dialog",
  "tauri-hotkey",
  "tauri-utils",
  "tempfile",
  "thiserror",
+ "tinyfiledialogs",
  "zip",
 ]
 
-[[package]]
-name = "tauri-dialog"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d5797e1fadcc36b32e29b9dc521abba8ebbc1847378ec96917cdcf548ba6fdc"
-dependencies = [
- "tauri-dialog-sys",
-]
-
-[[package]]
-name = "tauri-dialog-sys"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3eba3890c956c0234a27bc5648af00738c81e193351ce5f47faec7efc9156c02"
-dependencies = [
- "cc",
- "pkg-config",
-]
-
 [[package]]
 name = "tauri-hotkey"
 version = "0.0.0"
@@ -2934,6 +2915,16 @@ dependencies = [
  "url",
 ]
 
+[[package]]
+name = "tinyfiledialogs"
+version = "3.3.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c45fb26c3f37d9a8b556e51f6d7f13f685af766017030af56e9247e638aa6194"
+dependencies = [
+ "cc",
+ "libc",
+]
+
 [[package]]
 name = "tinyvec"
 version = "1.1.1"

+ 13 - 22
tauri/examples/communication/src-tauri/Cargo.lock

@@ -2760,33 +2760,14 @@ dependencies = [
  "serde_json",
  "serde_repr",
  "tar",
- "tauri-dialog",
  "tauri-hotkey",
  "tauri-utils",
  "tempfile",
  "thiserror",
+ "tinyfiledialogs",
  "zip",
 ]
 
-[[package]]
-name = "tauri-dialog"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d5797e1fadcc36b32e29b9dc521abba8ebbc1847378ec96917cdcf548ba6fdc"
-dependencies = [
- "tauri-dialog-sys",
-]
-
-[[package]]
-name = "tauri-dialog-sys"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3eba3890c956c0234a27bc5648af00738c81e193351ce5f47faec7efc9156c02"
-dependencies = [
- "cc",
- "pkg-config",
-]
-
 [[package]]
 name = "tauri-hotkey"
 version = "0.0.0"
@@ -2934,6 +2915,16 @@ dependencies = [
  "url",
 ]
 
+[[package]]
+name = "tinyfiledialogs"
+version = "3.3.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c45fb26c3f37d9a8b556e51f6d7f13f685af766017030af56e9247e638aa6194"
+dependencies = [
+ "cc",
+ "libc",
+]
+
 [[package]]
 name = "tinyvec"
 version = "1.1.1"
@@ -3687,9 +3678,9 @@ checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a"
 
 [[package]]
 name = "zip"
-version = "0.5.9"
+version = "0.5.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cc2896475a242c41366941faa27264df2cb935185a92e059a004d0048feb2ac5"
+checksum = "5a8977234acab718eb2820494b2f96cbb16004c19dddf88b7445b27381450997"
 dependencies = [
  "byteorder",
  "bzip2",

+ 11 - 20
tauri/examples/multiwindow/src-tauri/Cargo.lock

@@ -2702,33 +2702,14 @@ dependencies = [
  "serde_json",
  "serde_repr",
  "tar",
- "tauri-dialog",
  "tauri-hotkey",
  "tauri-utils",
  "tempfile",
  "thiserror",
+ "tinyfiledialogs",
  "zip",
 ]
 
-[[package]]
-name = "tauri-dialog"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d5797e1fadcc36b32e29b9dc521abba8ebbc1847378ec96917cdcf548ba6fdc"
-dependencies = [
- "tauri-dialog-sys",
-]
-
-[[package]]
-name = "tauri-dialog-sys"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3eba3890c956c0234a27bc5648af00738c81e193351ce5f47faec7efc9156c02"
-dependencies = [
- "cc",
- "pkg-config",
-]
-
 [[package]]
 name = "tauri-hotkey"
 version = "0.0.0"
@@ -2858,6 +2839,16 @@ dependencies = [
  "url",
 ]
 
+[[package]]
+name = "tinyfiledialogs"
+version = "3.3.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c45fb26c3f37d9a8b556e51f6d7f13f685af766017030af56e9247e638aa6194"
+dependencies = [
+ "cc",
+ "libc",
+]
+
 [[package]]
 name = "tinyvec"
 version = "1.1.1"

+ 7 - 7
tauri/src/endpoints/dialog.rs

@@ -1,6 +1,6 @@
 use crate::api::dialog::{
   ask as ask_dialog, message as message_dialog, pick_folder, save_file, select, select_multiple,
-  DialogSelection, Response,
+  AskResponse, Response,
 };
 use serde::Deserialize;
 use serde_json::Value as JsonValue;
@@ -72,8 +72,8 @@ impl Cmd {
       Self::MessageDialog { message } => {
         let exe = std::env::current_exe()?;
         let app_name = exe
-          .file_name()
-          .expect("failed to get exe filename")
+          .file_stem()
+          .expect("failed to get binary filename")
           .to_string_lossy()
           .to_string();
         message_dialog(app_name, message);
@@ -84,8 +84,8 @@ impl Cmd {
         let answer = ask(
           title.unwrap_or_else(|| {
             exe
-              .file_name()
-              .expect("failed to get exe filename")
+              .file_stem()
+              .expect("failed to get binary filename")
               .to_string_lossy()
               .to_string()
           }),
@@ -131,8 +131,8 @@ pub fn save(options: SaveDialogOptions) -> crate::Result<JsonValue> {
 
 /// Shows a dialog with a yes/no question.
 pub fn ask(title: String, message: String) -> crate::Result<bool> {
-  match ask_dialog(message, title) {
-    DialogSelection::Yes => Ok(true),
+  match ask_dialog(title, message) {
+    AskResponse::Yes => Ok(true),
     _ => Ok(false),
   }
 }

+ 3 - 4
tauri/src/endpoints/notification.rs

@@ -82,20 +82,19 @@ pub fn request_permission() -> crate::Result<String> {
     return Ok(if allow_notification { granted } else { denied });
   }
   let answer = tauri_api::dialog::ask(
-    "This app wants to show notifications. Do you allow?",
     "Permissions",
+    "This app wants to show notifications. Do you allow?",
   );
   match answer {
-    tauri_api::dialog::DialogSelection::Yes => {
+    tauri_api::dialog::AskResponse::Yes => {
       settings.allow_notification = Some(true);
       crate::settings::write_settings(settings)?;
       Ok(granted)
     }
-    tauri_api::dialog::DialogSelection::No => {
+    tauri_api::dialog::AskResponse::No => {
       settings.allow_notification = Some(false);
       crate::settings::write_settings(settings)?;
       Ok(denied)
     }
-    _ => Ok("default".to_string()),
   }
 }