Browse Source

Added LabelDemo.

Fixed incorrect foreground for disabled labels.

Signed-off-by: weisj <weisj@arcor.de>
pull/33/head
weisj 5 years ago
parent
commit
c23556c13f
  1. 1
      src/main/java/com/github/weisj/darklaf/icons/IconColorMapper.java
  2. 94
      src/main/java/com/github/weisj/darklaf/ui/label/DarkLabelUI.java
  3. 16
      src/main/java/com/github/weisj/darklaf/ui/tabframe/DarkTabFrameTabLabelUI.java
  4. 3
      src/main/resources/com/github/weisj/darklaf/properties/ui/label.properties
  5. 60
      src/test/java/ui/LabelDemo.java

1
src/main/java/com/github/weisj/darklaf/icons/IconColorMapper.java

@ -93,7 +93,6 @@ public final class IconColorMapper {
Stop stop1 = new Stop();
Stop stop2 = new Stop();
String color = toHexString(c);
System.out.println(c + " " + color);
try {
stop1.addAttribute("stop-color", AnimationElement.AT_XML, color);
stop1.addAttribute("offset", AnimationElement.AT_XML, "0");

94
src/main/java/com/github/weisj/darklaf/ui/label/DarkLabelUI.java

@ -25,28 +25,118 @@ package com.github.weisj.darklaf.ui.label;
import com.github.weisj.darklaf.util.GraphicsContext;
import com.github.weisj.darklaf.util.GraphicsUtil;
import sun.swing.SwingUtilities2;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicLabelUI;
import javax.swing.text.View;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
/**
* @author Jannis Weis
*/
public class DarkLabelUI extends BasicLabelUI {
public class DarkLabelUI extends BasicLabelUI implements PropertyChangeListener {
protected static final DarkLabelUI darkLabelUI = new DarkLabelUI();
private final Color inactiveForeground;
protected Rectangle paintIconR = new Rectangle();
protected Rectangle paintTextR = new Rectangle();
public DarkLabelUI() {
inactiveForeground = UIManager.getColor("Label.inactiveForeground");
}
public static ComponentUI createUI(final JComponent c) {
return darkLabelUI;
}
@Override
public void paint(final Graphics g, final JComponent c) {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
super.paint(g, c);
JLabel label = (JLabel) c;
String text = label.getText();
Icon icon = getIcon(label);
if ((icon == null) && (text == null)) {
return;
}
FontMetrics fm = SwingUtilities2.getFontMetrics(label, g);
String clippedText = layout(label, fm, c.getWidth(), c.getHeight());
if (icon != null) {
icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
}
if (text != null) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, paintTextR);
} else {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();
if (label.isEnabled()) {
paintEnabledText(label, g, clippedText, textX, textY);
} else {
paintDisabledText(label, g, clippedText, textX, textY);
}
}
}
config.restore();
}
@Override
protected void paintDisabledText(final JLabel l, final Graphics g, final String s,
final int textX, final int textY) {
int accChar = l.getDisplayedMnemonicIndex();
g.setColor(inactiveForeground);
SwingUtilities2.drawStringUnderlineCharAt(l, g, s, accChar,
textX, textY);
}
protected Icon getIcon(final JLabel label) {
Icon icon;
if (label.isEnabled()) {
icon = label.getIcon();
} else {
icon = label.getDisabledIcon();
}
return icon;
}
protected String layout(final JLabel label, final FontMetrics fm,
final int width, final int height) {
Insets insets = label.getInsets(null);
String text = label.getText();
Icon icon = getIcon(label);
Rectangle paintViewR = new Rectangle();
paintViewR.x = insets.left;
paintViewR.y = insets.top;
paintViewR.width = width - (insets.left + insets.right);
paintViewR.height = height - (insets.top + insets.bottom);
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
return layoutCL(label, fm, text, icon, paintViewR, paintIconR,
paintTextR);
}
@Override
public void propertyChange(final PropertyChangeEvent e) {
super.propertyChange(e);
String key = e.getPropertyName();
if ("componentOrientation".equals(key)) {
Object source = e.getSource();
if (source instanceof JLabel) {
((JLabel) source).doLayout();
((JLabel) source).repaint();
}
}
}
}

16
src/main/java/com/github/weisj/darklaf/ui/tabframe/DarkTabFrameTabLabelUI.java

@ -64,8 +64,6 @@ public class DarkTabFrameTabLabelUI extends DarkLabelUI implements PropertyChang
private Color selectedColor;
private Color hoverColor;
private RotatableIcon rotatableIcon = new RotatableIcon();
private Rectangle paintIconR = new Rectangle();
private Rectangle paintTextR = new Rectangle();
private boolean printing;
@ -258,20 +256,6 @@ public class DarkTabFrameTabLabelUI extends DarkLabelUI implements PropertyChang
return rotatableIcon;
}
private String layout(final JLabel label, final FontMetrics fm,
final int width, final int height) {
Insets insets = label.getInsets(null);
String text = label.getText();
Rectangle paintViewR = new Rectangle();
paintViewR.x = insets.left;
paintViewR.y = insets.top;
paintViewR.width = width - (insets.left + insets.right);
paintViewR.height = height - (insets.top + insets.bottom);
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
return layoutCL(label, fm, text, getIcon(), paintViewR, paintIconR, paintTextR);
}
protected Alignment mapOrientation(final Alignment newValue) {
switch (newValue) {
case CENTER:

3
src/main/resources/com/github/weisj/darklaf/properties/ui/label.properties

@ -22,4 +22,5 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
LabelUI = com.github.weisj.darklaf.ui.label.DarkLabelUI
LabelUI = com.github.weisj.darklaf.ui.label.DarkLabelUI
Label.inactiveForeground = %textForegroundInactive

60
src/test/java/ui/LabelDemo.java

@ -0,0 +1,60 @@
/*
* 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 ui;
import com.github.weisj.darklaf.icons.IconLoader;
import javax.swing.*;
import java.awt.*;
public class LabelDemo implements ComponentDemo {
public static void main(final String[] args) {
ComponentDemo.showDemo(new LabelDemo());
}
@Override
public JComponent createComponent() {
Icon icon = IconLoader.get().getIcon("files/folder.svg", 19, 19, true);
JLabel label = new JLabel("Test Label", icon, JLabel.LEFT);
DemoPanel panel = new DemoPanel(label);
JPanel controlPanel = panel.getControls();
controlPanel.setLayout(new GridLayout(2, 2));
controlPanel.add(new JCheckBox("enabled") {{
setSelected(label.isEnabled());
addActionListener(e -> label.setEnabled(isSelected()));
}});
controlPanel.add(new JCheckBox("LeftToRight") {{
setSelected(label.getComponentOrientation().isLeftToRight());
addActionListener(e -> label.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT
: ComponentOrientation.RIGHT_TO_LEFT));
}});
return panel;
}
@Override
public String getTitle() {
return "Label Demo";
}
}
Loading…
Cancel
Save