Skip to content

Make "find and replace" window always appear on top of the parent editor #6603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions app/src/cc/arduino/view/findreplace/FindReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPopupMenu;
import javax.swing.Action;
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 {
Expand All @@ -55,6 +59,7 @@ public class FindReplace extends javax.swing.JFrame {
public FindReplace(Editor editor, Map<String, Object> state) {
this.editor = editor;

isTranslucencySupported();
initComponents();

if (OSUtils.isMacOS()) {
Expand All @@ -67,16 +72,28 @@ public FindReplace(Editor editor, Map<String, Object> state) {
}

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) {
}
});

Expand All @@ -86,10 +103,20 @@ public void windowActivated(WindowEvent e) {
@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<String, Object> findDialogState() {
Map<String, Object> state = new HashMap<>();
state.put(FIND_TEXT, findField.getText());
Expand Down Expand Up @@ -161,6 +188,22 @@ private void initComponents() {

searchAllFilesBox.setText(tr("Search all Sketch Tabs"));

JPopupMenu menu = new JPopupMenu();
Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, tr("Cut"));
menu.add( cut );

Action copy = new DefaultEditorKit.CopyAction();
copy.putValue(Action.NAME, tr("Copy"));
menu.add( copy );

Action paste = new DefaultEditorKit.PasteAction();
paste.putValue(Action.NAME, tr("Paste"));
menu.add( paste );

findField.setComponentPopupMenu( menu );
replaceField.setComponentPopupMenu( menu );

findButton.setText(tr("Find"));
findButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Expand Down