Skip to content

Commit 147f501

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. Fixes: #2222
1 parent 0a68dea commit 147f501

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

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

Lines changed: 136 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,13 @@ 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+
private long searchCursorHandle;
106+
99107
boolean firstLoad = true;
100108
static boolean FirstCreate = true;
101109

@@ -784,13 +792,33 @@ public void create (Composite parent, int style) {
784792
onResize (event);
785793
break;
786794
}
795+
case SWT.KeyDown: {
796+
if (event.keyCode == 'f' && (event.stateMask & SWT.CTRL) == SWT.CTRL) {
797+
openSearchDialog();
798+
}
799+
break;
800+
}
787801
}
788802
};
789803
browser.addListener (SWT.Dispose, listener);
790804
browser.addListener (SWT.FocusIn, listener);
791805
browser.addListener (SWT.KeyDown, listener);
792806
browser.addListener (SWT.Resize, listener);
793807

808+
browser.addDisposeListener(e -> {
809+
closeSearchDialog();
810+
if (searchCursor != null && !searchCursor.isDisposed()) {
811+
searchCursor.dispose();
812+
}
813+
if (searchCursorHandle != 0) {
814+
OS.g_object_unref(searchCursorHandle);
815+
searchCursorHandle = 0;
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,111 @@ 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+
if (searchCursorHandle == 0) {
2721+
searchCursorHandle = GDK.gdk_cursor_new_from_name(GDK.gdk_display_get_default(), "fleur");
2722+
searchCursor = Cursor.gtk_new(Display.getCurrent(), searchCursorHandle);
2723+
}
2724+
Shell shell = new Shell(browser.getShell(), SWT.TOOL | SWT.MODELESS);
2725+
Rectangle browserArea = browser.getClientArea();
2726+
int height = 45;
2727+
Point location;
2728+
if (searchShellLocation != null) {
2729+
location = searchShellLocation;
2730+
} else {
2731+
location = browser.toDisplay(0, 0);
2732+
location.y += Math.max(0, browserArea.height - height);
2733+
}
2734+
shell.setLocation(location);
2735+
shell.setSize(250, height);
2736+
GridLayout l = new GridLayout();
2737+
l.marginWidth = 8;
2738+
l.marginHeight = 8;
2739+
l.numColumns = 4;
2740+
shell.setLayout(l);
2741+
Text text = new Text(shell, SWT.BORDER);
2742+
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
2743+
Cursor defaultCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW);
2744+
Button next = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.DOWN);
2745+
next.setCursor(defaultCursor);
2746+
Button previous = new Button(shell, SWT.FLAT | SWT.ARROW | SWT.UP);
2747+
previous.setCursor(defaultCursor);
2748+
shell.setCursor(searchCursor);
2749+
boolean[] mouseDown = new boolean[1];
2750+
int[] xPos = new int[1];
2751+
int[] yPos = new int[1];
2752+
shell.addMouseListener(new MouseAdapter() {
2753+
@Override
2754+
public void mouseUp(MouseEvent arg0) {
2755+
mouseDown[0] = false;
2756+
}
2757+
@Override
2758+
public void mouseDown(MouseEvent e) {
2759+
mouseDown[0] = true;
2760+
xPos[0] = e.x;
2761+
yPos[0] = e.y;
2762+
}
2763+
});
2764+
shell.addMouseMoveListener(e -> {
2765+
if (mouseDown[0]) {
2766+
shell.setLocation(shell.getLocation().x + (e.x - xPos[0]), shell.getLocation().y + (e.y - yPos[0]));
2767+
}
2768+
});
2769+
long findController = WebKitGTK.webkit_web_view_get_find_controller(webView);
2770+
Runnable searchNext = () -> search(findController, text::getText, WebKitGTK::webkit_find_controller_search_next);
2771+
Runnable searchPrevious = () -> search(findController, text::getText, WebKitGTK::webkit_find_controller_search_previous);
2772+
next.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> searchNext.run()));
2773+
previous.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> searchPrevious.run()));
2774+
text.addKeyListener(KeyListener.keyPressedAdapter(e -> {
2775+
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
2776+
searchNext.run();
2777+
}
2778+
}));
2779+
shell.addDisposeListener(e -> {
2780+
searchShellLocation = searchShell.getLocation();
2781+
WebKitGTK.webkit_find_controller_search_finish(findController);
2782+
searchShell = null;
2783+
});
2784+
shell.open();
2785+
searchShell = shell;
2786+
}
2787+
2788+
private void search(long findController, Supplier<String> currentText, Consumer<Long> incrementSearch) {
2789+
int maxMatchesCount = WebKitGTK.G_MAXUINT; // TODO: how to set no max count here?
2790+
int searchOptions = WebKitGTK.WEBKIT_FIND_OPTIONS_WRAP_AROUND;
2791+
String text = currentText.get();
2792+
if (!text.equals(searchText)) {
2793+
if (searchText != null) {
2794+
WebKitGTK.webkit_find_controller_search_finish(findController);
2795+
}
2796+
searchText = text;
2797+
byte[] textToSearch = Converter.wcsToMbcs(searchText, true);
2798+
WebKitGTK.webkit_find_controller_search(findController, textToSearch, searchOptions, maxMatchesCount);
2799+
} else {
2800+
incrementSearch.accept(Long.valueOf(findController));
2801+
}
2802+
}
2803+
26682804
static Object convertToJava (long ctx, long value) {
26692805
int type = WebKitGTK.JSValueGetType (ctx, value);
26702806
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)