ComboBox: support rounded selection

This commit is contained in:
Karl Tauber
2022-06-05 00:45:39 +02:00
parent 8262793751
commit bf0685cee2
9 changed files with 52 additions and 15 deletions

View File

@@ -114,6 +114,9 @@ import com.formdev.flatlaf.util.SystemInfo;
* @uiDefault ComboBox.buttonHoverArrowColor Color
* @uiDefault ComboBox.buttonPressedArrowColor Color
* @uiDefault ComboBox.popupBackground Color optional
* @uiDefault ComboBox.popupInsets Insets
* @uiDefault ComboBox.selectionInsets Insets
* @uiDefault ComboBox.selectionArc int
*
* @author Karl Tauber
*/
@@ -144,6 +147,9 @@ public class FlatComboBoxUI
@Styleable protected Color buttonPressedArrowColor;
@Styleable protected Color popupBackground;
/** @since 3 */ @Styleable protected Insets popupInsets;
/** @since 3 */ @Styleable protected Insets selectionInsets;
/** @since 3 */ @Styleable protected int selectionArc;
private MouseListener hoverListener;
protected boolean hover;
@@ -239,6 +245,9 @@ public class FlatComboBoxUI
buttonPressedArrowColor = UIManager.getColor( "ComboBox.buttonPressedArrowColor" );
popupBackground = UIManager.getColor( "ComboBox.popupBackground" );
popupInsets = UIManager.getInsets( "ComboBox.popupInsets" );
selectionInsets = UIManager.getInsets( "ComboBox.selectionInsets" );
selectionArc = UIManager.getInt( "ComboBox.selectionArc" );
// set maximumRowCount
int maximumRowCount = UIManager.getInt( "ComboBox.maximumRowCount" );
@@ -818,17 +827,12 @@ public class FlatComboBoxUI
// make opaque to avoid that background shines thru border (e.g. at 150% scaling)
setOpaque( true );
// set popup border
// use non-UIResource to avoid that it is overwritten when making
// popup visible (see JPopupMenu.setInvoker()) in theme editor preview
// set popup border
// use non-UIResource to avoid that it is overwritten when making
// popup visible (see JPopupMenu.setInvoker()) in theme editor preview
Border border = UIManager.getBorder( "PopupMenu.border" );
if( border != null )
setBorder( FlatUIUtils.nonUIResource( border ) );
}
@Override
protected void configureList() {
super.configureList();
list.setCellRenderer( new PopupListCellRenderer() );
updateStyle();
@@ -836,12 +840,21 @@ public class FlatComboBoxUI
void updateStyle() {
if( popupBackground != null )
list.setBackground( popupBackground );
list.setBackground( popupBackground );
// set popup background because it may shine thru when scaled (e.g. at 150%)
// use non-UIResource to avoid that it is overwritten when making
// popup visible (see JPopupMenu.setInvoker()) in theme editor preview
setBackground( FlatUIUtils.nonUIResource( list.getBackground() ) );
// set popup background because it may shine thru when scaled (e.g. at 150%)
// use non-UIResource to avoid that it is overwritten when making
// popup visible (see JPopupMenu.setInvoker()) in theme editor preview
setBackground( FlatUIUtils.nonUIResource( list.getBackground() ) );
scroller.setViewportBorder( (popupInsets != null) ? new FlatEmptyBorder( popupInsets ) : null );
scroller.setOpaque( false );
if( list.getUI() instanceof FlatListUI ) {
FlatListUI ui = (FlatListUI) list.getUI();
ui.selectionInsets = selectionInsets;
ui.selectionArc = selectionArc;
}
}
@Override

View File

@@ -37,6 +37,7 @@ import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.plaf.basic.BasicListUI;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
@@ -306,7 +307,8 @@ public class FlatListUI
// rounded selection or selection insets
if( isSelected &&
!isFileList && // rounded selection is not supported for file list
rendererComponent instanceof DefaultListCellRenderer &&
(rendererComponent instanceof DefaultListCellRenderer ||
rendererComponent instanceof BasicComboBoxRenderer) &&
(selectionArc > 0 ||
(selectionInsets != null &&
(selectionInsets.top != 0 || selectionInsets.left != 0 || selectionInsets.bottom != 0 || selectionInsets.right != 0))) )

View File

@@ -285,6 +285,10 @@ ComboBox.buttonDisabledArrowColor = @buttonDisabledArrowColor
ComboBox.buttonHoverArrowColor = @buttonHoverArrowColor
ComboBox.buttonPressedArrowColor = @buttonPressedArrowColor
ComboBox.popupInsets = 0,0,0,0
ComboBox.selectionInsets = 0,0,0,0
ComboBox.selectionArc = 0
#---- Component ----

View File

@@ -173,7 +173,10 @@ public class TestFlatStyleableInfo
"buttonHoverArrowColor", Color.class,
"buttonPressedArrowColor", Color.class,
"popupBackground", Color.class
"popupBackground", Color.class,
"popupInsets", Insets.class,
"selectionInsets", Insets.class,
"selectionArc", int.class
);
// border

View File

@@ -304,6 +304,9 @@ public class TestFlatStyling
ui.applyStyle( "buttonPressedArrowColor: #fff" );
ui.applyStyle( "popupBackground: #fff" );
ui.applyStyle( "popupInsets: 1,2,3,4" );
ui.applyStyle( "selectionInsets: 1,2,3,4" );
ui.applyStyle( "selectionArc: 8" );
// border
flatRoundBorder( style -> ui.applyStyle( style ) );