Skip to content

Commit 00e1efa

Browse files
meowteclucasfernog
andauthored
feat: customize button texts of message dialog (#4383)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 7a8d570 commit 00e1efa

File tree

13 files changed

+167
-35
lines changed

13 files changed

+167
-35
lines changed

.cargo/config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[env]
2+
__TAURI_WORKSPACE__ = "true"

.changes/custom-buttons-api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": minor
3+
---
4+
5+
Allow setting the text of the dialog buttons.

.changes/custom-buttons.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor
3+
---
4+
5+
Added `OkWithLabel` and `OkCancelWithLabels` variants to the `api::dialog::MessageDialogButtons` enum to set the text of the dialog buttons.

core/tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ico = { version = "0.2.0", optional = true }
8787
encoding_rs = "0.8.31"
8888

8989
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
90-
rfd = { version = "0.10", optional = true }
90+
rfd = { version = "0.10", optional = true, features=["gtk3", "common-controls-v6"] }
9191
notify-rust = { version = "4.5", default-features = false, features = [ "d" ], optional = true }
9292

9393
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]

core/tauri/Windows Manifest.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
2+
<dependency>
3+
<dependentAssembly>
4+
<assemblyIdentity
5+
type="win32"
6+
name="Microsoft.Windows.Common-Controls"
7+
version="6.0.0.0"
8+
processorArchitecture="*"
9+
publicKeyToken="6595b64144ccf1df"
10+
language="*"
11+
/>
12+
</dependentAssembly>
13+
</dependency>
14+
</assembly>

core/tauri/build.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ fn main() {
141141
CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
142142
)
143143
.expect("failed to write checked_features file");
144+
145+
// workaround needed to preven `STATUS_ENTRYPOINT_NOT_FOUND` error
146+
// see https://github.com/tauri-apps/tauri/pull/4383#issuecomment-1212221864
147+
let target_os = std::env::var("CARGO_CFG_TARGET_OS");
148+
let target_env = std::env::var("CARGO_CFG_TARGET_ENV");
149+
let is_tauri_workspace = std::env::var("__TAURI_WORKSPACE__").map_or(false, |v| v == "true");
150+
if is_tauri_workspace
151+
&& Ok("windows") == target_os.as_deref()
152+
&& Ok("msvc") == target_env.as_deref()
153+
{
154+
add_manifest();
155+
}
144156
}
145157

146158
// create aliases for the given module with its apis.
@@ -170,3 +182,20 @@ fn alias_module(module: &str, apis: &[&str], api_all: bool) {
170182

171183
alias(&format!("{}_any", AsSnakeCase(module)), any);
172184
}
185+
186+
fn add_manifest() {
187+
static WINDOWS_MANIFEST_FILE: &str = "Windows Manifest.xml";
188+
189+
let mut manifest = std::env::current_dir().unwrap();
190+
manifest.push(WINDOWS_MANIFEST_FILE);
191+
192+
println!("cargo:rerun-if-changed={}", WINDOWS_MANIFEST_FILE);
193+
// Embed the Windows application manifest file.
194+
println!("cargo:rustc-link-arg=/MANIFEST:EMBED");
195+
println!(
196+
"cargo:rustc-link-arg=/MANIFESTINPUT:{}",
197+
manifest.to_str().unwrap()
198+
);
199+
// Turn linker warnings into errors.
200+
println!("cargo:rustc-link-arg=/WX");
201+
}

core/tauri/scripts/bundle.global.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/api/dialog.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,18 @@ macro_rules! message_dialog_builder {
171171

172172
/// Options for action buttons on message dialogs.
173173
#[non_exhaustive]
174-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
174+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
175175
pub enum MessageDialogButtons {
176176
/// Ok button.
177177
Ok,
178178
/// Ok and Cancel buttons.
179179
OkCancel,
180180
/// Yes and No buttons.
181181
YesNo,
182+
/// OK button with customized text.
183+
OkWithLabel(String),
184+
/// Ok and Cancel buttons with customized text.
185+
OkCancelWithLabels(String, String),
182186
}
183187

184188
impl From<MessageDialogButtons> for rfd::MessageButtons {
@@ -187,6 +191,10 @@ impl From<MessageDialogButtons> for rfd::MessageButtons {
187191
MessageDialogButtons::Ok => Self::Ok,
188192
MessageDialogButtons::OkCancel => Self::OkCancel,
189193
MessageDialogButtons::YesNo => Self::YesNo,
194+
MessageDialogButtons::OkWithLabel(ok_text) => Self::OkCustom(ok_text),
195+
MessageDialogButtons::OkCancelWithLabels(ok_text, cancel_text) => {
196+
Self::OkCancelCustom(ok_text, cancel_text)
197+
}
190198
}
191199
}
192200
}

