refactor: make ShowTaskDialog take a wstr

This commit is contained in:
Charles Kerr 2021-03-30 18:06:00 -07:00
parent 9127cff58b
commit 1594c54933

View file

@ -58,36 +58,36 @@ CommonButtonID GetCommonID(const std::wstring& button) {
// Determine whether the buttons are common buttons, if so map common ID // Determine whether the buttons are common buttons, if so map common ID
// to button ID. // to button ID.
void MapToCommonID(const std::vector<std::u16string>& buttons, void MapToCommonID(const std::vector<std::wstring>& buttons,
std::map<int, int>* id_map, std::map<int, int>* id_map,
TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags, TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags,
std::vector<TASKDIALOG_BUTTON>* dialog_buttons) { std::vector<TASKDIALOG_BUTTON>* dialog_buttons) {
for (size_t i = 0; i < buttons.size(); ++i) { for (size_t i = 0; i < buttons.size(); ++i) {
auto common = GetCommonID(base::UTF16ToWide(buttons[i])); auto common = GetCommonID(buttons[i]);
if (common.button != -1) { if (common.button != -1) {
// It is a common button. // It is a common button.
(*id_map)[common.id] = i; (*id_map)[common.id] = i;
(*button_flags) |= common.button; (*button_flags) |= common.button;
} else { } else {
// It is a custom button. // It is a custom button.
dialog_buttons->push_back({static_cast<int>(i + kIDStart), dialog_buttons->push_back(
base::UTF16ToWide(buttons[i]).c_str()}); {static_cast<int>(i + kIDStart), buttons[i].c_str()});
} }
} }
} }
DialogResult ShowTaskDialogUTF16(NativeWindow* parent, DialogResult ShowTaskDialogWstr(NativeWindow* parent,
MessageBoxType type, MessageBoxType type,
const std::vector<std::u16string>& buttons, const std::vector<std::wstring>& buttons,
int default_id, int default_id,
int cancel_id, int cancel_id,
bool no_link, bool no_link,
const std::u16string& title, const std::wstring& title,
const std::u16string& message, const std::wstring& message,
const std::u16string& detail, const std::wstring& detail,
const std::u16string& checkbox_label, const std::wstring& checkbox_label,
bool checkbox_checked, bool checkbox_checked,
const gfx::ImageSkia& icon) { const gfx::ImageSkia& icon) {
TASKDIALOG_FLAGS flags = TASKDIALOG_FLAGS flags =
TDF_SIZE_TO_CONTENT | // Show all content. TDF_SIZE_TO_CONTENT | // Show all content.
TDF_ALLOW_DIALOG_CANCELLATION; // Allow canceling the dialog. TDF_ALLOW_DIALOG_CANCELLATION; // Allow canceling the dialog.
@ -107,11 +107,12 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
// TaskDialogIndirect doesn't allow empty name, if we set empty title it // TaskDialogIndirect doesn't allow empty name, if we set empty title it
// will show "electron.exe" in title. // will show "electron.exe" in title.
std::wstring app_name = base::UTF8ToWide(Browser::Get()->GetName()); if (title.empty()) {
if (title.empty()) std::wstring app_name = base::UTF8ToWide(Browser::Get()->GetName());
config.pszWindowTitle = app_name.c_str(); config.pszWindowTitle = app_name.c_str();
else } else {
config.pszWindowTitle = base::UTF16ToWide(title).c_str(); config.pszWindowTitle = title.c_str();
}
base::win::ScopedHICON hicon; base::win::ScopedHICON hicon;
if (!icon.isNull()) { if (!icon.isNull()) {
@ -138,14 +139,14 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
// If "detail" is empty then don't make message highlighted. // If "detail" is empty then don't make message highlighted.
if (detail.empty()) { if (detail.empty()) {
config.pszContent = base::UTF16ToWide(message).c_str(); config.pszContent = message.c_str();
} else { } else {
config.pszMainInstruction = base::UTF16ToWide(message).c_str(); config.pszMainInstruction = message.c_str();
config.pszContent = base::UTF16ToWide(detail).c_str(); config.pszContent = detail.c_str();
} }
if (!checkbox_label.empty()) { if (!checkbox_label.empty()) {
config.pszVerificationText = base::UTF16ToWide(checkbox_label).c_str(); config.pszVerificationText = checkbox_label.c_str();
if (checkbox_checked) if (checkbox_checked)
config.dwFlags |= TDF_VERIFICATION_FLAG_CHECKED; config.dwFlags |= TDF_VERIFICATION_FLAG_CHECKED;
} }
@ -156,8 +157,8 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
std::vector<TASKDIALOG_BUTTON> dialog_buttons; std::vector<TASKDIALOG_BUTTON> dialog_buttons;
if (no_link) { if (no_link) {
for (size_t i = 0; i < buttons.size(); ++i) for (size_t i = 0; i < buttons.size(); ++i)
dialog_buttons.push_back({static_cast<int>(i + kIDStart), dialog_buttons.push_back(
base::UTF16ToWide(buttons[i]).c_str()}); {static_cast<int>(i + kIDStart), buttons[i].c_str()});
} else { } else {
MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons); MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
} }
@ -185,20 +186,19 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
} }
DialogResult ShowTaskDialogUTF8(const MessageBoxSettings& settings) { DialogResult ShowTaskDialogUTF8(const MessageBoxSettings& settings) {
std::vector<std::u16string> utf16_buttons; std::vector<std::wstring> buttons;
for (const auto& button : settings.buttons) for (const auto& button : settings.buttons)
utf16_buttons.push_back(base::UTF8ToUTF16(button)); buttons.push_back(base::UTF8ToWide(button));
const std::u16string title_16 = base::UTF8ToUTF16(settings.title); const std::wstring title = base::UTF8ToWide(settings.title);
const std::u16string message_16 = base::UTF8ToUTF16(settings.message); const std::wstring message = base::UTF8ToWide(settings.message);
const std::u16string detail_16 = base::UTF8ToUTF16(settings.detail); const std::wstring detail = base::UTF8ToWide(settings.detail);
const std::u16string checkbox_label_16 = const std::wstring checkbox_label = base::UTF8ToWide(settings.checkbox_label);
base::UTF8ToUTF16(settings.checkbox_label);
return ShowTaskDialogUTF16( return ShowTaskDialogWstr(
settings.parent_window, settings.type, utf16_buttons, settings.default_id, settings.parent_window, settings.type, buttons, settings.default_id,
settings.cancel_id, settings.no_link, title_16, message_16, detail_16, settings.cancel_id, settings.no_link, title, message, detail,
checkbox_label_16, settings.checkbox_checked, settings.icon); checkbox_label, settings.checkbox_checked, settings.icon);
} }
} // namespace } // namespace
@ -221,9 +221,10 @@ void ShowMessageBox(const MessageBoxSettings& settings,
void ShowErrorBox(const std::u16string& title, const std::u16string& content) { void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
electron::UnresponsiveSuppressor suppressor; electron::UnresponsiveSuppressor suppressor;
ShowTaskDialogUTF16(nullptr, MessageBoxType::kError, {}, -1, 0, false, ShowTaskDialogWstr(nullptr, MessageBoxType::kError, {}, -1, 0, false,
base::UTF8ToUTF16("Error"), title, content, base::UTF8ToWide("Error"), base::UTF16ToWide(title),
base::UTF8ToUTF16(""), false, gfx::ImageSkia()); base::UTF16ToWide(content), base::UTF8ToWide(""), false,
gfx::ImageSkia());
} }
} // namespace electron } // namespace electron