From 17ce6d39b410faf0a05ed45f81cf755f005fb9cb Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 30 May 2024 17:20:21 +0200 Subject: [PATCH] Button and ToggleButton: UI properties `[Toggle]Button.selectedForeground` and `[Toggle]Button.pressedForeground` did not work for HTML text (issue #848) --- CHANGELOG.md | 2 ++ .../com/formdev/flatlaf/ui/FlatButtonUI.java | 13 +++++--- .../java/com/formdev/flatlaf/ui/FlatHTML.java | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b551a075..99255261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ FlatLaf Change Log - FlatLaf window decorations: Window top border on Windows 10 in "full window content" mode was not fully repainted when activating or deactivating window. (issue #809) +- Button and ToggleButton: UI properties `[Toggle]Button.selectedForeground` and + `[Toggle]Button.pressedForeground` did not work for HTML text. (issue #848) - HTML: Fixed font sizes for HTML tags `

`...`

`, ``, ``, ``, `` and `` in HTML text for components Button, CheckBox, RadioButton, MenuItem (and subclasses), JideLabel, JideButton, JXBusyLabel and diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java index 68935243..20101854 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java @@ -587,9 +587,16 @@ public class FlatButtonUI // paint text if( clippedText != null && !clippedText.isEmpty() ) { View view = (View) b.getClientProperty( BasicHTML.propertyKey ); - if( view != null ) + if( view != null ) { + // update foreground color in HTML view, which is necessary + // for selected and pressed states + // (only for enabled buttons, because UIManager.getColor("textInactiveText") + // is used for disabled components; see: javax.swing.text.GlyphView.paint()) + if( b.isEnabled() ) + FlatHTML.updateRendererCSSForeground( view, getForeground( b ) ); + view.paint( g, textR ); // HTML text - else + } else paintText( g, b, textR, clippedText ); } @@ -635,8 +642,6 @@ public class FlatButtonUI } public static void paintText( Graphics g, AbstractButton b, Rectangle textRect, String text, Color foreground ) { - if(foreground == null) - foreground=Color.red; FontMetrics fm = b.getFontMetrics( b.getFont() ); int mnemonicIndex = FlatLaf.isShowMnemonics() ? b.getDisplayedMnemonicIndex() : -1; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java index a7bd0087..f4a87666 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java @@ -16,12 +16,15 @@ package com.formdev.flatlaf.ui; +import java.awt.Color; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JComponent; import javax.swing.plaf.basic.BasicHTML; import javax.swing.text.Document; import javax.swing.text.LabelView; +import javax.swing.text.Style; +import javax.swing.text.StyleConstants; import javax.swing.text.View; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.StyleSheet; @@ -70,6 +73,34 @@ public class FlatHTML // dumpViews( view, 0 ); } + /** + * Updates foreground in style sheet of the HTML view. + * Adds "body { color: #; }" + */ + public static void updateRendererCSSForeground( View view, Color foreground ) { + Document doc = view.getDocument(); + if( !(doc instanceof HTMLDocument) || foreground == null ) + return; + + // add foreground rule if necessary + // - use tag 'body' because BasicHTML.createHTMLView() also uses this tag + // to set font and color styles to component font/color + // see: SwingUtilities2.displayPropertiesToCSS() + // - this color is not used if component is disabled; + // JTextComponent.getDisabledTextColor() is used for disabled text components; + // UIManager.getColor("textInactiveText") is used for other disabled components + // see: javax.swing.text.GlyphView.paint() + Style bodyStyle = ((HTMLDocument)doc).getStyle( "body" ); + if( bodyStyle == null ) { + StyleSheet styleSheet = ((HTMLDocument)doc).getStyleSheet(); + styleSheet.addRule( String.format( "body { color: #%06x; }", foreground.getRGB() & 0xffffff ) ); + clearViewCaches( view ); + } else if( !foreground.equals( bodyStyle.getAttribute( StyleConstants.Foreground ) ) ) { + bodyStyle.addAttribute( StyleConstants.Foreground, foreground ); + clearViewCaches( view ); + } + } + /** * Clears cached values in view so that CSS changes take effect. */