core/tauri/src/endpoints/dialog.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ use tauri_macros::{command_enum, module_command_handler, CommandModule};
1414
use std::path::PathBuf;
1515

1616
macro_rules! message_dialog {
17-
($fn_name: ident, $allowlist: ident, $buttons: expr) => {
17+
($fn_name: ident, $allowlist: ident, $button_labels_type: ty, $buttons: expr) => {
1818
#[module_command_handler($allowlist)]
1919
fn $fn_name<R: Runtime>(
2020
context: InvokeContext<R>,
2121
title: Option<String>,
2222
message: String,
2323
level: Option<MessageDialogType>,
24+
button_labels: $button_labels_type,
2425
) -> super::Result<bool> {
26+
let determine_button = $buttons;
2527
let mut builder = crate::api::dialog::blocking::MessageDialogBuilder::new(
2628
title.unwrap_or_else(|| context.window.app_handle.package_info().name.clone()),
2729
message,
2830
)
29-
.buttons($buttons);
31+
.buttons(determine_button(button_labels));
3032
#[cfg(any(windows, target_os = "macos"))]
3133
{
3234
builder = builder.parent(&context.window);
@@ -139,20 +141,26 @@ pub enum Cmd {
139141
message: String,
140142
#[serde(rename = "type")]
141143
level: Option<MessageDialogType>,
144+
#[serde(rename = "buttonLabel")]
145+
button_label: Option<String>,
142146
},
143147
#[cmd(dialog_ask, "dialog > ask")]
144148
AskDialog {
145149
title: Option<String>,
146150
message: String,
147151
#[serde(rename = "type")]
148152
level: Option<MessageDialogType>,
153+
#[serde(rename = "buttonLabels")]
154+
button_label: Option<(String, String)>,
149155
},
150156
#[cmd(dialog_confirm, "dialog > confirm")]
151157
ConfirmDialog {
152158
title: Option<String>,
153159
message: String,
154160
#[serde(rename = "type")]
155161
level: Option<MessageDialogType>,
162+
#[serde(rename = "buttonLabels")]
163+
button_labels: Option<(String, String)>,
156164
},
157165
}
158166

@@ -255,19 +263,36 @@ impl Cmd {
255263
message_dialog!(
256264
message_dialog,
257265
dialog_message,
258-
crate::api::dialog::MessageDialogButtons::Ok
266+
Option<String>,
267+
|label: Option<String>| {
268+
label
269+
.map(crate::api::dialog::MessageDialogButtons::OkWithLabel)
270+
.unwrap_or(crate::api::dialog::MessageDialogButtons::Ok)
271+
}
259272
);
260273

261274
message_dialog!(
262275
ask_dialog,
263276
dialog_ask,
264-
crate::api::dialog::MessageDialogButtons::YesNo
277+
Option<(String, String)>,
278+
|labels: Option<(String, String)>| {
279+
labels
280+
.map(|(yes, no)| crate::api::dialog::MessageDialogButtons::OkCancelWithLabels(yes, no))
281+
.unwrap_or(crate::api::dialog::MessageDialogButtons::YesNo)
282+
}
265283
);
266284

267285
message_dialog!(
268286
confirm_dialog,
269287
dialog_confirm,
270-
crate::api::dialog::MessageDialogButtons::OkCancel
288+
Option<(String, String)>,
289+
|labels: Option<(String, String)>| {
290+
labels
291+
.map(|(ok, cancel)| {
292+
crate::api::dialog::MessageDialogButtons::OkCancelWithLabels(ok, cancel)
293+
})
294+
.unwrap_or(crate::api::dialog::MessageDialogButtons::OkCancel)
295+
}
271296
);
272297
}
273298

0 commit comments

Comments
 (0)