Skip to content

Commit

Permalink
Merge pull request #579 from laeubi/cache_font
Browse files Browse the repository at this point in the history
Cache the font of the slider / do not dispose colors
  • Loading branch information
lcaron authored Mar 23, 2024
2 parents fddb19d + 60f2b68 commit 8622402
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class NebularSliderDefaultConfiguration implements NebulaSliderGraphicCon
private static final int BAR_HEIGHT = 12;
private static final int SELECTOR_HEIGHT = 32;
protected final NebulaSlider parentSlider;
private Font font;

public NebularSliderDefaultConfiguration(final NebulaSlider parentSlider) {
this.parentSlider = parentSlider;
Expand Down Expand Up @@ -68,14 +69,21 @@ public Color getArrowColor() {

@Override
public Font getTextFont() {
final FontData fontData = parentSlider.getFont().getFontData()[0];
final Font newFont = new Font(parentSlider.getDisplay(), fontData.getName(), Math.max(fontData.getHeight(), 14), SWT.BOLD);
parentSlider.addDisposeListener(e -> {
if (!newFont.isDisposed()) {
newFont.dispose();
}
});
return newFont;
if(font == null || font.isDisposed()) {
final FontData fontData = parentSlider.getFont().getFontData()[0];
Font newFont = new Font(parentSlider.getDisplay(), fontData.getName(), getFontSize(fontData), SWT.BOLD);
parentSlider.addDisposeListener(e -> {
if(!newFont.isDisposed()) {
newFont.dispose();
}
});
this.font = newFont;
}
return font;
}

protected int getFontSize(FontData fontData) {
return Math.max(fontData.getHeight(), 14);
}

@Override
Expand All @@ -99,13 +107,7 @@ public int getBarHeight() {
}

protected Color getAndDisposeColor(final int r, final int g, final int b) {
final Color color = new Color(parentSlider.getDisplay(), r, g, b);
parentSlider.addDisposeListener(e -> {
if (!color.isDisposed()) {
color.dispose();
}
});
return color;
return new Color(r, g, b);
}

@Override
Expand Down

0 comments on commit 8622402

Please sign in to comment.