Minor code improvements

Signed-off-by: jbicker <jan.bicker@typefox.io>
This commit is contained in:
jbicker 2019-08-16 10:21:59 +02:00
parent 9b255ac072
commit f76f4543e9
6 changed files with 36 additions and 120 deletions

View File

@ -42,15 +42,4 @@ export namespace ArduinoCommands {
export const TOGGLE_PRO_MODE: Command = {
id: "arduino-toggle-pro-mode"
}
export const CONNECT_TODO: Command = {
id: 'connect-to-attached-board',
label: 'Connect to Attached Board'
}
export const SEND: Command = {
id: 'send',
label: 'Send a Message to the Connected Board'
}
}

View File

@ -5,7 +5,7 @@ import { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
import { MessageService } from '@theia/core/lib/common/message-service';
import { CommandContribution, CommandRegistry, Command } from '@theia/core/lib/common/command';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { BoardsService, AttachedSerialBoard } from '../common/protocol/boards-service';
import { BoardsService } from '../common/protocol/boards-service';
import { ArduinoCommands } from './arduino-commands';
import { CoreService } from '../common/protocol/core-service';
import { WorkspaceServiceExt } from './workspace-service-ext';
@ -26,8 +26,6 @@ import {
StatusBar,
ShellLayoutRestorer,
StatusBarAlignment,
QuickOpenItem,
QuickOpenMode,
QuickOpenService,
LabelProvider
} from '@theia/core/lib/browser';
@ -74,9 +72,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
@inject(MonitorService)
protected readonly monitorService: MonitorService;
// TODO: make this better!
protected connectionId: string | undefined;
@inject(WorkspaceServiceExt)
protected readonly workspaceServiceExt: WorkspaceServiceExt;
@ -341,65 +336,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
},
isToggled: () => ARDUINO_PRO_MODE
});
registry.registerCommand(ArduinoCommands.CONNECT_TODO, {
execute: async () => {
const { boardsConfig } = this.boardsServiceClient;
const { selectedBoard, selectedPort } = boardsConfig;
if (!selectedBoard) {
this.messageService.warn('No boards selected.');
return;
}
const { name } = selectedBoard;
if (!selectedPort) {
this.messageService.warn(`No ports selected for board: '${name}'.`);
return;
}
const attachedBoards = await this.boardsService.getAttachedBoards();
const connectedBoard = attachedBoards.boards.filter(AttachedSerialBoard.is).find(board => BoardsConfig.Config.sameAs(boardsConfig, board));
if (!connectedBoard) {
this.messageService.warn(`The selected '${name}' board is not connected on ${selectedPort}.`);
return;
}
if (this.connectionId) {
console.log('>>> Disposing existing monitor connection before establishing a new one...');
const result = await this.monitorService.disconnect(this.connectionId);
if (!result) {
// TODO: better!!!
console.error(`Could not close connection: ${this.connectionId}. Check the backend logs.`);
} else {
console.log(`<<< Disposed ${this.connectionId} connection.`)
}
}
const { connectionId } = await this.monitorService.connect({ board: selectedBoard, port: selectedPort });
this.connectionId = connectionId;
}
});
registry.registerCommand(ArduinoCommands.SEND, {
isEnabled: () => !!this.connectionId,
execute: async () => {
const { monitorService, connectionId } = this;
const model = {
onType(lookFor: string, acceptor: (items: QuickOpenItem[]) => void): void {
acceptor([
new QuickOpenItem({
label: "Type your message and press 'Enter' to send it to the board. Escape to cancel.",
run: (mode: QuickOpenMode): boolean => {
if (mode !== QuickOpenMode.OPEN) {
return false;
}
monitorService.send(connectionId!, lookFor + '\n');
return true;
}
})
]);
}
};
const options = {
placeholder: "Your message. The message will be suffixed with a LF ['\\n'].",
};
this.quickOpenService.open(model, options);
}
})
}
registerMenus(registry: MenuModelRegistry) {

View File

@ -8,34 +8,26 @@ export class MonitorConnection {
@inject(MonitorService)
protected readonly monitorService: MonitorService;
protected _connectionId: string | undefined;
connectionId: string | undefined;
protected _connectionConfig: ConnectionConfig;
protected _connectionConfig: ConnectionConfig | undefined;
protected readonly onConnectionChangedEmitter = new Emitter<string | undefined>();
readonly onConnectionChanged: Event<string | undefined> = this.onConnectionChangedEmitter.event;
get connectionId(): string | undefined {
return this._connectionId;
}
set connectionId(cid: string | undefined) {
this._connectionId = cid;
}
get connectionConfig(): ConnectionConfig {
get connectionConfig(): ConnectionConfig | undefined {
return this._connectionConfig;
}
async connect(config: ConnectionConfig): Promise<string | undefined> {
if (this._connectionId) {
if (this.connectionId) {
await this.disconnect();
}
const { connectionId } = await this.monitorService.connect(config);
this._connectionId = connectionId;
this.connectionId = connectionId;
this._connectionConfig = config;
this.onConnectionChangedEmitter.fire(this._connectionId);
this.onConnectionChangedEmitter.fire(this.connectionId);
return connectionId;
}
@ -43,17 +35,18 @@ export class MonitorConnection {
async disconnect(): Promise<boolean> {
let result = true;
const connections = await this.monitorService.getConnectionIds();
if (this._connectionId && connections.findIndex(id => id === this._connectionId) >= 0) {
if (this.connectionId && connections.findIndex(id => id === this.connectionId) >= 0) {
console.log('>>> Disposing existing monitor connection before establishing a new one...');
result = await this.monitorService.disconnect(this._connectionId);
result = await this.monitorService.disconnect(this.connectionId);
if (!result) {
// TODO: better!!!
console.error(`Could not close connection: ${this._connectionId}. Check the backend logs.`);
console.error(`Could not close connection: ${this.connectionId}. Check the backend logs.`);
} else {
console.log(`<<< Disposed ${this._connectionId} connection.`);
this._connectionId = undefined;
console.log(`<<< Disposed ${this.connectionId} connection.`);
this.connectionId = undefined;
this._connectionConfig = undefined;
this.onConnectionChangedEmitter.fire(this.connectionId);
}
this.onConnectionChangedEmitter.fire(this._connectionId);
}
return result;
}

View File

@ -71,12 +71,9 @@ export class SerialMonitorOutput extends React.Component<SerialMonitorOutput.Pro
fontFamily: 'monospace',
};
for (let text of this.props.lines) {
for (const text of this.props.lines) {
result += text;
}
if (result.length === 0) {
result = '';
}
return <React.Fragment>
<div style={style}>{result}</div>
<div style={{ float: "left", clear: "both" }}
@ -116,7 +113,7 @@ export class MonitorWidget extends ReactWidget {
protected lines: string[];
protected tempData: string;
protected _baudRate: number;
protected baudRate: number;
protected _lineEnding: string;
constructor(
@ -168,10 +165,6 @@ export class MonitorWidget extends ReactWidget {
this.update();
}
get baudRate(): number | undefined {
return this._baudRate;
}
protected onAfterAttach(msg: Message) {
super.onAfterAttach(msg);
this.clear();
@ -296,7 +289,7 @@ export class MonitorWidget extends ReactWidget {
}
protected readonly onChangeBaudRate = (br: SelectOption) => {
this._baudRate = typeof br.value === 'number' ? br.value : 9600;
this.baudRate = typeof br.value === 'number' ? br.value : 9600;
}
protected renderSelectField(id: string, options: OptionsType<SelectOption>, defaultVal: SelectOption, onChange: (v: SelectOption) => void): React.ReactNode {

View File

@ -6,14 +6,19 @@ import { CommandRegistry } from "@theia/core";
import { LabelParser } from "@theia/core/lib/browser/label-parser";
export class ArduinoToolbarContainer extends Widget {
constructor(protected left: ArduinoToolbar, protected right: ArduinoToolbar) {
protected toolbars: ArduinoToolbar[];
constructor(...toolbars: ArduinoToolbar[]) {
super();
this.id = 'arduino-toolbar-container';
this.toolbars = toolbars;
}
onAfterAttach(msg: Message) {
Widget.attach(this.left, this.node);
Widget.attach(this.right, this.node);
for (const toolbar of this.toolbars) {
Widget.attach(toolbar, this.node);
}
}
}

View File

@ -45,17 +45,18 @@ export class ArduinoToolbarComponent extends React.Component<ArduinoToolbarCompo
}
render(): React.ReactNode {
const tooltip = <div key='arduino-toolbar-tooltip' className={'arduino-toolbar-tooltip'}>{this.state.tooltip}</div>;
const items = [
<React.Fragment>
{[...this.props.items].map(item => TabBarToolbarItem.is(item) ? this.renderItem(item) : item.render())}
</React.Fragment>
]
if (this.props.side === 'left') {
return <React.Fragment>
<div key='arduino-toolbar-tooltip' className={'arduino-toolbar-tooltip'}>{this.state.tooltip}</div>
{[...this.props.items].map(item => TabBarToolbarItem.is(item) ? this.renderItem(item) : item.render())}
</React.Fragment>;
items.unshift(tooltip);
} else {
return <React.Fragment>
{[...this.props.items].map(item => TabBarToolbarItem.is(item) ? this.renderItem(item) : item.render())}
<div key='arduino-toolbar-tooltip' className={'arduino-toolbar-tooltip'}>{this.state.tooltip}</div>
</React.Fragment>;
items.push(tooltip)
}
return items;
}
}
@ -91,8 +92,7 @@ export class ArduinoToolbar extends ReactWidget {
}
protected init(): void {
this.node.classList.add('theia-arduino-toolbar');
this.node.classList.add(this.side);
this.node.classList.add('theia-arduino-toolbar', this.side);
this.update();
}