From b8a6e6d58935879769193b65f562532395d17be3 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 10 Aug 2017 12:26:20 +0200 Subject: [PATCH] Find/Replace: display on top of the related Editor If the editor is focused, acquire alwaysOnTop capabilities but drop them as soon as both the editor and the dialog are out of focus try to implement partial transparency As implemented in Notepad++, a partially transparent search/replace window could make it easier to interact with the editor (particularly in fullscreen contexts, when the "always on top" window could hide the search results). Unfortunately, Java can only apply the transparency to undecorated window, so it's a no-go. Java gurus, please come to the rescue :D Fix find/replace focus on Windows Probably this approach could be adopted by all OSes with a little care on the window lifecycle. [Find/Replace][Win] grab focus when just opened Solves https://github.com/arduino/Arduino/issues/6951#issuecomment-351995084 Use Windows method for all OS --- .../arduino/view/findreplace/FindReplace.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/src/cc/arduino/view/findreplace/FindReplace.java b/app/src/cc/arduino/view/findreplace/FindReplace.java index 03e7b1094..988dc6f5a 100644 --- a/app/src/cc/arduino/view/findreplace/FindReplace.java +++ b/app/src/cc/arduino/view/findreplace/FindReplace.java @@ -43,6 +43,7 @@ import javax.swing.text.DefaultEditorKit; import java.util.HashMap; import java.util.Map; +import static java.awt.GraphicsDevice.WindowTranslucency.*; import static processing.app.I18n.tr; public class FindReplace extends javax.swing.JFrame { @@ -58,6 +59,7 @@ public class FindReplace extends javax.swing.JFrame { public FindReplace(Editor editor, Map state) { this.editor = editor; + isTranslucencySupported(); initComponents(); if (OSUtils.isMacOS()) { @@ -70,16 +72,28 @@ public class FindReplace extends javax.swing.JFrame { } Base.registerWindowCloseKeys(getRootPane(), e -> { + setAutoRequestFocus(true); setVisible(false); Base.FIND_DIALOG_STATE = findDialogState(); }); Base.setIcon(this); + editor.addWindowListener(new WindowAdapter() { + public void windowActivated(WindowEvent e) { + toFront(); + setAutoRequestFocus(false); + } + public void windowDeactivated(WindowEvent e) { + return; + } + }); + addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { - findField.requestFocusInWindow(); - findField.selectAll(); + return; + } + public void windowDeactivated(WindowEvent e) { } }); @@ -89,10 +103,20 @@ public class FindReplace extends javax.swing.JFrame { @Override public void setVisible(boolean b) { getRootPane().setDefaultButton(findButton); - + // means we are restoring the window visibility + setAutoRequestFocus(true); super.setVisible(b); } + private boolean useTranslucency; + + private void isTranslucencySupported() { + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice gd = ge.getDefaultScreenDevice(); + //If translucent windows aren't supported, exit. + useTranslucency = gd.isWindowTranslucencySupported(TRANSLUCENT); + } + private Map findDialogState() { Map state = new HashMap<>(); state.put(FIND_TEXT, findField.getText());