Skip to content

Commit

Permalink
SubMenuUsabilityHelper: added system property `flatlaf.useSubMenuSafe…
Browse files Browse the repository at this point in the history
…Triangle` to allow disabling submenu safe triangle for SWTSwing (issue #870)

also check whether EventQueue.push() succeeded; if not, disable submenu safe triangle
  • Loading branch information
DevCharly committed Aug 5, 2024
1 parent d27e056 commit 8089e66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ FlatLaf Change Log
window. (issue #869)
- HiDPI: Fixed occasional wrong repaint areas when using
`HiDPIUtils.installHiDPIRepaintManager()`. (see PR #864)
- Added system property `flatlaf.useSubMenuSafeTriangle` to allow disabling
submenu safe triangle (PR #490) for
[SWTSwing](https://github.com/Chrriis/SWTSwing). (issue #870)


## 3.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ public interface FlatSystemProperties
*/
String NATIVE_LIBRARY_PATH = "flatlaf.nativeLibraryPath";

/**
* Specifies whether safe triangle is used to improve usability of submenus.
* <p>
* <strong>Allowed Values</strong> {@code false} and {@code true}<br>
* <strong>Default</strong> {@code true}
* @since 3.5.1
*/
String USE_SUB_MENU_SAFE_TRIANGLE = "flatlaf.useSubMenuSafeTriangle";

/**
* Checks whether a system property is set and returns {@code true} if its value
* is {@code "true"} (case-insensitive), otherwise it returns {@code false}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.LoggingFacade;

/**
* Improves usability of submenus by using a
Expand All @@ -64,6 +65,7 @@ class SubMenuUsabilityHelper
// https://github.com/apache/netbeans/issues/4231#issuecomment-1179616607
private static SubMenuUsabilityHelper instance;

private boolean eventQueuePushNotSupported;
private SubMenuEventQueue subMenuEventQueue;
private SafeTrianglePainter safeTrianglePainter;
private boolean changePending;
Expand All @@ -83,6 +85,9 @@ static synchronized boolean install() {
if( instance != null )
return false;

if( !FlatSystemProperties.getBoolean( FlatSystemProperties.USE_SUB_MENU_SAFE_TRIANGLE, true ) )
return false;

instance = new SubMenuUsabilityHelper();
MenuSelectionManager.defaultManager().addChangeListener( instance );
return true;
Expand All @@ -99,7 +104,7 @@ static synchronized void uninstall() {

@Override
public void stateChanged( ChangeEvent e ) {
if( !FlatUIUtils.getUIBoolean( KEY_USE_SAFE_TRIANGLE, true ))
if( eventQueuePushNotSupported || !FlatUIUtils.getUIBoolean( KEY_USE_SAFE_TRIANGLE, true ))
return;

// handle menu selection change later, but only once in case of temporary changes
Expand Down Expand Up @@ -173,8 +178,29 @@ private void menuSelectionChanged() {
targetBottomY = popupLocation.y + popupSize.height;

// install own event queue to suppress mouse events when mouse is moved within safe triangle
if( subMenuEventQueue == null )
subMenuEventQueue = new SubMenuEventQueue();
if( subMenuEventQueue == null ) {
SubMenuEventQueue queue = new SubMenuEventQueue();

try {
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.getSystemEventQueue().push( queue );

// check whether push() worked
// (e.g. SWTSwing uses own event queue that does not support push())
if( toolkit.getSystemEventQueue() != queue ) {
eventQueuePushNotSupported = true;
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Failed to push submenu event queue. Disabling submenu safe triangle.", null );
return;
}

subMenuEventQueue = queue;
} catch( RuntimeException ex ) {
// catch runtime exception from EventQueue.push()
eventQueuePushNotSupported = true;
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Failed to push submenu event queue. Disabling submenu safe triangle.", ex );
return;
}
}

// create safe triangle painter
if( safeTrianglePainter == null && UIManager.getBoolean( KEY_SHOW_SAFE_TRIANGLE ) )
Expand Down Expand Up @@ -247,8 +273,6 @@ private class SubMenuEventQueue
}
} );
timeoutTimer.setRepeats( false );

Toolkit.getDefaultToolkit().getSystemEventQueue().push( this );
}

void uninstall() {
Expand Down

0 comments on commit 8089e66

Please sign in to comment.