Skip to content

Commit

Permalink
hide asset types, commands until a makecode project is actually loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
jwunderl committed Mar 18, 2023
1 parent d8b2ab3 commit b793807
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
29 changes: 17 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,32 @@
{
"id": "imageExplorer",
"name": "%makecode.imageExplorer.title%",
"icon": "media/logo.svg"
"icon": "media/logo.svg",
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"id": "animationExplorer",
"name": "%makecode.animationExplorer.title%",
"icon": "media/logo.svg"
"icon": "media/logo.svg",
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"id": "tileExplorer",
"name": "%makecode.tileExplorer.title%",
"icon": "media/logo.svg"
"icon": "media/logo.svg",
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"id": "tilemapExplorer",
"name": "%makecode.tilemapExplorer.title%",
"icon": "media/logo.svg"
"icon": "media/logo.svg",
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"id": "songExplorer",
"name": "%makecode.songExplorer.title%",
"icon": "media/logo.svg"
"icon": "media/logo.svg",
"when": "makecode.hasMakeCodeProjectOpen"
}
]
},
Expand Down Expand Up @@ -285,31 +290,31 @@
},
{
"command": "makecode.build",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"command": "makecode.simulate",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"command": "makecode.clean",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"command": "makecode.shareProject",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"command": "makecode.addDependency",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"command": "makecode.removeDependency",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
},
{
"command": "makecode.refreshAssets",
"when": "makecode.extensionActive"
"when": "makecode.hasMakeCodeProjectOpen"
}
]
},
Expand Down
47 changes: 34 additions & 13 deletions src/web/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.window.registerTreeDataProvider("songExplorer", new JResTreeProvider("song"))
);
context.subscriptions.push(
vscode.workspace.onDidChangeWorkspaceFolders(e => {
maybeSetInMakeCodeProjectAsync();
})
)

// This key is not sensitive, and is publicly available in client side apps logging to AI
const appInsightsKey = "0c6ae279ed8443289764825290e4f9e2-1a736e7c-1324-4338-be46-fc2a58ae4d14-7255";
Expand All @@ -124,28 +129,42 @@ export function activate(context: vscode.ExtensionContext) {

// Set a context key to indicate that we have activated, so context menu commands can show
vscode.commands.executeCommand('setContext', 'makecode.extensionActive', true);
maybeSetInMakeCodeProjectAsync();
}

async function chooseWorkspaceAsync(kind: "empty" | "project" | "any", silent = false): Promise<vscode.WorkspaceFolder | undefined> {
async function maybeSetInMakeCodeProjectAsync() {
const hasProjectFolder = await findOpenFolders("project");
if (!!hasProjectFolder.length) {
setInMakeCodeProjectAsync();
}
}

export function setInMakeCodeProjectAsync() {
vscode.commands.executeCommand("setContext", "makecode.hasMakeCodeProjectOpen", true);
}

async function findOpenFolders(kind: "empty" | "project" | "any") {
const folders = [];
let hasWorkspaceOpen = false;

if (vscode.workspace.workspaceFolders) {
hasWorkspaceOpen = !!vscode.workspace.workspaceFolders.length;
for (const folder of vscode.workspace.workspaceFolders) {
if (kind === "any") {
folders.push(folder);
}
else {
const pxtJSONExists = await fileExistsAsync(vscode.Uri.joinPath(folder.uri, "pxt.json"));
for (const folder of (vscode.workspace.workspaceFolders || [])) {
if (kind === "any") {
folders.push(folder);
}
else {
const pxtJSONExists = await fileExistsAsync(vscode.Uri.joinPath(folder.uri, "pxt.json"));

if ((kind === "project" && pxtJSONExists) || (kind === "empty" && !pxtJSONExists)) {
folders.push(folder);
}
if ((kind === "project" && pxtJSONExists) || (kind === "empty" && !pxtJSONExists)) {
folders.push(folder);
}
}
}

return folders;
}

async function chooseWorkspaceAsync(kind: "empty" | "project" | "any", silent = false): Promise<vscode.WorkspaceFolder | undefined> {
const folders = await findOpenFolders(kind);
const hasWorkspaceOpen = !!vscode.workspace.workspaceFolders?.length;

if (folders.length === 0) {
if (!silent) {
Expand Down Expand Up @@ -302,6 +321,7 @@ export async function importUrlCommand(url?: string, useWorkspace?: vscode.Works
}, async progress => {
try {
await downloadSharedProjectAsync(workspace!, toOpen!);
setInMakeCodeProjectAsync()
}
catch (e) {
showError(vscode.l10n.t("Unable to download project"));
Expand Down Expand Up @@ -502,6 +522,7 @@ async function createCommand() {
});
}

setInMakeCodeProjectAsync();
await renameProjectAsync(workspace, projectName);
}

Expand Down
3 changes: 2 additions & 1 deletion src/web/vfs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "path-browserify"
import * as vscode from "vscode"
import { importUrlCommand } from "./extension";
import { importUrlCommand, setInMakeCodeProjectAsync } from "./extension";

export class VFS implements vscode.FileSystemProvider {
private initializedDirs: {[index: string]: boolean} = {}
Expand Down Expand Up @@ -119,6 +119,7 @@ export class VFS implements vscode.FileSystemProvider {
if (!(await this.existsAsync(pxtJSON))) {
await importUrlCommand(shareId, { name: shareId, uri: projectDir, index: 0 })
}
setInMakeCodeProjectAsync();
this.initializedDirs[shareId] = true;
}

Expand Down

0 comments on commit b793807

Please sign in to comment.