Skip to content

Commit 4974699

Browse files
committed
fix: Added --ide-background-color and did formatting
1 parent 826aad0 commit 4974699

1 file changed

Lines changed: 142 additions & 133 deletions

File tree

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

Lines changed: 142 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ public class BaseHtmlProvider {
2020
private final Random random = new Random();
2121
private final Map<String, String> colorCache = new HashMap<>();
2222
private String nonce = "";
23-
24-
public String getCss() {
25-
return "";
26-
}
27-
28-
public String getJs() {
29-
return "";
30-
}
31-
32-
public String getInitScript() {
33-
return "";
34-
}
35-
36-
public String getNonce() {
37-
if(!nonce.isEmpty()) {
38-
return nonce;
39-
}
40-
String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
41-
StringBuilder nonceBuilder = new StringBuilder(32);
42-
for (int i = 0; i < 32; i++) {
43-
nonceBuilder.append(allowedChars.charAt(random.nextInt(allowedChars.length())));
44-
}
45-
nonce = nonceBuilder.toString();
46-
return nonce;
47-
}
48-
49-
public String getNoDescriptionHtml() {
23+
24+
public String getCss() {
25+
return "";
26+
}
27+
28+
public String getJs() {
29+
return "";
30+
}
31+
32+
public String getInitScript() {
33+
return "";
34+
}
35+
36+
public String getNonce() {
37+
if (!nonce.isEmpty()) {
38+
return nonce;
39+
}
40+
String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
41+
StringBuilder nonceBuilder = new StringBuilder(32);
42+
for (int i = 0; i < 32; i++) {
43+
nonceBuilder.append(allowedChars.charAt(random.nextInt(allowedChars.length())));
44+
}
45+
nonce = nonceBuilder.toString();
46+
return nonce;
47+
}
48+
49+
public String getNoDescriptionHtml() {
5050
String snykWarningText = Platform.getResourceString(Platform.getBundle("io.snyk.eclipse.plugin"),
5151
"snyk.panel.auth.trust.warning.text");
5252

@@ -94,116 +94,125 @@ public String getNoDescriptionHtml() {
9494
return html;
9595
}
9696

97+
public String replaceCssVariables(String html) {
98+
// Build the CSS with the nonce
99+
String nonce = getNonce();
100+
String css = "<style nonce=\"" + nonce + "\">" + getCss() + "</style>";
101+
html = html.replace("${ideStyle}", css);
102+
html = html.replace("<style nonce=\"ideNonce\" data-ide-style></style>", css);
103+
html = html.replace("var(--default-font)",
104+
" ui-sans-serif, \"SF Pro Text\", \"Segoe UI\", \"Ubuntu\", Tahoma, Geneva, Verdana, sans-serif;");
105+
106+
// Replace CSS variables with actual color values
107+
html = html.replace("var(--text-color)",
108+
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000"));
109+
html = html.replace("var(--ide-background-color)",
110+
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END", "#FFFFFF"));
111+
html = html.replace("var(--background-color)",
112+
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END", "#FFFFFF"));
113+
html = html.replace("var(--code-background-color)",
114+
getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
115+
html = html.replace("var(--button-color)",
116+
getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
117+
html = html.replace("var(--circle-color)",
118+
getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
119+
120+
html = html.replace("var(--border-color)",
121+
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
122+
html = html.replace("var(--link-color)", getColorAsHex("ACTIVE_HYPERLINK_COLOR", "#0000FF"));
123+
html = html.replace("var(--horizontal-border-color)",
124+
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
125+
126+
html = html.replace("${headerEnd}", "");
127+
html = html.replace("${nonce}", nonce);
128+
html = html.replace("ideNonce", nonce);
129+
html = html.replace("${ideScript}", "");
130+
131+
return html;
132+
}
97133

98-
public String replaceCssVariables(String html) {
99-
// Build the CSS with the nonce
100-
String nonce = getNonce();
101-
String css = "<style nonce=\"" + nonce + "\">" + getCss() + "</style>";
102-
html = html.replace("${ideStyle}", css);
103-
html = html.replace("<style nonce=\"ideNonce\" data-ide-style></style>", css);
104-
html = html.replace("var(--default-font)", " ui-sans-serif, \"SF Pro Text\", \"Segoe UI\", \"Ubuntu\", Tahoma, Geneva, Verdana, sans-serif;");
105-
106-
107-
// Replace CSS variables with actual color values
108-
html = html.replace("var(--text-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000"));
109-
html = html.replace("var(--background-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END", "#FFFFFF"));
110-
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
111-
html = html.replace("var(--button-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
112-
html = html.replace("var(--circle-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
113-
114-
html = html.replace("var(--border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
115-
html = html.replace("var(--link-color)", getColorAsHex("ACTIVE_HYPERLINK_COLOR", "#0000FF"));
116-
html = html.replace("var(--horizontal-border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
117-
118-
html = html.replace("${headerEnd}", "");
119-
html = html.replace("${nonce}", nonce);
120-
html = html.replace("ideNonce", nonce);
121-
html = html.replace("${ideScript}", "");
122-
123-
return html;
124-
}
125-
126-
public String getColorAsHex(String colorKey, String defaultColor) {
127-
if(Preferences.getInstance().isTest()) {
134+
public String getColorAsHex(String colorKey, String defaultColor) {
135+
if (Preferences.getInstance().isTest()) {
128136
return "";
129137
}
130-
return colorCache.computeIfAbsent(colorKey, key -> {
131-
ColorRegistry colorRegistry = getColorRegistry();
132-
Color color = colorRegistry.get(colorKey);
133-
if (color == null) {
134-
return defaultColor;
135-
} else {
136-
RGB rgb = color.getRGB();
137-
return String.format("#%02x%02x%02x", rgb.red, rgb.green, rgb.blue);
138-
}
139-
});
140-
}
141-
142-
public Boolean isDarkTheme() {
143-
var darkColor = getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "");
144-
return darkColor.equals("true");
145-
}
146-
147-
private ColorRegistry colorRegistry;
148-
private ColorRegistry getColorRegistry() {
149-
if(colorRegistry != null) {
150-
return colorRegistry;
151-
}
152-
ITheme currentTheme = getCurrentTheme();
153-
colorRegistry = currentTheme.getColorRegistry();
154-
return colorRegistry;
155-
}
156-
157-
158-
private ITheme currentTheme;
159-
public ITheme getCurrentTheme() {
160-
if(currentTheme != null) {
161-
return currentTheme;
162-
}
163-
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
164-
currentTheme = themeManager.getCurrentTheme();
165-
return currentTheme;
166-
}
167-
168-
public String getErrorHtml(String errorMessage, String path) {
138+
return colorCache.computeIfAbsent(colorKey, key -> {
139+
ColorRegistry colorRegistry = getColorRegistry();
140+
Color color = colorRegistry.get(colorKey);
141+
if (color == null) {
142+
return defaultColor;
143+
} else {
144+
RGB rgb = color.getRGB();
145+
return String.format("#%02x%02x%02x", rgb.red, rgb.green, rgb.blue);
146+
}
147+
});
148+
}
149+
150+
public Boolean isDarkTheme() {
151+
var darkColor = getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "");
152+
return darkColor.equals("true");
153+
}
154+
155+
private ColorRegistry colorRegistry;
156+
157+
private ColorRegistry getColorRegistry() {
158+
if (colorRegistry != null) {
159+
return colorRegistry;
160+
}
161+
ITheme currentTheme = getCurrentTheme();
162+
colorRegistry = currentTheme.getColorRegistry();
163+
return colorRegistry;
164+
}
165+
166+
private ITheme currentTheme;
167+
168+
public ITheme getCurrentTheme() {
169+
if (currentTheme != null) {
170+
return currentTheme;
171+
}
172+
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
173+
currentTheme = themeManager.getCurrentTheme();
174+
return currentTheme;
175+
}
176+
177+
public String getErrorHtml(String errorMessage, String path) {
169178
var html = """
170-
<!DOCTYPE html>
171-
<html lang="en">
172-
<head>
173-
<meta charset="UTF-8">
174-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
175-
<title>Snyk for Eclipse</title>
176-
<style>
177-
body {
178-
font-family: var(--default-font);
179-
background-color: var(--background-color);
180-
color: var(--text-color);
181-
}
182-
.container {
183-
display: flex;
184-
align-items: center;
185-
}
186-
.logo {
187-
margin-right: 20px;
188-
}
189-
</style>
190-
</head>
191-
<body>
192-
<div class="container">
193-
<div>
194-
<p><strong>An error occurred:</strong></p>
195-
<p>
196-
<table>
197-
<tr><td width="150" >Error message:</td><td>%s</td></tr>
198-
<tr></tr>
199-
<tr><td>Path:</td><td>%s</td></tr>
200-
</table>
201-
</p>
202-
</div>
203-
</div>
204-
</body>
205-
</html>
206-
""".formatted(errorMessage, path);
179+
<!DOCTYPE html>
180+
<html lang="en">
181+
<head>
182+
<meta charset="UTF-8">
183+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
184+
<title>Snyk for Eclipse</title>
185+
<style>
186+
body {
187+
font-family: var(--default-font);
188+
background-color: var(--background-color);
189+
color: var(--text-color);
190+
}
191+
.container {
192+
display: flex;
193+
align-items: center;
194+
}
195+
.logo {
196+
margin-right: 20px;
197+
}
198+
</style>
199+
</head>
200+
<body>
201+
<div class="container">
202+
<div>
203+
<p><strong>An error occurred:</strong></p>
204+
<p>
205+
<table>
206+
<tr><td width="150" >Error message:</td><td>%s</td></tr>
207+
<tr></tr>
208+
<tr><td>Path:</td><td>%s</td></tr>
209+
</table>
210+
</p>
211+
</div>
212+
</div>
213+
</body>
214+
</html>
215+
""".formatted(errorMessage, path);
207216
return replaceCssVariables(html);
208217
}
209218
}

0 commit comments

Comments
 (0)