-
Notifications
You must be signed in to change notification settings - Fork 14
/
common.ts
129 lines (124 loc) · 3.6 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { SuiScoreViewOperations } from "../render/sui/scoreViewOperations";
import { SuiTracker } from "../render/sui/tracker";
import { CompleteNotifier } from "../ui/common";
import { ModalComponent } from "../ui/common";
import { BrowserEventSource, EventHandler } from "../ui/eventSource";
/**
* A binding of a key to some action performed by a module
*/
export interface KeyBinding {
event: string,
key: string,
ctrlKey: boolean,
altKey: boolean,
shiftKey: boolean,
action: string,
module?: string
}
/**
* parameters for the key handlers
* @param view
* @param slashMode indicates that the dreaded 'slash' menus are up
* @param completeNotifier notified when modals close
* @param tracker
* @param eventSource register for event handlers
* @category SuiApplication
*/
export interface KeyCommandParams {
view: SuiScoreViewOperations;
slashMode: boolean;
completeNotifier: CompleteNotifier;
tracker: SuiTracker;
eventSource: BrowserEventSource;
}
/**
* Shared interface for menus, dialogs, etc that can
* accept UI events
* @category SuiUiBase
*/
export abstract class ModalEventHandler {
abstract mouseMove(ev: any): void;
abstract mouseClick(ev: any): void;
abstract evKey(evdata: any): void;
}
export type handler = (ev: any) => void;
export class SimpleEventHandler extends ModalEventHandler {
mouseMoveHandler: handler;;
mouseClickHandler: handler;
evKeyHandler: handler;
constructor(mouseMove: handler, mouseClick: handler, keyHandler: handler) {
super();
this.mouseMoveHandler = mouseMove;
this.mouseClickHandler = mouseClick;
this.evKeyHandler = keyHandler;
}
mouseMove(ev: any) {
this.mouseMove(ev);
}
mouseClick(ev: any) {
this.mouseClick(ev);
}
evKey(ev: any) {
this.evKeyHandler(ev);
}
}
/**
* Dependency injection, sends events to a proxy object, gets around some
* cyclic dependencies when bootstrapping the application.
* @category AppUtil
*/
export class ModalEventHandlerProxy {
_handler: ModalEventHandler | null = null;
eventSource: BrowserEventSource;
unbound: boolean = true;
keydownHandler: EventHandler | null = null;
mouseMoveHandler: EventHandler | null = null;
mouseClickHandler: EventHandler | null = null;
constructor(evSource: BrowserEventSource) {
this.eventSource = evSource;
this.bindEvents();
}
set handler(value: ModalEventHandler) {
this._handler = value;
this.unbound = false;
}
evKey(ev: any) {
if (this._handler) {
this._handler.evKey(ev);
}
}
mouseMove(ev: any) {
if (this._handler) {
this._handler.mouseMove(ev);
}
}
mouseClick(ev: any) {
if (this._handler) {
this._handler.mouseClick(ev);
}
}
bindEvents() {
this.mouseMoveHandler = this.eventSource.bindMouseMoveHandler(this, 'mouseMove');
this.mouseClickHandler = this.eventSource.bindMouseClickHandler(this, 'mouseClick');
this.keydownHandler = this.eventSource.bindKeydownHandler(this, 'evKey');
}
unbindKeyboardForModal(dialog: ModalComponent) {
if (this.unbound) {
console.log('received duplicate bind event');
return;
}
if (!this.keydownHandler || !this.mouseMoveHandler || !this.mouseClickHandler) {
console.log('received bind with no handlers');
return;
}
this.unbound = true;
const rebind = () => {
this.unbound = false;
this.bindEvents();
}
this.eventSource.unbindKeydownHandler(this.keydownHandler!);
this.eventSource.unbindMouseMoveHandler(this.mouseMoveHandler!);
this.eventSource.unbindMouseClickHandler(this.mouseClickHandler!);
dialog.closeModalPromise.then(rebind);
}
}