mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
refactor: lazy-create electron::api::GlobalShortcut
copy the lazy-create idom used by electron::api::Screen
This commit is contained in:
@@ -1,2 +1,36 @@
|
||||
const { globalShortcut } = process._linkedBinding('electron_browser_global_shortcut');
|
||||
export default globalShortcut;
|
||||
const { createGlobalShortcut } = process._linkedBinding('electron_browser_global_shortcut');
|
||||
|
||||
let globalShortcut: Electron.GlobalShortcut;
|
||||
|
||||
const createGlobalShortcutIfNeeded = () => {
|
||||
if (globalShortcut === undefined) {
|
||||
globalShortcut = createGlobalShortcut();
|
||||
}
|
||||
};
|
||||
|
||||
export default new Proxy({}, {
|
||||
get: (_target, property: keyof Electron.GlobalShortcut) => {
|
||||
createGlobalShortcutIfNeeded();
|
||||
const value = globalShortcut[property];
|
||||
if (typeof value === 'function') {
|
||||
return value.bind(globalShortcut);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
set: (_target, property: string, value: unknown) => {
|
||||
createGlobalShortcutIfNeeded();
|
||||
return Reflect.set(globalShortcut, property, value);
|
||||
},
|
||||
ownKeys: () => {
|
||||
createGlobalShortcutIfNeeded();
|
||||
return Reflect.ownKeys(globalShortcut);
|
||||
},
|
||||
has: (_target, property: string) => {
|
||||
createGlobalShortcutIfNeeded();
|
||||
return property in globalShortcut;
|
||||
},
|
||||
getOwnPropertyDescriptor: (_target, property: string) => {
|
||||
createGlobalShortcutIfNeeded();
|
||||
return Reflect.getOwnPropertyDescriptor(globalShortcut, property);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -268,8 +268,9 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
|
||||
gin::Dictionary dict{isolate, exports};
|
||||
dict.Set("globalShortcut", electron::api::GlobalShortcut::Create(isolate));
|
||||
gin_helper::Dictionary dict{isolate, exports};
|
||||
dict.SetMethod("createGlobalShortcut",
|
||||
base::BindRepeating(&electron::api::GlobalShortcut::Create));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "extensions/common/extension_id.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "shell/common/gin_helper/self_keep_alive.h"
|
||||
#include "ui/base/accelerators/accelerator.h"
|
||||
#include "ui/base/accelerators/global_accelerator_listener/global_accelerator_listener.h"
|
||||
|
||||
@@ -59,6 +60,7 @@ class GlobalShortcut final : private ui::GlobalAcceleratorListener::Observer,
|
||||
AcceleratorCallbackMap accelerator_callback_map_;
|
||||
CommandCallbackMap command_callback_map_;
|
||||
|
||||
gin_helper::SelfKeepAlive<GlobalShortcut> keep_alive_{this};
|
||||
base::WeakPtrFactory<GlobalShortcut> weak_ptr_factory_{this};
|
||||
};
|
||||
|
||||
|
||||
2
typings/internal-ambient.d.ts
vendored
2
typings/internal-ambient.d.ts
vendored
@@ -242,7 +242,7 @@ declare namespace NodeJS {
|
||||
_linkedBinding(name: 'electron_browser_crash_reporter'): CrashReporterBinding;
|
||||
_linkedBinding(name: 'electron_browser_desktop_capturer'): { createDesktopCapturer(): ElectronInternal.DesktopCapturer; isDisplayMediaSystemPickerAvailable(): boolean; };
|
||||
_linkedBinding(name: 'electron_browser_event_emitter'): { setEventEmitterPrototype(prototype: Object): void; };
|
||||
_linkedBinding(name: 'electron_browser_global_shortcut'): { globalShortcut: Electron.GlobalShortcut };
|
||||
_linkedBinding(name: 'electron_browser_global_shortcut'): { createGlobalShortcut(): Electron.GlobalShortcut };
|
||||
_linkedBinding(name: 'electron_browser_image_view'): { ImageView: any };
|
||||
_linkedBinding(name: 'electron_browser_in_app_purchase'): { inAppPurchase: Electron.InAppPurchase };
|
||||
_linkedBinding(name: 'electron_browser_message_port'): { createPair(): { port1: Electron.MessagePortMain, port2: Electron.MessagePortMain }; };
|
||||
|
||||
Reference in New Issue
Block a user