electron/atom/browser/api/atom_api_system_preferences_mac.mm

403 lines
12 KiB
Text
Raw Normal View History

2016-04-24 12:13:46 +00:00
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_system_preferences.h"
#include <map>
#import <Cocoa/Cocoa.h>
#include "atom/browser/mac/atom_application.h"
2016-05-18 05:40:19 +00:00
#include "atom/browser/mac/dict_util.h"
2016-04-25 06:35:52 +00:00
#include "atom/common/native_mate_converters/gurl_converter.h"
2018-04-20 18:47:04 +00:00
#include "atom/common/native_mate_converters/value_converter.h"
#include "base/strings/sys_string_conversions.h"
2016-05-18 05:40:19 +00:00
#include "base/values.h"
#include "native_mate/object_template_builder.h"
2016-04-25 06:35:52 +00:00
#include "net/base/mac/url_conversions.h"
namespace mate {
template <>
struct Converter<NSAppearance*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
NSAppearance** out) {
if (val->IsNull()) {
*out = nil;
return true;
}
std::string name;
if (!mate::ConvertFromV8(isolate, val, &name)) {
return false;
}
if (name == "light") {
*out = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
return true;
} else if (name == "dark") {
*out = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
return true;
}
return false;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, NSAppearance* val) {
if (val == nil) {
return v8::Null(isolate);
}
if (val.name == NSAppearanceNameAqua) {
return mate::ConvertToV8(isolate, "light");
}
if (val.name == NSAppearanceNameDarkAqua) {
return mate::ConvertToV8(isolate, "dark");
}
return mate::ConvertToV8(isolate, "unknown");
}
};
} // namespace mate
2016-04-24 12:13:46 +00:00
namespace atom {
namespace api {
namespace {
int g_next_id = 0;
// The map to convert |id| to |int|.
std::map<int, id> g_id_map;
} // namespace
2018-04-20 18:47:04 +00:00
void SystemPreferences::PostNotification(
const std::string& name,
const base::DictionaryValue& user_info) {
DoPostNotification(name, user_info, kNSDistributedNotificationCenter);
}
int SystemPreferences::SubscribeNotification(
2018-04-20 18:47:04 +00:00
const std::string& name,
const NotificationCallback& callback) {
return DoSubscribeNotification(name, callback,
kNSDistributedNotificationCenter);
}
void SystemPreferences::UnsubscribeNotification(int request_id) {
DoUnsubscribeNotification(request_id, kNSDistributedNotificationCenter);
}
2018-04-20 18:47:04 +00:00
void SystemPreferences::PostLocalNotification(
const std::string& name,
const base::DictionaryValue& user_info) {
DoPostNotification(name, user_info, kNSNotificationCenter);
}
int SystemPreferences::SubscribeLocalNotification(
2018-04-20 18:47:04 +00:00
const std::string& name,
const NotificationCallback& callback) {
return DoSubscribeNotification(name, callback, kNSNotificationCenter);
}
void SystemPreferences::UnsubscribeLocalNotification(int request_id) {
DoUnsubscribeNotification(request_id, kNSNotificationCenter);
}
2018-04-20 18:47:04 +00:00
void SystemPreferences::PostWorkspaceNotification(
const std::string& name,
const base::DictionaryValue& user_info) {
DoPostNotification(name, user_info, kNSWorkspaceNotificationCenter);
}
int SystemPreferences::SubscribeWorkspaceNotification(
2018-04-20 18:47:04 +00:00
const std::string& name,
const NotificationCallback& callback) {
return DoSubscribeNotification(name, callback,
kNSWorkspaceNotificationCenter);
}
void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
DoUnsubscribeNotification(request_id, kNSWorkspaceNotificationCenter);
}
2018-04-20 18:47:04 +00:00
void SystemPreferences::DoPostNotification(
const std::string& name,
const base::DictionaryValue& user_info,
NotificationCenterKind kind) {
NSNotificationCenter* center;
switch (kind) {
2018-04-20 18:47:04 +00:00
case kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter];
break;
case kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter];
break;
case kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter];
break;
default:
break;
}
2018-04-20 18:47:04 +00:00
[center postNotificationName:base::SysUTF8ToNSString(name)
object:nil
userInfo:DictionaryValueToNSDictionary(user_info)];
}
2018-04-20 18:47:04 +00:00
int SystemPreferences::DoSubscribeNotification(
const std::string& name,
const NotificationCallback& callback,
NotificationCenterKind kind) {
int request_id = g_next_id++;
2016-05-18 05:40:19 +00:00
__block NotificationCallback copied_callback = callback;
NSNotificationCenter* center;
switch (kind) {
2018-04-20 18:47:04 +00:00
case kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter];
break;
case kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter];
break;
case kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter];
break;
default:
break;
}
2018-04-20 18:47:04 +00:00
g_id_map[request_id] = [center
2018-04-20 18:47:04 +00:00
addObserverForName:base::SysUTF8ToNSString(name)
object:nil
queue:nil
usingBlock:^(NSNotification* notification) {
std::unique_ptr<base::DictionaryValue> user_info =
NSDictionaryToDictionaryValue(notification.userInfo);
if (user_info) {
copied_callback.Run(
base::SysNSStringToUTF8(notification.name), *user_info);
} else {
copied_callback.Run(
base::SysNSStringToUTF8(notification.name),
base::DictionaryValue());
}
}];
return request_id;
}
2018-04-20 18:47:04 +00:00
void SystemPreferences::DoUnsubscribeNotification(int request_id,
NotificationCenterKind kind) {
auto iter = g_id_map.find(request_id);
if (iter != g_id_map.end()) {
id observer = iter->second;
NSNotificationCenter* center;
switch (kind) {
2018-04-20 18:47:04 +00:00
case kNSDistributedNotificationCenter:
center = [NSDistributedNotificationCenter defaultCenter];
break;
case kNSNotificationCenter:
center = [NSNotificationCenter defaultCenter];
break;
case kNSWorkspaceNotificationCenter:
center = [[NSWorkspace sharedWorkspace] notificationCenter];
break;
default:
break;
}
[center removeObserver:observer];
g_id_map.erase(iter);
}
}
2016-04-25 06:35:52 +00:00
v8::Local<v8::Value> SystemPreferences::GetUserDefault(
2018-04-20 18:47:04 +00:00
const std::string& name,
const std::string& type) {
2016-04-25 06:35:52 +00:00
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* key = base::SysUTF8ToNSString(name);
if (type == "string") {
2018-04-20 18:47:04 +00:00
return mate::StringToV8(
isolate(), base::SysNSStringToUTF8([defaults stringForKey:key]));
2016-04-25 06:35:52 +00:00
} else if (type == "boolean") {
return v8::Boolean::New(isolate(), [defaults boolForKey:key]);
} else if (type == "float") {
return v8::Number::New(isolate(), [defaults floatForKey:key]);
} else if (type == "integer") {
return v8::Integer::New(isolate(), [defaults integerForKey:key]);
} else if (type == "double") {
return v8::Number::New(isolate(), [defaults doubleForKey:key]);
} else if (type == "url") {
return mate::ConvertToV8(isolate(),
2018-04-20 18:47:04 +00:00
net::GURLWithNSURL([defaults URLForKey:key]));
} else if (type == "array") {
std::unique_ptr<base::ListValue> list =
NSArrayToListValue([defaults arrayForKey:key]);
if (list == nullptr)
list.reset(new base::ListValue());
return mate::ConvertToV8(isolate(), *list);
} else if (type == "dictionary") {
std::unique_ptr<base::DictionaryValue> dictionary =
NSDictionaryToDictionaryValue([defaults dictionaryForKey:key]);
if (dictionary == nullptr)
dictionary.reset(new base::DictionaryValue());
return mate::ConvertToV8(isolate(), *dictionary);
} else {
return v8::Undefined(isolate());
}
}
2017-12-12 18:08:09 +00:00
void SystemPreferences::RegisterDefaults(mate::Arguments* args) {
base::DictionaryValue value;
2018-04-20 18:47:04 +00:00
if (!args->GetNext(&value)) {
2017-12-12 18:08:09 +00:00
args->ThrowError("Invalid userDefault data provided");
} else {
@try {
NSDictionary* dict = DictionaryValueToNSDictionary(value);
2017-12-13 19:02:43 +00:00
for (id key in dict) {
id value = [dict objectForKey:key];
if ([value isKindOfClass:[NSNull class]] || value == nil) {
args->ThrowError("Invalid userDefault data provided");
return;
}
}
2017-12-12 18:08:09 +00:00
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
} @catch (NSException* exception) {
args->ThrowError("Invalid userDefault data provided");
}
}
}
void SystemPreferences::SetUserDefault(const std::string& name,
const std::string& type,
mate::Arguments* args) {
const auto throwConversionError = [&] {
args->ThrowError("Unable to convert value to: " + type);
};
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* key = base::SysUTF8ToNSString(name);
if (type == "string") {
std::string value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
[defaults setObject:base::SysUTF8ToNSString(value) forKey:key];
} else if (type == "boolean") {
bool value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
[defaults setBool:value forKey:key];
} else if (type == "float") {
float value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
[defaults setFloat:value forKey:key];
} else if (type == "integer") {
int value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
[defaults setInteger:value forKey:key];
} else if (type == "double") {
double value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
[defaults setDouble:value forKey:key];
} else if (type == "url") {
GURL value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
if (NSURL* url = net::NSURLWithGURL(value)) {
[defaults setURL:url forKey:key];
}
} else if (type == "array") {
base::ListValue value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
if (NSArray* array = ListValueToNSArray(value)) {
[defaults setObject:array forKey:key];
}
} else if (type == "dictionary") {
base::DictionaryValue value;
if (!args->GetNext(&value)) {
throwConversionError();
return;
}
if (NSDictionary* dict = DictionaryValueToNSDictionary(value)) {
[defaults setObject:dict forKey:key];
}
} else {
args->ThrowError("Invalid type: " + type);
return;
}
}
void SystemPreferences::RemoveUserDefault(const std::string& name) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:base::SysUTF8ToNSString(name)];
}
bool SystemPreferences::IsDarkMode() {
NSString* mode = [[NSUserDefaults standardUserDefaults]
stringForKey:@"AppleInterfaceStyle"];
return [mode isEqualToString:@"Dark"];
}
2016-04-24 12:13:46 +00:00
bool SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled() {
return [NSEvent isSwipeTrackingFromScrollEventsEnabled];
}
v8::Local<v8::Value> SystemPreferences::GetEffectiveAppearance(
v8::Isolate* isolate) {
if (@available(macOS 10.14, *)) {
return mate::ConvertToV8(
isolate, [NSApplication sharedApplication].effectiveAppearance);
}
return v8::Null(isolate);
}
v8::Local<v8::Value> SystemPreferences::GetAppLevelAppearance(
v8::Isolate* isolate) {
if (@available(macOS 10.14, *)) {
return mate::ConvertToV8(isolate,
[NSApplication sharedApplication].appearance);
}
return v8::Null(isolate);
}
void SystemPreferences::SetAppLevelAppearance(mate::Arguments* args) {
if (@available(macOS 10.14, *)) {
NSAppearance* appearance;
if (args->GetNext(&appearance)) {
[[NSApplication sharedApplication] setAppearance:appearance];
} else {
args->ThrowError("Invalid app appearance provided as first argument");
}
}
}
2016-04-24 12:13:46 +00:00
} // namespace api
} // namespace atom