Skip to content

Commit c45cec6

Browse files
committed
Fix theme colors
1 parent fc23e01 commit c45cec6

File tree

6 files changed

+77
-55
lines changed

6 files changed

+77
-55
lines changed

openapi-plugin/src/main/java/com/xliic/core/ide/ui/LafManager.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
import org.eclipse.e4.ui.css.swt.theme.ITheme;
44

5-
import com.xliic.core.ui.UIManager;
6-
75
@SuppressWarnings("restriction")
86
public class LafManager {
97

8+
@SuppressWarnings("unused")
109
private final ITheme theme;
1110

1211
public LafManager(ITheme theme) {
1312
this.theme = theme;
1413
}
15-
16-
public UIManager.LookAndFeelInfo getCurrentLookAndFeel() {
17-
String name = UIManager.isDarkTheme(theme) ? "Darcula" : "Default";
18-
return new UIManager.LookAndFeelInfo(name);
19-
}
2014
}

openapi-plugin/src/main/java/com/xliic/core/ui/Color.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public Color(int rgb, int darkRGB) {
1515
}
1616

1717
public org.eclipse.swt.graphics.Color getSwtGraphicsColor() {
18-
return getColor(isDarkTheme() ? darkRGB : rgb);
18+
return getColor(UIManager.isDarkTheme() ? darkRGB : rgb);
1919
}
2020

2121
private org.eclipse.swt.graphics.Color getColor(int hex) {
@@ -24,9 +24,4 @@ private org.eclipse.swt.graphics.Color getColor(int hex) {
2424
int b = (hex & 0xFF);
2525
return new org.eclipse.swt.graphics.Color(new RGB(r, g, b));
2626
}
27-
28-
private static boolean isDarkTheme() {
29-
String name = UIManager.getLookAndFeel().getName();
30-
return name.contains("Darcula") || name.contains("High contrast");
31-
}
3227
}
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
package com.xliic.core.ui;
22

3-
import org.eclipse.e4.ui.css.swt.theme.ITheme;
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
46
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
5-
import org.eclipse.ui.IWorkbench;
7+
import org.eclipse.swt.widgets.Shell;
8+
import org.eclipse.ui.IWorkbenchWindow;
69
import org.eclipse.ui.PlatformUI;
710
import org.jetbrains.annotations.NotNull;
811

