Skip to content

Commit 3bb7886

Browse files
committed
[WIP] 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 4862ef8 commit 3bb7886

File tree

1 file changed

+78
-0
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser

1 file changed

+78
-0
lines changed

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

Lines changed: 78 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,7 +97,10 @@ class WebKit extends WebBrowser {
9697
URI tlsErrorUri;
9798
String tlsErrorType;
9899

100+
private Shell searchShell;
101+
99102
boolean firstLoad = true;
103+
100104
static boolean FirstCreate = true;
101105

102106
/**
@@ -784,6 +788,12 @@ public void create (Composite parent, int style) {
784788
onResize (event);
785789
break;
786790
}
791+
case SWT.KeyDown: {
792+
if (event.keyCode == 'f' && (event.stateMask & SWT.CTRL) == SWT.CTRL) {
793+
openSearchDialog();
794+
}
795+
break;
796+
}
787797
}
788798
};
789799
browser.addListener (SWT.Dispose, listener);
@@ -2665,6 +2675,74 @@ private void webkit_settings_set(byte [] property, int value) {
26652675
OS.g_object_set(settings, property, value, 0);
26662676
}
26672677

2678+
private void openSearchDialog() {
2679+
if (searchShell != null && !searchShell.isDisposed()) {
2680+
return;
2681+
}
2682+
/*
2683+
* TODO:
2684+
* how to set no max count here? 65535 should be the value of UINT_MAX,
2685+
* which should be G_MAXUINT, which should be the maximum count here
2686+
*/
2687+
int maxMatchesCount = 65535;
2688+
int searchOptions = 0;
2689+
Shell shell = new Shell(browser.getShell(), SWT.TOOL | SWT.ON_TOP | SWT.RESIZE);
2690+
shell.setText("Search for text");
2691+
shell.setLayout(new FillLayout());
2692+
Point browserLocation = browser.getLocation();
2693+
Rectangle browserArea = browser.getClientArea();
2694+
Point location = browser.getShell().getLocation();
2695+
location.x += browserLocation.x;
2696+
location.y += browserLocation.y + browserArea.height;
2697+
shell.setLocation(location);
2698+
Composite composite = new Composite(shell, SWT.NONE);
2699+
GridLayout l = new GridLayout();
2700+
l.numColumns = 3;
2701+
composite.setLayout(l);
2702+
Text text = new Text(composite, SWT.BORDER);
2703+
Button next = new Button(composite, SWT.FLAT | SWT.ARROW | SWT.DOWN);
2704+
Button previous = new Button(composite, SWT.FLAT | SWT.ARROW | SWT.UP);
2705+
2706+
AtomicBoolean searched = new AtomicBoolean(false);
2707+
long findController = WebKitGTK.webkit_web_view_get_find_controller(webView);
2708+
Runnable searchNext = () -> {
2709+
if (!searched.getAndSet(true)) {
2710+
System.out.println("search");
2711+
String searchText = text.getText();
2712+
WebKitGTK.webkit_find_controller_search(findController, searchText.getBytes(), searchOptions, maxMatchesCount);
2713+
} else {
2714+
WebKitGTK.webkit_find_controller_search_next(findController);
2715+
System.out.println("next");
2716+
}
2717+
};
2718+
Runnable searchPrevious = () -> {
2719+
if (!searched.getAndSet(true)) {
2720+
String searchText = text.getText();
2721+
WebKitGTK.webkit_find_controller_search(findController, searchText.getBytes(), searchOptions, maxMatchesCount);
2722+
} else {
2723+
WebKitGTK.webkit_find_controller_search_previous(findController);
2724+
}
2725+
};
2726+
next.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> searchNext.run()));
2727+
previous.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> searchPrevious.run()));
2728+
text.addKeyListener(KeyListener.keyPressedAdapter(e -> {
2729+
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
2730+
searchNext.run();
2731+
}
2732+
}));
2733+
text.addModifyListener(e -> {
2734+
WebKitGTK.webkit_find_controller_search_finish(findController);
2735+
searched.getAndSet(false);
2736+
});
2737+
shell.addDisposeListener(e -> {
2738+
WebKitGTK.webkit_find_controller_search_finish(findController);
2739+
searchShell = null;
2740+
});
2741+
shell.pack();
2742+
shell.open();
2743+
searchShell = shell;
2744+
}
2745+
26682746
static Object convertToJava (long ctx, long value) {
26692747
int type = WebKitGTK.JSValueGetType (ctx, value);
26702748
switch (type) {

0 commit comments

Comments
 (0)