Skip to content

Commit f924e0a

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. Fixes: #2222
1 parent e39c288 commit f924e0a

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

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

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