Skip to content

Commit f76f454

Browse files
committed
Minor code improvements
Signed-off-by: jbicker <jan.bicker@typefox.io>
1 parent 9b255ac commit f76f454

File tree

6 files changed

+36
-120
lines changed

6 files changed

+36
-120
lines changed

arduino-ide-extension/src/browser/arduino-commands.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,4 @@ export namespace ArduinoCommands {
4242
export const TOGGLE_PRO_MODE: Command = {
4343
id: "arduino-toggle-pro-mode"
4444
}
45-
46-
export const CONNECT_TODO: Command = {
47-
id: 'connect-to-attached-board',
48-
label: 'Connect to Attached Board'
49-
}
50-
51-
export const SEND: Command = {
52-
id: 'send',
53-
label: 'Send a Message to the Connected Board'
54-
}
55-
5645
}

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
55
import { MessageService } from '@theia/core/lib/common/message-service';
66
import { CommandContribution, CommandRegistry, Command } from '@theia/core/lib/common/command';
77
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
8-
import { BoardsService, AttachedSerialBoard } from '../common/protocol/boards-service';
8+
import { BoardsService } from '../common/protocol/boards-service';
99
import { ArduinoCommands } from './arduino-commands';
1010
import { CoreService } from '../common/protocol/core-service';
1111
import { WorkspaceServiceExt } from './workspace-service-ext';
@@ -26,8 +26,6 @@ import {
2626
StatusBar,
2727
ShellLayoutRestorer,
2828
StatusBarAlignment,
29-
QuickOpenItem,
30-
QuickOpenMode,
3129
QuickOpenService,
3230
LabelProvider
3331
} from '@theia/core/lib/browser';
@@ -74,9 +72,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
7472
@inject(MonitorService)
7573
protected readonly monitorService: MonitorService;
7674

77-
// TODO: make this better!
78-
protected connectionId: string | undefined;
79-
8075
@inject(WorkspaceServiceExt)
8176
protected readonly workspaceServiceExt: WorkspaceServiceExt;
8277

@@ -341,65 +336,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
341336
},
342337
isToggled: () => ARDUINO_PRO_MODE
343338
});
344-
registry.registerCommand(ArduinoCommands.CONNECT_TODO, {
345-
execute: async () => {
346-
const { boardsConfig } = this.boardsServiceClient;
347-
const { selectedBoard, selectedPort } = boardsConfig;
348-
if (!selectedBoard) {
349-
this.messageService.warn('No boards selected.');
350-
return;
351-
}
352-
const { name } = selectedBoard;
353-
if (!selectedPort) {
354-
this.messageService.warn(`No ports selected for board: '${name}'.`);
355-
return;
356-
}
357-
const attachedBoards = await this.boardsService.getAttachedBoards();
358-
const connectedBoard = attachedBoards.boards.filter(AttachedSerialBoard.is).find(board => BoardsConfig.Config.sameAs(boardsConfig, board));
359-
if (!connectedBoard) {
360-
this.messageService.warn(`The selected '${name}' board is not connected on ${selectedPort}.`);
361-
return;
362-
}
363-
if (this.connectionId) {
364-
console.log('>>> Disposing existing monitor connection before establishing a new one...');
365-
const result = await this.monitorService.disconnect(this.connectionId);
366-
if (!result) {
367-
// TODO: better!!!
368-
console.error(`Could not close connection: ${this.connectionId}. Check the backend logs.`);
369-
} else {
370-
console.log(`<<< Disposed ${this.connectionId} connection.`)
371-
}
372-
}
373-
const { connectionId } = await this.monitorService.connect({ board: selectedBoard, port: selectedPort });
374-
this.connectionId = connectionId;
375-
}
376-
});
377-
registry.registerCommand(ArduinoCommands.SEND, {
378-
isEnabled: () => !!this.connectionId,
379-
execute: async () => {
380-
const { monitorService, connectionId } = this;
381-
const model = {
382-
onType(lookFor: string, acceptor: (items: QuickOpenItem[]) => void): void {
383-
acceptor([
384-
new QuickOpenItem({
385-
label: "Type your message and press 'Enter' to send it to the board. Escape to cancel.",
386-
run: (mode: QuickOpenMode): boolean => {
387-
if (mode !== QuickOpenMode.OPEN) {
388-
return false;
389-
}
390-
monitorService.send(connectionId!, lookFor + '\n');
391-
return true;
392-
}
393-
})
394-
]);
395-
}
396-
};
397-
const options = {
398-
placeholder: "Your message. The message will be suffixed with a LF ['\\n'].",
399-
};
400-
this.quickOpenService.open(model, options);
401-
}
402-
})
403339
}
404340

