-
Notifications
You must be signed in to change notification settings - Fork 2
/
TitleBar.tsx
173 lines (158 loc) · 5.85 KB
/
TitleBar.tsx
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as React from "react";
import { ReactWrapperWidget } from "@mavenomics/ui";
import { Title, Widget } from "@phosphor/widgets";
import { DashboardLayoutRegion } from "./DashboardLayoutRegion";
export function TitleBar({caption, color, background}: TitleBar.IProps) {
return (
<span className={"m-TitleBar"} style={{ color, background }}>{caption}</span>
);
}
export namespace TitleBar {
export interface IProps {
caption: string;
color: string;
background: string;
}
interface IEditableProps extends IProps {
captionChanged: (this: void, caption: string) => void;
onBlur: (this: void) => void;
}
export const EditableTitleBar: React.SFC<IEditableProps> = ({
caption,
color,
background,
captionChanged,
onBlur
}) => {
const maybeBlur: React.KeyboardEventHandler = (ev) => {
if (ev.key === "Enter" || ev.key === "Escape") {
onBlur.call(void 0);
}
};
return (
<span className={"m-TitleBar m-TitleBar-editable"} style={{color, background}}>
<input type="text"
className={"m-TitleBar-editableInput"}
value={caption}
onChange={(ev) => captionChanged.call(void 0, ev.target.value)}
onFocus={({target}) => target.select()} // select the full content of the text area
onBlur={() => onBlur.call(void 0)}
onKeyDown={maybeBlur}
autoFocus={true}
/>
</span>
);
};
export class WidgetWrapper extends ReactWrapperWidget {
private readonly widgetTitle: Title<Widget>;
private readonly owner: DashboardLayoutRegion;
private dragStartPosition: [number, number] | null = null;
private editable = false;
constructor(owner: DashboardLayoutRegion<any>) {
super();
this.addClass("m-TitleBar-Wrapper");
this.owner = owner;
this.widgetTitle = this.owner.title;
// update when the title changes
this.widgetTitle.changed.connect(this.update, this);
}
public dispose() {
if (this.isDisposed) {
return;
}
this.widgetTitle.changed.disconnect(this.update, this);
super.dispose();
}
public edit() {
this.editable = true;
this.update();
}
public handleEvent(ev: PointerEvent) {
switch (ev.type) {
case "pointerdown":
this.onStartDrag(ev);
break;
case "pointermove":
this.onMove(ev);
break;
case "pointerup":
this.endDrag();
break;
default:
throw Error("Unhandled event");
}
}
protected render() {
if (this.isDisposed) {
throw Error("TitleBarWidget disposed");
}
let color = this.widgetTitle.dataset["color"];
if (color === "transparent") {
color = "";
}
let background = this.widgetTitle.dataset["background"];
if (background === "transparent") {
background = "";
}
if (this.editable) {
return (
<EditableTitleBar
caption={this.widgetTitle.label}
color={color}
background={background}
captionChanged={(caption) => {
this.owner.setLayoutProperty("caption", caption);
}}
onBlur={() => {
this.editable = false;
this.update();
}}/>
);
}
// NB: Title.prototype.caption is not actually the caption we want
// (Language is slippery)
return (
<TitleBar
caption={this.widgetTitle.label}
color={color}
background={background}/>
);
}
protected onAfterAttach() {
this.node.addEventListener("pointerdown", this);
}
protected onBeforeDetach() {
this.node.removeEventListener("pointerdown", this);
}
private onStartDrag(ev: PointerEvent) {
if (ev.pointerType === "mouse" && ev.button !== 0) {
// this is not a left click, don't drag
return;
}
if (this.editable) return; // don't drag when editing
this.dragStartPosition = [ev.clientX, ev.clientY];
document.addEventListener("pointermove", this, {capture: true});
document.addEventListener("pointerup", this, {capture: true});
}
private onMove(ev: PointerEvent) {
if (!this.node.contains(ev.target as Element)) {
// mouseup occured off the titlebar, don't start the drag for
// reliability reasons
this.endDrag();
return;
}
const norm = Math.abs(ev.clientX - this.dragStartPosition![0])
+ Math.abs(ev.clientY - this.dragStartPosition![1]);
if (norm > 2) {
this.endDrag();
// trigger a drag
this.owner.startDrag(ev.clientX, ev.clientY);
}
}
private endDrag() {
document.removeEventListener("pointerup", this, {capture: true});
document.removeEventListener("pointermove", this, {capture: true});
this.dragStartPosition = null;
}
}
}