Skip to content

Commit e99c01f

Browse files
committed
Add search capabilities to WebKit based Browser
This changes adds a search dialog to WebKit browsers. The search dialog is opened with Ctrl+F and has next/previous word matching capabilities. The search dialog is not available for Browsers in a Shell with the SWT.TOOL style. The search capabilities can be disabled with the following system property: -Dorg.eclipse.swt.internal.webkitgtk.disableBrowserSearch=true Fixes: #2222
1 parent de1f3ef commit e99c01f

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.function.*;
2626

2727
import org.eclipse.swt.*;
28+
import org.eclipse.swt.events.*;
2829
import org.eclipse.swt.graphics.*;
2930
import org.eclipse.swt.internal.*;
3031
import org.eclipse.swt.internal.gtk.*;
@@ -96,6 +97,12 @@ class WebKit extends WebBrowser {
9697
URI tlsErrorUri;
9798
String tlsErrorType;
9899

100+
private final ControlListener browserMoveListener = ControlListener.controlMovedAdapter(this::browserShellMoved);
101+
private Point searchShellLocation;
102+
private Shell searchShell;
103+
private String searchText;
104+
private Cursor searchCursor;
105+
99106
boolean firstLoad = true;
100107
static boolean FirstCreate = true;
101108

@@ -201,6 +208,9 @@ class WebKit extends WebBrowser {
201208
/** Flag indicating whether TLS errors (like self-signed certificates) are to be ignored. */
202209
static final boolean ignoreTls;
203210

211+
/** Flag that disables browser searching added on top of the WebKit browser. */
212+
static final boolean disableBrowserSearch;
213+
204214
static {
205215
Proc2 = new Callback (WebKit.class, "Proc", 2); //$NON-NLS-1$
206216
Proc3 = new Callback (WebKit.class, "Proc", 3); //$NON-NLS-1$
@@ -248,6 +258,7 @@ class WebKit extends WebBrowser {
248258
NativePendingCookies = null;
249259
}
250260
ignoreTls = "true".equals(System.getProperty("org.eclipse.swt.internal.webkitgtk.ignoretlserrors"));
261+
disableBrowserSearch = "true".equals(System.getProperty("org.eclipse.swt.internal.webkitgtk.disableBrowserSearch"));
251262
}
252263

253264
@Override
@@ -784,13 +795,30 @@ public void create (Composite parent, int style) {
784795
onResize (event);
785796
break;
786797
}
798+
case SWT.KeyDown: {
799+
if (!disableBrowserSearch && event.keyCode == 'f' && (event.stateMask & SWT.CTRL) == SWT.CTRL) {
800+
openSearchDialog();
801+
}
802+
break;
803+
}
787804
}
788805
};
789806
browser.addListener (SWT.Dispose, listener);
790807
browser.addListener (SWT.FocusIn, listener);
791808
browser.addListener (SWT.KeyDown, listener);
792809
browser.addListener (SWT.Resize, listener);
793810

