feat: IHotkeyUsing 补充 when 方法

This commit is contained in:
AncTe committed 2026-11-05 18:51:26 +08:00
1 parent 5b1f866409
commit b950c53f98
2 files changed
+18

No files matched your search

@@ -13,6 +13,8 @@ class HotkeyUsing implements IHotkeyUsing {
private keyScope: symbol | null = null;
/** 当前的按键实现对象 */
private impl: Map<string, (info: IHotkeyEmitInfo) => void> = new Map();
/** 当前设置的 `when` 判断函数 */
private whenFunc: (() => boolean) | null = null;
constructor(
readonly system: IHotkeySystem,
@@ -26,6 +28,7 @@ class HotkeyUsing implements IHotkeyUsing {
const scope = system.enterScope(clearImpl);
this.keyScope = scope;
resolve(scope);
if (this.whenFunc) system.when(this.whenFunc);
for (const [command, func] of this.impl) {
system.implement(command, func);
}
@@ -45,6 +48,15 @@ class HotkeyUsing implements IHotkeyUsing {
return this.scopePromise;
}
when(func: () => boolean): this {
if (this.mounted) {
this.system.when(func);
} else {
this.whenFunc = func;
}
return this;
}
implement(command: string, func: (info: IHotkeyEmitInfo) => void): this {
if (this.mounted) {
this.system.implement(command, func);
@@ -6,6 +6,12 @@ export interface IHotkeyUsing {
*/
scope(): Promise<symbol>;
/**
* 设置当前的按键判断函数,当函数返回 `true` 时,按键才会真正生效
* @param func 判断函数,返回当前是否应该生效
*/
when(func: () => boolean): this;
/**
* 实现按键指令
* @param command 按键指令