-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
status-bar.tsx
211 lines (181 loc) · 7.22 KB
/
status-bar.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
174
175
176
177
178
179
180
181
182
183
184
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
/********************************************************************************
* Copyright (C) 2017-2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as React from 'react';
import { injectable, inject } from 'inversify';
import Octicon, { getIconByName } from '@primer/octicons-react';
import { CommandService } from '../../common';
import { ReactWidget } from '../widgets/react-widget';
import { FrontendApplicationStateService } from '../frontend-application-state';
import { LabelParser, LabelIcon } from '../label-parser';
export interface StatusBarEntry {
/**
* For icons we use octicons and fontawesome icons. octicons take precedence over fontawesome. Get more information and the class names
* here: http://fontawesome.io/icons/
* To set a text with icon use the following pattern in text string:
* $(fontawesomeClassName)
* To use animated icons use the following pattern:
* $(fontawesomeClassName~typeOfAnimation)
* Type of animation can be either spin or pulse.
* Look here for more information to animated icons:
* http://fontawesome.io/examples/#animated
*/
text: string;
alignment: StatusBarAlignment;
color?: string;
className?: string;
tooltip?: string;
command?: string;
// tslint:disable-next-line:no-any
arguments?: any[];
priority?: number;
onclick?: (e: MouseEvent) => void;
}
export enum StatusBarAlignment {
LEFT, RIGHT
}
export interface StatusBarEntryAttributes {
className?: string;
title?: string;
style?: object;
onClick?: (e: MouseEvent) => void;
}
export const STATUSBAR_WIDGET_FACTORY_ID = 'statusBar';
export const StatusBar = Symbol('StatusBar');
export interface StatusBar {
setBackgroundColor(color?: string): Promise<void>;
setColor(color?: string): Promise<void>;
setElement(id: string, entry: StatusBarEntry): Promise<void>;
removeElement(id: string): Promise<void>;
}
@injectable()
export class StatusBarImpl extends ReactWidget implements StatusBar {
protected backgroundColor: string | undefined;
protected color: string | undefined;
protected entries: Map<string, StatusBarEntry> = new Map();
constructor(
@inject(CommandService) protected readonly commands: CommandService,
@inject(LabelParser) protected readonly entryService: LabelParser,
@inject(FrontendApplicationStateService) protected readonly applicationStateService: FrontendApplicationStateService
) {
super();
delete this.scrollOptions;
this.id = 'theia-statusBar';
this.addClass('noselect');
}
protected get ready(): Promise<void> {
return this.applicationStateService.reachedAnyState('initialized_layout', 'ready');
}
async setElement(id: string, entry: StatusBarEntry): Promise<void> {
await this.ready;
this.entries.set(id, entry);
this.update();
}
async removeElement(id: string): Promise<void> {
await this.ready;
this.entries.delete(id);
this.update();
}
async setBackgroundColor(color?: string): Promise<void> {
await this.ready;
this.internalSetBackgroundColor(color);
this.update();
}
protected internalSetBackgroundColor(color?: string): void {
this.backgroundColor = color;
// tslint:disable-next-line:no-null-keyword
this.node.style.backgroundColor = this.backgroundColor ? this.backgroundColor : null;
}
async setColor(color?: string): Promise<void> {
await this.ready;
this.internalSetColor(color);
this.update();
}
protected internalSetColor(color?: string): void {
this.color = color;
}
protected render(): JSX.Element {
const leftEntries: JSX.Element[] = [];
const rightEntries: JSX.Element[] = [];
const elements = Array.from(this.entries).sort((left, right) => {
const lp = left[1].priority || 0;
const rp = right[1].priority || 0;
return rp - lp;
});
elements.forEach(([id, entry]) => {
if (entry.alignment === StatusBarAlignment.LEFT) {
leftEntries.push(this.renderElement(id, entry));
} else {
rightEntries.push(this.renderElement(id, entry));
}
});
return <React.Fragment>
<div className='area left'>{leftEntries}</div>
<div className='area right'>{rightEntries}</div>
</React.Fragment>;
}
protected onclick(entry: StatusBarEntry): () => void {
return () => {
if (entry.command) {
const args = entry.arguments || [];
this.commands.executeCommand(entry.command, ...args);
}
};
}
protected createAttributes(entry: StatusBarEntry): StatusBarEntryAttributes {
const attrs: StatusBarEntryAttributes = {};
if (entry.command) {
attrs.onClick = this.onclick(entry);
attrs.className = 'element hasCommand';
} else if (entry.onclick) {
attrs.onClick = e => {
if (entry.onclick) {
entry.onclick(e);
}
};
attrs.className = 'element hasCommand';
} else {
attrs.className = 'element';
}
if (entry.tooltip) {
attrs.title = entry.tooltip;
}
attrs.style = {
color: entry.color || this.color
};
if (entry.className) {
attrs.className += ' ' + entry.className;
}
return attrs;
}
protected renderElement(id: string, entry: StatusBarEntry): JSX.Element {
const childStrings = this.entryService.parse(entry.text);
const children: JSX.Element[] = [];
childStrings.forEach((val, key) => {
if (!(typeof val === 'string') && LabelIcon.is(val)) {
const octicon = getIconByName(val.name);
if (octicon) {
children.push(<span key={key} className={val.animation ? 'fa-' + val.animation : 'fa'}><Octicon icon={octicon} height={12.5} width={12.5} /></span>);
} else {
children.push(<span key={key} className={`fa fa-${val.name} ${val.animation ? 'fa-' + val.animation : ''}`}></span>);
}
} else {
children.push(<span key={key}>{val}</span>);
}
});
const elementInnerDiv = <React.Fragment>{children}</React.Fragment>;
return React.createElement('div', { key: id, ...this.createAttributes(entry) }, elementInnerDiv);
}
}