From 2b50431081b55f6ef1aff1bb276ac9a702a2c6ef Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 21 Oct 2020 11:24:06 +0200 Subject: [PATCH] TabbedPane: fixed scaling of client property "JTabbedPane.tabHeight"; avoid storing scaled values in UI delegate --- .../formdev/flatlaf/ui/FlatTabbedPaneUI.java | 41 ++++++++++++++----- .../flatlaf/testing/FlatContainerTest.java | 2 +- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java index ec86fcd7..e2d79a14 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java @@ -97,7 +97,7 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault TabbedPane.shadow Color used for scroll arrows and cropped line * @uiDefault TabbedPane.textIconGap int * @uiDefault TabbedPane.tabInsets Insets - * @uiDefault TabbedPane.selectedTabPadInsets Insets + * @uiDefault TabbedPane.selectedTabPadInsets Insets unused * @uiDefault TabbedPane.tabAreaInsets Insets * @uiDefault TabbedPane.tabsOverlapBorder boolean * @uiDefault TabbedPane.tabRunOverlay int @@ -152,6 +152,7 @@ public class FlatTabbedPaneUI protected Color tabSeparatorColor; protected Color contentAreaColor; + private int textIconGapUnscaled; protected int tabHeight; protected int tabSelectionHeight; protected int contentSeparatorHeight; @@ -215,6 +216,7 @@ public class FlatTabbedPaneUI tabSeparatorColor = UIManager.getColor( "TabbedPane.tabSeparatorColor" ); contentAreaColor = UIManager.getColor( "TabbedPane.contentAreaColor" ); + textIconGapUnscaled = UIManager.getInt( "TabbedPane.textIconGap" ); tabHeight = UIManager.getInt( "TabbedPane.tabHeight" ); tabSelectionHeight = UIManager.getInt( "TabbedPane.tabSelectionHeight" ); contentSeparatorHeight = UIManager.getInt( "TabbedPane.contentSeparatorHeight" ); @@ -228,12 +230,7 @@ public class FlatTabbedPaneUI moreTabsButtonToolTipText = UIManager.getString( "TabbedPane.moreTabsButtonToolTipText", l ); // scale - textIconGap = scale( textIconGap ); - tabInsets = scale( tabInsets ); - selectedTabPadInsets = scale( selectedTabPadInsets ); - tabAreaInsets = scale( tabAreaInsets ); - tabHeight = scale( tabHeight ); - tabSelectionHeight = scale( tabSelectionHeight ); + textIconGap = scale( textIconGapUnscaled ); // replace focus forward/backward traversal keys with TAB/Shift+TAB because // the default also includes Ctrl+TAB/Ctrl+Shift+TAB, which we need to switch tabs @@ -517,6 +514,9 @@ public class FlatTabbedPaneUI @Override protected int calculateTabWidth( int tabPlacement, int tabIndex, FontMetrics metrics ) { + // update textIconGap before used in super class + textIconGap = scale( textIconGapUnscaled ); + int tabWidth = super.calculateTabWidth( tabPlacement, tabIndex, metrics ) - 3 /* was added by superclass */; if( isTabClosable( tabIndex ) ) tabWidth += closeIcon.getIconWidth(); @@ -525,16 +525,21 @@ public class FlatTabbedPaneUI @Override protected int calculateTabHeight( int tabPlacement, int tabIndex, int fontHeight ) { - int tabHeight = clientPropertyInt( tabPane, TABBED_PANE_TAB_HEIGHT, this.tabHeight ); + int tabHeight = scale( clientPropertyInt( tabPane, TABBED_PANE_TAB_HEIGHT, this.tabHeight ) ); return Math.max( tabHeight, super.calculateTabHeight( tabPlacement, tabIndex, fontHeight ) - 2 /* was added by superclass */ ); } @Override protected Insets getTabInsets( int tabPlacement, int tabIndex ) { Object value = getTabClientProperty( tabIndex, TABBED_PANE_TAB_INSETS ); - return (value instanceof Insets) - ? scale( (Insets) value ) - : super.getTabInsets( tabPlacement, tabIndex ); + return scale( (value instanceof Insets) + ? (Insets) value + : super.getTabInsets( tabPlacement, tabIndex ) ); + } + + @Override + protected Insets getSelectedTabPadInsets( int tabPlacement ) { + return new Insets( 0, 0, 0, 0 ); } @Override @@ -549,6 +554,9 @@ public class FlatTabbedPaneUI // Giving it large values clips painting of the cropped edge and makes it invisible. currentTabAreaInsets.top = currentTabAreaInsets.left = -10000; + // scale insets (before adding leading/trailing component sizes) + insets = scale( insets ); + // increase insets for wrap layout if using leading/trailing components if( tabPane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT ) { if( leadingComponent != null ) { @@ -765,6 +773,7 @@ public class FlatTabbedPaneUI Insets contentInsets = getContentBorderInsets( tabPlacement ); // paint underline selection + int tabSelectionHeight = scale( this.tabSelectionHeight ); switch( tabPlacement ) { case TOP: default: @@ -871,6 +880,16 @@ public class FlatTabbedPaneUI { } + @Override + protected void layoutLabel( int tabPlacement, FontMetrics metrics, int tabIndex, String title, Icon icon, + Rectangle tabRect, Rectangle iconRect, Rectangle textRect, boolean isSelected ) + { + // update textIconGap before used in super class + textIconGap = scale( textIconGapUnscaled ); + + super.layoutLabel( tabPlacement, metrics, tabIndex, title, icon, tabRect, iconRect, textRect, isSelected ); + } + @Override public int tabForCoordinate( JTabbedPane pane, int x, int y ) { if( moreTabsButton != null ) { diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatContainerTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatContainerTest.java index 6326e004..dee6b937 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatContainerTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatContainerTest.java @@ -300,7 +300,7 @@ public class FlatContainerTest } private void smallerTabHeightChanged() { - Integer tabHeight = smallerTabHeightCheckBox.isSelected() ? 20 : null; + Integer tabHeight = smallerTabHeightCheckBox.isSelected() ? 26 : null; putTabbedPanesClientProperty( TABBED_PANE_TAB_HEIGHT, tabHeight ); }