-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatusbar.ts
196 lines (171 loc) · 5.38 KB
/
statusbar.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ArrayExt } from '@lumino/algorithm';
import {
DisposableDelegate,
DisposableSet,
IDisposable
} from '@lumino/disposable';
import { Message } from '@lumino/messaging';
import { Panel, PanelLayout, Widget } from '@lumino/widgets';
import {
statusBar as barStyle,
item as itemStyle,
leftSide as leftSideStyle,
rightSide as rightSideStyle,
side as sideStyle
} from './style/statusbar';
import { IStatusBar } from './tokens';
/**
* Main status bar object which contains all items.
*/
export class StatusBar extends Widget implements IStatusBar {
constructor() {
super();
this.addClass(barStyle);
const rootLayout = (this.layout = new PanelLayout());
const leftPanel = (this._leftSide = new Panel());
const middlePanel = (this._middlePanel = new Panel());
const rightPanel = (this._rightSide = new Panel());
leftPanel.addClass(sideStyle);
leftPanel.addClass(leftSideStyle);
middlePanel.addClass(sideStyle);
rightPanel.addClass(sideStyle);
rightPanel.addClass(rightSideStyle);
rootLayout.addWidget(leftPanel);
rootLayout.addWidget(middlePanel);
rootLayout.addWidget(rightPanel);
}
/**
* Register a new status item.
*
* @param id - a unique id for the status item.
*
* @param statusItem - The item to add to the status bar.
*/
registerStatusItem(id: string, statusItem: IStatusBar.IItem): IDisposable {
if (id in this._statusItems) {
throw new Error(`Status item ${id} already registered.`);
}
// Populate defaults for the optional properties of the status item.
const fullStatusItem = {
...Private.statusItemDefaults,
...statusItem
} as Private.IFullItem;
const { align, item, rank } = fullStatusItem;
// Connect the activeStateChanged signal to refreshing the status item,
// if the signal was provided.
const onActiveStateChanged = () => {
this._refreshItem(id);
};
if (fullStatusItem.activeStateChanged) {
fullStatusItem.activeStateChanged.connect(onActiveStateChanged);
}
const rankItem = { id, rank };
fullStatusItem.item.addClass(itemStyle);
this._statusItems[id] = fullStatusItem;
if (align === 'left') {
const insertIndex = this._findInsertIndex(this._leftRankItems, rankItem);
if (insertIndex === -1) {
this._leftSide.addWidget(item);
this._leftRankItems.push(rankItem);
} else {
ArrayExt.insert(this._leftRankItems, insertIndex, rankItem);
this._leftSide.insertWidget(insertIndex, item);
}
} else if (align === 'right') {
const insertIndex = this._findInsertIndex(this._rightRankItems, rankItem);
if (insertIndex === -1) {
this._rightSide.addWidget(item);
this._rightRankItems.push(rankItem);
} else {
ArrayExt.insert(this._rightRankItems, insertIndex, rankItem);
this._rightSide.insertWidget(insertIndex, item);
}
} else {
this._middlePanel.addWidget(item);
}
this._refreshItem(id); // Initially refresh the status item.
const disposable = new DisposableDelegate(() => {
delete this._statusItems[id];
if (fullStatusItem.activeStateChanged) {
fullStatusItem.activeStateChanged.disconnect(onActiveStateChanged);
}
item.parent = null;
item.dispose();
});
this._disposables.add(disposable);
return disposable;
}
/**
* Dispose of the status bar.
*/
dispose(): void {
this._leftRankItems.length = 0;
this._rightRankItems.length = 0;
this._disposables.dispose();
super.dispose();
}
/**
* Handle an 'update-request' message to the status bar.
*/
protected onUpdateRequest(msg: Message): void {
this._refreshAll();
super.onUpdateRequest(msg);
}
private _findInsertIndex(
side: Private.IRankItem[],
newItem: Private.IRankItem
): number {
return ArrayExt.findFirstIndex(side, item => item.rank > newItem.rank);
}
private _refreshItem(id: string) {
const statusItem = this._statusItems[id];
if (statusItem.isActive()) {
statusItem.item.show();
statusItem.item.update();
} else {
statusItem.item.hide();
}
}
private _refreshAll(): void {
Object.keys(this._statusItems).forEach(id => {
this._refreshItem(id);
});
}
private _leftRankItems: Private.IRankItem[] = [];
private _rightRankItems: Private.IRankItem[] = [];
private _statusItems: { [id: string]: Private.IFullItem } = {};
private _disposables = new DisposableSet();
private _leftSide: Panel;
private _middlePanel: Panel;
private _rightSide: Panel;
}
/**
* A namespace for private functionality.
*/
namespace Private {
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
/**
* Default options for a status item, less the item itself.
*/
export const statusItemDefaults: Omit<IStatusBar.IItem, 'item'> = {
align: 'left',
rank: 0,
isActive: () => true,
activeStateChanged: undefined
};
/**
* An interface for storing the rank of a status item.
*/
export interface IRankItem {
id: string;
rank: number;
}
export type DefaultKeys = 'align' | 'rank' | 'isActive';
/**
* Type of statusbar item with defaults filled in.
*/
export type IFullItem = Required<Pick<IStatusBar.IItem, DefaultKeys>> &
Omit<IStatusBar.IItem, DefaultKeys>;
}