2929
3030package org .scijava .ui .swing .script ;
3131
32- import java .awt .Color ;
33- import java .awt .Component ;
34- import java .awt .Dimension ;
35- import java .awt .RenderingHints ;
36- import java .awt .Insets ;
37- import java .awt .Graphics2D ;
38- import java .awt .Graphics ;
39- import java .awt .FontMetrics ;
40- import java .awt .Point ;
32+ import java .awt .*;
4133import java .awt .event .InputEvent ;
4234import java .awt .event .KeyEvent ;
4335import java .awt .event .MouseAdapter ;
@@ -327,6 +319,13 @@ static void addMenubarSeparator(final JMenu menu, final String header) {
327319 menu .addSeparator ();
328320 }
329321 try {
322+ if (usesScreenMenuBar ()) {
323+ // The native macOS menu bar only translates menu items, not arbitrary components
324+ final JMenuItem item = new JMenuItem (header );
325+ item .setEnabled (false );
326+ menu .add (item );
327+ return ;
328+ }
330329 final JLabel label = new JLabel (" " + header );
331330 label .setEnabled (false );
332331 label .setForeground (getDisabledComponentColor ());
@@ -336,6 +335,11 @@ static void addMenubarSeparator(final JMenu menu, final String header) {
336335 }
337336 }
338337
338+ /** Whether menu bars are displayed in the native macOS menu bar (at the top of the screen) */
339+ private static boolean usesScreenMenuBar () {
340+ return PlatformUtils .isMac () && Boolean .parseBoolean (System .getProperty ("apple.laf.useScreenMenuBar" ));
341+ }
342+
339343 static void addPopupMenuSeparator (final JPopupMenu menu , final String header ) {
340344 if (menu .getComponentCount () > 1 ) {
341345 menu .addSeparator ();
@@ -474,7 +478,7 @@ static Color accentColor() {
474478 try {
475479 Color c = UIManager .getColor ("Component.accentColor" );
476480 if (c == null ) c = UIManager .getColor ("Component.focusColor" );
477- return (c == null ) ? new Color (0x3E73B9 ) : new Color (c .getRGB ()); // plain color: UIResource ones are reset
481+ return (c == null ) ? new Color (0x3E73B9 ) : new Color (c .getRGB ()); // plain color: UIResource ones are reset!?
478482 } catch (final Exception ignored ) {
479483 // do nothing
480484 }
@@ -490,13 +494,43 @@ static boolean isDark(final Color c) {
490494 return (c .getRed () * 0.299 + c .getGreen () * 0.587 + c .getBlue () * 0.114 ) < 186 ;
491495 }
492496
493- /** Small "+" button for the trailing area of a tabbed pane */
494- static JButton newTabButton (final Runnable action ) {
495- final JButton b = new JButton ("+" );
496- b .setToolTipText ("New file" );
497- b .setFocusable (false );
498- b .putClientProperty (FlatClientProperties .BUTTON_TYPE , FlatClientProperties .BUTTON_TYPE_TOOLBAR_BUTTON );
499- b .addActionListener (e -> action .run ());
497+ /** Small toggle button that looks like a tree selection when selected */
498+ static JToggleButton smallToggleButton (final String text ) {
499+ return smallToggleButton (text , null );
500+ }
501+
502+ /**
503+ * As {@link #smallToggleButton(String)}.
504+ *
505+ * @param extraStyle Optional. FlatLaf style properties to add to the ones set here. NB: Setting the
506+ * style property of the button later would replace them
507+ */
508+ static JToggleButton smallToggleButton (final String text , final String extraStyle ) {
509+ return smallToggleButton (text , extraStyle , true );
510+ }
511+
512+ /**
513+ * As {@link #smallToggleButton(String, String)}.
514+ *
515+ * @param secondary If true, the text of the unselected button is in secondary color. If false, in
516+ * the default (primary) color of buttons
517+ */
518+ static JToggleButton smallToggleButton (final String text , final String extraStyle , final boolean secondary ) {
519+ final JToggleButton b = new JToggleButton (text );
520+ b .putClientProperty (FlatClientProperties .STYLE_CLASS , "small" );
521+ // NB: setBackground() would not do, as FlatLaf darkens it
522+ b .putClientProperty (FlatClientProperties .STYLE ,
523+ "selectedBackground: $Tree.selectionBackground; toolbar.selectedBackground: $Tree.selectionBackground"
524+ + (extraStyle == null || extraStyle .isEmpty () ? "" : "; " + extraStyle ));
525+ // NB: Foregrounds must be set after the style: applying it resets them
526+ final Runnable updateForeground = () -> {
527+ final Color sel = UIManager .getColor ("Tree.selectionForeground" );
528+ final Color rest = secondary ? secondaryColor () : UIManager .getColor ("Button.foreground" );
529+ final Color fGrnd = (b .isSelected () && sel != null ) ? sel : rest ;
530+ if (fGrnd != null ) b .setForeground (fGrnd );
531+ };
532+ updateForeground .run ();
533+ b .addItemListener (e -> updateForeground .run ());
500534 return b ;
501535 }
502536
@@ -505,27 +539,27 @@ static JButton ellipsisButton(final Runnable action) {
505539 b .setFocusable (false );
506540 b .putClientProperty (FlatClientProperties .BUTTON_TYPE , FlatClientProperties .BUTTON_TYPE_TOOLBAR_BUTTON );
507541 if (action != null ) b .addActionListener (e -> action .run ());
508- b . setForeground ( secondaryColor ()); // the icon follows the foreground. Set last: style updates reset UI foregrounds
542+ // NBL he icon follows the foreground. To change its caller setForground() needs to be Set last
509543 return b ;
510544 }
511545
512-
513- /** Small round "?" button, in the style of the editor's other help buttons */
514- static JButton helpButton (final Runnable action ) {
515- return helpButton (action , .75f , true );
516- }
517-
518- private static JButton helpButton (final Runnable action , final float scale , final boolean borderless ) {
546+ /**
547+ * Round help "?" button
548+ *
549+ * @param sizeToComponent Optional. If not null, the icon will be scaled to height of this component.
550+ */
551+ static JButton helpButton (final Runnable action , final Component sizeToComponent ) {
519552 final FlatHelpButtonIcon icon = new FlatHelpButtonIcon ();
520- icon .setScale (scale );
521553 icon .applyStyleProperty ("questionMarkColor" , secondaryColor ());
522554 icon .applyStyleProperty ("background" , UIManager .getColor ("Panel.background" ));
523555 final JButton b = new JButton (icon );
524556 b .setFocusable (false );
525557 b .putClientProperty (FlatClientProperties .BUTTON_TYPE , FlatClientProperties .BUTTON_TYPE_TOOLBAR_BUTTON );
526- if (borderless ) {
527- b .putClientProperty ( "JButton.buttonType" , "borderless" );
558+ if (sizeToComponent != null ) {
559+ b .setMargin ( null );
528560 b .setBorder (null );
561+ b .putClientProperty ("JButton.buttonType" , "borderless" ); // probably not needed?
562+ icon .setScale (Icons .scaleToFont (icon , sizeToComponent ));
529563 }
530564 if (action != null ) b .addActionListener (e -> action .run ());
531565 return b ;
@@ -581,15 +615,7 @@ static void installSplitHandle(final JComponent handle, final JSplitPane split)
581615 split .setDividerSize (active ? 0 : regularSize );
582616 handle .setCursor (active ? java .awt .Cursor .getPredefinedCursor (java .awt .Cursor .N_RESIZE_CURSOR ) : null );
583617 };
584- // children must not inherit the resize cursor
585- final java .util .function .Consumer <java .awt .Container >[] resetCursors = new java .util .function .Consumer [1 ];
586- resetCursors [0 ] = parent -> {
587- for (final java .awt .Component child : parent .getComponents ()) {
588- if (!child .isCursorSet ()) child .setCursor (java .awt .Cursor .getDefaultCursor ());
589- if (child instanceof java .awt .Container ) resetCursors [0 ].accept ((java .awt .Container ) child );
590- }
591- };
592- resetCursors [0 ].accept (handle );
618+ defaultCursorForInteractive (handle );
593619 update .run ();
594620 split .addPropertyChangeListener (JSplitPane .ORIENTATION_PROPERTY , evt -> update .run ());
595621 final int [] grab = new int [1 ];
@@ -611,6 +637,20 @@ public void mouseDragged(final java.awt.event.MouseEvent e) {
611637 });
612638 }
613639
640+ /**
641+ * Components that handle mouse events themselves must not inherit the resize cursor of a
642+ * split handle. Labels, glue and gaps do (they move the split)
643+ */
644+ private static void defaultCursorForInteractive (final java .awt .Container parent ) {
645+ for (final java .awt .Component child : parent .getComponents ()) {
646+ final boolean interactive = child instanceof AbstractButton
647+ || child instanceof javax .swing .text .JTextComponent || child instanceof JComboBox
648+ || child instanceof JSpinner || child instanceof JList || child instanceof JSlider ;
649+ if (interactive && !child .isCursorSet ()) child .setCursor (java .awt .Cursor .getDefaultCursor ());
650+ if (child instanceof java .awt .Container ) defaultCursorForInteractive ((java .awt .Container ) child );
651+ }
652+ }
653+
614654 static void collapseAllTreeNodes (final JTree tree ) {
615655 final int row1 = (tree .isRootVisible ()) ? 1 : 0 ;
616656 for (int i = row1 ; i < tree .getRowCount (); i ++)
@@ -631,6 +671,8 @@ static JTabbedPane getJTabbedPane() {
631671 static JTabbedPane getJTabbedPane (final int initialPlacement ) {
632672 final JTabbedPane tabbed = new JTabbedPane ();
633673 tabbed .setTabLayoutPolicy (JTabbedPane .SCROLL_TAB_LAYOUT );
674+ tabbed .putClientProperty (FlatClientProperties .TABBED_PANE_TAB_AREA_ALIGNMENT ,
675+ FlatClientProperties .TABBED_PANE_ALIGN_LEADING );
634676 tabbed .putClientProperty (FlatClientProperties .TABBED_PANE_TABS_POPUP_POLICY , "asNeeded" ); // overflow dropdown
635677 tabbed .putClientProperty (FlatClientProperties .TABBED_PANE_SCROLL_BUTTONS_POLICY , "never" ); // do not show arrows
636678 tabbed .putClientProperty (FlatClientProperties .TABBED_PANE_TAB_ROTATION ,
@@ -693,6 +735,24 @@ static JTabbedPane getJTabbedPane(final int initialPlacement) {
693735 return tabbed ;
694736 }
695737
738+ /**
739+ * Assembles a secondary pane heading
740+ * @param text the header text
741+ * @param help optional An action to be attached to an option help button to the right of the heading
742+ * @return a small header in secondary color
743+ */
744+ static JComponent getHeader (final String text , final Runnable help ) {
745+ final JLabel label = new JLabel (text );
746+ label .putClientProperty (FlatClientProperties .STYLE_CLASS , "small" );
747+ label .setForeground (GuiUtils .secondaryColor ());
748+ final JPanel panel = new JPanel (new BorderLayout ());
749+ panel .setBorder (BorderFactory .createEmptyBorder (0 , 2 , 0 , 0 ));
750+ panel .setOpaque (false );
751+ panel .add (label , BorderLayout .CENTER );
752+ if (help != null ) panel .add (helpButton (help , label ), BorderLayout .EAST );
753+ return panel ;
754+ }
755+
696756 private static String placementName (final int placement ) {
697757 switch (placement ) {
698758 case JTabbedPane .LEFT : return "Left" ;
0 commit comments