From e2914d8b7cead1b5095ff0ed4e3eef8c480d2f16 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Fri, 18 Nov 2022 18:31:47 +0100 Subject: [PATCH] fix: make builder methods chainable --- examples/test/src/dialog.rs | 66 ++++++++++++++++------------------ src/dialog.rs | 39 +++++++++++--------- src/window.rs | 71 ++++++++++++++++++++++++------------- 3 files changed, 101 insertions(+), 75 deletions(-) diff --git a/examples/test/src/dialog.rs b/examples/test/src/dialog.rs index d6a8093..72c384f 100644 --- a/examples/test/src/dialog.rs +++ b/examples/test/src/dialog.rs @@ -2,11 +2,9 @@ use anyhow::ensure; use tauri_sys::dialog::{FileDialogBuilder, MessageDialogBuilder, MessageDialogKind}; pub async fn ask() -> anyhow::Result<()> { - let mut builder = MessageDialogBuilder::new(); - builder.set_title("Tauri"); - builder.set_kind(MessageDialogKind::Warning); - - let works = builder + let works = MessageDialogBuilder::new() + .set_title("Tauri") + .set_kind(MessageDialogKind::Warning) .ask("Does this work? \n Click Yes to mark this test as passing") .await?; @@ -16,11 +14,9 @@ pub async fn ask() -> anyhow::Result<()> { } pub async fn confirm() -> anyhow::Result<()> { - let mut builder = MessageDialogBuilder::new(); - builder.set_title("Tauri"); - builder.set_kind(MessageDialogKind::Warning); - - let works = builder + let works = MessageDialogBuilder::new() + .set_title("Tauri") + .set_kind(MessageDialogKind::Warning) .confirm("Does this work? \n Click Ok to mark this test as passing") .await?; @@ -30,20 +26,20 @@ pub async fn confirm() -> anyhow::Result<()> { } pub async fn message() -> anyhow::Result<()> { - let mut builder = MessageDialogBuilder::new(); - builder.set_title("Tauri"); - builder.set_kind(MessageDialogKind::Warning); - - builder.message("This is a message just for you!").await?; + MessageDialogBuilder::new() + .set_title("Tauri") + .set_kind(MessageDialogKind::Warning) + .message("This is a message just for you!") + .await?; Ok(()) } pub async fn pick_file() -> anyhow::Result<()> { - let mut builder = FileDialogBuilder::new(); - builder.set_title("Select a file to mark this test as passing"); - - let file = builder.pick_file().await?; + let file = FileDialogBuilder::new() + .set_title("Select a file to mark this test as passing") + .pick_file() + .await?; ensure!(file.is_some()); @@ -51,10 +47,10 @@ pub async fn pick_file() -> anyhow::Result<()> { } pub async fn pick_files() -> anyhow::Result<()> { - let mut builder = FileDialogBuilder::new(); - builder.set_title("Select a multiple files to mark this test as passing"); - - let file = builder.pick_files().await?; + let file = FileDialogBuilder::new() + .set_title("Select a multiple files to mark this test as passing") + .pick_files() + .await?; ensure!(file.is_some()); ensure!(file.unwrap().len() > 1); @@ -63,10 +59,10 @@ pub async fn pick_files() -> anyhow::Result<()> { } pub async fn pick_folder() -> anyhow::Result<()> { - let mut builder = FileDialogBuilder::new(); - builder.set_title("Select a folder to mark this test as passing"); - - let file = builder.pick_folder().await?; + let file = FileDialogBuilder::new() + .set_title("Select a folder to mark this test as passing") + .pick_folder() + .await?; ensure!(file.is_some()); @@ -74,10 +70,10 @@ pub async fn pick_folder() -> anyhow::Result<()> { } pub async fn pick_folders() -> anyhow::Result<()> { - let mut builder = FileDialogBuilder::new(); - builder.set_title("Select a multiple folders to mark this test as passing"); - - let file = builder.pick_folders().await?; + let file = FileDialogBuilder::new() + .set_title("Select a multiple folders to mark this test as passing") + .pick_folders() + .await?; ensure!(file.is_some()); ensure!(file.unwrap().len() > 1); @@ -86,10 +82,10 @@ pub async fn pick_folders() -> anyhow::Result<()> { } pub async fn save() -> anyhow::Result<()> { - let mut builder = FileDialogBuilder::new(); - builder.set_title("Select a file to mark this test as passing"); - - let file = builder.save().await?; + let file = FileDialogBuilder::new() + .set_title("Select a file to mark this test as passing") + .save() + .await?; ensure!(file.is_some()); diff --git a/src/dialog.rs b/src/dialog.rs index 1e64f20..c21b294 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -49,8 +49,9 @@ impl<'a> FileDialogBuilder<'a> { } /// Set starting file name or directory of the dialog. - pub fn set_default_path(&mut self, default_path: &'a Path) { + pub fn set_default_path(&mut self, default_path: &'a Path) -> &mut Self { self.default_path = Some(default_path); + self } /// If directory is true, indicates that it will be read recursively later. @@ -66,8 +67,9 @@ impl<'a> FileDialogBuilder<'a> { /// # Ok(()) /// # } /// ``` - pub fn set_recursive(&mut self, recursive: bool) { + pub fn set_recursive(&mut self, recursive: bool) -> &mut Self { self.recursive = recursive; + self } /// Set the title of the dialog. @@ -82,8 +84,9 @@ impl<'a> FileDialogBuilder<'a> { /// # Ok(()) /// # } /// ``` - pub fn set_title(&mut self, title: &'a str) { + pub fn set_title(&mut self, title: &'a str) -> &mut Self { self.title = Some(title); + self } /// Add file extension filter. Takes in the name of the filter, and list of extensions @@ -98,8 +101,9 @@ impl<'a> FileDialogBuilder<'a> { /// # Ok(()) /// # } /// ``` - pub fn add_filter(&mut self, name: &'a str, extensions: &'a [&'a str]) { + pub fn add_filter(&mut self, name: &'a str, extensions: &'a [&'a str]) -> &mut Self { self.filters.push(DialogFilter { name, extensions }); + self } /// Add many file extension filters. @@ -114,13 +118,14 @@ impl<'a> FileDialogBuilder<'a> { /// # Ok(()) /// # } /// ``` - pub fn add_filters(&mut self, filters: impl IntoIterator) { + pub fn add_filters(&mut self, filters: impl IntoIterator) -> &mut Self { for (name, extensions) in filters.into_iter() { self.filters.push(DialogFilter { name: name.as_ref(), extensions, }); } + self } /// Shows the dialog to select a single file. @@ -137,7 +142,7 @@ impl<'a> FileDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > open`](https://tauri.app/v1/api/config#dialogallowlistconfig.open) to be enabled. - pub async fn pick_file(self) -> crate::Result> { + pub async fn pick_file(&self) -> crate::Result> { let raw = inner::open(serde_wasm_bindgen::to_value(&self)?).await?; Ok(serde_wasm_bindgen::from_value(raw)?) @@ -157,7 +162,7 @@ impl<'a> FileDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > open`](https://tauri.app/v1/api/config#dialogallowlistconfig.open) to be enabled. - pub async fn pick_files(mut self) -> crate::Result>> { + pub async fn pick_files(&mut self) -> crate::Result>> { self.multiple = true; let raw = inner::open(serde_wasm_bindgen::to_value(&self)?).await?; @@ -179,7 +184,7 @@ impl<'a> FileDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > open`](https://tauri.app/v1/api/config#dialogallowlistconfig.open) to be enabled. - pub async fn pick_folder(mut self) -> crate::Result> { + pub async fn pick_folder(&mut self) -> crate::Result> { self.directory = true; let raw = inner::open(serde_wasm_bindgen::to_value(&self)?).await?; @@ -201,7 +206,7 @@ impl<'a> FileDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > open`](https://tauri.app/v1/api/config#dialogallowlistconfig.open) to be enabled. - pub async fn pick_folders(mut self) -> crate::Result>> { + pub async fn pick_folders(&mut self) -> crate::Result>> { self.directory = true; self.multiple = true; @@ -230,7 +235,7 @@ impl<'a> FileDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > save`](https://tauri.app/v1/api/config#dialogallowlistconfig.save) to be enabled. - pub async fn save(self) -> crate::Result> { + pub async fn save(&self) -> crate::Result> { let raw = inner::save(serde_wasm_bindgen::to_value(&self)?).await?; Ok(serde_wasm_bindgen::from_value(raw)?) @@ -274,8 +279,9 @@ impl<'a> MessageDialogBuilder<'a> { /// # Ok(()) /// # } /// ``` - pub fn set_title(&mut self, title: &'a str) { + pub fn set_title(&mut self, title: &'a str) -> &mut Self { self.title = Some(title); + self } /// Set the type of the dialog. @@ -290,8 +296,9 @@ impl<'a> MessageDialogBuilder<'a> { /// # Ok(()) /// # } /// ``` - pub fn set_kind(&mut self, kind: MessageDialogKind) { - self.kind = r#kind; + pub fn set_kind(&mut self, kind: MessageDialogKind) -> &mut Self { + self.kind = kind; + self } /// Shows a message dialog with an `Ok` button. @@ -308,7 +315,7 @@ impl<'a> MessageDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > message`](https://tauri.app/v1/api/config#dialogallowlistconfig.message) to be enabled. - pub async fn message(self, message: &str) -> crate::Result<()> { + pub async fn message(&self, message: &str) -> crate::Result<()> { Ok(inner::message(message, serde_wasm_bindgen::to_value(&self)?).await?) } @@ -326,7 +333,7 @@ impl<'a> MessageDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > ask`](https://tauri.app/v1/api/config#dialogallowlistconfig.ask) to be enabled. - pub async fn ask(self, message: &str) -> crate::Result { + pub async fn ask(&self, message: &str) -> crate::Result { let raw = inner::ask(message, serde_wasm_bindgen::to_value(&self)?).await?; Ok(serde_wasm_bindgen::from_value(raw)?) @@ -346,7 +353,7 @@ impl<'a> MessageDialogBuilder<'a> { /// ``` /// /// Requires [`allowlist > dialog > confirm`](https://tauri.app/v1/api/config#dialogallowlistconfig.confirm) to be enabled. - pub async fn confirm(self, message: &str) -> crate::Result { + pub async fn confirm(&self, message: &str) -> crate::Result { let raw = inner::confirm(message, serde_wasm_bindgen::to_value(&self)?).await?; Ok(serde_wasm_bindgen::from_value(raw)?) diff --git a/src/window.rs b/src/window.rs index 0f07374..a29d6c5 100644 --- a/src/window.rs +++ b/src/window.rs @@ -227,138 +227,161 @@ impl<'a> WebviewWindowBuilder<'a> { /// - URL such as `https://github.com/tauri-apps` is opened directly on a Tauri window. /// - data: URL such as `data:text/html,...` is only supported with the `window-data-url` Cargo feature for the `tauri` dependency. /// - local file path or route such as `/path/to/page.html` or `/users` is appended to the application URL (the devServer URL on development, or `tauri://localhost/` and `https://tauri.localhost/` on production). - pub fn set_url(&mut self, url: &'a str) { + pub fn set_url(&mut self, url: &'a str) -> &mut Self { self.inner.url = Some(url); + self } /// Show window in the center of the screen. - pub fn set_center(&mut self, center: bool) { + pub fn set_center(&mut self, center: bool) -> &mut Self { self.inner.center = center; + self } /// The initial position. - pub fn set_position(&mut self, position: PhysicalPosition) { + pub fn set_position(&mut self, position: PhysicalPosition) -> &mut Self { self.inner.x = Some(position.x()); self.inner.y = Some(position.y()); + self } /// The initial size. - pub fn set_size(&mut self, size: PhysicalSize) { + pub fn set_size(&mut self, size: PhysicalSize) -> &mut Self { self.inner.width = Some(size.width()); self.inner.height = Some(size.height()); + self } /// Minimum window size. - pub fn set_min_size(&mut self, min_size: PhysicalSize) { + pub fn set_min_size(&mut self, min_size: PhysicalSize) -> &mut Self { self.inner.min_width = Some(min_size.width()); self.inner.min_height = Some(min_size.height()); + self } /// Maximum window size. - pub fn set_max_size(&mut self, max_size: PhysicalSize) { + pub fn set_max_size(&mut self, max_size: PhysicalSize) -> &mut Self { self.inner.max_width = Some(max_size.width()); self.inner.max_height = Some(max_size.height()); + self } /// Whether the window is resizable or not. - pub fn set_resizable(&mut self, resizable: bool) { + pub fn set_resizable(&mut self, resizable: bool) -> &mut Self { self.inner.resizable = resizable; + self } /// Window title. - pub fn set_title(&mut self, title: &'a str) { + pub fn set_title(&mut self, title: &'a str) -> &mut Self { self.inner.title = Some(title); + self } /// Whether the window is in fullscreen mode or not. - pub fn set_fullscreen(&mut self, fullscreen: bool) { + pub fn set_fullscreen(&mut self, fullscreen: bool) -> &mut Self { self.inner.fullscreen = fullscreen; + self } /// Whether the window will be initially focused or not. - pub fn set_focus(&mut self, focus: bool) { + pub fn set_focus(&mut self, focus: bool) -> &mut Self { self.inner.focus = focus; + self } /// Whether the window is transparent or not. /// /// Note that on `macOS` this requires the `macos-private-api` feature flag, enabled under `tauri.conf.json > tauri > macOSPrivateApi`. /// WARNING: Using private APIs on `macOS` prevents your application from being accepted to the `App Store`. - pub fn set_transparent(&mut self, transparent: bool) { + pub fn set_transparent(&mut self, transparent: bool) -> &mut Self { self.inner.transparent = transparent; + self } /// Whether the window should be maximized upon creation or not. - pub fn set_maximized(&mut self, maximized: bool) { + pub fn set_maximized(&mut self, maximized: bool) -> &mut Self { self.inner.maximized = maximized; + self } /// Whether the window should be immediately visible upon creation or not. - pub fn set_visible(&mut self, visible: bool) { + pub fn set_visible(&mut self, visible: bool) -> &mut Self { self.inner.visible = visible; + self } /// Whether the window should have borders and bars or not. - pub fn set_decorations(&mut self, decorations: bool) { + pub fn set_decorations(&mut self, decorations: bool) -> &mut Self { self.inner.decorations = decorations; + self } /// Whether the window should always be on top of other windows or not. - pub fn set_always_on_top(&mut self, always_on_top: bool) { + pub fn set_always_on_top(&mut self, always_on_top: bool) -> &mut Self { self.inner.always_on_top = always_on_top; + self } /// Whether or not the window icon should be added to the taskbar. - pub fn set_skip_taskbar(&mut self, skip_taskbar: bool) { + pub fn set_skip_taskbar(&mut self, skip_taskbar: bool) -> &mut Self { self.inner.skip_taskbar = skip_taskbar; + self } /// Whether the file drop is enabled or not on the webview. By default it is enabled. /// /// Disabling it is required to use drag and drop on the frontend on Windows. - pub fn set_file_drop_enabled(&mut self, file_drop_enabled: bool) { + pub fn set_file_drop_enabled(&mut self, file_drop_enabled: bool) -> &mut Self { self.inner.file_drop_enabled = file_drop_enabled; + self } /// The initial window theme. Defaults to the system theme. /// /// Only implemented on Windows and macOS 10.14+. - pub fn set_theme(&mut self, theme: Theme) { + pub fn set_theme(&mut self, theme: Theme) -> &mut Self { self.inner.theme = Some(theme); + self } /// The style of the macOS title bar. - pub fn set_title_bar_style(&mut self, title_bar_style: TitleBarStyle) { + pub fn set_title_bar_style(&mut self, title_bar_style: TitleBarStyle) -> &mut Self { self.inner.title_bar_style = Some(title_bar_style); + self } /// If `true`, sets the window title to be hidden on macOS. - pub fn set_hidden_title(&mut self, hidden_title: bool) { + pub fn set_hidden_title(&mut self, hidden_title: bool) -> &mut Self { self.inner.hidden_title = hidden_title; + self } /// Whether clicking an inactive window also clicks through to the webview. - pub fn set_accept_first_mouse(&mut self, accept_first_mouse: bool) { + pub fn set_accept_first_mouse(&mut self, accept_first_mouse: bool) -> &mut Self { self.inner.accept_first_mouse = accept_first_mouse; + self } /// Defines the window [tabbing identifier](https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier) on macOS. /// /// Windows with the same tabbing identifier will be grouped together. /// If the tabbing identifier is not set, automatic tabbing will be disabled. - pub fn set_tabbing_identifier(&mut self, tabbing_identifier: &'a str) { + pub fn set_tabbing_identifier(&mut self, tabbing_identifier: &'a str) -> &mut Self { self.inner.tabbing_identifier = Some(tabbing_identifier); + self } /// The user agent for the webview. - pub fn set_user_agent(&mut self, user_agent: &'a str) { + pub fn set_user_agent(&mut self, user_agent: &'a str) -> &mut Self { self.inner.user_agent = Some(user_agent); + self } /// Creates a new webview window. /// /// Requires [`allowlist > window > create`](https://tauri.app/v1/api/config#windowallowlistconfig.create) to be enabled. - pub fn build(self) -> crate::Result { + pub fn build(&self) -> crate::Result { let opts = serde_wasm_bindgen::to_value(&self.inner)?; Ok(WebviewWindow(inner::WebviewWindow::new(self.label, opts)))