hotkeys for specific shells

This commit is contained in:
Eugene Pankov
2018-10-12 17:59:22 +02:00
parent cc610e158e
commit 9b6a09129c
10 changed files with 128 additions and 25 deletions

View File

@@ -5,4 +5,6 @@ export interface IHotkeyDescription {
export abstract class HotkeyProvider {
hotkeys: IHotkeyDescription[] = []
abstract provide (): Promise<IHotkeyDescription[]>
}

View File

@@ -9,15 +9,19 @@ import { HostAppService } from './hostApp.service'
const configMerge = (a, b) => require('deepmerge')(a, b, { arrayMerge: (_d, s) => s })
function isStructuralMember (v) {
return v instanceof Object && !(v instanceof Array) &&
Object.keys(v).length > 0 && !v.__nonStructural
}
function isNonStructuralObjectMember (v) {
return v instanceof Object && !(v instanceof Array) && v.__nonStructural
}
export class ConfigProxy {
constructor (real: any, defaults: any) {
for (let key in defaults) {
if (
defaults[key] instanceof Object &&
!(defaults[key] instanceof Array) &&
Object.keys(defaults[key]).length > 0 &&
!defaults[key].__nonStructural
) {
if (isStructuralMember(defaults[key])) {
if (!real[key]) {
real[key] = {}
}
@@ -38,15 +42,36 @@ export class ConfigProxy {
{
enumerable: true,
configurable: false,
get: () => (real[key] !== undefined) ? real[key] : defaults[key],
get: () => this.getValue(key),
set: (value) => {
real[key] = value
this.setValue(key, value)
}
}
)
}
}
this.getValue = (key: string) => {
if (real[key] !== undefined) {
return real[key]
} else {
if (isNonStructuralObjectMember(defaults[key])) {
real[key] = {...defaults[key]}
delete real[key].__nonStructural
return real[key]
} else {
return defaults[key]
}
}
}
this.setValue = (key: string, value: any) => {
real[key] = value
}
}
getValue (key: string): any { } // tslint:disable-line
setValue (key: string, value: any) { } // tslint:disable-line
}
@Injectable()

View File

@@ -24,13 +24,13 @@ export class HotkeysService {
globalHotkey = new EventEmitter()
private currentKeystrokes: EventBufferEntry[] = []
private disabledLevel = 0
private hotkeyDescriptions: IHotkeyDescription[]
private hotkeyDescriptions: IHotkeyDescription[] = []
constructor (
private zone: NgZone,
private electron: ElectronService,
private config: ConfigService,
@Inject(HotkeyProvider) hotkeyProviders: HotkeyProvider[],
@Inject(HotkeyProvider) private hotkeyProviders: HotkeyProvider[],
) {
let events = ['keydown', 'keyup']
events.forEach((event) => {
@@ -42,11 +42,13 @@ export class HotkeysService {
}
})
})
this.hotkeyDescriptions = this.config.enabledServices(hotkeyProviders).map(x => x.hotkeys).reduce((a, b) => a.concat(b))
this.config.changed$.subscribe(() => {
this.registerGlobalHotkey()
})
this.registerGlobalHotkey()
this.getHotkeyDescriptions().then(hotkeys => {
this.hotkeyDescriptions = hotkeys
})
}
pushKeystroke (name, nativeEvent) {
@@ -102,14 +104,25 @@ export class HotkeysService {
}
getHotkeysConfig () {
return this.getHotkeysConfigRecursive(this.config.store.hotkeys)
}
getHotkeysConfigRecursive (branch) {
let keys = {}
for (let key in this.config.store.hotkeys) {
let value = this.config.store.hotkeys[key]
if (typeof value === 'string') {
value = [value]
for (let key in branch) {
let value = branch[key]
if (value instanceof Object && !(value instanceof Array)) {
let subkeys = this.getHotkeysConfigRecursive(value)
for (let subkey in subkeys) {
keys[key + '.' + subkey] = subkeys[subkey]
}
} else {
if (typeof value === 'string') {
value = [value]
}
value = value.map((item) => (typeof item === 'string') ? [item] : item)
keys[key] = value
}
value = value.map((item) => (typeof item === 'string') ? [item] : item)
keys[key] = value
}
return keys
}
@@ -169,6 +182,15 @@ export class HotkeysService {
isEnabled () {
return this.disabledLevel === 0
}
async getHotkeyDescriptions (): Promise<IHotkeyDescription[]> {
return (
await Promise.all(
this.config.enabledServices(this.hotkeyProviders)
.map(async x => x.provide ? x.provide() : x.hotkeys)
)
).reduce((a, b) => a.concat(b))
}
}
@Injectable()
@@ -243,4 +265,8 @@ export class AppHotkeyProvider extends HotkeyProvider {
name: 'Tab 10',
},
]
async provide (): Promise<IHotkeyDescription[]> {
return this.hotkeys
}
}