Skip to content

Commit

Permalink
Merge pull request apache#3741 from sdedic/lsp/dark-laf-support
Browse files Browse the repository at this point in the history
Support for dark themes for VSCode
  • Loading branch information
sdedic authored Mar 10, 2022
2 parents a757f69 + c8440eb commit 409100c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
6 changes: 6 additions & 0 deletions java/java.lsp.server/nbcode/integration/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@
<specification-version>9.24</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.swing.laf.flatlaf</code-name-base>
<run-dependency>
<specification-version>1.0</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
<test-dependencies>
<test-type>
Expand Down
57 changes: 53 additions & 4 deletions java/java.lsp.server/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ export function awaitClient() : Promise<NbLanguageClient> {
}

function findJDK(onChange: (path : string | null) => void): void {
function find(): string | null {
let nowDark : boolean = isDarkColorTheme();
function find(): string | null {
let nbJdk = workspace.getConfiguration('netbeans').get('jdkhome');
if (nbJdk) {
return nbJdk as string;
Expand All @@ -175,13 +176,27 @@ function findJDK(onChange: (path : string | null) => void): void {
let currentJdk = find();
let timeout: NodeJS.Timeout | undefined = undefined;
workspace.onDidChangeConfiguration(params => {
if (timeout || (!params.affectsConfiguration('java') && !params.affectsConfiguration('netbeans'))) {
if (timeout) {
return;
}
let interested : boolean = false;
if (params.affectsConfiguration('netbeans') || params.affectsConfiguration('java')) {
interested = true;
} else if (params.affectsConfiguration('workbench.colorTheme')) {
let d = isDarkColorTheme();
if (d != nowDark) {
interested = true;
}
}
if (!interested) {
return;
}
timeout = setTimeout(() => {
timeout = undefined;
let newJdk = find();
if (newJdk !== currentJdk) {
let newD = isDarkColorTheme();
if (newJdk !== currentJdk || newD != nowDark) {
nowDark = newD;
currentJdk = newJdk;
onChange(currentJdk);
}
Expand Down Expand Up @@ -581,6 +596,36 @@ function killNbProcess(notifyKill : boolean, log : vscode.OutputChannel, specPro
}
}

/**
* Attempts to determine if the Workbench is using dark-style color theme, so that NBLS
* starts with some dark L&F for icon resource selection.
*/
function isDarkColorTheme() : boolean {
const themeName = workspace.getConfiguration('workbench')?.get('colorTheme');
if (!themeName) {
return false;
}
for (const ext of vscode.extensions.all) {
const themeList : object[] = ext.packageJSON?.contributes && ext.packageJSON?.contributes['themes'];
if (!themeList) {
continue;
}
let t : any;
for (t of themeList) {
if (t.id !== themeName) {
continue;
}
const uiTheme = t.uiTheme;
if (typeof(uiTheme) == 'string') {
if (uiTheme.includes('-dark') || uiTheme.includes('-black')) {
return true;
}
}
}
}
return false;
}

function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContext, log : vscode.OutputChannel, notifyKill: boolean,
setClient : [(c : NbLanguageClient) => void, (err : any) => void]
): void {
Expand Down Expand Up @@ -638,7 +683,11 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
stdOut = null;
}
}
let p = launcher.launch(info, "--modules", "--list", "-J-XX:PerfMaxStringConstLength=10240");
let extras : string[] = ["--modules", "--list", "-J-XX:PerfMaxStringConstLength=10240"];
if (isDarkColorTheme()) {
extras.push('--laf', 'com.formdev.flatlaf.FlatDarkLaf');
}
let p = launcher.launch(info, ...extras);
handleLog(log, "LSP server launching: " + p.pid);
p.stdout.on('data', function(d: any) {
logAndWaitForEnabled(d.toString(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ SplashProgressBarCornerColor=0xB4A2B5
# error messages
ERR_UIExpected=UI class name expected, using default UI...
ERR_UserDirExpected=Directory expected after --userdir or --cachedir switch
ERR_UINotFound=UI class not found, using default UI...
ERR_UINotFound=UI class {0} not found, using default UI...
ERR_FontSizeExpected=Font size expected, using default font size...
ERR_BadFontSize=Bad format of the font size, using default font size...
ERR_BrandingNeedsArgument=Option --branding requires one argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class CLIOptions extends CLIHandler {

/* The class of the UIManager to be used for netbeans - can be set by command-line argument -ui <class name> */
protected static Class uiClass;

static String uiClassName;

/* The size of the fonts in the UI - 0 pt, the default value is set in NbTheme (for Metal L&F), for other L&Fs is set
in the class Main. The value can be changed in Themes.xml in system directory or by command-line argument -fontsize <size> */
private static int uiFontSize = 0;
Expand Down Expand Up @@ -152,12 +155,13 @@ public final int cli(String[] args) {
} else if ("Aqua".equals(ui)) {
ui = "com.apple.laf.AquaLookAndFeel";
}
uiClassName = ui;
uiClass = Class.forName(ui);
} catch(ArrayIndexOutOfBoundsException e) {
System.err.println(getString("ERR_UIExpected"));
return 2;
} catch (ClassNotFoundException e2) {
System.err.println(getString("ERR_UINotFound"));
// ignore, will be reported later. Maybe.
}
} else if (isOption (args[i], "fontsize")) { // NOI18N
args[i] = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Collection;
import org.openide.util.Lookup;
import org.openide.util.BaseUtilities;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected abstract void loadDefaultSection (
public abstract void setStatusText (String status);

public abstract void initializePlaf (Class uiClass, int uiFontSize, java.net.URL themeURL);

public abstract int cli(
String[] string,
InputStream inputStream,
Expand Down
19 changes: 18 additions & 1 deletion platform/core.startup/src/org/netbeans/core/startup/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,26 @@ public static void initializeURLFactory () {
* process.
*/
public static void initUICustomizations() {
if (!CLIOptions.isGui ()) {
if (!CLIOptions.isGui () && CLIOptions.uiClassName == null) {
return;
}

Class uiClass = CLIOptions.uiClass;
// try again loading L&F class, this time with full module system.
if (CLIOptions.uiClassName != null && CLIOptions.uiClass == null) {
// try again
ClassLoader loader = Lookup.getDefault().lookup(ClassLoader.class);
try {
uiClass = loader.loadClass(CLIOptions.uiClassName);
CLIOptions.uiClass = uiClass;
} catch (ClassNotFoundException ex) {
// ignore
System.err.println(NbBundle.getMessage(Main.class, "ERR_UINotFound", CLIOptions.uiClassName));
if (!CLIOptions.isGui ()) {
return;
}
}
}

URL themeURL = null;
boolean wantTheme = Boolean.getBoolean ("netbeans.useTheme") ||
Expand Down

0 comments on commit 409100c

Please sign in to comment.