Skip to content

Commit 50ea458

Browse files
fix: update font scaling logic to not use hard coded values (#264)
* fix: update font scaling logic to not use hard coded values * chore: allow resource checks to run on main and master branches * formatting: update comments in BaseHtmlProvider.java * fix: update font size handling for unit tests * fix: pmd checks - ignore empty catch block in uts * fix: pmd checks - ignore empty catch block in uts
1 parent 2e40678 commit 50ea458

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

.github/workflows/resource-check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Static Resource Checking
22
on:
33
push:
4-
branches: [ master ]
4+
branches: [ main, master ]
55
pull_request:
66

77
jobs:
@@ -14,7 +14,7 @@ jobs:
1414
- name: Check Static Resources
1515
run: |
1616
declare -A resources
17-
# Add each resource as a key, value pair, mapping the local resource to the reference file (which should be stored in the lanaguage server repository). For example:
17+
# Add each resource as a key, value pair, mapping the local resource to the reference file (which should be stored in the language server repository). For example:
1818
# resources["<path_to_local_file>"]="<url_of_reference_file>"
1919
resources["plugin/src/main/resources/ui/html/ScanSummaryInit.html"]="https://raw.githubusercontent.com/snyk/snyk-ls/refs/heads/main/shared_ide_resources/ui/html/ScanSummaryInit.html"
2020
for key in ${!resources[@]}; do

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public String replaceCssVariables(String html) {
103103
htmlStyled = htmlStyled.replace("<style nonce=\"ideNonce\" data-ide-style></style>", css);
104104
htmlStyled = htmlStyled.replace("var(--default-font)",
105105
" ui-sans-serif, \"SF Pro Text\", \"Segoe UI\", \"Ubuntu\", Tahoma, Geneva, Verdana, sans-serif;");
106-
htmlStyled = htmlStyled.replace("var(--main-font-size)", getScaledFontSize());
106+
htmlStyled = htmlStyled.replace("var(--main-font-size)", getRelativeFontSize(getDefaultFontSize()));
107107

108108
// Replace CSS variables with actual color values
109109
htmlStyled = htmlStyled.replace("var(--text-color)",
@@ -135,19 +135,31 @@ public String replaceCssVariables(String html) {
135135
return htmlStyled;
136136
}
137137

138-
private String getScaledFontSize() {
139-
int defaultHeight;
138+
@SuppressWarnings("PMD.EmptyCatchBlock")
139+
private int getDefaultFontSize() {
140+
int fontSize = 13;
140141
try {
141-
defaultHeight = getCurrentTheme().getFontRegistry().getFontData(JFaceResources.TEXT_FONT)[0].getHeight();
142+
fontSize = getCurrentTheme().getFontRegistry().getFontData(JFaceResources.TEXT_FONT)[0].getHeight();
142143
} catch (IllegalStateException e) {
143-
defaultHeight = 13;
144+
// TODO improve the logic here. Expected only in unit tests.
144145
}
145-
// Language server HTML assumes a base font size of 10px. The default Eclipse
146-
// font size is 17px (13pt), so we
147-
// apply a scaling factor here. This ensures that HTML fonts scale correctly if
148-
// the user changes the text size.
149-
int scaledHeight = (int) (defaultHeight / 1.7);
150-
return scaledHeight + "pt";
146+
return fontSize;
147+
}
148+
149+
// Utility function to scale Eclipse fonts appropriately for use in HTML elements that have been designed with
150+
// px values in mind.
151+
private String getRelativeFontSize(int inputFontSizePt) {
152+
// Target size is the base size for which the HTML element was designed.
153+
int targetSizePx = 10;
154+
int startingFontSizePt = inputFontSizePt > 0 ? inputFontSizePt : getDefaultFontSize();
155+
156+
// FontRegistry uses pt sizes, not px, so we convert here, using standard web values from
157+
// https://www.w3.org/TR/css3-values/#absolute-lengths
158+
double pxToPtMultiplier = 72.0 / 96.0;
159+
double targetSizePt = targetSizePx * pxToPtMultiplier;
160+
161+
// CSS allows 3 decimal places of precision for calculations.
162+
return String.format("%.3frem", targetSizePt / startingFontSizePt);
151163
}
152164

153165
public String getColorAsHex(String colorKey, String defaultColor) {

plugin/src/main/resources/ui/html/ScanSummaryInit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
to { transform: rotate(360deg); }
3232
}
3333

34-
.snx-h1 { font-size: 2rem; font-weight: 600; margin: .8rem 0; }
34+
.snx-h1 { font-size: 1.6rem; font-weight: 400; margin: .4rem 0; }
3535

3636
.snx-status { display:flex; align-items:center; padding: .4rem 1.2rem; background-color: rgba(255,255,255,.1); border-radius: 1rem; }
3737

0 commit comments

Comments
 (0)