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
// 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,
TASKDIALOG_COMMON_BUTTON_FLAGS* button_flags,
std::vector<TASKDIALOG_BUTTON>* dialog_buttons) {
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) {
// It is a common button.
(*id_map)[common.id] = i;
(*button_flags) |= common.button;
} else {
// It is a custom button.
dialog_buttons->push_back({static_cast<int>(i + kIDStart),
base::UTF16ToWide(buttons[i]).c_str()});
dialog_buttons->push_back(
{static_cast<int>(i + kIDStart), buttons[i].c_str()});
}
}
}
DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
MessageBoxType type,
const std::vector<std::u16string>& buttons,
int default_id,
int cancel_id,
bool no_link,
const std::u16string& title,
const std::u16string& message,
const std::u16string& detail,
const std::u16string& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon) {
DialogResult ShowTaskDialogWstr(NativeWindow* parent,
MessageBoxType type,
const std::vector<std::wstring>& buttons,
int default_id,
int cancel_id,
bool no_link,
const std::wstring& title,
const std::wstring& message,
const std::wstring& detail,
const std::wstring& checkbox_label,
bool checkbox_checked,
const gfx::ImageSkia& icon) {
TASKDIALOG_FLAGS flags =
TDF_SIZE_TO_CONTENT | // Show all content.
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
// 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();
else
config.pszWindowTitle = base::UTF16ToWide(title).c_str();
} else {
config.pszWindowTitle = title.c_str();
}
base::win::ScopedHICON hicon;
if (!icon.isNull()) {
@ -138,14 +139,14 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
// If "detail" is empty then don't make message highlighted.
if (detail.empty()) {
config.pszContent = base::UTF16ToWide(message).c_str();
config.pszContent = message.c_str();
} else {
config.pszMainInstruction = base::UTF16ToWide(message).c_str();
config.pszContent = base::UTF16ToWide(detail).c_str();
config.pszMainInstruction = message.c_str();
config.pszContent = detail.c_str();
}
if (!checkbox_label.empty()) {
config.pszVerificationText = base::UTF16ToWide(checkbox_label).c_str();
config.pszVerificationText = checkbox_label.c_str();
if (checkbox_checked)
config.dwFlags |= TDF_VERIFICATION_FLAG_CHECKED;
}
@ -156,8 +157,8 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
std::vector<TASKDIALOG_BUTTON> dialog_buttons;
if (no_link) {
for (size_t i = 0; i < buttons.size(); ++i)
dialog_buttons.push_back({static_cast<int>(i + kIDStart),
base::UTF16ToWide(buttons[i]).c_str()});
dialog_buttons.push_back(
{static_cast<int>(i + kIDStart), buttons[i].c_str()});
} else {
MapToCommonID(buttons, &id_map, &config.dwCommonButtons, &dialog_buttons);
}
@ -185,20 +186,19 @@ DialogResult ShowTaskDialogUTF16(NativeWindow* parent,
}
DialogResult ShowTaskDialogUTF8(const MessageBoxSettings& settings) {
std::vector<std::u16string> utf16_buttons;
std::vector<std::wstring> 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::u16string message_16 = base::UTF8ToUTF16(settings.message);
const std::u16string detail_16 = base::UTF8ToUTF16(settings.detail);
const std::u16string checkbox_label_16 =
base::UTF8ToUTF16(settings.checkbox_label);
const std::wstring title = base::UTF8ToWide(settings.title);
const std::wstring message = base::UTF8ToWide(settings.message);
const std::wstring detail = base::UTF8ToWide(settings.detail);
const std::wstring checkbox_label = base::UTF8ToWide(settings.checkbox_label);
return ShowTaskDialogUTF16(
settings.parent_window, settings.type, utf16_buttons, settings.default_id,
settings.cancel_id, settings.no_link, title_16, message_16, detail_16,
checkbox_label_16, settings.checkbox_checked, settings.icon);
return ShowTaskDialogWstr(
settings.parent_window, settings.type, buttons, settings.default_id,
settings.cancel_id, settings.no_link, title, message, detail,
checkbox_label, settings.checkbox_checked, settings.icon);
}
} // namespace
@ -221,9 +221,10 @@ void ShowMessageBox(const MessageBoxSettings& settings,
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
electron::UnresponsiveSuppressor suppressor;
ShowTaskDialogUTF16(nullptr, MessageBoxType::kError, {}, -1, 0, false,
base::UTF8ToUTF16("Error"), title, content,
base::UTF8ToUTF16(""), false, gfx::ImageSkia());
ShowTaskDialogWstr(nullptr, MessageBoxType::kError, {}, -1, 0, false,
base::UTF8ToWide("Error"), base::UTF16ToWide(title),
base::UTF16ToWide(content), base::UTF8ToWide(""), false,
gfx::ImageSkia());
}
} // namespace electron