405341
registerMenus(registry: MenuModelRegistry) {

arduino-ide-extension/src/browser/monitor/monitor-connection.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,45 @@ export class MonitorConnection {
88
@inject(MonitorService)
99
protected readonly monitorService: MonitorService;
1010

11-
protected _connectionId: string | undefined;
11+
connectionId: string | undefined;
1212

13-
protected _connectionConfig: ConnectionConfig;
13+
protected _connectionConfig: ConnectionConfig | undefined;
1414

1515
protected readonly onConnectionChangedEmitter = new Emitter<string | undefined>();
1616
readonly onConnectionChanged: Event<string | undefined> = this.onConnectionChangedEmitter.event;
1717

18-
get connectionId(): string | undefined {
19-
return this._connectionId;
20-
}
21-
22-
set connectionId(cid: string | undefined) {
23-
this._connectionId = cid;
24-
}
25-
26-
get connectionConfig(): ConnectionConfig {
18+
get connectionConfig(): ConnectionConfig | undefined {
2719
return this._connectionConfig;
2820
}
2921

3022
async connect(config: ConnectionConfig): Promise<string | undefined> {
31-
if (this._connectionId) {
23+
if (this.connectionId) {
3224
await this.disconnect();
3325
}
3426
const { connectionId } = await this.monitorService.connect(config);
35-
this._connectionId = connectionId;
27+
this.connectionId = connectionId;
3628
this._connectionConfig = config;
3729

38-
this.onConnectionChangedEmitter.fire(this._connectionId);
30+
this.onConnectionChangedEmitter.fire(this.connectionId);
3931

4032
return connectionId;
4133
}
4234

4335
async disconnect(): Promise<boolean> {
4436
let result = true;
4537
const connections = await this.monitorService.getConnectionIds();
46-
if (this._connectionId && connections.findIndex(id => id === this._connectionId) >= 0) {
38+
if (this.connectionId && connections.findIndex(id => id === this.connectionId) >= 0) {
4739
console.log('>>> Disposing existing monitor connection before establishing a new one...');
48-
result = await this.monitorService.disconnect(this._connectionId);
40+
result = await this.monitorService.disconnect(this.connectionId);
4941
if (!result) {
5042
// TODO: better!!!
51-
console.error(`Could not close connection: ${this._connectionId}. Check the backend logs.`);
43+
console.error(`Could not close connection: ${this.connectionId}. Check the backend logs.`);
5244
} else {
53-
console.log(`<<< Disposed ${this._connectionId} connection.`);
54-
this._connectionId = undefined;
45+
console.log(`<<< Disposed ${this.connectionId} connection.`);
46+
this.connectionId = undefined;
47+
this._connectionConfig = undefined;
48+
this.onConnectionChangedEmitter.fire(this.connectionId);
5549
}
56-
this.onConnectionChangedEmitter.fire(this._connectionId);
5750
}
5851
return result;
5952
}

arduino-ide-extension/src/browser/monitor/monitor-widget.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,9 @@ export class SerialMonitorOutput extends React.Component<SerialMonitorOutput.Pro
7171
fontFamily: 'monospace',
7272
};
7373

74-
for (let text of this.props.lines) {
74+
for (const text of this.props.lines) {
7575
result += text;
7676
}
77-
if (result.length === 0) {
78-
result = '';
79-
}
8077
return <React.Fragment>
8178
<div style={style}>{result}</div>
8279
<div style={{ float: "left", clear: "both" }}
@@ -116,7 +113,7 @@ export class MonitorWidget extends ReactWidget {
116113

117114
protected lines: string[];
118115
protected tempData: string;
119-
protected _baudRate: number;
116+
protected baudRate: number;
120117
protected _lineEnding: string;
121118

122119
constructor(
@@ -168,16 +165,12 @@ export class MonitorWidget extends ReactWidget {
168165
this.update();
169166
}
170167

171-
get baudRate(): number | undefined {
172-
return this._baudRate;
173-
}
174-
175168
protected onAfterAttach(msg: Message) {
176169
super.onAfterAttach(msg);
177170
this.clear();
178171
this.connect();
179172
this.toDisposeOnDetach.push(this.boardsServiceClient.onBoardsChanged(async states => {
180-
const currentConnectionConfig = this.connection.connectionConfig;
173+
const currentConnectionConfig = this.connection.connectionConfig;
181174
const connectedBoard = states.newState.boards
182175
.filter(AttachedSerialBoard.is)
183176
.find(board => {
@@ -296,7 +289,7 @@ export class MonitorWidget extends ReactWidget {
296289
}
297290

298291
protected readonly onChangeBaudRate = (br: SelectOption) => {
299-
this._baudRate = typeof br.value === 'number' ? br.value : 9600;
292+
this.baudRate = typeof br.value === 'number' ? br.value : 9600;
300293
}
301294

302295
protected renderSelectField(id: string, options: OptionsType<SelectOption>, defaultVal: SelectOption, onChange: (v: SelectOption) => void): React.ReactNode {

arduino-ide-extension/src/browser/toolbar/arduino-toolbar-contribution.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import { CommandRegistry } from "@theia/core";
66
import { LabelParser } from "@theia/core/lib/browser/label-parser";
77

88
export class ArduinoToolbarContainer extends Widget {
9-
constructor(protected left: ArduinoToolbar, protected right: ArduinoToolbar) {
9+
10+
protected toolbars: ArduinoToolbar[];
11+
12+
constructor(...toolbars: ArduinoToolbar[]) {
1013
super();
1114
this.id = 'arduino-toolbar-container';
15+
this.toolbars = toolbars;
1216
}
1317

1418
onAfterAttach(msg: Message) {
15-
Widget.attach(this.left, this.node);
16-
Widget.attach(this.right, this.node);
19+
for (const toolbar of this.toolbars) {
20+
Widget.attach(toolbar, this.node);
21+
}
1722
}
1823
}
1924

arduino-ide-extension/src/browser/toolbar/arduino-toolbar.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ export class ArduinoToolbarComponent extends React.Component<ArduinoToolbarCompo
4545
}
4646

4747
render(): React.ReactNode {
48-
if (this.props.side === 'left') {
49-
return <React.Fragment>
50-
<div key='arduino-toolbar-tooltip' className={'arduino-toolbar-tooltip'}>{this.state.tooltip}</div>
48+
const tooltip = <div key='arduino-toolbar-tooltip' className={'arduino-toolbar-tooltip'}>{this.state.tooltip}</div>;
49+
const items = [
50+
<React.Fragment>
5151
{[...this.props.items].map(item => TabBarToolbarItem.is(item) ? this.renderItem(item) : item.render())}
52-
</React.Fragment>;
52+
</React.Fragment>
53+
]
54+
if (this.props.side === 'left') {
55+
items.unshift(tooltip);
5356
} else {
54-
return <React.Fragment>
55-
{[...this.props.items].map(item => TabBarToolbarItem.is(item) ? this.renderItem(item) : item.render())}
56-
<div key='arduino-toolbar-tooltip' className={'arduino-toolbar-tooltip'}>{this.state.tooltip}</div>
57-
</React.Fragment>;
57+
items.push(tooltip)
5858
}
59+
return items;
5960
}
6061
}
6162

@@ -91,8 +92,7 @@ export class ArduinoToolbar extends ReactWidget {
9192
}
9293

9394
protected init(): void {
94-
this.node.classList.add('theia-arduino-toolbar');
95-
this.node.classList.add(this.side);
95+
this.node.classList.add('theia-arduino-toolbar', this.side);
9696
this.update();
9797
}
9898

0 commit comments

Comments
 (0)