Window decorations: embedded menu bar did not always respond to mouse events after adding menus and when running in JetBrains Runtime (issue #151)

This commit is contained in:
Karl Tauber
2020-08-06 11:45:47 +02:00
parent 368fbcdeb0
commit b0426b81a7
5 changed files with 117 additions and 8 deletions

View File

@@ -1,6 +1,14 @@
FlatLaf Change Log
==================
## Unreleased
#### Fixed bugs
- Custom window decorations: Embedded menu bar did not always respond to mouse
events after adding menus and when running in JetBrains Runtime. (issue #151)
## 0.39
#### New features

View File

@@ -304,6 +304,9 @@ public class FlatRootPaneUI
Container contentPane = rootPane.getContentPane();
if( contentPane != null )
contentPane.setBounds( 0, nextY, width, Math.max( height - nextY, 0 ) );
if( titlePane != null )
titlePane.menuBarLayouted();
}
@Override

View File

@@ -391,6 +391,12 @@ public class FlatTitlePane
return FlatUIUtils.subtractInsets( bounds, UIScale.scale( getMenuBarMargins() ) );
}
protected Insets getMenuBarMargins() {
return getComponentOrientation().isLeftToRight()
? menuBarMargins
: new Insets( menuBarMargins.top, menuBarMargins.right, menuBarMargins.bottom, menuBarMargins.left );
}
protected void menuBarChanged() {
menuBarPlaceholder.invalidate();
@@ -400,12 +406,27 @@ public class FlatTitlePane
} );
}
protected Insets getMenuBarMargins() {
return getComponentOrientation().isLeftToRight()
? menuBarMargins
: new Insets( menuBarMargins.top, menuBarMargins.right, menuBarMargins.bottom, menuBarMargins.left );
protected void menuBarLayouted() {
updateJBRHitTestSpotsAndTitleBarHeightLater();
}
/*debug
@Override
public void paint( Graphics g ) {
super.paint( g );
if( debugTitleBarHeight > 0 ) {
g.setColor( Color.green );
g.drawLine( 0, debugTitleBarHeight, getWidth(), debugTitleBarHeight );
}
if( debugHitTestSpots != null ) {
g.setColor( Color.blue );
for( Rectangle r : debugHitTestSpots )
g.drawRect( r.x, r.y, r.width, r.height );
}
}
debug*/
@Override
protected void paintComponent( Graphics g ) {
g.setColor( getBackground() );
@@ -583,6 +604,12 @@ public class FlatTitlePane
titleBarHeight--;
JBRCustomDecorations.setHitTestSpotsAndTitleBarHeight( window, hitTestSpots, titleBarHeight );
/*debug
debugHitTestSpots = hitTestSpots;
debugTitleBarHeight = titleBarHeight;
repaint();
debug*/
}
protected void addJBRHitTestSpot( JComponent c, boolean subtractMenuBarMargins, List<Rectangle> hitTestSpots ) {
@@ -599,6 +626,11 @@ public class FlatTitlePane
hitTestSpots.add( r );
}
/*debug
private List<Rectangle> debugHitTestSpots;
private int debugTitleBarHeight;
debug*/
//---- class TitlePaneBorder ----------------------------------------------
protected class FlatTitlePaneBorder

View File

@@ -22,6 +22,7 @@ import java.awt.event.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import com.formdev.flatlaf.FlatClientProperties;
import net.miginfocom.swing.*;
@@ -117,6 +118,32 @@ public class FlatWindowDecorationsTest
rootPane.putClientProperty( FlatClientProperties.MENU_BAR_EMBEDDED, menuBarEmbeddedCheckBox.isSelected() );
}
private void addMenu() {
JMenu menu = new JMenu( "Hello" );
menu.add( new JMenuItem( "world" ) );
menuBar.add( menu );
menuBar.revalidate();
}
private void removeMenu() {
int menuCount = menuBar.getMenuCount();
if( menuCount <= 0 )
return;
menuBar.remove( menuCount - 1 );
menuBar.revalidate();
}
private void changeMenu() {
int menuCount = menuBar.getMenuCount();
if( menuCount <= 0 )
return;
int len = new Random().nextInt( 20 );
String text = "1234567890abcdefghij".substring( 0, len + 1 );
menuBar.getMenu( menuCount - 1 ).setText( text );
}
private void resizableChanged() {
Window window = SwingUtilities.windowForComponent( this );
if( window instanceof Frame )
@@ -218,6 +245,9 @@ public class FlatWindowDecorationsTest
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
menuBarCheckBox = new JCheckBox();
JButton addMenuButton = new JButton();
JButton removeMenuButton = new JButton();
JButton changeMenuButton = new JButton();
menuBarEmbeddedCheckBox = new JCheckBox();
resizableCheckBox = new JCheckBox();
maximizedBoundsCheckBox = new JCheckBox();
@@ -271,7 +301,7 @@ public class FlatWindowDecorationsTest
"ltr,insets dialog,hidemode 3",
// columns
"[left]para" +
"[fill]",
"[left]",
// rows
"para[]0" +
"[]0" +
@@ -287,6 +317,21 @@ public class FlatWindowDecorationsTest
menuBarCheckBox.addActionListener(e -> menuBarChanged());
add(menuBarCheckBox, "cell 0 0");
//---- addMenuButton ----
addMenuButton.setText("Add menu");
addMenuButton.addActionListener(e -> addMenu());
add(addMenuButton, "cell 1 0 1 2,align left top,grow 0 0");
//---- removeMenuButton ----
removeMenuButton.setText("Remove menu");
removeMenuButton.addActionListener(e -> removeMenu());
add(removeMenuButton, "cell 1 0 1 2,align left top,grow 0 0");
//---- changeMenuButton ----
changeMenuButton.setText("Change menu");
changeMenuButton.addActionListener(e -> changeMenu());
add(changeMenuButton, "cell 1 0 1 2,align left top,grow 0 0");
//---- menuBarEmbeddedCheckBox ----
menuBarEmbeddedCheckBox.setText("embedded menu bar");
menuBarEmbeddedCheckBox.setSelected(true);

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.2.0.298" Java: "13.0.2" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.2.0.298" Java: "14" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -8,7 +8,7 @@ new FormModel {
}
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[left]para[fill]"
"$columnConstraints": "[left]para[left]"
"$rowConstraints": "para[]0[]0[]0[][][top][]"
} ) {
name: "this"
@@ -23,6 +23,27 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "addMenuButton"
"text": "Add menu"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "addMenu", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0 1 2,align left top,grow 0 0"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "removeMenuButton"
"text": "Remove menu"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "removeMenu", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0 1 2,align left top,grow 0 0"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "changeMenuButton"
"text": "Change menu"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "changeMenu", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0 1 2,align left top,grow 0 0"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "menuBarEmbeddedCheckBox"
"text": "embedded menu bar"
@@ -238,7 +259,7 @@ new FormModel {
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 450, 380 )
"size": new java.awt.Dimension( 550, 380 )
} )
add( new FormContainer( "javax.swing.JMenuBar", new FormLayoutManager( class javax.swing.JMenuBar ) ) {
name: "menuBar"