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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/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>
|
|
|
|
|
2014-03-16 01:37:04 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
2013-09-22 09:11:09 +00:00
|
|
|
#include "base/callback.h"
|
2016-02-08 23:18:05 +00:00
|
|
|
#include "base/mac/mac_util.h"
|
2013-05-03 13:03:26 +00:00
|
|
|
#include "base/strings/sys_string_conversions.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 atom {
|
|
|
|
|
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 {
|
|
|
|
|
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:
|
2017-04-13 18:36:48 +00:00
|
|
|
alert.alertStyle = NSInformationalAlertStyle;
|
2013-05-03 13:03:26 +00:00
|
|
|
break;
|
2019-05-03 18:11:41 +00:00
|
|
|
case MessageBoxType::kWarning:
|
|
|
|
case MessageBoxType::kError:
|
2017-04-13 18:36:48 +00:00
|
|
|
// NSWarningAlertStyle shows the app icon while NSCriticalAlertStyle
|
|
|
|
// shows a warning icon with an app icon badge. Since there is no
|
|
|
|
// error variant, lets just use NSCriticalAlertStyle.
|
|
|
|
alert.alertStyle = NSCriticalAlertStyle;
|
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) {
|
2016-01-11 13:12:07 +00:00
|
|
|
// Focus the button at default_id if the user opted to do so.
|
|
|
|
// The first button added gets set as the default selected.
|
|
|
|
// So remove that default, and make the requested button the default.
|
|
|
|
[[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
|
|
|
}
|
|
|
|
|
2018-08-19 20:41:03 +00:00
|
|
|
// Bind cancel id button to escape key if there is more than one button
|
2019-06-14 15:26:25 +00:00
|
|
|
if (button_count > 1 && settings.cancel_id >= 0 &&
|
|
|
|
settings.cancel_id < button_count) {
|
|
|
|
[[ns_buttons objectAtIndex:settings.cancel_id] setKeyEquivalent:@"\e"];
|
2018-08-19 20:41:03 +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);
|
|
|
|
alert.suppressionButton.state =
|
|
|
|
settings.checkbox_checked ? NSOnState : NSOffState;
|
2017-02-06 15:35:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.icon.isNull()) {
|
2016-03-08 14:28:53 +00:00
|
|
|
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
|
2019-06-14 15:26:25 +00:00
|
|
|
*settings.icon.bitmap(), base::mac::GetGenericRGBColorSpace());
|
2016-02-08 23:18:05 +00:00
|
|
|
[alert setIcon:image];
|
|
|
|
}
|
|
|
|
|
2013-09-22 10:47:00 +00:00
|
|
|
return alert;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-14 15:26:25 +00:00
|
|
|
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
|
|
|
|
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
|
|
|
|
// window to wait for.
|
2019-06-14 15:26:25 +00:00
|
|
|
if (!settings.parent_window)
|
2014-03-05 11:51:26 +00:00
|
|
|
return [[alert autorelease] runModal];
|
|
|
|
|
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) {
|
2017-09-13 07:36:28 +00:00
|
|
|
int ret = [[alert autorelease] runModal];
|
2019-03-12 18:06:59 +00:00
|
|
|
std::move(callback).Run(ret, alert.suppressionButton.state == NSOnState);
|
2017-09-12 23:26:31 +00:00
|
|
|
} else {
|
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);
|
|
|
|
|
2019-02-27 18:23:17 +00:00
|
|
|
[alert beginSheetModalForWindow:window
|
|
|
|
completionHandler:^(NSModalResponse response) {
|
2019-03-12 18:06:59 +00:00
|
|
|
std::move(callback_).Run(
|
|
|
|
response, alert.suppressionButton.state == NSOnState);
|
2019-02-27 18:23:17 +00:00
|
|
|
}];
|
2017-09-12 23:26:31 +00:00
|
|
|
}
|
2013-09-22 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 09:27:04 +00:00
|
|
|
void ShowErrorBox(const base::string16& title, const base::string16& content) {
|
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert setMessageText:base::SysUTF16ToNSString(title)];
|
|
|
|
[alert setInformativeText:base::SysUTF16ToNSString(content)];
|
2017-04-13 18:36:48 +00:00
|
|
|
[alert setAlertStyle:NSCriticalAlertStyle];
|
2014-11-05 09:27:04 +00:00
|
|
|
[alert runModal];
|
|
|
|
[alert release];
|
|
|
|
}
|
|
|
|
|
2013-05-03 13:03:26 +00:00
|
|
|
} // namespace atom
|