Skip to content
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

Properly update find/replace overlay size and displayed widgets #2006 #2030

Merged
merged 1 commit into from
Jul 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -830,7 +843,7 @@ private void positionToPart() {
getShell().setSize(new Point(newWidth, newHeight));
getShell().setLocation(newPosition);
getShell().layout(true);

configureDisplayedWidgetsForWidth(newWidth);
repositionTextSelection();
}

Expand Down
Loading