|
| 1 | +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +use windows::core::{w, HSTRING, PCWSTR}; |
| 6 | + |
| 7 | +enum Level { |
| 8 | + Error, |
| 9 | + #[allow(unused)] |
| 10 | + Warning, |
| 11 | + #[allow(unused)] |
| 12 | + Info, |
| 13 | +} |
| 14 | + |
| 15 | +pub fn error<S: AsRef<str>>(err: S) { |
| 16 | + dialog_inner(err.as_ref(), Level::Error); |
| 17 | +} |
| 18 | + |
| 19 | +fn dialog_inner(err: &str, level: Level) { |
| 20 | + let title = match level { |
| 21 | + Level::Warning => w!("Warning"), |
| 22 | + Level::Error => w!("Error"), |
| 23 | + Level::Info => w!("Info"), |
| 24 | + }; |
| 25 | + |
| 26 | + #[cfg(not(feature = "common-controls-v6"))] |
| 27 | + { |
| 28 | + use windows::Win32::UI::WindowsAndMessaging::*; |
| 29 | + |
| 30 | + let err = remove_hyperlink(err); |
| 31 | + let err = HSTRING::from(err); |
| 32 | + |
| 33 | + unsafe { |
| 34 | + MessageBoxW( |
| 35 | + None, |
| 36 | + err, |
| 37 | + title, |
| 38 | + match level { |
| 39 | + Level::Warning => MB_ICONWARNING, |
| 40 | + Level::Error => MB_ICONERROR, |
| 41 | + Level::Info => MB_ICONINFORMATION, |
| 42 | + }, |
| 43 | + ) |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + #[cfg(feature = "common-controls-v6")] |
| 48 | + { |
| 49 | + use windows::core::HRESULT; |
| 50 | + use windows::Win32::Foundation::*; |
| 51 | + use windows::Win32::UI::Controls::*; |
| 52 | + use windows::Win32::UI::Shell::*; |
| 53 | + use windows::Win32::UI::WindowsAndMessaging::*; |
| 54 | + |
| 55 | + extern "system" fn task_dialog_callback( |
| 56 | + _hwnd: HWND, |
| 57 | + msg: TASKDIALOG_NOTIFICATIONS, |
| 58 | + _wparam: WPARAM, |
| 59 | + lparam: LPARAM, |
| 60 | + _data: isize, |
| 61 | + ) -> HRESULT { |
| 62 | + if msg == TDN_HYPERLINK_CLICKED { |
| 63 | + let link = PCWSTR(lparam.0 as _); |
| 64 | + let _ = unsafe { ShellExecuteW(None, None, link, None, None, SW_SHOWNORMAL) }; |
| 65 | + } |
| 66 | + |
| 67 | + S_OK |
| 68 | + } |
| 69 | + |
| 70 | + let err = HSTRING::from(err); |
| 71 | + let err = PCWSTR(err.as_ptr()); |
| 72 | + |
| 73 | + let task_dialog_config = TASKDIALOGCONFIG { |
| 74 | + cbSize: std::mem::size_of::<TASKDIALOGCONFIG>() as u32, |
| 75 | + dwFlags: TDF_ALLOW_DIALOG_CANCELLATION | TDF_ENABLE_HYPERLINKS, |
| 76 | + pszWindowTitle: title, |
| 77 | + pszContent: err, |
| 78 | + Anonymous1: TASKDIALOGCONFIG_0 { |
| 79 | + pszMainIcon: match level { |
| 80 | + Level::Warning => TD_WARNING_ICON, |
| 81 | + Level::Error => TD_ERROR_ICON, |
| 82 | + Level::Info => TD_INFORMATION_ICON, |
| 83 | + }, |
| 84 | + }, |
| 85 | + dwCommonButtons: TDCBF_OK_BUTTON, |
| 86 | + pfCallback: Some(task_dialog_callback), |
| 87 | + ..Default::default() |
| 88 | + }; |
| 89 | + |
| 90 | + let _ = unsafe { TaskDialogIndirect(&task_dialog_config, None, None, None) }; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(not(feature = "common-controls-v6"))] |
| 95 | +fn remove_hyperlink(str: &str) -> String { |
| 96 | + let mut result = String::new(); |
| 97 | + let mut in_hyperlink = false; |
| 98 | + |
| 99 | + for c in str.chars() { |
| 100 | + if c == '<' { |
| 101 | + in_hyperlink = true; |
| 102 | + } else if c == '>' { |
| 103 | + in_hyperlink = false; |
| 104 | + } else if !in_hyperlink { |
| 105 | + result.push(c); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + result |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +#[cfg(not(feature = "common-controls-v6"))] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_remove_hyperlink() { |
| 119 | + let input = "This is a <A href=\"some link\">test</A> string."; |
| 120 | + let expected = "This is a test string."; |
| 121 | + let result = remove_hyperlink(input); |
| 122 | + assert_eq!(result, expected); |
| 123 | + } |
| 124 | +} |
0 commit comments