From 21c0c678dacebd4b71554ea1f14edfe168abace8 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Sat, 28 Sep 2024 11:00:07 +0200 Subject: [PATCH] Find/replace overlay: improve replace toggle button appearance The button to toggle whether the replace bar in a find/replace overlay is shown currently appears to be a rather heavyweight button with a border. With this change, the button is replace with a lightweight toolbar item like used for all other buttons in the overlay. This improves the appearance as well as the implementation as the same implementation patterns can now be applied to all buttons in the overlay. --- .../overlay/AccessibleToolItem.java | 2 ++ .../overlay/FindReplaceOverlay.java | 36 +++++++++---------- .../findandreplace/overlay/OverlayAccess.java | 5 ++- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java index ea68f106fa1..d2490d8c5c9 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java @@ -22,6 +22,7 @@ import org.eclipse.swt.widgets.ToolItem; import org.eclipse.jface.bindings.keys.KeyStroke; +import org.eclipse.jface.layout.GridDataFactory; class AccessibleToolItem { private final ToolItem toolItem; @@ -30,6 +31,7 @@ class AccessibleToolItem { AccessibleToolItem(Composite parent, int styleBits) { ToolBar toolbar = new ToolBar(parent, SWT.FLAT | SWT.HORIZONTAL); + GridDataFactory.fillDefaults().grab(true, true).align(SWT.CENTER, SWT.CENTER).applyTo(toolbar); toolItem = new ToolItem(toolbar, styleBits); addToolItemTraverseListener(toolbar); } 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 1ba6878127b..2abad115952 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 @@ -13,8 +13,6 @@ *******************************************************************************/ package org.eclipse.ui.internal.findandreplace.overlay; -import static org.eclipse.ui.internal.findandreplace.overlay.FindReplaceShortcutUtil.registerActionShortcutsAtControl; - import java.util.List; import java.util.function.Consumer; @@ -28,7 +26,7 @@ import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; -import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.graphics.Color; @@ -37,7 +35,6 @@ import org.eclipse.swt.graphics.RGBA; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; @@ -119,8 +116,8 @@ private final class KeyboardShortcuts { private boolean replaceBarOpen; private Composite container; - private Button replaceToggle; - private FindReplaceOverlayAction replaceToggleShortcut; + private AccessibleToolBar replaceToggleTools; + private ToolItem replaceToggle; private Composite contentGroup; @@ -496,7 +493,7 @@ private Control createDialog(final Composite parent) { private void initializeSearchShortcutHandlers() { searchTools.registerActionShortcutsAtControl(searchBar); closeTools.registerActionShortcutsAtControl(searchBar); - registerActionShortcutsAtControl(replaceToggleShortcut, searchBar); + replaceToggleTools.registerActionShortcutsAtControl(searchBar); } /** @@ -734,15 +731,16 @@ private void createMainContainer(final Composite parent) { } private void createReplaceToggle() { - replaceToggleShortcut = new FindReplaceOverlayAction(this::toggleReplace); - replaceToggleShortcut.addShortcuts(KeyboardShortcuts.TOGGLE_REPLACE); - replaceToggle = new Button(container, SWT.FLAT | SWT.PUSH); - GridDataFactory.fillDefaults().grab(false, true).align(GridData.BEGINNING, GridData.FILL) - .applyTo(replaceToggle); - replaceToggle.setToolTipText(replaceToggleShortcut - .addShortcutHintToTooltipText(FindReplaceMessages.FindReplaceOverlay_replaceToggle_toolTip)); - replaceToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_OPEN_REPLACE_AREA)); - replaceToggle.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> toggleReplace())); + replaceToggleTools = new AccessibleToolBar(container); + GridDataFactory.fillDefaults().grab(false, true).align(GridData.FILL, GridData.FILL) + .applyTo(replaceToggleTools); + replaceToggleTools.addMouseListener(MouseListener.mouseDownAdapter(__ -> toggleReplace())); + + replaceToggle = new AccessibleToolItemBuilder(replaceToggleTools) + .withShortcuts(KeyboardShortcuts.TOGGLE_REPLACE) + .withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_OPEN_REPLACE_AREA)) + .withToolTipText(FindReplaceMessages.FindReplaceOverlay_replaceToggle_toolTip) + .withOperation(this::toggleReplace).build(); } private void toggleReplace() { @@ -788,7 +786,7 @@ private void createReplaceDialog() { private void initializeReplaceShortcutHandlers() { replaceTools.registerActionShortcutsAtControl(replaceBar); closeTools.registerActionShortcutsAtControl(replaceBar); - registerActionShortcutsAtControl(replaceToggleShortcut, replaceBar); + replaceToggleTools.registerActionShortcutsAtControl(replaceBar); } private void enableSearchTools(boolean enable) { @@ -801,8 +799,8 @@ private void enableReplaceToggle(boolean enable) { return; } boolean visible = enable && findReplaceLogic.getTarget().isEditable(); - ((GridData) replaceToggle.getLayoutData()).exclude = !visible; - replaceToggle.setVisible(visible); + ((GridData) replaceToggleTools.getLayoutData()).exclude = !visible; + replaceToggleTools.setVisible(visible); } private void enableReplaceTools(boolean enable) { diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/overlay/OverlayAccess.java b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/overlay/OverlayAccess.java index e81a1f47b56..51450754661 100644 --- a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/overlay/OverlayAccess.java +++ b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/overlay/OverlayAccess.java @@ -24,7 +24,6 @@ import java.util.stream.Collectors; import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; @@ -54,7 +53,7 @@ class OverlayAccess implements IFindReplaceUIAccess { private final ToolItem searchBackward; - private final Button openReplaceDialog; + private final ToolItem openReplaceDialog; private HistoryTextWrapper replace; @@ -78,7 +77,7 @@ class OverlayAccess implements IFindReplaceUIAccess { inSelection= widgetExtractor.findToolItem("searchInSelection"); searchForward= widgetExtractor.findToolItem("searchForward"); searchBackward= widgetExtractor.findToolItem("searchBackward"); - openReplaceDialog= widgetExtractor.findButton("replaceToggle"); + openReplaceDialog= widgetExtractor.findToolItem("replaceToggle"); extractReplaceWidgets(); }