From d567b6354d110137c4231a016c8e897bfce6fd53 Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 11 Mar 2020 17:57:56 +0100 Subject: [PATCH] Added demo for rotatable icons. Fixed rotated icon painting. Fixed OverlayScrollPane layout. Enhanced animations. --- .../darklaf/components/OverlayScrollPane.java | 12 ++-- .../ui/scrollpane/DarkScrollBarUI.java | 1 + .../github/weisj/darklaf/util/Animator.java | 2 +- .../test/java/defaults/UIManagerDefaults.java | 4 +- .../src/test/java/icon/RotatableIconDemo.java | 59 +++++++++++++++++++ .../test/java/ui/tabFrame/TabFrameDemo.java | 2 +- .../weisj/darklaf/icons/DarkSVGIcon.java | 4 +- .../weisj/darklaf/icons/RotatableIcon.java | 5 +- 8 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 core/src/test/java/icon/RotatableIconDemo.java diff --git a/core/src/main/java/com/github/weisj/darklaf/components/OverlayScrollPane.java b/core/src/main/java/com/github/weisj/darklaf/components/OverlayScrollPane.java index 5851860a..1a14a049 100644 --- a/core/src/main/java/com/github/weisj/darklaf/components/OverlayScrollPane.java +++ b/core/src/main/java/com/github/weisj/darklaf/components/OverlayScrollPane.java @@ -132,16 +132,12 @@ public class OverlayScrollPane extends JLayeredPane { private static final class PopupScrollBar extends JScrollBar { - private final JScrollPane pane; - - private PopupScrollBar(final int direction, final JScrollPane pane) { + private PopupScrollBar(final int direction) { super(direction); - this.pane = pane; putClientProperty("JScrollBar.fastWheelScrolling", true); setOpaque(false); } - @Override public boolean isOpaque() { return false; @@ -166,6 +162,7 @@ public class OverlayScrollPane extends JLayeredPane { @Override public void layoutContainer(final Container parent) { super.layoutContainer(parent); + viewport = getViewport(); if (viewport != null) { Rectangle bounds = viewport.getBounds(); Rectangle vertBounds = verticalScrollBar.getBounds(); @@ -217,11 +214,11 @@ public class OverlayScrollPane extends JLayeredPane { */ public void setUI(final ScrollPaneUI ui) { if (verticalScrollBar == null) { - verticalScrollBar = new PopupScrollBar(JScrollBar.VERTICAL, this); + verticalScrollBar = new PopupScrollBar(JScrollBar.VERTICAL); verticalScrollBar.putClientProperty("JScrollBar.scrollPaneParent", this); } if (horizontalScrollBar == null) { - horizontalScrollBar = new PopupScrollBar(JScrollBar.HORIZONTAL, this); + horizontalScrollBar = new PopupScrollBar(JScrollBar.HORIZONTAL); horizontalScrollBar.putClientProperty("JScrollBar.scrollPaneParent", this); } super.setUI(ui); @@ -257,7 +254,6 @@ public class OverlayScrollPane extends JLayeredPane { private ControlPanel(final OScrollPane scrollPane) { setLayout(null); - scrollPane.setVerticalScrollBar(scrollPane.verticalScrollBar); if (scrollPane.getVerticalScrollBarPolicy() != JScrollPane.VERTICAL_SCROLLBAR_NEVER) { showVertical = true; diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarUI.java index 07645c9f..66216343 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollBarUI.java @@ -436,6 +436,7 @@ public class DarkScrollBarUI extends BasicScrollBarUI { g.setColor(scrollbar.getBackground()); g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); } + if (trackAlpha == 0) return; Graphics2D g2 = (Graphics2D) g.create(); g2.setColor(getTrackColor()); g2.setComposite(COMPOSITE.derive(trackAlpha)); diff --git a/core/src/main/java/com/github/weisj/darklaf/util/Animator.java b/core/src/main/java/com/github/weisj/darklaf/util/Animator.java index 6af1db7b..f72a6c73 100644 --- a/core/src/main/java/com/github/weisj/darklaf/util/Animator.java +++ b/core/src/main/java/com/github/weisj/darklaf/util/Animator.java @@ -79,7 +79,7 @@ public abstract class Animator { ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, r -> { final Thread thread = new Thread(r, "Darcula Animations"); thread.setDaemon(true); - thread.setPriority(Thread.NORM_PRIORITY); + thread.setPriority(Thread.MAX_PRIORITY); return thread; }); executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); diff --git a/core/src/test/java/defaults/UIManagerDefaults.java b/core/src/test/java/defaults/UIManagerDefaults.java index 6c512836..9d549b83 100644 --- a/core/src/test/java/defaults/UIManagerDefaults.java +++ b/core/src/test/java/defaults/UIManagerDefaults.java @@ -115,13 +115,11 @@ public class UIManagerDefaults implements ItemListener, ComponentDemo { ((DefaultTableModel) table.getModel()).setRowCount(0); buildItemsMap(); - final Vector comboBoxItems = new Vector<>(50); - + FontMetrics fm = comboBox.getFontMetrics(comboBox.getFont()); for (final Object key : items.keySet()) { comboBoxItems.add((String) key); } - comboBox.removeItemListener(this); comboBox.setModel(new DefaultComboBoxModel<>(comboBoxItems)); comboBox.setSelectedIndex(-1); diff --git a/core/src/test/java/icon/RotatableIconDemo.java b/core/src/test/java/icon/RotatableIconDemo.java new file mode 100644 index 00000000..cc1e24fa --- /dev/null +++ b/core/src/test/java/icon/RotatableIconDemo.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package icon; + +import com.github.weisj.darklaf.icons.IconLoader; +import com.github.weisj.darklaf.icons.RotatableIcon; +import ui.ComponentDemo; +import ui.DemoPanel; + +import javax.swing.*; + +public class RotatableIconDemo implements ComponentDemo { + + public static void main(final String[] args) { + ComponentDemo.showDemo(new RotatableIconDemo()); + } + + @Override + public JComponent createComponent() { + RotatableIcon folderIcon = new RotatableIcon( + IconLoader.get().getIcon("files/folder.svg", 19, 19, true)); + JLabel label = new JLabel(folderIcon); + SwingUtilities.invokeLater(() -> { + Timer timer = new Timer(1000, e -> { + folderIcon.setOrientation(folderIcon.getOrientation().clockwise()); + label.repaint(); + }); + timer.setRepeats(true); + timer.start(); + }); + return new DemoPanel(label); + } + + @Override + public String getTitle() { + return "RotatableIcon Demo"; + } +} diff --git a/core/src/test/java/ui/tabFrame/TabFrameDemo.java b/core/src/test/java/ui/tabFrame/TabFrameDemo.java index b1cc69c7..92f51a4a 100644 --- a/core/src/test/java/ui/tabFrame/TabFrameDemo.java +++ b/core/src/test/java/ui/tabFrame/TabFrameDemo.java @@ -64,7 +64,7 @@ public class TabFrameDemo implements ComponentDemo { @Override public JComponent createComponent() { - Icon folderIcon = IconLoader.get().getUIAwareIcon("files/folder.svg", 19, 19); + Icon folderIcon = IconLoader.get().getIcon("files/folder.svg", 19, 19, true); JTabFrame tabFrame = new JTabFrame(); for (Alignment o : Alignment.values()) { if (o != Alignment.CENTER) { diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java index a1dc4533..5a64643c 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DarkSVGIcon.java @@ -27,7 +27,6 @@ import com.kitfox.svg.app.beans.SVGIcon; import javax.swing.*; import java.awt.*; -import java.awt.geom.AffineTransform; import java.io.Serializable; import java.net.URI; import java.util.concurrent.atomic.AtomicBoolean; @@ -108,8 +107,7 @@ public class DarkSVGIcon implements Icon, Serializable { Graphics2D g2 = (Graphics2D) g.create(); g2.translate(x, y); if (rotation != 0) { - g2.setTransform(AffineTransform.getRotateInstance(rotation, size.width / 2.0, - size.height / 2.0)); + g2.rotate(rotation, size.width / 2.0, size.height / 2.0); } icon.setPreferredSize(size); icon.paintIcon(c, g2, 0, 0); diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java index 6f2b1041..0d5d6ed4 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/RotatableIcon.java @@ -39,10 +39,9 @@ public class RotatableIcon implements Icon { this(null); } - public RotatableIcon(final Icon icon) { setIcon(icon); - this.alignment = null; + setOrientation(null); } public void setIcon(final Icon icon) { @@ -110,6 +109,6 @@ public class RotatableIcon implements Icon { } public void setOrientation(final Alignment alignment) { - this.alignment = alignment; + this.alignment = alignment != null ? alignment : Alignment.NORTH; } }