Skip to content

Commit

Permalink
Add workaround for Mac Text control cancel icon not working with sing…
Browse files Browse the repository at this point in the history
…le click

- Clear text on single mouse click
  • Loading branch information
Phillipus committed Sep 27, 2024
1 parent c8c79cc commit 8077bf8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 8077bf8

Please sign in to comment.