This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathexampleProvider.ts
107 lines (90 loc) · 3.46 KB
/
exampleProvider.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
// 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(null);
});
}
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;
}
}