Browse Source

Refactored string painting.

Added missing disabled foreground for tabbed pane.
pull/154/head
weisj 5 years ago
parent
commit
2d9d120884
  1. 15
      core/src/main/java/com/github/weisj/darklaf/graphics/PaintUtil.java
  2. 9
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java
  3. 50
      core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java
  4. 48
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java
  5. 92
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUIBridge.java
  6. 2
      core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java
  7. 2
      core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties
  8. 6
      core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java

15
core/src/main/java/com/github/weisj/darklaf/graphics/PaintUtil.java

@ -33,6 +33,7 @@ import javax.swing.*;
import javax.swing.text.View; import javax.swing.text.View;
import com.github.weisj.darklaf.ui.html.DarkHTML; import com.github.weisj.darklaf.ui.html.DarkHTML;
import com.github.weisj.darklaf.util.PropertyUtil;
public class PaintUtil { public class PaintUtil {
@ -235,14 +236,14 @@ public class PaintUtil {
g.fillRect(x, y + height - thickness, width, thickness); g.fillRect(x, y + height - thickness, width, thickness);
} }
public static void drawString(final Graphics g, final JComponent c, public static <T extends JComponent> void drawString(final Graphics g, final T c,
final String text, final Rectangle textRect, final String text, final Rectangle textRect,
final FontMetrics fm, final FontMetrics fm,
final PaintMethod paintMethod) { final PaintMethod<T> paintMethod) {
GraphicsContext context = GraphicsUtil.setupAntialiasing(g); GraphicsContext context = GraphicsUtil.setupAntialiasing(g);
g.setClip(textRect); g.setClip(textRect);
if (text != null && !text.equals("")) { if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(DarkHTML.propertyKey); View v = PropertyUtil.getObject(c, DarkHTML.propertyKey, View.class);
if (v != null) { if (v != null) {
v.paint(g, textRect); v.paint(g, textRect);
} else { } else {
@ -253,9 +254,9 @@ public class PaintUtil {
context.restore(); context.restore();
} }
public interface PaintMethod { public interface PaintMethod<T extends JComponent> {
void paintText(final Graphics g, final JComponent c, final Rectangle rect, final String text); void paintText(final Graphics g, final T c, final Rectangle rect, final String text);
} }
public enum Outline { public enum Outline {

9
core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java

@ -165,7 +165,7 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
String text = layout(b, c, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight()); String text = layout(b, c, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight());
paintIcon(g, b, c); paintIcon(g, b, c);
paintText(g, b, c, text); paintText(g, b, text);
config.restore(); config.restore();
} }
@ -262,7 +262,6 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
@Override @Override
protected void paintText(final Graphics g, final JComponent c, protected void paintText(final Graphics g, final JComponent c,
final Rectangle textRect, final String text) { final Rectangle textRect, final String text) {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
AbstractButton button = (AbstractButton) c; AbstractButton button = (AbstractButton) c;
ButtonModel model = button.getModel(); ButtonModel model = button.getModel();
g.setColor(getForeground(button)); g.setColor(getForeground(button));
@ -273,12 +272,10 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(), textRect.x + getTextShiftOffset(),
textRect.y + getTextShiftOffset()); textRect.y + getTextShiftOffset());
config.restore();
} }
protected void paintText(final Graphics g, final AbstractButton b, final JComponent c, final String text) { protected void paintText(final Graphics g, final AbstractButton b, final String text) {
PaintUtil.drawString(g, b, text, textRect, SwingUtilities2.getFontMetrics(b, g), PaintUtil.drawString(g, b, text, textRect, SwingUtilities2.getFontMetrics(b, g), this::paintText);
(g1, c1, r1, t1) -> paintText(g1, b, r1, t1));
} }
protected void paintIcon(final Graphics g, final AbstractButton b, final JComponent c) { protected void paintIcon(final Graphics g, final AbstractButton b, final JComponent c) {

50
core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java

@ -37,7 +37,9 @@ import sun.swing.UIAction;
import com.github.weisj.darklaf.graphics.GraphicsContext; import com.github.weisj.darklaf.graphics.GraphicsContext;
import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.GraphicsUtil;
import com.github.weisj.darklaf.util.*; import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.LazyActionMap;
import com.github.weisj.darklaf.util.StringUtil;
/** /**
* @author Konstantin Bulenkov * @author Konstantin Bulenkov
@ -161,42 +163,26 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g); GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
rightAlignAccText(lh, lr); rightAlignAccText(lh, lr);
if (!StringUtil.isBlank(lh.getAccText())) { if (!StringUtil.isBlank(lh.getAccText())) {
ButtonModel model = mi.getModel();
g.setFont(lh.getAccFontMetrics().getFont()); g.setFont(lh.getAccFontMetrics().getFont());
if (!model.isEnabled()) { g.setColor(getAcceleratorForeground(mi));
// *** paint the accText disabled SwingUtilities2.drawString(mi, g, lh.getAccText(), lr.getAccRect().x,
if (disabledForeground != null) { lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
g.setColor(disabledForeground);
SwingUtilities2.drawString(mi, g,
lh.getAccText(), lr.getAccRect().x,
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
} else {
g.setColor(mi.getBackground().brighter());
SwingUtilities2.drawString(mi, g,
lh.getAccText(), lr.getAccRect().x,
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
g.setColor(mi.getBackground().darker());
SwingUtilities2.drawString(mi, g,
lh.getAccText(), lr.getAccRect().x - 1,
lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
}
} else {
// *** paint the accText normally
if (model.isArmed()
|| (mi instanceof JMenu
&& model.isSelected())) {
g.setColor(acceleratorSelectionForeground);
} else {
g.setColor(acceleratorForeground);
}
SwingUtilities2.drawString(mi, g, lh.getAccText(),
lr.getAccRect().x, lr.getAccRect().y +
lh.getAccFontMetrics().getAscent());
}
} }
config.restore(); config.restore();
} }
protected Color getAcceleratorForeground(final AbstractButton b) {
ButtonModel model = b.getModel();
if (!model.isEnabled()) return disabledForeground;
if (model.isArmed()
|| (b instanceof JMenu
&& model.isSelected())) {
return acceleratorSelectionForeground;
} else {
return acceleratorForeground;
}
}
protected void paintIcon(final Graphics g, final JMenuItem mi, final MenuItemLayoutHelper lh, protected void paintIcon(final Graphics g, final JMenuItem mi, final MenuItemLayoutHelper lh,
final MenuItemLayoutHelper.LayoutResult lr, final Color holdc) { final MenuItemLayoutHelper.LayoutResult lr, final Color holdc) {
if (lh.getIcon() != null) { if (lh.getIcon() != null) {

48
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java

@ -35,6 +35,9 @@ import java.util.function.Consumer;
import javax.swing.*; import javax.swing.*;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource; import javax.swing.plaf.UIResource;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;
import com.github.weisj.darklaf.components.uiresource.UIResourceWrapper; import com.github.weisj.darklaf.components.uiresource.UIResourceWrapper;
import com.github.weisj.darklaf.graphics.GraphicsContext; import com.github.weisj.darklaf.graphics.GraphicsContext;
@ -110,6 +113,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
protected Color selectedBackground; protected Color selectedBackground;
protected Color hoverBackground; protected Color hoverBackground;
protected Color tabAreaBackground; protected Color tabAreaBackground;
protected Color disabledForeground;
protected Icon moreTabsIcon; protected Icon moreTabsIcon;
protected Icon newTabIcon; protected Icon newTabIcon;
@ -170,6 +174,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
super.uninstallUI(c); super.uninstallUI(c);
} }
@Override
public void paint(final Graphics g, final JComponent c) { public void paint(final Graphics g, final JComponent c) {
int selectedIndex = tabPane.getSelectedIndex(); int selectedIndex = tabPane.getSelectedIndex();
int tabPlacement = tabPane.getTabPlacement(); int tabPlacement = tabPane.getTabPlacement();
@ -203,9 +208,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
} }
@Override @Override
protected void paintContentBorder(final Graphics g, final int tabPlacement, final int selectedIndex) { protected void paintContentBorder(final Graphics g, final int tabPlacement, final int selectedIndex) {}
}
@Override @Override
protected void paintTabArea(final Graphics g, final int tabPlacement, final int selectedIndex) { protected void paintTabArea(final Graphics g, final int tabPlacement, final int selectedIndex) {
@ -371,13 +374,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
} }
} }
@Override protected void paintTabAreaBorder(final Graphics g, final int tabPlacement) {
protected void paintFocusIndicator(final Graphics g, final int tabPlacement,
final Rectangle[] rects, final int tabIndex,
final Rectangle iconRect, final Rectangle textRect,
final boolean isSelected) {}
private void paintTabAreaBorder(final Graphics g, final int tabPlacement) {
int width = tabPane.getWidth(); int width = tabPane.getWidth();
int height = tabPane.getHeight(); int height = tabPane.getHeight();
Insets ins = tabPane.getInsets(); Insets ins = tabPane.getInsets();
@ -702,10 +699,38 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
final FontMetrics metrics, final int tabIndex, final String title, final FontMetrics metrics, final int tabIndex, final String title,
final Rectangle textRect, final boolean isSelected) { final Rectangle textRect, final boolean isSelected) {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g); GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
super.paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected); g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
v.paint(g, textRect);
} else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
g.setColor(getTabForeground(tabIndex, isSelected));
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
}
config.restore(); config.restore();
} }
protected Color getTabForeground(final int index, final boolean isSelected) {
if (tabPane.isEnabled() && tabPane.isEnabledAt(index)) {
Color fg = tabPane.getForegroundAt(index);
if (isSelected && (fg instanceof UIResource)) {
Color selectedFG = selectedForeground;
if (selectedFG != null) {
fg = selectedFG;
}
}
return fg;
} else {
return disabledForeground;
}
}
@Override @Override
protected void installDefaults() { protected void installDefaults() {
super.installDefaults(); super.installDefaults();
@ -718,6 +743,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
selectedBackground = UIManager.getColor("TabbedPane.selectedBackground"); selectedBackground = UIManager.getColor("TabbedPane.selectedBackground");
hoverBackground = UIManager.getColor("TabbedPane.hoverBackground"); hoverBackground = UIManager.getColor("TabbedPane.hoverBackground");
tabAreaBackground = UIManager.getColor("TabbedPane.tabAreaBackground"); tabAreaBackground = UIManager.getColor("TabbedPane.tabAreaBackground");
disabledForeground = UIManager.getColor("TabbedPane.disabledForeground");
focusSize = UIManager.getInt("TabbedPane.focusBarHeight"); focusSize = UIManager.getInt("TabbedPane.focusBarHeight");
moreTabsIcon = UIManager.getIcon("TabbedPane.moreTabs.icon"); moreTabsIcon = UIManager.getIcon("TabbedPane.moreTabs.icon");

92
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUIBridge.java

@ -390,26 +390,7 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
tabContainer = null; tabContainer = null;
} }
public void paint(final Graphics g, final JComponent c) { public abstract void paint(final Graphics g, final JComponent c);
int selectedIndex = tabPane.getSelectedIndex();
int tabPlacement = tabPane.getTabPlacement();
ensureCurrentLayout();
// Paint content border and tab area
if (tabsOverlapBorder) {
paintContentBorder(g, tabPlacement, selectedIndex);
}
// If scrollable tabs are enabled, the tab area will be
// painted by the scrollable tab panel instead.
//
if (!scrollableTabLayoutEnabled()) { // WRAP_TAB_LAYOUT
paintTabArea(g, tabPlacement, selectedIndex);
}
if (!tabsOverlapBorder) {
paintContentBorder(g, tabPlacement, selectedIndex);
}
}
public Dimension getMinimumSize(final JComponent c) { public Dimension getMinimumSize(final JComponent c) {
// Default to LayoutManager's minimumLayoutSize // Default to LayoutManager's minimumLayoutSize
@ -709,47 +690,10 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
* @param textRect the text rectangle * @param textRect the text rectangle
* @param isSelected selection status * @param isSelected selection status
*/ */
protected void paintText(final Graphics g, final int tabPlacement, protected abstract void paintText(final Graphics g, final int tabPlacement,
final Font font, final FontMetrics metrics, final int tabIndex, final Font font, final FontMetrics metrics, final int tabIndex,
final String title, final Rectangle textRect, final String title, final Rectangle textRect,
final boolean isSelected) { final boolean isSelected);
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
v.paint(g, textRect);
} else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
Color fg = tabPane.getForegroundAt(tabIndex);
if (isSelected && (fg instanceof UIResource)) {
Color selectedFG = selectedForeground;
if (selectedFG != null) {
fg = selectedFG;
}
}
g.setColor(fg);
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
} else { // tab disabled
g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
title, mnemIndex,
textRect.x - 1, textRect.y + metrics.getAscent() - 1);
}
}
}
/** /**
* Paints an icon. * Paints an icon.
@ -882,15 +826,7 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
* *
* @since 1.4 * @since 1.4
*/ */
protected void installComponents() { protected abstract void installComponents();
if (scrollableTabLayoutEnabled()) {
if (tabScroller == null) {
tabScroller = new ScrollableTabSupport(this);
tabPane.add(tabScroller.viewport);
}
}
installTabContainer();
}
/** /**
* Install the defaults. * Install the defaults.
@ -1366,22 +1302,6 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
// BasicTabbedPaneUI methods // BasicTabbedPaneUI methods
/**
* Paint focus indicator.
*
* @param g the g
* @param tabPlacement the tab placement
* @param rects the rects
* @param tabIndex the tab index
* @param iconRect the icon rect
* @param textRect the text rect
* @param isSelected the is selected
*/
protected abstract void paintFocusIndicator(final Graphics g, final int tabPlacement,
final Rectangle[] rects, final int tabIndex,
final Rectangle iconRect, final Rectangle textRect,
final boolean isSelected);
/** /**
* Assure the rectangles are created. * Assure the rectangles are created.
* *

2
core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java

@ -90,7 +90,7 @@ public class DarkToggleButtonUI extends DarkButtonUI implements ToggleButtonCons
String text = layout(b, c, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight()); String text = layout(b, c, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight());
paintSlider((Graphics2D) g, b); paintSlider((Graphics2D) g, b);
paintIcon(g, b, c); paintIcon(g, b, c);
paintText(g, b, c, text); paintText(g, b, text);
config.restore(); config.restore();
} else { } else {
super.paint(g, c); super.paint(g, c);

2
core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties

@ -36,6 +36,8 @@ TabbedPane.focus = %backgroundSelected
TabbedPane.hoverBackground = %backgroundHover TabbedPane.hoverBackground = %backgroundHover
TabbedPane.selectedHoverBackground = %hoverHighlightSecondary TabbedPane.selectedHoverBackground = %hoverHighlightSecondary
TabbedPane.tabAreaBackground = %background TabbedPane.tabAreaBackground = %background
TabbedPane.selectedForeground = %textForegroundHighlight
TabbedPane.disabledForeground = %textForegroundInactive
TabbedPane.focusBarHeight = 3 TabbedPane.focusBarHeight = 3
TabbedPane.contentBorderInsets = 0,0,0,0 TabbedPane.contentBorderInsets = 0,0,0,0

6
core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java

@ -96,6 +96,12 @@ public class TabbedPaneDemo implements ComponentDemo {
}, "sgx"); }, "sgx");
controlPanel = panel.addControls(); controlPanel = panel.addControls();
controlPanel.add(new JCheckBox("enabled") {
{
setSelected(tabbedPane.isEnabled());
addActionListener(e -> tabbedPane.setEnabled(isSelected()));
}
});
controlPanel.add(new JCheckBox("LeftToRight") { controlPanel.add(new JCheckBox("LeftToRight") {
{ {
setSelected(tabbedPane.getComponentOrientation().isLeftToRight()); setSelected(tabbedPane.getComponentOrientation().isLeftToRight());

Loading…
Cancel
Save