From 907956994f1c1cac1eaa829b74cb50e49d2f3239 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 8 Jun 2020 15:34:35 +0200 Subject: [PATCH] Extras: FlatInspector: - do not increase inspection level when activated with keyboard shortcut - added some javadoc - added to CHANGELOG.md and flatlaf-extras/README.md --- CHANGELOG.md | 1 + flatlaf-extras/README.md | 3 ++ .../formdev/flatlaf/extras/FlatInspector.java | 29 +++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ed32cd4..fb91470e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ FlatLaf Change Log - Button: Support non-square icon-only buttons. (issue #110) - Ubuntu Linux: Fixed poorly rendered font. (issue #105) - macOS Catalina: Use Helvetica Neue font. +- `FlatInspector` added (see [FlatLaf Extras](flatlaf-extras)). ## 0.35 diff --git a/flatlaf-extras/README.md b/flatlaf-extras/README.md index 8d8d6988..6e6e5bcd 100644 --- a/flatlaf-extras/README.md +++ b/flatlaf-extras/README.md @@ -3,6 +3,9 @@ FlatLaf Extras This sub-project provides some additional components and classes: +- [FlatInspector](src/main/java/com/formdev/flatlaf/extras/FlatInspector.java): + A simple UI inspector that shows information about UI component at mouse + location in a tooltip. - [FlatSVGIcon](src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java): An icon that displays SVG using [svgSalamander](https://github.com/JFormDesigner/svgSalamander). diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatInspector.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatInspector.java index 8e029b43..33366b38 100644 --- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatInspector.java +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatInspector.java @@ -61,6 +61,24 @@ import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.util.UIScale; /** + * A simple UI inspector that shows information about UI component at mouse location + * in a tooltip. + *

+ * To use it in an application install it with: + *

+ * FlatInspector.install( "ctrl shift alt X" );
+ * 
+ * This can be done e.g. in the main() method and allows enabling (and disabling) + * the UI inspector for the active window with the given keystroke. + *

+ * When the UI inspector is active some additional keys are available: + *

+ * * @author Karl Tauber */ public class FlatInspector @@ -80,6 +98,7 @@ public class FlatInspector private int lastX; private int lastY; private int inspectParentLevel; + private boolean wasCtrlOrShiftKeyPressed; private JComponent highlightFigure; private JToolTip tip; @@ -129,8 +148,14 @@ public class FlatInspector keyListener = e -> { KeyEvent keyEvent = (KeyEvent) e; int keyCode = keyEvent.getKeyCode(); + int id = e.getID(); - if( e.getID() == KeyEvent.KEY_RELEASED ) { + if( id == KeyEvent.KEY_PRESSED ) { + // this avoids that the inspection level is changed when UI inspector + // is enabled with keyboard shortcut (e.g. Ctrl+Shift+Alt+X) + if( keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_SHIFT ) + wasCtrlOrShiftKeyPressed = true; + } else if( id == KeyEvent.KEY_RELEASED && wasCtrlOrShiftKeyPressed ) { if( keyCode == KeyEvent.VK_CONTROL ) { inspectParentLevel++; inspect( lastX, lastY ); @@ -144,7 +169,7 @@ public class FlatInspector // consume pressed and released ESC key events to e.g. avoid that dialog is closed keyEvent.consume(); - if( e.getID() == KeyEvent.KEY_PRESSED ) { + if( id == KeyEvent.KEY_PRESSED ) { FlatInspector inspector = (FlatInspector) rootPane.getClientProperty( FlatInspector.class ); if( inspector == FlatInspector.this ) { uninstall();