Skip to content

Commit

Permalink
refactor(core): use tinyfiledialogs-rs for message/confirmation dialo…
Browse files Browse the repository at this point in the history
…gs (#1255)
  • Loading branch information
lucasfernog committed Feb 18, 2021
1 parent f51801f commit 6eee355
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .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`.
2 changes: 1 addition & 1 deletion tauri-api/Cargo.toml
Expand Up @@ -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"
Expand Down
51 changes: 28 additions & 23 deletions tauri-api/src/dialog.rs
Expand Up @@ -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,
Expand All @@ -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>>,
Expand Down
31 changes: 11 additions & 20 deletions tauri/examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 13 additions & 22 deletions tauri/examples/communication/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 11 additions & 20 deletions tauri/examples/multiwindow/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions 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;
Expand Down Expand Up @@ -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);
Expand All @@ -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()
}),
Expand Down Expand Up @@ -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),
}
}
7 changes: 3 additions & 4 deletions tauri/src/endpoints/notification.rs
Expand Up @@ -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()),
}
}

0 comments on commit 6eee355

Please sign in to comment.