package org.saturnclient.ui.components; import org.saturnclient.config.Theme; import org.saturnclient.ui.Element; import org.saturnclient.ui.ElementContext; import org.saturnclient.ui.RenderScope; import org.saturnclient.ui.resources.Fonts; /** * A theme preset: a small mock-up of a panel in its colors, and its name. * Clicking it applies the preset. */ public class ThemePresetCard extends Element { public static final int HEIGHT = 92; private static final int PADDING = 10; private static final int PREVIEW_HEIGHT = 44; private static final float NAME_SCALE = 0.68f; private final Theme.Preset preset; private final Runnable onApply; /** * @param onApply runs after the preset is applied, to refresh what shows the * old colors */ public ThemePresetCard(Theme.Preset preset, int width, Runnable onApply) { this.preset = preset; this.onApply = onApply; this.width = width; this.height = HEIGHT; } @Override public void render(RenderScope renderScope, ElementContext ctx) { boolean active = preset.isActive(); int border = active ? Theme.ACCENT.value : Theme.withAlpha(ctx.isHovering() ? 0.22f : 0.06f, Theme.FOREGROUND.value); renderScope.drawRoundedRectangle(0, 0, width, height, Theme.WIDGET_RADIUS.value, active ? 2 : 1, Theme.withAlpha(0.72f, Theme.PRIMARY.value), border); // A panel in the preset's colors: a line of text over an accent and a widget int previewWidth = width - PADDING * 2; renderScope.drawRoundedRectangle(PADDING, PADDING, previewWidth, PREVIEW_HEIGHT, 8, preset.background()); renderScope.drawRoundedRectangle(PADDING + 8, PADDING + 8, previewWidth * 6 / 10, 5, 3, Theme.withAlpha(0.8f, preset.foreground())); renderScope.drawRoundedRectangle(PADDING + 8, PADDING + PREVIEW_HEIGHT - 20, 34, 12, 4, preset.accent()); renderScope.drawRoundedRectangle(PADDING + 47, PADDING + PREVIEW_HEIGHT - 20, 22, 12, 4, preset.primary()); int nameY = PADDING + PREVIEW_HEIGHT + (height - PADDING - PREVIEW_HEIGHT - (int) (Fonts.getHeight() * NAME_SCALE)) / 2; renderScope.drawText(NAME_SCALE, preset.name(), PADDING, nameY, Theme.FONT.value, Theme.FOREGROUND.value); } @Override public void click(int mouseX, int mouseY) { preset.apply(); onApply.run(); } }