Skip to content

Commit cefc646

Browse files
authored
fix: run init js after load complete (#227)
1 parent efb46f9 commit cefc646

5 files changed

Lines changed: 134 additions & 44 deletions

File tree

plugin/src/main/java/io/snyk/eclipse/plugin/html/BaseHtmlProvider.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ public String replaceCssVariables(String html) {
5555
// Replace CSS variables with actual color values
5656
html = html.replace("var(--text-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000"));
5757
html = html.replace("var(--background-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_START", "#FFFFFF"));
58-
html = html.replace("var(--border-color)", getColorAsHex( "org.eclipse.ui.workbench.ACTIVE_TAB_BORDER_COLOR", "#CCCCCC"));
59-
html = html.replace("var(--link-color)", getColorAsHex("org.eclipse.ui.workbench.HYPERLINK_COLOR", "#0000FF"));
60-
html = html.replace("var(--horizontal-border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_HIGHLIGHT_BORDER_COLOR", "#CCCCCC"));
61-
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.CODE_BACKGROUND_COLOR", "#F0F0F0"));
58+
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "#F0F0F0"));
59+
60+
html = html.replace("var(--border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
61+
html = html.replace("var(--link-color)", getColorAsHex("ACTIVE_HYPERLINK_COLOR", "#0000FF"));
62+
html = html.replace("var(--horizontal-border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
6263

6364
html = html.replace("${headerEnd}", "");
6465
html = html.replace("${nonce}", nonce);
@@ -84,14 +85,29 @@ public String getColorAsHex(String colorKey, String defaultColor) {
8485
});
8586
}
8687

88+
public Boolean isDarkTheme() {
89+
var darkColor = getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "");
90+
return darkColor != "";
91+
}
92+
8793
private ColorRegistry colorRegistry;
8894
private ColorRegistry getColorRegistry() {
8995
if(colorRegistry != null) {
9096
return colorRegistry;
9197
}
92-
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
93-
ITheme currentTheme = themeManager.getCurrentTheme();
98+
ITheme currentTheme = getCurrentTheme();
9499
colorRegistry = currentTheme.getColorRegistry();
95100
return colorRegistry;
96101
}
102+
103+
104+
private ITheme currentTheme;
105+
public ITheme getCurrentTheme() {
106+
if(currentTheme != null) {
107+
return currentTheme;
108+
}
109+
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
110+
currentTheme = themeManager.getCurrentTheme();
111+
return currentTheme;
112+
}
97113
}

plugin/src/main/java/io/snyk/eclipse/plugin/html/CodeHtmlProvider.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.snyk.eclipse.plugin.html;
22

