Skip to content

Commit

Permalink
Tasks for demo classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
weisJ committed Apr 11, 2020
1 parent fa34d54 commit 9b12768
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 141 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ jobs:
name: macOS (Java 11)
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 10
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build
run: ./gradlew build
- uses: actions/checkout@v2
with:
fetch-depth: 10
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build
run: ./gradlew build
16 changes: 16 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ tasks.jar {
}
}

project.properties
.filter { it.key.startsWith("demoClass") }
.forEach {
val name = it.key.split(".")[1]
rootProject.tasks.register(name, JavaExec::class) {
group = "Demo"
description = "Run the demo '$name'."
dependsOn(tasks.testClasses)

workingDir = File(project.rootDir, "build")
workingDir.mkdirs()
main = it.value?.toString() ?: throw GradleException("Main class for demo ``$name` not specified.")
classpath(sourceSets.main.get().runtimeClasspath, sourceSets.test.get().runtimeClasspath)
}
}

val makeDocumentation by tasks.registering(JavaExec::class) {
group = "Development"
description = "Builds the documentation"
Expand Down
42 changes: 41 additions & 1 deletion core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
generatePomFile = true
generatePomFile = true

demoClass.UIManagerDefaults = defaults.UIManagerDefaults
demoClass.AllIcons = icon.AllIcons
demoClass.IconDemo = icon.IconDemo
demoClass.RotatableIconDemo = icon.RotatableIconDemo
demoClass.ButtonDemo = ui.button.ButtonDemo
demoClass.GroupedButtonDemo = ui.button.GroupedButtonDemo
demoClass.ToggleButtonDemo = ui.button.ToggleButtonDemo
demoClass.CheckBoxDemo = ui.checkBox.CheckBoxDemo
demoClass.TriCheckBoxDemo = ui.checkBox.TriCheckBoxDemo
demoClass.ColorChooserDemo = ui.colorChooser.ColorChooserDemo
demoClass.ComboBoxDemo = ui.comboBox.ComboBoxDemo
demoClass.DialogDemo = ui.dialog.DialogDemo
demoClass.FileChooserDemo = ui.fileChooser.FileChooserDemo
demoClass.InternalFrameDemo = ui.internalFrame.InternalFrameDemo
demoClass.LabelDemo = ui.label.LabelDemo
demoClass.ListDemo = ui.list.ListDemo
demoClass.PopupMenuDemo = ui.popupMenu.PopupMenuDemo
demoClass.ProgressBarDemo = ui.progressBar.ProgressBarDemo
demoClass.RadioButtonDemo = ui.radioButton.RadioButtonDemo
demoClass.ScrollPaneDemo = ui.scrollPane.ScrollPaneDemo
demoClass.OverlayScrollPaneDemo = ui.scrollPane.OverlayScrollPaneDemo
demoClass.SliderDemo = ui.slider.SliderDemo
demoClass.SpinnerDemo = ui.spinner.SpinnerDemo
demoClass.SplitPaneDemo = ui.splitPane.SplitPaneDemo
demoClass.TabbedPaneDemo = ui.tabbedPane.TabbedPaneDemo
demoClass.TabbedPaneKeyboardShortcutDemo = ui.tabbedPane.TabbedPaneKeyboardShortcut
demoClass.ClosableTabbedPaneDemo = ui.tabbedPane.ClosableTabbedPaneDemo
demoClass.TabFrameDemo = ui.tabFrame.TabFrameDemo
demoClass.TableDemo = ui.table.TableDemo
demoClass.EditorPaneDemo = ui.text.EditorPaneDemo
demoClass.FormattedTextFieldDemo = ui.text.FormattedTextFieldDemo
demoClass.PasswordFieldDemo = ui.text.PasswordFieldDemo
demoClass.TextAreaDemo = ui.text.TextAreaDemo
demoClass.TextFieldDemo = ui.text.TextFieldDemo
demoClass.TextPaneDemo = ui.text.TextPaneDemo
demoClass.ToolBarDemo = ui.toolBar.ToolBarDemo
demoClass.ToolTipDemo = ui.toolTip.ToolTipDemo
demoClass.TreeDemo = ui.tree.TreeDemo
demoClass.PreferenceChangeDemo = PreferenceChangeDemo
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github.weisj.darklaf.util.Pair;

import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -108,9 +109,10 @@ private ColorInfo getColorInfo(final Object o, final ColorInfo info) {
private Object mapColor(final ColorInfo info, final double[] hsbMatch, final Properties properties) {
Object obj = properties.get(info.key);
if (obj instanceof Color) {
return DarkColorModelHSB.getColorFromHSBValues(hsbMatch[0] * (info.hAdj / 100.0),
hsbMatch[1] * (info.sAdj / 100.0),
hsbMatch[2] * (info.bAdj / 100.0));
Color color = DarkColorModelHSB.getColorFromHSBValues(hsbMatch[0] * (info.hAdj / 100.0),
hsbMatch[1] * (info.sAdj / 100.0),
hsbMatch[2] * (info.bAdj / 100.0));
return new ColorUIResource(color);
}
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public static void paintOutlineBorder(final Graphics2D g, final int width, final
private static void doPaint(final Graphics2D g, final int width, final int height, final float arc,
final float bw) {
GraphicsContext context = GraphicsUtil.setupStrokePainting(g);

Shape outerRect = new RoundRectangle2D.Float(0, 0, width, height, arc + bw, arc + bw);
Shape innerRect = new RoundRectangle2D.Float(bw, bw, width - 2 * bw, height - 2 * bw, arc - bw, arc - bw);
Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
Expand Down Expand Up @@ -183,11 +182,12 @@ public static void paintLineBorder(final Graphics2D g, final float x, final floa
final boolean growByStroke) {
GraphicsContext config = GraphicsUtil.setupStrokePainting(g);
Stroke stroke = g.getStroke();
float lw = stroke instanceof BasicStroke ? ((BasicStroke) stroke).getLineWidth() : 1;
float lw = stroke instanceof BasicStroke ? ((BasicStroke) stroke).getLineWidth() : 1f;
lw /= 2;
if (growByStroke) {
g.draw(new RoundRectangle2D.Float(x - lw, y - lw, width + lw, height + lw, arc, arc));
} else {
g.draw(new RoundRectangle2D.Float(x, y, width, height, arc, arc));
g.draw(new RoundRectangle2D.Float(x, y, width - lw, height - lw, arc - 3, arc - 3));
}
config.restore();
}
Expand Down
114 changes: 32 additions & 82 deletions core/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.github.weisj.darklaf.util;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

Expand Down Expand Up @@ -63,11 +62,33 @@ public static Image createDragImage(final Component c, final Rectangle bounds,
* @param bounds the bounds inside the component to capture.
* @return image containing the captured area.
*/
public static BufferedImage imageFromComponent(final Component c, final Rectangle bounds) {
return scaledImageFromComponent(c, bounds, 1.0, 1.0, true);
}

public static Image scaledImageFromComponent(final Component c, final Rectangle bounds) {
return scaledImageFromComponent(c, bounds, Scale.SCALE_X, Scale.SCALE_Y);
/**
* Create image from component.
*
* @param c the component.
* @param bounds the bounds inside the component to capture.
* @return image containing the captured area.
*/
public static BufferedImage imageFromComponent(final Component c, final Rectangle bounds, final boolean print) {
return scaledImageFromComponent(c, bounds, 1.0, 1.0, print);
}

/**
* Create image from component.
*
* @param c the component.
* @param bounds the bounds inside the component to capture.
* @return image containing the captured area.
*/
public static BufferedImage scaledImageFromComponent(final Component c, final Rectangle bounds) {
return scaledImageFromComponent(c, bounds, Scale.SCALE_X, Scale.SCALE_Y, true);
}


/**
* Create image from component.
*
Expand All @@ -77,9 +98,9 @@ public static Image scaledImageFromComponent(final Component c, final Rectangle
* @param scaley the y scale
* @return image containing the captured area.
*/

public static Image scaledImageFromComponent(final Component c, final Rectangle bounds,
final double scalex, final double scaley) {
public static BufferedImage scaledImageFromComponent(final Component c, final Rectangle bounds,
final double scalex, final double scaley,
final boolean print) {
BufferedImage image;
boolean scale = scalex != 1.0 || scaley != 1.0;
if (scale) {
Expand All @@ -93,87 +114,16 @@ public static Image scaledImageFromComponent(final Component c, final Rectangle
g2d.scale(scalex, scaley);
}
g2d.translate(-bounds.x, -bounds.y);
c.printAll(g2d);

g2d.dispose();
return image;
}

/**
* Create image from component.
*
* @param c the component.
* @param bounds the bounds inside the component to capture.
* @return image containing the captured area.
*/

public static Image imageFromComponent(final Component c, final Rectangle bounds) {
return scaledImageFromComponent(c, bounds, 1.0, 1.0);
}


public static Icon cropIcon(final Icon icon, int maxWidth, int maxHeight) {
if (icon.getIconHeight() <= maxHeight && icon.getIconWidth() <= maxWidth) {
return icon;
}

Image image = toImage(icon);
if (image == null) return icon;

double scale = 1f;
BufferedImage bi = ImageUtil.toBufferedImage(image);
final Graphics2D g = bi.createGraphics();

int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);

maxWidth = maxWidth == Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) Math.round(maxWidth * scale);
maxHeight = maxHeight == Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) Math.round(maxHeight * scale);
final int w = Math.min(imageWidth, maxWidth);
final int h = Math.min(imageHeight, maxHeight);

final BufferedImage img = new BufferedImage(w, h, Transparency.TRANSLUCENT);
final int offX = imageWidth > maxWidth ? (imageWidth - maxWidth) / 2 : 0;
final int offY = imageHeight > maxHeight ? (imageHeight - maxHeight) / 2 : 0;
for (int col = 0; col < w; col++) {
for (int row = 0; row < h; row++) {
img.setRGB(col, row, bi.getRGB(col + offX, row + offY));
}
}
g.dispose();
return new ImageIcon(img);
}

public static Image toImage(final Icon icon) {
if (icon instanceof ImageIcon) {
return ((ImageIcon) icon).getImage();
if (print) {
c.printAll(g2d);
} else {
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
icon.paintIcon(null, image.getGraphics(), 0, 0);
return image;
c.paintAll(g2d);
}
}


public static BufferedImage toBufferedImage(final Image image) {
if (image == null) {
throw new NullPointerException("Can't covert null image");
}
if (image instanceof BufferedImage) {
return (BufferedImage) image;
} else {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bufferedImage;
}
g2d.dispose();
return image;
}


public static BufferedImage createCompatibleTranslucentImage(final int width,
final int height) {
return isHeadless() ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ Table.alternateRowColor = true
Tree.alternateRowColor = true
List.alternateRowColor = true
FileChooser.listViewWindowsStyle = false
CheckBox.borderInsets = 2,2,2,2
RadioButton.borderInsets = 2,2,2,2
PopupMenu.defaultLightWeightPopups = false
ToolTip.paintShadow = false
47 changes: 39 additions & 8 deletions core/src/test/java/PreferenceChangeDemo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.icons.SolidColorIcon;
import com.github.weisj.darklaf.theme.Theme;
import ui.ComponentDemo;
import ui.DemoPanel;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -40,20 +42,49 @@ public static void main(final String[] args) {
@Override
public JComponent createComponent() {
LafManager.addThemePreferenceChangeListener(LafManager::installTheme);
return new JPanel(new GridBagLayout()) {{
add(new JToggleButton("Start") {{
addActionListener(e -> {
setText(isSelected() ? "Stop" : "Start");
});
}});
}};
DemoPanel panel = new DemoPanel(new JToggleButton("Start") {{
addActionListener(e -> {
setText(isSelected() ? "Stop" : "Start");
LafManager.enabledPreferenceChangeReporting(isSelected());
});
}});
Icon accentColorIcon = new SolidColorIcon() {
@Override
public Color getColor() {
return LafManager.getTheme().getAccentColorRule().getAccentColor();
}
};
Icon selectionColorIcon = new SolidColorIcon() {
@Override
public Color getColor() {
return LafManager.getTheme().getAccentColorRule().getSelectionColor();
}
};
JPanel controlPanel = panel.addControls();
controlPanel.add(new JLabel("Accent Color", accentColorIcon, JLabel.LEFT));
controlPanel.add(new JLabel("Selection Color", selectionColorIcon, JLabel.LEFT));

controlPanel = panel.addControls();
controlPanel.add(new JTextArea() {{
setMargin(new Insets(5, 5, 5, 5));
setEditable(false);
setText("Press start/stop to enable/disable preference monitoring.\n"
+ "Then do one of the following\n"
+ " - switch between dark/light theme (Windows/macOS)\n"
+ " - toggle high contrast mode (Windows/macOS)\n"
+ " - change accent color (Windows/macOS)\n"
+ " - change selection color (macOS)\n"
+ " - change font scaling (Windows)\n"
+ "The theme should then adjust automatically (if monitoring is started).\n");
}});
return panel;
}

@Override
public WindowListener createWindowListener() {
return new WindowAdapter() {
@Override
public void windowClosed(final WindowEvent e) {
public void windowClosing(final WindowEvent e) {
LafManager.enabledPreferenceChangeReporting(false);
}
};
Expand Down
14 changes: 9 additions & 5 deletions core/src/test/java/ui/DemoPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ public DemoPanel(final JComponent component, final LayoutManager layoutManager,
add(controls, BorderLayout.SOUTH);
}

public JPanel addControls(final int columns) {
public JPanel addControls(final LayoutManager layoutManager) {
JPanel control = new JPanel();
control.setLayout(layoutManager);
control.setBorder(DarkBorders.createLineBorder(1, 0, 0, 0));
controls.add(control);
return control;
}

public JPanel addControls(final int columns) {
String constraints = "fillx";
if (columns > 0) {
constraints += ", wrap" + columns;
}
control.setLayout(new MigLayout(constraints, "[][grow]"));
control.setBorder(DarkBorders.createLineBorder(1, 0, 0, 0));
controls.add(control);
return control;
return addControls(new MigLayout(constraints, "[][grow]"));
}

public JPanel addControls() {
Expand Down
Loading

0 comments on commit 9b12768

Please sign in to comment.