From 8077bf898a506b975b46b4d8188899747e57533f Mon Sep 17 00:00:00 2001
From: Phillipus
Date: Fri, 27 Sep 2024 17:44:40 +0100
Subject: [PATCH] Add workaround for Mac Text control cancel icon not working
with single click
- Clear text on single mouse click
---
.../com/archimatetool/editor/ui/UIUtils.java | 34 +++++++++++++++++++
.../views/tree/search/SearchWidget.java | 3 ++
2 files changed, 37 insertions(+)
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/ui/UIUtils.java b/com.archimatetool.editor/src/com/archimatetool/editor/ui/UIUtils.java
index f4ad79a16..0c97c3f80 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/ui/UIUtils.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/ui/UIUtils.java
@@ -6,7 +6,10 @@
package com.archimatetool.editor.ui;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
@@ -117,6 +120,37 @@ public static void applyMacUndoBugFilter(Control control) {
});
}
}
+
+ /**
+ * On Mac single-click on the Cancel icon in a text control doesn't work.
+ * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=551405
+ * @param text The Text control
+ */
+ public static void applyMacCancelIconListener(Text text) {
+ if(PlatformUtils.isMac() && (text.getStyle() & SWT.ICON_CANCEL) != 0) {
+ text.addMouseListener(new MouseAdapter() {
+ boolean mouseDownWasClickedOnCancelIcon;
+
+ @Override
+ public void mouseDown(MouseEvent e) {
+ mouseDownWasClickedOnCancelIcon = isCancelIconClicked(e);
+ }
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+ if(mouseDownWasClickedOnCancelIcon && isCancelIconClicked(e)) {
+ text.setText("");
+ }
+ };
+
+ private boolean isCancelIconClicked(MouseEvent e) {
+ Rectangle bounds = text.getBounds();
+ return !text.getText().isEmpty() && e.x > bounds.width - 25 && e.x < bounds.width - 5
+ && e.y > 0 && e.y < bounds.height;
+ }
+ });
+ }
+ }
/**
* Apply a traverse listener to a Multi-line text control such that tabbing or pressing Ctrl + Enter
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/search/SearchWidget.java b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/search/SearchWidget.java
index 5f5bbb57b..836f6b053 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/search/SearchWidget.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/search/SearchWidget.java
@@ -152,6 +152,9 @@ private void createSearchTextWidget() {
fSearchText = UIUtils.createSingleTextControl(this, SWT.SEARCH | SWT.ICON_CANCEL | SWT.ICON_SEARCH, false);
fSearchText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+ // Bug on Mac, single-click doesn't work on the cancel icon
+ UIUtils.applyMacCancelIconListener(fSearchText);
+
// Mac bug workaround
UIUtils.applyMacUndoBugFilter(fSearchText);