hotkeys.service.ts 6.9 KB
Newer Older
E
.  
Eugene Pankov 已提交
1
import { Injectable, Inject, NgZone, EventEmitter } from '@angular/core'
E
wip  
Eugene Pankov 已提交
2
import { IHotkeyDescription, HotkeyProvider } from '../api/hotkeyProvider'
E
.  
Eugene Pankov 已提交
3 4 5
import { NativeKeyEvent, stringifyKeySequence } from './hotkeys.util'
import { ConfigService } from '../services/config.service'
import { ElectronService } from '../services/electron.service'
E
.  
Eugene Pankov 已提交
6

E
.  
Eugene Pankov 已提交
7 8 9 10 11 12
export interface PartialHotkeyMatch {
    id: string,
    strokes: string[],
    matchedLength: number,
}

E
.  
Eugene Pankov 已提交
13
const KEY_TIMEOUT = 2000
E
.  
Eugene Pankov 已提交
14

E
.  
Eugene Pankov 已提交
15 16 17
interface EventBufferEntry {
    event: NativeKeyEvent,
    time: number,
E
.  
Eugene Pankov 已提交
18 19 20 21
}

@Injectable()
export class HotkeysService {
E
.  
Eugene Pankov 已提交
22
    key = new EventEmitter<NativeKeyEvent>()
E
.  
Eugene Pankov 已提交
23
    matchedHotkey = new EventEmitter<string>()
E
.  
Eugene Pankov 已提交
24
    globalHotkey = new EventEmitter()
E
.  
Eugene Pankov 已提交
25
    private currentKeystrokes: EventBufferEntry[] = []
E
.  
Eugene Pankov 已提交
26
    private disabledLevel = 0
E
.  
Eugene Pankov 已提交
27
    private hotkeyDescriptions: IHotkeyDescription[]
E
.  
Eugene Pankov 已提交
28

E
lint  
Eugene Pankov 已提交
29
    constructor (
E
.  
Eugene Pankov 已提交
30 31
        private zone: NgZone,
        private electron: ElectronService,
E
.  
Eugene Pankov 已提交
32
        private config: ConfigService,
E
.  
Eugene Pankov 已提交
33
        @Inject(HotkeyProvider) hotkeyProviders: HotkeyProvider[],
E
.  
Eugene Pankov 已提交
34
    ) {
E
.  
Eugene Pankov 已提交
35
        let events = ['keydown', 'keyup']
E
.  
Eugene Pankov 已提交
36
        events.forEach((event) => {
E
.  
Eugene Pankov 已提交
37
            document.addEventListener(event, (nativeEvent) => {
E
lint  
Eugene Pankov 已提交
38
                if (document.querySelectorAll('input:focus').length === 0) {
E
.  
Eugene Pankov 已提交
39 40 41
                    this.pushKeystroke(event, nativeEvent)
                    this.processKeystrokes()
                    this.emitKeyEvent(nativeEvent)
E
.  
Eugene Pankov 已提交
42
                }
E
.  
Eugene Pankov 已提交
43 44
            })
        })
E
Eugene Pankov 已提交
45
        this.hotkeyDescriptions = this.config.enabledServices(hotkeyProviders).map(x => x.hotkeys).reduce((a, b) => a.concat(b))
E
wip  
Eugene Pankov 已提交
46
        this.config.changed$.subscribe(() => {
E
.  
Eugene Pankov 已提交
47 48 49
            this.registerGlobalHotkey()
        })
        this.registerGlobalHotkey()
E
.  
Eugene Pankov 已提交
50 51
    }

E
.  
Eugene Pankov 已提交
52
    pushKeystroke (name, nativeEvent) {
E
.  
Eugene Pankov 已提交
53 54
        nativeEvent.event = name
        this.currentKeystrokes.push({ event: nativeEvent, time: performance.now() })
E
.  
Eugene Pankov 已提交
55
    }
E
.  
Eugene Pankov 已提交
56

E
.  
Eugene Pankov 已提交
57
    processKeystrokes () {
E
.  
Eugene Pankov 已提交
58 59
        if (this.isEnabled()) {
            this.zone.run(() => {
E
.  
Eugene Pankov 已提交
60 61 62 63 64 65
                let matched = this.getCurrentFullyMatchedHotkey()
                if (matched) {
                    console.log('Matched hotkey', matched)
                    this.matchedHotkey.emit(matched)
                    this.clearCurrentKeystrokes()
                }
E
.  
Eugene Pankov 已提交
66 67
            })
        }
E
.  
Eugene Pankov 已提交
68 69 70 71
    }

    emitKeyEvent (nativeEvent) {
        this.zone.run(() => {
E
.  
Eugene Pankov 已提交
72
            this.key.emit(nativeEvent)
E
.  
Eugene Pankov 已提交
73 74
        })
    }
E
.  
Eugene Pankov 已提交
75

E
.  
Eugene Pankov 已提交
76 77 78 79
    clearCurrentKeystrokes () {
        this.currentKeystrokes = []
    }

E
lint  
Eugene Pankov 已提交
80
    getCurrentKeystrokes (): string[] {
E
.  
Eugene Pankov 已提交
81 82 83 84
        this.currentKeystrokes = this.currentKeystrokes.filter((x) => performance.now() - x.time < KEY_TIMEOUT )
        return stringifyKeySequence(this.currentKeystrokes.map((x) => x.event))
    }

E
.  
Eugene Pankov 已提交
85
    registerGlobalHotkey () {
E
.  
Eugene Pankov 已提交
86
        this.electron.globalShortcut.unregisterAll()
E
.  
Eugene Pankov 已提交
87
        let value = this.config.store.hotkeys['toggle-window']
E
lint  
Eugene Pankov 已提交
88
        if (typeof value === 'string') {
E
.  
Eugene Pankov 已提交
89 90 91
            value = [value]
        }
        value.forEach(item => {
E
lint  
Eugene Pankov 已提交
92
            item = (typeof item === 'string') ? [item] : item
E
.  
Eugene Pankov 已提交
93

E
wip  
Eugene Pankov 已提交
94 95 96 97 98 99 100
            try {
                this.electron.globalShortcut.register(item[0].replace(/-/g, '+'), () => {
                    this.globalHotkey.emit()
                })
            } catch (err) {
                console.error('Could not register the global hotkey:', err)
            }
E
.  
Eugene Pankov 已提交
101 102
        })
    }
E
.  
Eugene Pankov 已提交
103 104 105