3+
import org.eclipse.jface.resource.JFaceResources;
34
import org.eclipse.ui.PlatformUI;
45
import org.eclipse.ui.themes.ITheme;
56
import org.eclipse.ui.themes.IThemeManager;
@@ -56,40 +57,24 @@ function navigateToIssue(e, target) {
5657
}
5758
""" + themeScript;
5859
}
59-
60-
private ITheme currentTheme;
61-
private ITheme getCurrentTheme() {
62-
if(currentTheme != null) {
63-
return currentTheme;
64-
}
65-
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
66-
currentTheme = themeManager.getCurrentTheme();
67-
return currentTheme;
68-
}
69-
60+
7061
private String getThemeScript() {
7162
if(Preferences.getInstance().isTest()) {
7263
return "";
7364
}
74-
ITheme currentTheme = getCurrentTheme();
75-
String themeId = currentTheme.getId().toLowerCase();
7665

77-
boolean isDarkTheme = themeId.contains("dark");
78-
boolean isHighContrast = themeId.contains("highcontrast") || themeId.contains("high-contrast");
79-
80-
String themeScript = "var isDarkTheme = " + isDarkTheme + ";\n" +
81-
"var isHighContrast = " + isHighContrast + ";\n" +
82-
"document.body.classList.add(isHighContrast ? 'high-contrast' : (isDarkTheme ? 'dark' : 'light'));";
66+
String themeScript = "var isDarkTheme = " + isDarkTheme() + ";\n" +
67+
"document.body.classList.add(isDarkTheme ? 'dark' : 'light');";
8368
return themeScript;
8469
}
8570

8671
@Override
8772
public String replaceCssVariables(String html) {
8873
html = super.replaceCssVariables(html);
89-
74+
9075
// Replace CSS variables with actual color values
91-
html = html.replace("var(--example-line-removed-color)", super.getColorAsHex("org.eclipse.ui.workbench.lineRemovedColor", "#ff0000"));
92-
html = html.replace("var(--example-line-added-color)", super.getColorAsHex("org.eclipse.ui.workbench.lineAddedColor", "#00ff00"));
76+
html = html.replace("var(--example-line-removed-color)", super.getColorAsHex("DELETION_COLOR", "#ff0000"));
77+
html = html.replace("var(--example-line-added-color)", super.getColorAsHex("ADDITION_COLOR", "#00ff00"));
9378

9479
return html;
9580
}

plugin/src/main/java/io/snyk/eclipse/plugin/html/OssHtmlProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public static OssHtmlProvider getInstance() {
1515
@Override
1616
public String replaceCssVariables(String html) {
1717
html = super.replaceCssVariables(html);
18-
html = html.replace("var(--container-background-color)", super.getColorAsHex("org.eclipse.ui.workbench.CODE_BACKGROUND_COLOR", "#F0F0F0"));
19-
18+
html = html.replace("var(--container-background-color)", super.getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "#F0F0F0"));
2019
return html;
2120
}
2221
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.snyk.eclipse.plugin.html;
2+
3+
import org.eclipse.core.runtime.Platform;
4+
import org.osgi.framework.Bundle;
5+
6+
import io.snyk.eclipse.plugin.utils.ResourceUtils;
7+
8+
public class StaticPageHtmlProvider extends BaseHtmlProvider {
9+
private static StaticPageHtmlProvider instance = new StaticPageHtmlProvider();
10+
11+
public static StaticPageHtmlProvider getInstance() {
12+
if (instance == null) {
13+
synchronized (StaticPageHtmlProvider.class) {
14+
if (instance == null) {
15+
instance = new StaticPageHtmlProvider();
16+
}
17+
}
18+
}
19+
return instance;
20+
}
21+
22+
public String getInitHtml() {
23+
String snykWarningText = Platform.getResourceString(Platform.getBundle("io.snyk.eclipse.plugin"),
24+
"%snyk.trust.dialog.warning.text");
25+
26+
Bundle bundle = Platform.getBundle("io.snyk.eclipse.plugin");
27+
String base64Image = ResourceUtils.getBase64Image(bundle, "logo_snyk.png");
28+
29+
var html = """
30+
<!DOCTYPE html>
31+
<html lang="en">
32+
<head>
33+
<meta charset="UTF-8">
34+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
35+
<title>Snyk for Eclipse</title>
36+
<style>
37+
body {
38+
font-family: var(--default-font);
39+
background-color: var(--background-color);
40+
color: var(--text-color);
41+
}
42+
.container {
43+
display: flex;
44+
align-items: center;
45+
}
46+
.logo {
47+
margin-right: 20px;
48+
}
49+
</style>
50+
</head>
51+
<body>
52+
<div class="container">
53+
<img src='data:image/png;base64,%s' alt='Snyk Logo'>
54+
<div>
55+
<p><strong>Welcome to Snyk for Eclipse</strong></p>
56+
<p>%s</p>
57+
</div>
58+
</div>
59+
</body>
60+
</html>
61+
""".formatted(base64Image, snykWarningText);
62+
return replaceCssVariables(html);
63+
}
64+
65+
public String getLoadingHtml() {
66+
var html = """
67+
<!DOCTYPE html>
68+
<html lang="en">
69+
<head>
70+
<meta charset="UTF-8">
71+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
72+
<title>Snyk for Eclipse</title>
73+
<style>
74+
body {
75+
font-family: var(--default-font);
76+
background-color: var(--background-color);
77+
color: var(--text-color);
78+
}
79+
</style>
80+
</head>
81+
<body>
82+
Loading...
83+
</body>
84+
</html>
85+
""";
86+
return replaceCssVariables(html);
87+
}
88+
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/BrowserHandler.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.nio.file.Paths;
44
import java.util.concurrent.CompletableFuture;
55

6+
import org.apache.commons.lang3.StringUtils;
67
import org.eclipse.core.runtime.Platform;
78
import org.eclipse.jface.viewers.ISelectionChangedListener;
89
import org.eclipse.jface.viewers.IStructuredSelection;
@@ -17,15 +18,19 @@
1718
import org.eclipse.swt.browser.BrowserFunction;
1819
import org.eclipse.swt.browser.LocationEvent;
1920
import org.eclipse.swt.browser.LocationListener;
21+
import org.eclipse.swt.browser.ProgressAdapter;
22+
import org.eclipse.swt.browser.ProgressEvent;
2023
import org.eclipse.swt.program.Program;
2124
import org.eclipse.swt.widgets.Display;
2225
import org.osgi.framework.Bundle;
2326

2427
import io.snyk.eclipse.plugin.html.HtmlProviderFactory;
28+
import io.snyk.eclipse.plugin.html.StaticPageHtmlProvider;
2529
import io.snyk.eclipse.plugin.utils.ResourceUtils;
2630

2731
public class BrowserHandler {
2832
private Browser browser;
33+
private String initScript = "";
2934
public BrowserHandler(Browser browser) {
3035
this.browser = browser;
3136
}
@@ -77,6 +82,14 @@ public void changed(LocationEvent event) {
7782
}
7883
});
7984

85+
browser.addProgressListener(new ProgressAdapter() {
86+
@Override
87+
public void completed(ProgressEvent event) {
88+
if(!StringUtils.isEmpty(initScript)) {
89+
browser.execute(initScript);
90+
}
91+
}
92+
});
8093
initBrowserText();
8194
}
8295

@@ -88,7 +101,7 @@ public void updateBrowserContent(String text) {
88101
public CompletableFuture<Void> updateBrowserContent(TreeNode node) {
89102
// Generate HTML content based on the selected node
90103
if (!(node instanceof IssueTreeNode)) return CompletableFuture.completedFuture(null);
91-
browser.setText("Loading...");
104+
browser.setText(StaticPageHtmlProvider.getInstance().getLoadingHtml());
92105

93106
return CompletableFuture.supplyAsync(() -> {
94107
return generateHtmlContent(node);
@@ -98,8 +111,8 @@ public CompletableFuture<Void> updateBrowserContent(TreeNode node) {
98111
var product = ((ProductTreeNode) node.getParent().getParent()).getProduct();
99112
var htmlProvider = HtmlProviderFactory.GetHtmlProvider(product);
100113
var content = htmlProvider.replaceCssVariables(htmlContent);
114+
initScript = htmlProvider.getInitScript();
101115
browser.setText(content);
102-
browser.execute(htmlProvider.getInitScript());
103116
});
104117
});
105118

@@ -117,17 +130,6 @@ public String generateHtmlContent(String text) {
117130
}
118131

119132
public void initBrowserText() {
120-
String snykWarningText = Platform.getResourceString(Platform.getBundle("io.snyk.eclipse.plugin"),
121-
"%snyk.trust.dialog.warning.text");
122-
123-
Bundle bundle = Platform.getBundle("io.snyk.eclipse.plugin");
124-
String base64Image = ResourceUtils.getBase64Image(bundle, "logo_snyk.png");
125-
126-
browser.setText("<!DOCTYPE html> <html lang=\"en\"> <head> <meta charset=\"UTF-8\"> "
127-
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> "
128-
+ "<title>Snyk for Eclipse</title> <style> .container { display: flex; align-items: center; } .logo { margin-right: 20px; } "
129-
+ "</style> </head> <body> <div class=\"container\"> " + "<img src='data:image/png;base64,"
130-
+ base64Image + "' alt='Snyk Logo'>" + "<div> <p><strong>Welcome to Snyk for Eclipse</strong></p>"
131-
+ " <p>\n" + snykWarningText + "</body>\n" + "</html>");
133+
browser.setText(StaticPageHtmlProvider.getInstance().getInitHtml());
132134
}
133135
}

0 commit comments

Comments
 (0)