From 7544b9d5a1437f2f40f5f5e6a32e0b23f90efbad Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Thu, 4 Jul 2024 16:05:43 +0200 Subject: [PATCH] Properly update find/replace overlay size and displayed widgets #2006 Currently, the widgets in the find/replace overlay (search options, replace expand bar) are not always properly updated on size changes of the target editor. This is complicated by the implementation mixing up the calculation of the overlay size and the calculations to determine which widgets are displayed, which also leads to the missing updates the UI upon certain events requiring the resizing of the overlay. With this change, the calculations for the overlay size and the determination of the displayed widgets are separated. This also makes a template string for a "compromise text length" obsolete. It also removes the "size gap" between the minimal width of the overlay with the replace expansion bar, taking up 70% of the editor width, and the size of the width of the overlay without the replace expansion bar, taking up 95% of the editor width. Fixes https://github.com/eclipse-platform/eclipse.platform.ui/issues/2006 --- .../overlay/FindReplaceOverlay.java | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java index b7d737c403a..cbbcaf98f7c 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java @@ -74,8 +74,7 @@ public class FindReplaceOverlay extends Dialog { private static final String REPLACE_BAR_OPEN_DIALOG_SETTING = "replaceBarOpen"; //$NON-NLS-1$ private static final double WORST_CASE_RATIO_EDITOR_TO_OVERLAY = 0.95; private static final double BIG_WIDTH_RATIO_EDITOR_TO_OVERLAY = 0.7; - private static final String MINIMAL_WIDTH_TEXT = "THIS TEXT "; //$NON-NLS-1$ - private static final String COMPROMISE_WIDTH_TEXT = "THIS TEXT HAS A REASONABLE"; //$NON-NLS-1$ + private static final String MINIMAL_WIDTH_TEXT = "THIS TEXT IS SHORT "; //$NON-NLS-1$ private static final String IDEAL_WIDTH_TEXT = "THIS TEXT HAS A REASONABLE LENGTH FOR SEARCHING"; //$NON-NLS-1$ private FindReplaceLogic findReplaceLogic; private IWorkbenchPart targetPart; @@ -746,37 +745,51 @@ private void enableReplaceTools(boolean enable) { } private int getIdealDialogWidth(Rectangle targetBounds) { + int idealOverlayWidth = calculateOverlayWidthWithToolbars(IDEAL_WIDTH_TEXT); + int minimumOverlayWidth = Math.min(calculateOverlayWidthWithoutToolbars(MINIMAL_WIDTH_TEXT), + (int) (targetBounds.width * WORST_CASE_RATIO_EDITOR_TO_OVERLAY)); + int maximumOverlayWidth = (int) (targetBounds.width * BIG_WIDTH_RATIO_EDITOR_TO_OVERLAY); + + int overlayWidth = idealOverlayWidth; + if (overlayWidth > maximumOverlayWidth) { + overlayWidth = maximumOverlayWidth; + } + if (overlayWidth < minimumOverlayWidth) { + overlayWidth = minimumOverlayWidth; + } + + return overlayWidth; + } + + private void configureDisplayedWidgetsForWidth(int overlayWidth) { + int minimumWidthWithToolbars = calculateOverlayWidthWithoutToolbars(IDEAL_WIDTH_TEXT); + int minimumWidthWithReplaceToggle = calculateOverlayWidthWithoutToolbars(MINIMAL_WIDTH_TEXT); + enableSearchTools(overlayWidth >= minimumWidthWithToolbars); + enableReplaceTools(overlayWidth >= minimumWidthWithToolbars); + enableReplaceToggle(overlayWidth >= minimumWidthWithReplaceToggle); + } + + private int calculateOverlayWidthWithToolbars(String searchInput) { + int toolbarWidth = searchTools.getSize().x; + return calculateOverlayWidthWithoutToolbars(searchInput) + toolbarWidth; + } + + private int calculateOverlayWidthWithoutToolbars(String searchInput) { int replaceToggleWidth = 0; if (okayToUse(replaceToggle)) { replaceToggleWidth = replaceToggle.getBounds().width; } - int toolBarWidth = searchTools.getSize().x + closeTools.getSize().x; + int closeButtonWidth = closeTools.getSize().x; + int searchInputWidth = getTextWidthInSearchBar(searchInput); + return replaceToggleWidth + closeButtonWidth + searchInputWidth; + } + + private int getTextWidthInSearchBar(String input) { GC gc = new GC(searchBar); gc.setFont(searchBar.getFont()); - int idealWidth = gc.stringExtent(IDEAL_WIDTH_TEXT).x; // $NON-NLS-1$ - int idealCompromiseWidth = gc.stringExtent(COMPROMISE_WIDTH_TEXT).x; // $NON-NLS-1$ - int worstCompromiseWidth = gc.stringExtent(MINIMAL_WIDTH_TEXT).x; // $NON-NLS-1$ + int textWidth = gc.stringExtent(input).x; // $NON-NLS-1$ gc.dispose(); - - int newWidth = idealWidth + toolBarWidth + replaceToggleWidth; - if (newWidth > targetBounds.width * BIG_WIDTH_RATIO_EDITOR_TO_OVERLAY) { - newWidth = (int) (targetBounds.width * BIG_WIDTH_RATIO_EDITOR_TO_OVERLAY); - enableSearchTools(true); - enableReplaceTools(true); - enableReplaceToggle(true); - } - if (newWidth < idealCompromiseWidth + toolBarWidth) { - enableSearchTools(false); - enableReplaceTools(false); - enableReplaceToggle(true); - } - if (newWidth < worstCompromiseWidth + toolBarWidth) { - newWidth = (int) (targetBounds.width * WORST_CASE_RATIO_EDITOR_TO_OVERLAY); - enableReplaceToggle(false); - enableSearchTools(false); - enableReplaceTools(false); - } - return newWidth; + return textWidth; } private Point getNewPosition(Widget targetTextWidget, Point targetOrigin, Rectangle targetBounds, @@ -830,7 +843,7 @@ private void positionToPart() { getShell().setSize(new Point(newWidth, newHeight)); getShell().setLocation(newPosition); getShell().layout(true); - + configureDisplayedWidgetsForWidth(newWidth); repositionTextSelection(); }