    getHotkeysConfig () {
        let keys = {}
E
config  
Eugene Pankov 已提交
106 107
        for (let key in this.config.store.hotkeys) {
            let value = this.config.store.hotkeys[key]
E
lint  
Eugene Pankov 已提交
108
            if (typeof value === 'string') {
E
.  
Eugene Pankov 已提交
109 110
                value = [value]
            }
E
lint  
Eugene Pankov 已提交
111
            value = value.map((item) => (typeof item === 'string') ? [item] : item)
E
.  
Eugene Pankov 已提交
112
            keys[key] = value
E
.  
Eugene Pankov 已提交
113 114 115 116
        }
        return keys
    }

E
lint  
Eugene Pankov 已提交
117
    getCurrentFullyMatchedHotkey (): string {
E
.  
Eugene Pankov 已提交
118 119 120 121
        for (let id in this.getHotkeysConfig()) {
            for (let sequence of this.getHotkeysConfig()[id]) {
                let currentStrokes = this.getCurrentKeystrokes()
                if (currentStrokes.length < sequence.length) {
E
.  
Eugene Pankov 已提交
122
                    continue
E
.  
Eugene Pankov 已提交
123 124
                }
                if (sequence.every((x, index) => {
E
lint  
Eugene Pankov 已提交
125
                    return x.toLowerCase() === currentStrokes[currentStrokes.length - sequence.length + index].toLowerCase()
E
.  
Eugene Pankov 已提交
126 127 128 129 130 131 132 133
                })) {
                    return id
                }
            }
        }
        return null
    }

E
lint  
Eugene Pankov 已提交
134
    getCurrentPartiallyMatchedHotkeys (): PartialHotkeyMatch[] {
E
.  
Eugene Pankov 已提交
135 136 137 138 139 140 141
        let result = []
        for (let id in this.getHotkeysConfig()) {
            for (let sequence of this.getHotkeysConfig()[id]) {
                let currentStrokes = this.getCurrentKeystrokes()

                for (let matchLength = Math.min(currentStrokes.length, sequence.length); matchLength > 0; matchLength--) {
                    if (sequence.slice(0, matchLength).every((x, index) => {
E
lint  
Eugene Pankov 已提交
142
                        return x.toLowerCase() === currentStrokes[currentStrokes.length - matchLength + index].toLowerCase()
E
.  
Eugene Pankov 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156
                    })) {
                        result.push({
                            matchedLength: matchLength,
                            id,
                            strokes: sequence
                        })
                        break
                    }
                }
            }
        }
        return result
    }

E
lint  
Eugene Pankov 已提交
157 158
    getHotkeyDescription (id: string): IHotkeyDescription {
        return this.hotkeyDescriptions.filter((x) => x.id === id)[0]
E
.  
Eugene Pankov 已提交
159
    }
E
.  
Eugene Pankov 已提交
160 161 162 163 164 165 166 167 168 169

    enable () {
        this.disabledLevel--
    }

    disable () {
        this.disabledLevel++
    }

    isEnabled () {
E
lint  
Eugene Pankov 已提交
170
        return this.disabledLevel === 0
E
.  
Eugene Pankov 已提交
171
    }
E
.  
Eugene Pankov 已提交
172
}
E
.  
Eugene Pankov 已提交
173 174 175 176

@Injectable()
export class AppHotkeyProvider extends HotkeyProvider {
    hotkeys: IHotkeyDescription[] = [
E
.  
Eugene Pankov 已提交
177 178 179 180
        {
            id: 'toggle-window',
            name: 'Toggle terminal window',
        },
E
Eugene Pankov 已提交
181 182 183 184
        {
            id: 'toggle-fullscreen',
            name: 'Toggle fullscreen mode',
        },
E
.  
Eugene Pankov 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
        {
            id: 'close-tab',
            name: 'Close tab',
        },
        {
            id: 'toggle-last-tab',
            name: 'Toggle last tab',
        },
        {
            id: 'next-tab',
            name: 'Next tab',
        },
        {
            id: 'previous-tab',
            name: 'Previous tab',
        },
        {
            id: 'tab-1',
            name: 'Tab 1',
        },
        {
            id: 'tab-2',
            name: 'Tab 2',
        },
        {
            id: 'tab-3',
            name: 'Tab 3',
        },
        {
            id: 'tab-4',
            name: 'Tab 4',
        },
        {
            id: 'tab-5',
            name: 'Tab 5',
        },
        {
            id: 'tab-6',
            name: 'Tab 6',
        },
        {
            id: 'tab-7',
            name: 'Tab 7',
        },
        {
            id: 'tab-8',
            name: 'Tab 8',
        },
        {
            id: 'tab-9',
            name: 'Tab 9',
        },
        {
            id: 'tab-10',
            name: 'Tab 10',
        },
    ]
}