From fc44255456951ee0727f4fc02c9f61b7c97e9b2d Mon Sep 17 00:00:00 2001 From: Maximilian Wittmer Date: Mon, 3 Jun 2024 13:50:26 +0200 Subject: [PATCH] Find/Replace overlay: remove search scope when leaving overlay #1919 Replace the FocusListener with a ShellAdapter which will remove the search scope as soon as the overlay is left. The FocusListener didn't listen to the case where the focus is in the search bar and the search bar is left (and the overlay is left implicitly) fixes #1919 --- .../ui/texteditor/FindReplaceOverlay.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceOverlay.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceOverlay.java index 88fdddfcee9..95bf517fcca 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceOverlay.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceOverlay.java @@ -26,6 +26,8 @@ import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.events.ShellAdapter; +import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Point; @@ -226,10 +228,17 @@ public void controlResized(ControlEvent e) { } }; - private FocusListener overlayFocusListener = FocusListener.focusLostAdapter(e -> { - findReplaceLogic.activate(SearchOptions.GLOBAL); - searchInSelectionButton.setSelection(false); - }); + private ShellAdapter overlayDeactivationListener = new ShellAdapter() { + @Override + public void shellActivated(ShellEvent e) { + // Do nothing + } + + @Override + public void shellDeactivated(ShellEvent e) { + removeSearchScope(); + } + }; private PaintListener widgetMovementListener = __ -> positionToPart(); @@ -317,7 +326,7 @@ public int open() { } overlayOpen = true; applyOverlayColors(backgroundToUse, true); - initFindStringFromSelection(); + updateFromTargetSelection(); getShell().layout(); positionToPart(); @@ -361,7 +370,7 @@ private void applyOverlayColors(Color color, boolean tryToColorReplaceBar) { } private void unbindListeners() { - getShell().removeFocusListener(overlayFocusListener); + getShell().removeShellListener(overlayDeactivationListener); if (targetPart != null && targetPart instanceof StatusTextEditor textEditor) { Control targetWidget = textEditor.getSourceViewer().getTextWidget(); if (targetWidget != null) { @@ -373,7 +382,7 @@ private void unbindListeners() { } private void bindListeners() { - getShell().addFocusListener(overlayFocusListener); + getShell().addShellListener(overlayDeactivationListener); if (targetPart instanceof StatusTextEditor textEditor) { Control targetWidget = textEditor.getSourceViewer().getTextWidget(); @@ -815,7 +824,7 @@ private void performSearch(boolean forward) { findReplaceLogic.activate(SearchOptions.INCREMENTAL); } - private void initFindStringFromSelection() { + private void updateFromTargetSelection() { String initText = findReplaceLogic.getTarget().getSelectionText(); if (initText.isEmpty()) { return; @@ -863,4 +872,9 @@ private static boolean okayToUse(Widget widget) { public void setPositionToTop(boolean shouldPositionOverlayOnTop) { positionAtTop = shouldPositionOverlayOnTop; } + + private void removeSearchScope() { + findReplaceLogic.activate(SearchOptions.GLOBAL); + searchInSelectionButton.setSelection(false); + } } \ No newline at end of file