Skip to content

Commit

Permalink
Refine example explorer view. (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaohaizh committed Aug 4, 2017
1 parent 19971ea commit 01b98de
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,3 +4,5 @@ node_modules
.vscode-test
.idea
src/views/app/sprites-generated
**/package-lock.json
test/**/c_cpp_properties.json
1 change: 1 addition & 0 deletions images/examples/FolderOpen_16x.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/examples/FolderOpen_16x_inverse.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/examples/Folder_16x.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/examples/Folder_16x_inverse.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/examples/ino_16x.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"aiKey": "83dd2c27-6594-41d3-85a9-bdb22070eb42",
"preview": true,
"engines": {
"vscode": "^1.7.0"
"vscode": "^1.13.0"
},
"icon": "images/arduino.png",
"license": "SEE LICENSE IN LICENSE.txt",
Expand Down Expand Up @@ -116,6 +116,15 @@
"title": "Arduino: Examples"
}
],
"views": {
"explorer": [
{
"id": "arduinoExampleExplorer",
"name": "Arduino Examples",
"when": "vscode-arduino:showExampleExplorer"
}
]
},
"debuggers": [
{
"type": "arduino",
Expand Down
8 changes: 8 additions & 0 deletions src/arduino/boardManager.ts
Expand Up @@ -30,6 +30,8 @@ export class BoardManager {

private _currentBoard: IBoard;

private _onBoardTypeChanged = new vscode.EventEmitter<void>();

constructor(private _settings: IArduinoSettings, private _arduinoApp: ArduinoApp) {
this._boardConfigStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.BOARD);
this._boardConfigStatusBar.command = "arduino.showBoardConfig";
Expand Down Expand Up @@ -111,13 +113,19 @@ export class BoardManager {
return true;
}

public get onBoardTypeChanged(): vscode.Event<void> {
return this._onBoardTypeChanged.event;
}

public doChangeBoardType(targetBoard: IBoard) {
const dc = DeviceContext.getInstance();
dc.board = targetBoard.key;
this._currentBoard = targetBoard;
dc.configuration = this._currentBoard.customConfig;
this._boardConfigStatusBar.text = targetBoard.name;
this._arduinoApp.addLibPath(null);

this._onBoardTypeChanged.fire();
}

public get packages(): IPackage[] {
Expand Down
107 changes: 107 additions & 0 deletions src/arduino/exampleProvider.ts
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as path from "path";
import * as vscode from "vscode";

import { DeviceContext } from "../deviceContext";
import { BoardManager } from "./boardManager";
import { ExampleManager, IExampleNode } from "./exampleManager";

export class ExampleProvider implements vscode.TreeDataProvider<ExampleItem> {

private _onDidChangeTreeData: vscode.EventEmitter<null> = new vscode.EventEmitter<null>();

// tslint:disable-next-line:member-ordering
public readonly onDidChangeTreeData: vscode.Event<null> = this._onDidChangeTreeData.event;

private _exmaples: IExampleNode[] = null;

constructor(private _exampleManager: ExampleManager, private _boardManager: BoardManager) {
this._boardManager.onBoardTypeChanged(() => {
this.loadData();
});
}

public getTreeItem(element: ExampleItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element;
}

public getChildren(element?: ExampleItem): ExampleItem[] {
if (!this._exmaples) {
this.loadData();
return null;
}
if (!element) {
return this.createExampleItemList(this._exmaples);
} else {
return this.createExampleItemList(element.getChildren());
}
}

private loadData() {
this._exmaples = null;
this._exampleManager.loadExamples().then((examples) => {
this._exmaples = examples;
this._onDidChangeTreeData.fire();
});
}

private createExampleItemList(examples: IExampleNode[]): ExampleItem[] {
const result = [];
if (examples && examples.length) {
examples.forEach((example) => {
result.push(new ExampleItem(example));
});
}
return result;
}
}

class ExampleItem extends vscode.TreeItem {
/**
* These static fields/methods provide delay loading and a single copy of icons.
*/
private static _folderIcon;
private static _inoIcon;

private static getFolderIcon() {
if (!ExampleItem._folderIcon) {
ExampleItem._folderIcon = {
light: ExampleItem.getIconUri("Folder_16x.svg"),
dark: ExampleItem.getIconUri("Folder_16x_inverse.svg"),
};
}
return ExampleItem._folderIcon;
}

private static getInoIcon() {
if (!ExampleItem._inoIcon) {
ExampleItem._inoIcon = ExampleItem.getIconUri("ino_16x.svg");
}
return ExampleItem._inoIcon;
}

private static getIconUri(uriPath: string) {
const dc = DeviceContext.getInstance();
return vscode.Uri.file(path.join(dc.extensionPath, "images/examples", uriPath));
}

constructor(private _example: IExampleNode) {
super(_example.name, _example.isLeaf ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed);
if (_example.isLeaf) {
this.command = {
title: "Open Example",
command: "arduino.openExample",
arguments: [_example.path],
};
this.iconPath = ExampleItem.getInoIcon();
} else {
this.iconPath = ExampleItem.getFolderIcon();
}
}

public getChildren(): IExampleNode[] {
return this._example.children;
}
}
6 changes: 6 additions & 0 deletions src/arduinoActivator.ts
@@ -1,10 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as vscode from "vscode";

import { ArduinoApp } from "./arduino/arduino";
import { ArduinoSettings } from "./arduino/arduinoSettings";
import { BoardManager } from "./arduino/boardManager";
import { ExampleManager } from "./arduino/exampleManager";
import { ExampleProvider } from "./arduino/exampleProvider";
import { LibraryManager } from "./arduino/libraryManager";
import ArduinoContext from "./arduinoContext";
import { DeviceContext } from "./deviceContext";
Expand Down Expand Up @@ -33,6 +36,9 @@ class ArduinoActivator {
arduinoApp.libraryManager = new LibraryManager(arduinoSettings, arduinoApp);
arduinoApp.exampleManager = new ExampleManager(arduinoSettings, arduinoApp);
ArduinoContext.arduinoApp = arduinoApp;

const exampleProvider = new ExampleProvider(arduinoApp.exampleManager, arduinoApp.boardManager);
vscode.window.registerTreeDataProvider("arduinoExampleExplorer", exampleProvider);
})();
await this._initializePromise;
}
Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Expand Up @@ -157,6 +157,7 @@ export async function activate(context: vscode.ExtensionContext) {
});

registerArduinoCommand("arduino.addLibPath", (path) => ArduinoContext.arduinoApp.addLibPath(path));
registerArduinoCommand("arduino.openExample", (path) => ArduinoContext.arduinoApp.openExample(path));

// Arduino debugger
registerArduinoCommand("arduino.debug.startSession", async (config) => {
Expand Down Expand Up @@ -198,6 +199,7 @@ export async function activate(context: vscode.ExtensionContext) {
SerialMonitor.getInstance().initialize();
}
ArduinoContext.boardManager.updateStatusBar(true);
vscode.commands.executeCommand("setContext", "vscode-arduino:showExampleExplorer", true);
})();
}
vscode.window.onDidChangeActiveTextEditor(async () => {
Expand All @@ -213,6 +215,7 @@ export async function activate(context: vscode.ExtensionContext) {
SerialMonitor.getInstance().initialize();
}
ArduinoContext.boardManager.updateStatusBar(true);
vscode.commands.executeCommand("setContext", "vscode-arduino:showExampleExplorer", true);
}
});
Logger.traceUserData("end-activate-extension", {correlationId: activeGuid});
Expand Down
3 changes: 1 addition & 2 deletions src/serialmonitor/usbDetector.ts
Expand Up @@ -150,9 +150,8 @@ export class UsbDetector {
const readme = path.join(ArduinoContext.boardManager.currentBoard.platform.rootBoardPath, "README.md");
if (util.fileExistsSync(readme)) {
vscode.commands.executeCommand("markdown.showPreview", vscode.Uri.file(readme));
vscode.commands.executeCommand("setContext", "vscode-arduino:showExampleExplorer", true);
}
vscode.commands.executeCommand("arduino.reloadExample");
vscode.commands.executeCommand("arduino.showExamples");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/app/sprites/FolderOpen_16x.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/views/app/sprites/FolderOpen_16x_inverse.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/views/app/sprites/Folder_16x.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/views/app/sprites/Folder_16x_inverse.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 1 addition & 39 deletions src/views/app/sprites/ino_16x.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/views/package.json
Expand Up @@ -19,7 +19,7 @@
"file-loader": "^0.10.0",
"html-webpack-plugin": "^2.28.0",
"node-sass": "^4.5.0",
"rc-tree": "^1.4.5",
"rc-tree": "~1.4.5",
"react": "^15.4.2",
"react-bootstrap": "^0.30.7",
"react-dom": "^15.4.2",
Expand Down

0 comments on commit 01b98de

Please sign in to comment.