811+
browser.addDisposeListener(e -> {
812+
closeSearchDialog();
813+
if (searchCursor != null && !searchCursor.isDisposed()) {
814+
searchCursor.dispose();
815+
searchCursor = null;
816+
}
817+
});
818+
browser.addControlListener(ControlListener.controlResizedAdapter(this::browserShellMoved));
819+
browser.addControlListener(ControlListener.controlMovedAdapter(this::browserShellMoved));
820+
browser.getShell().addControlListener(browserMoveListener);
821+
794822
/*
795823
* Bug in WebKitGTK. MouseOver/MouseLeave events are not consistently sent from
796824
* the DOM when the mouse enters and exits the browser control, see
@@ -835,6 +863,9 @@ public boolean close () {
835863
// false = blocks disposal. In Browser.java, user is told widget was not disposed.
836864
// See Snippet326.
837865
boolean close (boolean showPrompters) {
866+
if (browser != null && !browser.isDisposed()) {
867+
browser.getShell().removeControlListener(browserMoveListener);
868+
}
838869
// don't execute any JavaScript if it's disabled or requested to get disabled
839870
// we need to check jsEnabledOnNextPage here because jsEnabled is updated asynchronously
840871
// and may not reflect the proper state (bug 571746 and bug 567881)
@@ -2665,6 +2696,118 @@ private void webkit_settings_set(byte [] property, int value) {
26652696
OS.g_object_set(settings, property, value, 0);
26662697
}
26672698

2699+
private void browserShellMoved(ControlEvent e) {
2700+
closeSearchDialog();
2701+
searchShellLocation = null;
2702+
}
2703+
2704+
private void closeSearchDialog() {
2705+
if (searchShell != null && !searchShell.isDisposed()) {
2706+
searchShellLocation = searchShell.getLocation();
2707+
searchShell.close();
2708+
if (searchText != null && webView != 0) {
2709+
long findController = WebKitGTK.webkit_web_view_get_find_controller(webView);
2710+
WebKitGTK.webkit_find_controller_search_finish(findController);
2711+
}
2712+
searchText = null;
2713+
}
2714+
}
2715+
2716+
private void openSearchDialog() {
2717+
if (webView == 0 || (searchShell != null && !searchShell.isDisposed())) {
2718+
return;
2719+
}
2720+
Shell browserShell = browser.getShell();
2721+
if (browserShell == null || browserShell.isDisposed() || (browserShell.getStyle() & SWT.TOOL) == SWT.TOOL) {
2722+
/*
2723+
* We don't provide search capabilities for browsers in a pop-up.
2724+
* We could cause issues with pop-up focus handling when the search shell is opened.
2725+
*/
2726+
return;
2727+
}
2728+
if (searchCursor == null) {
2729+
searchCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_SIZEALL);
2730+
}
2731+
Shell shell = new Shell(browserShell, SWT.TOOL | SWT.MODELESS);
2732+
Rectangle browserArea = browser.getClientArea();
2733+
int height = 45;
2734+
Point location;
2735+
if (searchShellLocation != null) {
2736+
location = searchShellLocation;
2737+
} else {
2738+
location = browser.toDisplay(0, 0);
2739+
location.y += Math.max(0, browserArea.height - height);
2740+
}
2741+
shell.setLocation(location);
2742+
shell.setSize(250, height);
2743+
GridLayout l = new GridLayout();
2744+
l.marginWidth = 8;
2745+
l.marginHeight = 8;
2746+
l.numColumns = 4;
2747+
shell.setLayout(l);
2748+
Text text = new Text(shell, SWT.BORDER);
2749+
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
2750+
Cursor defaultCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW);
2751+
Button next = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.DOWN);
2752+
next.setCursor(defaultCursor);
2753+
Button previous = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.UP);
2754+
previous.setCursor(defaultCursor);
2755+
shell.setCursor(searchCursor);
2756+
boolean[] mouseDown = new boolean[1];
2757+
int[] xPos = new int[1];
2758+
int[] yPos = new int[1];
2759+
shell.addMouseListener(new MouseAdapter() {
2760+
@Override
2761+
public void mouseUp(MouseEvent arg0) {
2762+
mouseDown[0] = false;
2763+
}
2764+
@Override
2765+
public void mouseDown(MouseEvent e) {
2766+
mouseDown[0] = true;
2767+
xPos[0] = e.x;
2768+
yPos[0] = e.y;
2769+
}
2770+
});
2771+
shell.addMouseMoveListener(e -> {
2772+
if (mouseDown[0]) {
2773+
shell.setLocation(shell.getLocation().x + (e.x - xPos[0]), shell.getLocation().y + (e.y - yPos[0]));
2774+
}
2775+
});
2776+
long findController = WebKitGTK.webkit_web_view_get_find_controller(webView);
2777+
Runnable searchNext = () -> search(findController, text::getText, WebKitGTK::webkit_find_controller_search_next);
2778+
Runnable searchPrevious = () -> search(findController, text::getText, WebKitGTK::webkit_find_controller_search_previous);
2779+
next.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> searchNext.run()));
2780+
previous.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> searchPrevious.run()));
2781+
text.addKeyListener(KeyListener.keyPressedAdapter(e -> {
2782+
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
2783+
searchNext.run();
2784+
}
2785+
}));
2786+
shell.addDisposeListener(e -> {
2787+
searchShellLocation = searchShell.getLocation();
2788+
WebKitGTK.webkit_find_controller_search_finish(findController);
2789+
searchShell = null;
2790+
});
2791+
shell.open();
2792+
searchShell = shell;
2793+
}
2794+
2795+
private void search(long findController, Supplier<String> currentText, Consumer<Long> incrementSearch) {
2796+
int maxMatchesCount = WebKitGTK.G_MAXUINT; // TODO: how to set no max count here?
2797+
int searchOptions = WebKitGTK.WEBKIT_FIND_OPTIONS_WRAP_AROUND;
2798+
String text = currentText.get();
2799+
if (!text.equals(searchText)) {
2800+
if (searchText != null) {
2801+
WebKitGTK.webkit_find_controller_search_finish(findController);
2802+
}
2803+
searchText = text;
2804+
byte[] textToSearch = Converter.wcsToMbcs(searchText, true);
2805+
WebKitGTK.webkit_find_controller_search(findController, textToSearch, searchOptions, maxMatchesCount);
2806+
} else {
2807+
incrementSearch.accept(Long.valueOf(findController));
2808+
}
2809+
}
2810+
26682811
static Object convertToJava (long ctx, long value) {
26692812
int type = WebKitGTK.JSValueGetType (ctx, value);
26702813
switch (type) {

bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public class WebKitGTK extends C {
9090
public static final int WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START = 0;
9191
public static final int WEBKIT_USER_CONTENT_INJECT_TOP_FRAME = 1;
9292

93+
public static final int G_MAXUINT = 65535;
94+
public static final int WEBKIT_FIND_OPTIONS_WRAP_AROUND = 1 << 4;
95+
9396
/** Signals */
9497

9598
// Authentication.

0 commit comments

Comments
 (0)