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

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());
paintIcon(g, b, c);
paintText(g, b, c, text);
paintText(g, b, text);
config.restore();
}
@ -262,7 +262,6 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
@Override
protected void paintText(final Graphics g, final JComponent c,
final Rectangle textRect, final String text) {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
AbstractButton button = (AbstractButton) c;
ButtonModel model = button.getModel();
g.setColor(getForeground(button));
@ -273,12 +272,10 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + getTextShiftOffset());
config.restore();
}
protected void paintText(final Graphics g, final AbstractButton b, final JComponent c, final String text) {
PaintUtil.drawString(g, b, text, textRect, SwingUtilities2.getFontMetrics(b, g),
(g1, c1, r1, t1) -> paintText(g1, b, r1, t1));
protected void paintText(final Graphics g, final AbstractButton b, final String text) {
PaintUtil.drawString(g, b, text, textRect, SwingUtilities2.getFontMetrics(b, g), this::paintText);
}
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.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
@ -161,42 +163,26 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
rightAlignAccText(lh, lr);
if (!StringUtil.isBlank(lh.getAccText())) {
ButtonModel model = mi.getModel();
g.setFont(lh.getAccFontMetrics().getFont());
if (!model.isEnabled()) {
// *** paint the accText disabled
if (disabledForeground != null) {
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());
}
g.setColor(getAcceleratorForeground(mi));
SwingUtilities2.drawString(mi, g, lh.getAccText(), lr.getAccRect().x,
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
}
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,
final MenuItemLayoutHelper.LayoutResult lr, final Color holdc) {
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.plaf.ComponentUI;
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.graphics.GraphicsContext;
@ -110,6 +113,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
protected Color selectedBackground;
protected Color hoverBackground;
protected Color tabAreaBackground;
protected Color disabledForeground;
protected Icon moreTabsIcon;
protected Icon newTabIcon;
@ -170,6 +174,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
super.uninstallUI(c);
}
@Override
public void paint(final Graphics g, final JComponent c) {
int selectedIndex = tabPane.getSelectedIndex();
int tabPlacement = tabPane.getTabPlacement();
@ -203,9 +208,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
}
@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
protected void paintTabArea(final Graphics g, final int tabPlacement, final int selectedIndex) {
@ -371,13 +374,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
}
}
@Override
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) {
protected void paintTabAreaBorder(final Graphics g, final int tabPlacement) {
int width = tabPane.getWidth();
int height = tabPane.getHeight();
Insets ins = tabPane.getInsets();
@ -702,10 +699,38 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
final FontMetrics metrics, final int tabIndex, final String title,
final Rectangle textRect, final boolean isSelected) {
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();
}
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
protected void installDefaults() {
super.installDefaults();
@ -718,6 +743,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
selectedBackground = UIManager.getColor("TabbedPane.selectedBackground");
hoverBackground = UIManager.getColor("TabbedPane.hoverBackground");
tabAreaBackground = UIManager.getColor("TabbedPane.tabAreaBackground");
disabledForeground = UIManager.getColor("TabbedPane.disabledForeground");
focusSize = UIManager.getInt("TabbedPane.focusBarHeight");
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;
}
public 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 abstract void paint(final Graphics g, final JComponent c);
public Dimension getMinimumSize(final JComponent c) {
// Default to LayoutManager's minimumLayoutSize
@ -709,47 +690,10 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
* @param textRect the text rectangle
* @param isSelected selection status
*/
protected void paintText(final Graphics g, final int tabPlacement,
final Font font, final FontMetrics metrics, final int tabIndex,
final String title, final Rectangle textRect,
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);
}
}
}
protected abstract void paintText(final Graphics g, final int tabPlacement,
final Font font, final FontMetrics metrics, final int tabIndex,
final String title, final Rectangle textRect,
final boolean isSelected);
/**
* Paints an icon.
@ -882,15 +826,7 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
*
* @since 1.4
*/
protected void installComponents() {
if (scrollableTabLayoutEnabled()) {
if (tabScroller == null) {
tabScroller = new ScrollableTabSupport(this);
tabPane.add(tabScroller.viewport);
}
}
installTabContainer();
}
protected abstract void installComponents();
/**
* Install the defaults.
@ -1366,22 +1302,6 @@ public abstract class DarkTabbedPaneUIBridge extends TabbedPaneUI implements Swi
// 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.
*

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());
paintSlider((Graphics2D) g, b);
paintIcon(g, b, c);
paintText(g, b, c, text);
paintText(g, b, text);
config.restore();
} else {
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.selectedHoverBackground = %hoverHighlightSecondary
TabbedPane.tabAreaBackground = %background
TabbedPane.selectedForeground = %textForegroundHighlight
TabbedPane.disabledForeground = %textForegroundInactive
TabbedPane.focusBarHeight = 3
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");
controlPanel = panel.addControls();
controlPanel.add(new JCheckBox("enabled") {
{
setSelected(tabbedPane.isEnabled());
addActionListener(e -> tabbedPane.setEnabled(isSelected()));
}
});
controlPanel.add(new JCheckBox("LeftToRight") {
{
setSelected(tabbedPane.getComponentOrientation().isLeftToRight());

Loading…
Cancel
Save