-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetupSidebar.ts
More file actions
159 lines (136 loc) · 4.38 KB
/
Copy pathsetupSidebar.ts
File metadata and controls
159 lines (136 loc) · 4.38 KB
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
import { spawn } from 'child_process';
import { join } from 'path';
import { Event, EventEmitter, ExtensionContext, ProviderResult, TreeDataProvider, TreeItem, TreeItemCollapsibleState, window } from 'vscode';
import { state } from './extension';
import { existsSync } from 'fs';
export interface Sketch {
type?: "sketch";
name: string;
path: string;
mode?: string;
}
export interface Folder {
type?: "folder";
name: string;
path: string;
mode?: string;
children?: Folder[];
sketches?: Sketch[];
}
export async function setupSidebar(context: ExtensionContext) {
// TODO: Show welcome screens whilst we are starting Processing
setupSketchTreeView('sketchbook list', 'processingSidebarSketchbookView', context);
setupSketchTreeView('contributions examples list', 'processingSidebarExamplesView', context, true);
}
function setupSketchTreeView(command: string, viewId: string, context: ExtensionContext, readonly = false) {
const provider = new ProcessingWindowDataProvider(command, context, readonly);
window.createTreeView(viewId, { treeDataProvider: provider });
}
class ProcessingWindowDataProvider implements TreeDataProvider<FolderTreeItem | SketchTreeItem> {
constructor(
public readonly command: string,
public readonly context: ExtensionContext,
public readonly readonly = false
) {
this.cached();
this.populate();
this.listen();
}
private _folders: Folder[] = [];
private _onDidChangeTreeData: EventEmitter<null> = new EventEmitter<null>();
readonly onDidChangeTreeData: Event<null> = this._onDidChangeTreeData.event;
async listen() {
state.onDidVersionChange.on(null, async () => {
this.populate();
});
}
async cached() {
const data = await this.context.globalState.get<string>(`processing-tree-view-${this.command}-cache`);
if (data) {
try {
this._folders = JSON.parse(data) as Folder[];
} catch (e) {
console.error(`Error parsing cached JSON: ${e}`);
}
}
this._onDidChangeTreeData.fire(null);
}
async populate() {
this._folders = await this.grabSketches();
this._onDidChangeTreeData.fire(null);
}
getTreeItem(element: FolderTreeItem): TreeItem | Thenable<TreeItem> {
return element;
}
getChildren(element?: FolderTreeItem): ProviderResult<(FolderTreeItem | SketchTreeItem)[]> {
if (element === undefined) {
return this._folders.map((folder) => new FolderTreeItem(folder)) ?? [];
} else {
const sketches = element.folder.sketches?.map((sketch) => {
return new SketchTreeItem(sketch, this.readonly);
}) ?? [];
const folders = element.folder.children?.map((folder) => {
return new FolderTreeItem(folder);
}) ?? [];
// Sort sketches and folders
sketches.sort((a, b) => a.sketch.name.localeCompare(b.sketch.name));
folders.sort((a, b) => a.folder.name.localeCompare(b.folder.name));
return [...sketches, ...folders];
}
}
grabSketches(): Promise<Folder[]> {
return new Promise<Folder[]>((resolve) => {
const process = spawn(state.selectedVersion.path, this.command.split(' '));
let data = '';
process.stdout.on('data', (chunk) => {
data += chunk;
});
process.on('close', (code) => {
if (code !== 0) {
console.error(`Process exited with code ${code}`);
resolve([]);
return;
}
try {
this.context.globalState.update(`processing-tree-view-${this.command}-cache`, data);
const folders = JSON.parse(data) as Folder[];
resolve(folders);
} catch (e) {
console.error(`Error parsing JSON: ${e}`);
resolve([]);
}
});
});
}
}
class FolderTreeItem extends TreeItem {
constructor(
public readonly folder: Folder
) {
const label = folder.name;
super(label, TreeItemCollapsibleState.Collapsed);
this.tooltip = `${this.label}`;
}
}
class SketchTreeItem extends TreeItem {
constructor(
public readonly sketch: Sketch,
public readonly readonly = false
) {
const label = sketch.name;
super(label, TreeItemCollapsibleState.None);
this.tooltip = `${this.label}`;
this.iconPath = join(__dirname, "..", "..", "media/processing-flat-color.svg");
this.command = {
command: 'processing.sketch.open',
title: 'Open Sketch',
arguments: [this.sketch.path, this.readonly]
};
// TODO: add right-click menu to open in new window, open containing folder, etc.
// TODO: Make showing a preview a toggleable setting
const preview = `${sketch.path}/${sketch.name}.png`;
if (existsSync(preview)) {
this.iconPath = preview;
}
}
}