From 2daa27787a226aabc389878711344462c3068ae2 Mon Sep 17 00:00:00 2001 From: sheche Date: Tue, 18 Jan 2022 11:09:14 +0800 Subject: [PATCH 1/2] Support toggling awt development in help center Signed-off-by: sheche --- src/commands/handler.ts | 23 +++++++++++++++++ src/commands/index.ts | 3 ++- .../assets/components/GetStartedPage.tsx | 5 ++-- .../assets/components/NavigationPanel.tsx | 25 +++++++++++++++++-- src/welcome/index.ts | 11 +++++++- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/commands/handler.ts b/src/commands/handler.ts index 84d528f9..84825d6e 100644 --- a/src/commands/handler.ts +++ b/src/commands/handler.ts @@ -7,6 +7,7 @@ import { validateAndRecommendExtension } from "../recommendation"; import { sendInfo } from "vscode-extension-telemetry-wrapper"; import { getReleaseNotesEntries, findLatestReleaseNotes } from "../utils"; import { gt, eq } from "semver"; +import { fetchInitProps } from "../welcome/index"; export async function createMavenProjectCmdHandler(_context: vscode.ExtensionContext) { if (!await validateAndRecommendExtension("vscjava.vscode-maven", "Maven extension is recommended to help create Java projects and work with custom goals.", true)) { @@ -104,3 +105,25 @@ export async function showReleaseNotesHandler(context: vscode.ExtensionContext, return await showReleaseNotes(context, operationId, version); } + +export async function toggleAwtDevelopmentHandler(context: vscode.ExtensionContext, _operationId: string, enable: boolean) { + const workspaceConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("java.completion"); + const disabledList: string[] = workspaceConfig.get("filteredTypes") || []; + const filteredTypes: string[] = disabledList.filter((type) => { + return !type.startsWith("java.awt."); + }); + + if (!enable) { + filteredTypes.push("java.awt.*"); + } + + try { + await workspaceConfig.update("filteredTypes", filteredTypes, vscode.ConfigurationTarget.Workspace); + } catch (e) { + vscode.window.showErrorMessage((e as Error).message); + return; + } + + fetchInitProps(context); + vscode.window.showInformationMessage(`Java AWT development is ${enable ? "enabled" : "disabled"}.`); +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 0b1df59d..04ec75c0 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -12,7 +12,7 @@ import { javaRuntimeCmdHandler } from "../java-runtime"; import { overviewCmdHandler } from "../overview"; import { webviewCmdLinkHandler } from "../utils"; import { showWelcomeWebview, showWelcomeWebviewBeside } from "../welcome"; -import { createMavenProjectCmdHandler, createMicroProfileStarterProjectCmdHandler, createQuarkusProjectCmdHandler, createSpringBootProjectCmdHandler, installExtensionCmdHandler, openUrlCmdHandler, showExtensionCmdHandler, showReleaseNotesHandler } from "./handler"; +import { createMavenProjectCmdHandler, createMicroProfileStarterProjectCmdHandler, createQuarkusProjectCmdHandler, createSpringBootProjectCmdHandler, installExtensionCmdHandler, openUrlCmdHandler, showExtensionCmdHandler, showReleaseNotesHandler, toggleAwtDevelopmentHandler } from "./handler"; export function initialize(context: vscode.ExtensionContext) { registerCommandHandler(context, "java.overview", overviewCmdHandler); @@ -35,6 +35,7 @@ export function initialize(context: vscode.ExtensionContext) { registerCommandHandler(context, "java.classpathConfiguration", showClasspathConfigurationPage); registerCommandHandler(context, "java.installJdk", showInstallJdkWebviewBeside); registerCommandHandler(context, "java.installJdk.fromWalkthrough", showInstallJdkWebview); + registerCommandHandler(context, "java.toggleAwtDevelopment", toggleAwtDevelopmentHandler); } type CommandHandler = (context: vscode.ExtensionContext, operationId: string, ...args: any[]) => any; diff --git a/src/welcome/assets/components/GetStartedPage.tsx b/src/welcome/assets/components/GetStartedPage.tsx index efcc7493..264cadd0 100644 --- a/src/welcome/assets/components/GetStartedPage.tsx +++ b/src/welcome/assets/components/GetStartedPage.tsx @@ -13,6 +13,7 @@ import TourPage from "./TourPage"; export class GetStartedPage extends React.Component<{ showWhenUsingJava: boolean, firstTimeRun: boolean, + isAwtDisabled: boolean, }> { render() { @@ -25,7 +26,7 @@ export class GetStartedPage extends React.Component<{ } renderWelcomePage() { - const {showWhenUsingJava} = this.props; + const {showWhenUsingJava, isAwtDisabled} = this.props; return ( @@ -40,7 +41,7 @@ export class GetStartedPage extends React.Component<{ - + diff --git a/src/welcome/assets/components/NavigationPanel.tsx b/src/welcome/assets/components/NavigationPanel.tsx index 1e32fd16..efbd4bd7 100644 --- a/src/welcome/assets/components/NavigationPanel.tsx +++ b/src/welcome/assets/components/NavigationPanel.tsx @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +import * as _ from "lodash"; import Tabs from "react-bootstrap/Tabs"; import Tab from "react-bootstrap/Tab"; import React from "react"; @@ -13,14 +14,16 @@ import { ListGroup } from "react-bootstrap"; import { reportTabSwitch, WEBVIEW_ID } from "../utils"; import { encodeCommandUriWithTelemetry } from "../../../utils/webview"; -export default class NavigationPanel extends React.Component { +export default class NavigationPanel extends React.Component<{ + isAwtDisabled: boolean; +}> { private groups = [ { name: "General", icon: , actions: [ { name: "Configure Java Runtime", command: "java.runtime" }, - { name: "Open Java Settings", command: "workbench.action.openSettings", args: ["java."] }, + { name: "Open Java Settings", command: "workbench.action.openSettings", args: ["java."] as any[] }, { name: "Install Extensions...", command: "java.extGuide" }, { name: "Configure Formatter Settings", command: "java.formatterSettings" } ] @@ -50,6 +53,24 @@ export default class NavigationPanel extends React.Component { private currentTab: string = this.groups[0].name; render() { + const {isAwtDisabled} = this.props; + const studentSection = _.find(this.groups, {name: "Student"}); + if (studentSection) { + studentSection.actions = studentSection.actions.filter((action) => { + return action.command !== "java.toggleAwtDevelopment"; + }); + + if (isAwtDisabled) { + studentSection.actions.push({ + name: "Enable AWT Development", command: "java.toggleAwtDevelopment", args: [true] + }); + } else { + studentSection.actions.push({ + name: "Disable AWT Development", command: "java.toggleAwtDevelopment", args: [false] + }); + } + } + const itemIcon = ; const tabItems = this.groups.map(group => { const actionItems = group.actions.map(action => ( diff --git a/src/welcome/index.ts b/src/welcome/index.ts index 107511d6..a91cb57f 100644 --- a/src/welcome/index.ts +++ b/src/welcome/index.ts @@ -124,12 +124,13 @@ const setFirstTimeRun = (context: vscode.ExtensionContext, firstTimeRun: boolean context.globalState.update(KEY_IS_WELCOME_PAGE_VIEWED, !firstTimeRun); }; -const fetchInitProps = async (context: vscode.ExtensionContext) => { +export const fetchInitProps = async (context: vscode.ExtensionContext) => { welcomeView?.webview.postMessage({ command: "onDidFetchInitProps", props: { showWhenUsingJava: context.globalState.get(KEY_SHOW_WHEN_USING_JAVA), firstTimeRun: context.globalState.get(KEY_IS_WELCOME_PAGE_VIEWED) !== true, + isAwtDisabled: isAwtDisabled(), } }); setFirstTimeRun(context, false); @@ -141,7 +142,15 @@ const showTourPage = async (context: vscode.ExtensionContext) => { props: { showWhenUsingJava: context.globalState.get(KEY_SHOW_WHEN_USING_JAVA), firstTimeRun: true, + isAwtDisabled: isAwtDisabled(), } }); setFirstTimeRun(context, false); }; + +function isAwtDisabled(): boolean { + const filteredTypes: string[] = vscode.workspace.getConfiguration("java.completion").get("filteredTypes") || []; + return filteredTypes.some((type: string) => { + return type.startsWith("java.awt."); + }); +} From d4e55b9750e7aeae0d6d5a61cb31832d9b5f7449 Mon Sep 17 00:00:00 2001 From: sheche Date: Tue, 18 Jan 2022 14:34:53 +0800 Subject: [PATCH 2/2] Simplify the code Signed-off-by: sheche --- .../assets/components/NavigationPanel.tsx | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/welcome/assets/components/NavigationPanel.tsx b/src/welcome/assets/components/NavigationPanel.tsx index efbd4bd7..7f4a91ff 100644 --- a/src/welcome/assets/components/NavigationPanel.tsx +++ b/src/welcome/assets/components/NavigationPanel.tsx @@ -23,7 +23,7 @@ export default class NavigationPanel extends React.Component<{ icon: , actions: [ { name: "Configure Java Runtime", command: "java.runtime" }, - { name: "Open Java Settings", command: "workbench.action.openSettings", args: ["java."] as any[] }, + { name: "Open Java Settings", command: "workbench.action.openSettings", args: ["java."] }, { name: "Install Extensions...", command: "java.extGuide" }, { name: "Configure Formatter Settings", command: "java.formatterSettings" } ] @@ -45,7 +45,8 @@ export default class NavigationPanel extends React.Component<{ { name: "Tutorial: Running and Debugging", command: "java.helper.openUrl", args: ["https://code.visualstudio.com/docs/java/java-debugging"] }, { name: "Tutorial: Testing", command: "java.helper.openUrl", args: ["https://code.visualstudio.com/docs/java/java-testing"] }, { name: "Configure Sources, Dependencies, Output Folder...", command: "java.classpathConfiguration" }, - { name: "Quick Start: Jupyter Notebook for Java", command: "java.helper.openUrl", args: ["https://github.com/microsoft/vscode-java-pack/wiki/Quick-Start:-Jupyter-Notebook-for-Java"] } + { name: "Quick Start: Jupyter Notebook for Java", command: "java.helper.openUrl", args: ["https://github.com/microsoft/vscode-java-pack/wiki/Quick-Start:-Jupyter-Notebook-for-Java"] }, + { name: "Enable AWT Development", command: "java.toggleAwtDevelopment", args: [true] }, ] }, ]; @@ -56,18 +57,11 @@ export default class NavigationPanel extends React.Component<{ const {isAwtDisabled} = this.props; const studentSection = _.find(this.groups, {name: "Student"}); if (studentSection) { - studentSection.actions = studentSection.actions.filter((action) => { - return action.command !== "java.toggleAwtDevelopment"; - }); - - if (isAwtDisabled) { - studentSection.actions.push({ - name: "Enable AWT Development", command: "java.toggleAwtDevelopment", args: [true] - }); - } else { - studentSection.actions.push({ - name: "Disable AWT Development", command: "java.toggleAwtDevelopment", args: [false] - }); + for (const action of studentSection.actions) { + if (action.command === "java.toggleAwtDevelopment") { + action.name = `${isAwtDisabled ? "Enable" : "Disable"} AWT Development`; + action.args = [isAwtDisabled]; + } } }