Add systemPreferences.subscribeNotification
This commit is contained in:
parent
ddd8eae661
commit
9557226223
3 changed files with 50 additions and 2 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_system_preferences.h"
|
#include "atom/browser/api/atom_api_system_preferences.h"
|
||||||
|
|
||||||
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
|
@ -46,6 +47,11 @@ void SystemPreferences::BuildPrototype(
|
||||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
.SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
|
.SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
|
||||||
|
#elif defined(OS_MACOSX)
|
||||||
|
.SetMethod("subscribeNotification",
|
||||||
|
&SystemPreferences::SubscribeNotification)
|
||||||
|
.SetMethod("unsubscribeNotification",
|
||||||
|
&SystemPreferences::UnsubscribeNotification)
|
||||||
#endif
|
#endif
|
||||||
.SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
|
.SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,10 @@
|
||||||
#ifndef ATOM_BROWSER_API_ATOM_API_SYSTEM_PREFERENCES_H_
|
#ifndef ATOM_BROWSER_API_ATOM_API_SYSTEM_PREFERENCES_H_
|
||||||
#define ATOM_BROWSER_API_ATOM_API_SYSTEM_PREFERENCES_H_
|
#define ATOM_BROWSER_API_ATOM_API_SYSTEM_PREFERENCES_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/api/event_emitter.h"
|
#include "atom/browser/api/event_emitter.h"
|
||||||
|
#include "base/callback.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -21,6 +24,10 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
bool IsAeroGlassEnabled();
|
bool IsAeroGlassEnabled();
|
||||||
|
#elif defined(OS_MACOSX)
|
||||||
|
int SubscribeNotification(const std::string& name,
|
||||||
|
const base::Closure& callback);
|
||||||
|
void UnsubscribeNotification(int id);
|
||||||
#endif
|
#endif
|
||||||
bool IsDarkMode();
|
bool IsDarkMode();
|
||||||
|
|
||||||
|
|
|
@ -4,19 +4,54 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_system_preferences.h"
|
#include "atom/browser/api/atom_api_system_preferences.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
|
#include "base/strings/sys_string_conversions.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
#if defined(OS_MACOSX)
|
namespace {
|
||||||
|
|
||||||
|
int g_next_id = 0;
|
||||||
|
|
||||||
|
// The map to convert |id| to |int|.
|
||||||
|
std::map<int, id> g_id_map;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int SystemPreferences::SubscribeNotification(const std::string& name,
|
||||||
|
const base::Closure& callback) {
|
||||||
|
int request_id = g_next_id++;
|
||||||
|
__block base::Closure copied_callback = callback;
|
||||||
|
g_id_map[request_id] = [[NSDistributedNotificationCenter defaultCenter]
|
||||||
|
addObserverForName:base::SysUTF8ToNSString(name)
|
||||||
|
object:nil
|
||||||
|
queue:nil
|
||||||
|
usingBlock:^(NSNotification* notification) {
|
||||||
|
copied_callback.Run();
|
||||||
|
}
|
||||||
|
];
|
||||||
|
return request_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SystemPreferences::UnsubscribeNotification(int request_id) {
|
||||||
|
auto iter = g_id_map.find(request_id);
|
||||||
|
if (iter != g_id_map.end()) {
|
||||||
|
id observer = iter->second;
|
||||||
|
[[NSDistributedNotificationCenter defaultCenter] removeObserver:observer];
|
||||||
|
g_id_map.erase(iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool SystemPreferences::IsDarkMode() {
|
bool SystemPreferences::IsDarkMode() {
|
||||||
NSString* mode = [[NSUserDefaults standardUserDefaults]
|
NSString* mode = [[NSUserDefaults standardUserDefaults]
|
||||||
stringForKey:@"AppleInterfaceStyle"];
|
stringForKey:@"AppleInterfaceStyle"];
|
||||||
return [mode isEqualToString:@"Dark"];
|
return [mode isEqualToString:@"Dark"];
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue