From 2d9d120884302cd5a6b09f47456e7898017351ad Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 28 Apr 2020 08:43:43 +0200 Subject: [PATCH] Refactored string painting. Added missing disabled foreground for tabbed pane. --- .../weisj/darklaf/graphics/PaintUtil.java | 15 +-- .../weisj/darklaf/ui/button/DarkButtonUI.java | 9 +- .../darklaf/ui/menu/DarkMenuItemUIBase.java | 50 ++++------ .../ui/tabbedpane/DarkTabbedPaneUI.java | 48 +++++++--- .../ui/tabbedpane/DarkTabbedPaneUIBridge.java | 92 ++----------------- .../ui/togglebutton/DarkToggleButtonUI.java | 2 +- .../properties/ui/tabbedPane.properties | 2 + .../java/ui/tabbedPane/TabbedPaneDemo.java | 6 ++ 8 files changed, 81 insertions(+), 143 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/graphics/PaintUtil.java b/core/src/main/java/com/github/weisj/darklaf/graphics/PaintUtil.java index b91f7221..f297ef0d 100644 --- a/core/src/main/java/com/github/weisj/darklaf/graphics/PaintUtil.java +++ b/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 void drawString(final Graphics g, final T c, + final String text, final Rectangle textRect, + final FontMetrics fm, + final PaintMethod 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 { - 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 { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java index e51dc8f0..c7a233f0 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java +++ b/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) { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java b/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java index 4997eda0..abc48239 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java +++ b/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) { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java index 7f0fd708..df19c07a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java +++ b/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"); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUIBridge.java b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUIBridge.java index b335351f..57642cb2 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUIBridge.java +++ b/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. * diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java index cb572ad2..d6cf1c8a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java +++ b/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); diff --git a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties index 48b24c7a..55b41f03 100644 --- a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties +++ b/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 diff --git a/core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java b/core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java index d801bb20..7ce1c38a 100644 --- a/core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java +++ b/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());