2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-03 13:03:26 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/message_box.h"
|
2013-05-03 13:03:26 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-05-03 13:03:26 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
#include "base/containers/contains.h"
|
2024-01-05 11:18:31 +00:00
|
|
|
#include "base/containers/flat_map.h"
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/callback.h"
|
2016-02-08 23:18:05 +00:00
|
|
|
#include "base/mac/mac_util.h"
|
2021-07-14 22:59:27 +00:00
|
|
|
#include "base/no_destructor.h"
|
2013-05-03 13:03:26 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2021-11-01 21:08:31 +00:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
2016-02-08 23:18:05 +00:00
|
|
|
#include "skia/ext/skia_utils_mac.h"
|
2018-04-10 06:23:16 +00:00
|
|
|
#include "ui/gfx/image/image_skia.h"
|
2013-05-03 13:03:26 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
MessageBoxSettings::MessageBoxSettings() = default;
|
|
|
|
MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
|
|
|
|
MessageBoxSettings::~MessageBoxSettings() = default;
|
|
|
|
|
2013-09-22 10:47:00 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
// <ID, messageBox> map
|
2024-01-05 11:18:31 +00:00
|
|
|
base::flat_map<int, NSAlert*>& GetDialogsMap() {
|
|
|
|
static base::NoDestructor<base::flat_map<int, NSAlert*>> dialogs;
|
2021-07-14 22:59:27 +00:00
|
|
|
return *dialogs;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
NSAlert* CreateNSAlert(const MessageBoxSettings& settings) {
|
2013-05-03 13:03:26 +00:00
|
|
|
// Ignore the title; it's the window title on other platforms and ignorable.
|
2013-09-22 10:47:00 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
2019-06-14 15:26:25 +00:00
|
|
|
[alert setMessageText:base::SysUTF8ToNSString(settings.message)];
|
|
|
|
[alert setInformativeText:base::SysUTF8ToNSString(settings.detail)];
|
2013-05-03 13:03:26 +00:00
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
switch (settings.type) {
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kInformation:
|
2022-06-01 06:12:47 +00:00
|
|
|
alert.alertStyle = NSAlertStyleInformational;
|
2013-05-03 13:03:26 +00:00
|
|
|
break;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kWarning:
|
|
|
|
case MessageBoxType::kError:
|
2022-06-01 06:12:47 +00:00
|
|
|
// NSWarningAlertStyle shows the app icon while NSAlertStyleCritical
|
2017-04-13 18:36:48 +00:00
|
|
|
// shows a warning icon with an app icon badge. Since there is no
|
2022-06-01 06:12:47 +00:00
|
|
|
// error variant, lets just use NSAlertStyleCritical.
|
|
|
|
alert.alertStyle = NSAlertStyleCritical;
|
2013-05-03 13:03:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
for (size_t i = 0; i < settings.buttons.size(); ++i) {
|
|
|
|
NSString* title = base::SysUTF8ToNSString(settings.buttons[i]);
|
2016-06-18 13:26:26 +00:00
|
|
|
// An empty title causes crash on macOS.
|
2019-06-14 15:26:25 +00:00
|
|
|
if (settings.buttons[i].empty())
|
2014-10-01 07:51:32 +00:00
|
|
|
title = @"(empty)";
|
2013-05-03 13:03:26 +00:00
|
|
|
NSButton* button = [alert addButtonWithTitle:title];
|
|
|
|
[button setTag:i];
|
2016-01-11 13:12:07 +00:00
|
|
|
}
|
2016-01-08 04:46:45 +00:00
|
|
|
|
2016-01-11 13:12:07 +00:00
|
|
|
NSArray* ns_buttons = [alert buttons];
|
2017-02-22 18:05:16 +00:00
|
|
|
int button_count = static_cast<int>([ns_buttons count]);
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
if (settings.default_id >= 0 && settings.default_id < button_count) {
|
2019-12-31 13:48:43 +00:00
|
|
|
// The first button added gets set as the default selected, so remove
|
|
|
|
// that and set the button @ default_id to be default.
|
2016-01-11 13:12:07 +00:00
|
|
|
[[ns_buttons objectAtIndex:0] setKeyEquivalent:@""];
|
2019-06-14 15:26:25 +00:00
|
|
|
[[ns_buttons objectAtIndex:settings.default_id] setKeyEquivalent:@"\r"];
|
2013-05-03 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 12:21:15 +00:00
|
|
|
if (button_count > 1 && settings.cancel_id >= 0) {
|
|
|
|
// Bind cancel id button to escape key if there is more than one button.
|
|
|
|
if (settings.cancel_id < button_count) {
|
|
|
|
[[ns_buttons objectAtIndex:settings.cancel_id] setKeyEquivalent:@"\e"];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@codebytere): This behavior violates HIG & should be deprecated.
|
|
|
|
if (settings.cancel_id == settings.default_id) {
|
|
|
|
[[ns_buttons objectAtIndex:settings.default_id] highlight:YES];
|
|
|
|
}
|
2023-01-05 08:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.checkbox_label.empty()) {
|
2017-02-06 15:35:36 +00:00
|
|
|
alert.showsSuppressionButton = YES;
|
2019-06-14 15:26:25 +00:00
|
|
|
alert.suppressionButton.title =
|
|
|
|
base::SysUTF8ToNSString(settings.checkbox_label);
|
2023-07-01 20:22:55 +00:00
|
|
|
alert.suppressionButton.state = settings.checkbox_checked
|
|
|
|
? NSControlStateValueOn
|
|
|
|
: NSControlStateValueOff;
|
2017-02-06 15:35:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.icon.isNull()) {
|
2024-01-11 06:33:40 +00:00
|
|
|
NSImage* image = skia::SkBitmapToNSImage(*settings.icon.bitmap());
|
2016-02-08 23:18:05 +00:00
|
|
|
[alert setIcon:image];
|
|
|
|
}
|
|
|
|
|
2021-09-23 10:56:14 +00:00
|
|
|
if (settings.text_width > 0) {
|
|
|
|
NSRect rect = NSMakeRect(0, 0, settings.text_width, 0);
|
|
|
|
NSView* accessoryView = [[NSView alloc] initWithFrame:rect];
|
2023-08-04 08:47:29 +00:00
|
|
|
[alert setAccessoryView:accessoryView];
|
2021-09-23 10:56:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-22 10:47:00 +00:00
|
|
|
return alert;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
|
2023-08-04 08:47:29 +00:00
|
|
|
NSAlert* alert(CreateNSAlert(settings));
|
2013-09-22 10:47:00 +00:00
|
|
|
|
2014-03-05 11:51:26 +00:00
|
|
|
// Use runModal for synchronous alert without parent, since we don't have a
|
2020-04-13 08:25:55 +00:00
|
|
|
// window to wait for. Also use it when window is provided but it is not
|
|
|
|
// shown as it would be impossible to dismiss the alert if it is connected
|
|
|
|
// to invisible window (see #22671).
|
|
|
|
if (!settings.parent_window || !settings.parent_window->IsVisible())
|
2020-10-22 06:46:58 +00:00
|
|
|
return [alert runModal];
|
2014-03-05 11:51:26 +00:00
|
|
|
|
2019-02-27 18:23:17 +00:00
|
|
|
__block int ret_code = -1;
|
2014-03-05 11:51:26 +00:00
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
NSWindow* window =
|
|
|
|
settings.parent_window->GetNativeWindow().GetNativeNSWindow();
|
2014-03-05 11:51:26 +00:00
|
|
|
[alert beginSheetModalForWindow:window
|
2019-02-27 18:23:17 +00:00
|
|
|
completionHandler:^(NSModalResponse response) {
|
|
|
|
ret_code = response;
|
|
|
|
[NSApp stopModal];
|
|
|
|
}];
|
2014-03-05 11:51:26 +00:00
|
|
|
|
|
|
|
[NSApp runModalForWindow:window];
|
|
|
|
return ret_code;
|
2013-05-03 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
void ShowMessageBox(const MessageBoxSettings& settings,
|
2019-03-12 18:06:59 +00:00
|
|
|
MessageBoxCallback callback) {
|
2019-06-14 15:26:25 +00:00
|
|
|
NSAlert* alert = CreateNSAlert(settings);
|
2013-09-23 06:29:55 +00:00
|
|
|
|
2017-09-12 23:26:31 +00:00
|
|
|
// Use runModal for synchronous alert without parent, since we don't have a
|
|
|
|
// window to wait for.
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.parent_window) {
|
2023-08-04 08:47:29 +00:00
|
|
|
int ret = [alert runModal];
|
2023-07-01 20:22:55 +00:00
|
|
|
std::move(callback).Run(
|
|
|
|
ret, alert.suppressionButton.state == NSControlStateValueOn);
|
2017-09-12 23:26:31 +00:00
|
|
|
} else {
|
2021-07-14 22:59:27 +00:00
|
|
|
if (settings.id) {
|
|
|
|
if (base::Contains(GetDialogsMap(), *settings.id))
|
|
|
|
CloseMessageBox(*settings.id);
|
|
|
|
GetDialogsMap()[*settings.id] = alert;
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:25:19 +00:00
|
|
|
NSWindow* window =
|
2019-06-14 15:26:25 +00:00
|
|
|
settings.parent_window
|
|
|
|
? settings.parent_window->GetNativeWindow().GetNativeNSWindow()
|
|
|
|
: nil;
|
2019-03-12 18:06:59 +00:00
|
|
|
|
2019-02-27 18:23:17 +00:00
|
|
|
// Duplicate the callback object here since c is a reference and gcd would
|
|
|
|
// only store the pointer, by duplication we can force gcd to store a copy.
|
2019-03-12 18:06:59 +00:00
|
|
|
__block MessageBoxCallback callback_ = std::move(callback);
|
2024-01-10 22:23:35 +00:00
|
|
|
__block std::optional<int> id = std::move(settings.id);
|
2021-07-14 22:59:27 +00:00
|
|
|
__block int cancel_id = settings.cancel_id;
|
2019-03-12 18:06:59 +00:00
|
|
|
|
2021-11-01 21:08:31 +00:00
|
|
|
auto handler = ^(NSModalResponse response) {
|
|
|
|
if (id)
|
|
|
|
GetDialogsMap().erase(*id);
|
|
|
|
// When the alert is cancelled programmatically, the response would be
|
|
|
|
// something like -1000. This currently only happens when users call
|
|
|
|
// CloseMessageBox API, and we should return cancelId as result.
|
|
|
|
if (response < 0)
|
|
|
|
response = cancel_id;
|
2023-07-01 20:22:55 +00:00
|
|
|
bool suppressed = alert.suppressionButton.state == NSControlStateValueOn;
|
2021-11-01 21:08:31 +00:00
|
|
|
// The completionHandler runs inside a transaction commit, and we should
|
|
|
|
// not do any runModal inside it. However since we can not control what
|
|
|
|
// users will run in the callback, we have to delay running the callback
|
|
|
|
// until next tick, otherwise crash like this may happen:
|
|
|
|
// https://github.com/electron/electron/issues/26884
|
2022-05-17 16:48:40 +00:00
|
|
|
content::GetUIThreadTaskRunner({})->PostTask(
|
|
|
|
FROM_HERE,
|
2021-11-01 21:08:31 +00:00
|
|
|
base::BindOnce(std::move(callback_), response, suppressed));
|
|
|
|
};
|
|
|
|
[alert beginSheetModalForWindow:window completionHandler:handler];
|
2017-09-12 23:26:31 +00:00
|
|
|
}
|
2013-09-22 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 22:59:27 +00:00
|
|
|
void CloseMessageBox(int id) {
|
|
|
|
auto it = GetDialogsMap().find(id);
|
|
|
|
if (it == GetDialogsMap().end()) {
|
|
|
|
LOG(ERROR) << "CloseMessageBox called with nonexistent ID";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[NSApp endSheet:it->second.window];
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
|
2014-11-05 09:27:04 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert setMessageText:base::SysUTF16ToNSString(title)];
|
|
|
|
[alert setInformativeText:base::SysUTF16ToNSString(content)];
|
2022-06-01 06:12:47 +00:00
|
|
|
[alert setAlertStyle:NSAlertStyleCritical];
|
2014-11-05 09:27:04 +00:00
|
|
|
[alert runModal];
|
|
|
|
}
|
|
|
|
|
2013-05-03 13:03:26 +00:00
|
|
|
} // namespace electron
|