912
@SuppressWarnings("restriction")
1013
public class UIManager {
1114

12-
public static LookAndFeelInfo getLookAndFeel() {
13-
IWorkbench workbench = PlatformUI.getWorkbench();
14-
IThemeEngine themeEngine = workbench.getService(IThemeEngine.class);
15-
ITheme theme = themeEngine.getActiveTheme();
16-
String name = isDarkTheme(theme) ? "Darcula" : "Default";
17-
return new LookAndFeelInfo(name);
18-
}
15+
@NotNull
16+
private final Map<String, org.eclipse.swt.graphics.Color> colors = new HashMap<>();
1917

20-
public static boolean isDarkTheme(@NotNull ITheme theme) {
21-
String label = theme.getLabel();
22-
return (label != null) && label.contains("Dark");
23-
}
18+
private UIManager() {}
2419

25-
public static class LookAndFeelInfo {
26-
27-
private String name;
20+
private UIManager(Shell sh) {
21+
colors.put("Panel.foreground", sh.getForeground());
22+
colors.put("Panel.background", sh.getBackground());
23+
}
2824

29-
public LookAndFeelInfo(String name) {
30-
this.name = name;
25+
@NotNull
26+
public static UIManager getInstance() {
27+
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
28+
for (int i = 0; i < windows.length; i++) {
29+
Shell sh = windows[i].getShell();
30+
if (sh != null) {
31+
return new UIManager(sh);
32+
}
3133
}
34+
return new UIManager();
35+
}
3236

33-
public String getName() {
34-
return name;
35-
}
37+
public org.eclipse.swt.graphics.Color getColor(@NotNull String key) {
38+
return colors.get(key);
39+
}
40+
41+
public static boolean isDarkTheme() {
42+
IThemeEngine themeEngine = PlatformUI.getWorkbench().getService(IThemeEngine.class);
43+
String label = themeEngine.getActiveTheme().getLabel();
44+
return label != null && (label.contains("Dark") || label.contains("High Contrast"));
3645
}
3746
}

openapi-plugin/src/main/java/com/xliic/openapi/webapp/WebApp.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.xliic.core.ide.ui.LafManagerListener;
1919
import com.xliic.core.project.Project;
2020
import com.xliic.core.ui.PanelViewPart.ViewPartHandler;
21+
import com.xliic.core.ui.UIManager;
2122
import com.xliic.core.ui.jcef.CefLoadHandlerAdapter;
2223
import com.xliic.core.ui.jcef.JBCefBrowser;
2324
import com.xliic.core.wm.ToolWindow;
@@ -72,7 +73,10 @@ public void onLoadingStateChange(JBCefBrowser browser, boolean isLoading, boolea
7273
cefApp.registerSchemeHandlerFactory("http", DOMAIN, SCHEME_HANDLER_FACTORY);
7374
regShFactory = false;
7475
}
75-
loadURL(HTTP_SCHEMA_PREFIX + resourceId + ".html");
76+
ApplicationManager.getApplication().invokeAndWait(() -> {
77+
SCHEME_HANDLER_FACTORY.setUIManager(UIManager.getInstance());
78+
loadURL(HTTP_SCHEMA_PREFIX + resourceId + ".html");
79+
});
7680
}
7781

7882
@Nullable
@@ -90,6 +94,8 @@ public void dispose() {
9094

9195
@Override
9296
public void lookAndFeelChanged(@NotNull LafManager source) {
93-
new ChangeTheme().send(getCefBrowser());
97+
ApplicationManager.getApplication().invokeAndWait(() -> {
98+
new ChangeTheme(UIManager.getInstance()).send(getCefBrowser());
99+
});
94100
}
95101
}

openapi-plugin/src/main/java/com/xliic/openapi/webapp/messages/ChangeTheme.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.xliic.openapi.webapp.messages;
22

3-
import java.awt.Color;
43
import java.util.HashMap;
54
import java.util.HashSet;
65
import java.util.LinkedList;
@@ -9,13 +8,13 @@
98
import java.util.Objects;
109
import java.util.Set;
1110

12-
import javax.swing.UIManager;
13-
1411
import org.eclipse.e4.ui.css.swt.theme.ITheme;
1512
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
13+
import org.eclipse.swt.graphics.Color;
1614
import org.eclipse.ui.PlatformUI;
1715
import org.jetbrains.annotations.NotNull;
1816

17+
import com.xliic.core.ui.UIManager;
1918
import com.xliic.openapi.parser.ast.node.Node;
2019
import com.xliic.openapi.utils.NetUtils;
2120
import com.xliic.openapi.utils.Utils;
@@ -54,18 +53,22 @@ public class ChangeTheme extends WebAppConsume {
5453
}
5554
}
5655

