Sfoglia il codice sorgente

[feat] add dialog title option. closes #3232 (#3233)

Jonas Kruckenberg 3 anni fa
parent
commit
ce03909fb6

+ 6 - 0
core/tauri/src/api/dialog.rs

@@ -70,6 +70,12 @@ impl FileDialogBuilder {
     self
   }
 
+  /// Set the title of the dialog.
+  pub fn set_title(mut self, title: &str) -> Self {
+    self.0 = self.0.set_title(title);
+    self
+  }
+
   /// Pick one file.
   pub fn pick_file<F: FnOnce(Option<PathBuf>) + Send + 'static>(self, f: F) {
     run_dialog!(self.0.pick_file(), f)

+ 11 - 0
core/tauri/src/endpoints/dialog.rs

@@ -26,6 +26,8 @@ pub struct DialogFilter {
 #[derive(Deserialize)]
 #[serde(rename_all = "camelCase")]
 pub struct OpenDialogOptions {
+  /// The title of the dialog window.
+  pub title: Option<String>,
   /// The filters of the dialog.
   #[serde(default)]
   pub filters: Vec<DialogFilter>,
@@ -43,6 +45,8 @@ pub struct OpenDialogOptions {
 #[derive(Deserialize)]
 #[serde(rename_all = "camelCase")]
 pub struct SaveDialogOptions {
+  /// The title of the dialog window.
+  pub title: Option<String>,
   /// The filters of the dialog.
   #[serde(default)]
   pub filters: Vec<DialogFilter>,
@@ -156,6 +160,9 @@ pub fn open<R: Runtime>(
     let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
     dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
   }
+  if let Some(title) = options.title {
+    dialog_builder = dialog_builder.set_title(&title);
+  }
 
   let (tx, rx) = channel();
 
@@ -189,6 +196,10 @@ pub fn save<R: Runtime>(
     let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
     dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
   }
+  if let Some(title) = options.title {
+    dialog_builder = dialog_builder.set_title(&title);
+  }
+
   let (tx, rx) = channel();
   dialog_builder.save_file(move |p| tx.send(p).unwrap());
   Ok(rx.recv().unwrap().into())

+ 4 - 2
examples/api/src-tauri/src/main.rs

@@ -123,7 +123,8 @@ fn main() {
             };
             item_handle.set_title(new_title).unwrap();
           }
-          "new" => app
+          "new" => {
+            app
             .create_window(
               "new",
               WindowUrl::App("index.html".into()),
@@ -131,7 +132,8 @@ fn main() {
                 (window_builder.title("Tauri"), webview_attributes)
               },
             )
-            .unwrap(),
+            .unwrap();
+          },
           #[cfg(target_os = "macos")]
           "icon_1" => {
             app.tray_handle().set_icon_as_template(true).unwrap();

+ 2 - 0
examples/api/src/components/Dialog.svelte

@@ -22,6 +22,7 @@
 
   function openDialog() {
     open({
+      title: 'My wonderful open dialog',
       defaultPath,
       filters: filter
         ? [
@@ -69,6 +70,7 @@
 
   function saveDialog() {
     save({
+      title: 'My wonderful save dialog',
       defaultPath,
       filters: filter
         ? [

+ 4 - 0
tooling/api/src/dialog.ts

@@ -43,6 +43,8 @@ interface DialogFilter {
 
 /** Options for the open dialog. */
 interface OpenDialogOptions {
+  /** The title of the dialog window. */
+  title?: string
   /** The filters of the dialog. */
   filters?: DialogFilter[]
   /** Initial directory or file path. */
@@ -55,6 +57,8 @@ interface OpenDialogOptions {
 
 /** Options for the save dialog. */
 interface SaveDialogOptions {
+  /** The title of the dialog window. */
+  title?: string
   /** The filters of the dialog. */
   filters?: DialogFilter[]
   /**