57-
public ChangeTheme() {
56+
@NotNull
57+
private final UIManager manager;
58+
59+
public ChangeTheme(@NotNull UIManager manager) {
5860
super("changeTheme");
61+
this.manager = manager;
5962
}
6063

6164
@Override
6265
protected @NotNull Map<String, Object> getPayload() {
63-
return getChangeThemePayload();
66+
return getChangeThemePayload(manager);
6467
}
6568

66-
public static Map<String, Object> getChangeThemePayload() {
69+
public static Map<String, Object> getChangeThemePayload(@NotNull UIManager manager) {
6770
Map<String, Object> payload = new HashMap<>();
68-
String kind = getKind();
71+
String kind = getKind(manager);
6972
payload.put("kind", kind);
7073
Map<String, Object> colors = new HashMap<>();
7174
String themeId = getCurrentScheme().getId();
@@ -75,28 +78,34 @@ public static Map<String, Object> getChangeThemePayload() {
7578
// Get all colors from the default theme
7679
setColors(KIND_DEFAULT_THEMES.get(kind), colors);
7780
}
78-
//updateColor("foreground", "Panel.foreground", colors);
79-
// Light theme in IDEA has grey Panel.background, skip it here
80-
// if (!isWhiteBackgroundTheme(themeId)) {
81-
// updateColor("background", "Panel.background", colors);
82-
// updateColor("sidebarBackground", "Panel.background", colors);
83-
// }
81+
if (isCustomBackgroundTheme(themeId) && !"light".equals(kind)) {
82+
updateColor("background", "Panel.background", colors, manager);
83+
updateColor("sidebarBackground", "Panel.background", colors, manager);
84+
}
8485
payload.put("theme", colors);
8586
return payload;
8687
}
8788

89+
private static boolean isCustomBackgroundTheme(String themeId) {
90+
return !isWhiteBackgroundTheme(themeId) && !isDarkBackgroundTheme(themeId);
91+
}
92+
8893
private static boolean isWhiteBackgroundTheme(String themeId) {
8994
return "org.eclipse.e4.ui.css.theme.e4_default".equals(themeId) || "org.eclipse.e4.ui.css.theme.e4_classic".equals(themeId);
9095
}
9196

97+
private static boolean isDarkBackgroundTheme(String themeId) {
98+
return "org.eclipse.e4.ui.css.theme.e4_dark".equals(themeId) || "org.eclipse.e4.ui.css.theme.high-contrast".equals(themeId);
99+
}
100+
92101
private static void setColors(String themeId, Map<String, Object> colors) {
93102
for (WebAppColor color: THEME_COLORS.get(themeId)) {
94103
colors.put(color.getId(), color.getValue());
95104
}
96105
}
97106

98-
private static void updateColor(String key, String uiKey, Map<String, Object> colors) {
99-
Color color = UIManager.getColor(uiKey);
107+
private static void updateColor(String key, String uiKey, Map<String, Object> colors, UIManager manager) {
108+
Color color = manager.getColor(uiKey);
100109
if (color != null) {
101110
colors.put(key, toHex(color));
102111
}
@@ -107,12 +116,12 @@ private static ITheme getCurrentScheme() {
107116
return themeEngine.getActiveTheme();
108117
}
109118

110-
private static String getKind() {
119+
private static String getKind(UIManager manager) {
111120
String kind = getKindFromScheme(getCurrentScheme());
112121
if (kind != null) {
113122
return kind;
114123
}
115-
Color color = UIManager.getColor("Panel.background");
124+
Color color = manager.getColor("Panel.background");
116125
if (color != null) {
117126
int grey = (int) ((color.getRed() + color.getGreen() + color.getBlue()) / 3.0);
118127
return 100 <= grey ? "light" : (grey <= 10 ? "highContrast" : "dark");

openapi-plugin/src/main/java/com/xliic/openapi/webapp/scheme/SchemeHandlerFactory.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import java.util.Map;
55

66
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
78

89
import com.equo.middleware.api.handler.IResponseHandler;
910
import com.equo.middleware.api.resource.Request;
11+
import com.xliic.core.ui.UIManager;
1012
import com.xliic.openapi.utils.NetUtils;
1113
import com.xliic.openapi.utils.Utils;
1214
import com.xliic.openapi.webapp.messages.ChangeTheme;
@@ -20,9 +22,16 @@ public class SchemeHandlerFactory implements IResponseHandler {
2022

2123
@NotNull
2224
private final String indexHTML;
25+
@Nullable
26+
private UIManager manager;
2327

2428
public SchemeHandlerFactory() {
2529
indexHTML = NetUtils.getWebAppResource(getClass(), "index.html");
30+
manager = null;
31+
}
32+
33+
public void setUIManager(@NotNull UIManager manager) {
34+
this.manager = manager;
2635
}
2736

2837
private ResourceHandler create(@NotNull String url) {
@@ -44,13 +53,13 @@ private String getResourceIndexHTML(@NotNull String resourceId) {
4453
String page = indexHTML.replace("${style-css}", STYLE_CSS_URL);
4554
page = page.replace("${index-script}", HTTP_SCHEMA_PREFIX + resourceId + ".js");
4655
page = page.replace("window.__EclipseJTools.postMessage", getBrowserFunctionName(resourceId));
47-
return page.replace("$theme", "" + Utils.serialize(ChangeTheme.getChangeThemePayload(), true));
56+
return page.replace("$theme", "" + Utils.serialize(ChangeTheme.getChangeThemePayload(manager), true));
4857
}
4958

5059
@Override
5160
public InputStream getResponseData(Request request, Map<String, String> headers) {
5261
String url = request.getUrl();
53-
if (url == null) {
62+
if (url == null || manager == null) {
5463
return null;
5564
}
5665
ResourceHandler resourceHandler = create(url);

0 commit comments

Comments
 (0)