Skip to content

Conversation

@trancexpress
Copy link
Contributor

See: eclipse-platform/eclipse.platform#1890

Since we'll need to use WebKitBrowser native API for searching, it will be useful to add the respective native methods with a preliminary PR. By doing this, maintainers who review a PR for eclipse-platform/eclipse.platform#1890 wont need to compile native libraries locally.

@trancexpress
Copy link
Contributor Author

trancexpress commented Jun 9, 2025

Example very rudimentary search after adding the native methods:

diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
index 5b03fc50f4..a8a10b9be1 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java        
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java        
@@ -25,6 +25,7 @@ import java.util.concurrent.atomic.*;
 import java.util.function.*;
 
 import org.eclipse.swt.*;
+import org.eclipse.swt.events.*;
 import org.eclipse.swt.graphics.*;
 import org.eclipse.swt.internal.*;
 import org.eclipse.swt.internal.gtk.*;
@@ -784,6 +785,12 @@ public void create (Composite parent, int style) {
                                onResize (event);
                                break;
                        }
+                       case SWT.KeyDown: {
+                               if (event.keyCode == 'f' && (event.stateMask & SWT.CTRL) == SWT.CTRL) {
+                                       openSearchDialog();
+                               }
+                               break;
+                       }
                }
        };
        browser.addListener (SWT.Dispose, listener);
@@ -2665,6 +2672,53 @@ private void webkit_settings_set(byte [] property, int value) {
        OS.g_object_set(settings, property, value, 0);
 }
 
+private void openSearchDialog() {
+       Shell shell = new Shell(browser.getShell());
+       shell.setText("Search for text");
+       shell.setLayout(new FillLayout());
+       shell.setSize(400, 100);
+       Composite c = new Composite(shell, SWT.NONE);
+       GridLayout l = new GridLayout();
+       l.numColumns = 3;
+       c.setLayout(l);
+       Text t = new Text(c, SWT.BORDER);
+       Button n = new Button(c, SWT.BORDER);
+       n.setText("Next");
+       Button p = new Button(c, SWT.BORDER);
+       p.setText("Prev");
+
+       long findController = WebKitGTK.webkit_web_view_get_find_controller(webView);
+       AtomicBoolean searched = new AtomicBoolean(false);
+       n.addSelectionListener(new SelectionAdapter() {
+               @Override
+               public void widgetSelected(SelectionEvent e) {
+                       if (!searched.getAndSet(true)) {
+                               searchText(findController, t::getText);
+                               p.setEnabled(true);
+                       } else {
+                               WebKitGTK.webkit_find_controller_search_next(findController);
+                       }
+               }
+       });
+       p.addSelectionListener(new SelectionAdapter() {
+               @Override
+               public void widgetSelected(SelectionEvent e) {
+                       WebKitGTK.webkit_find_controller_search_previous(findController);
+               }
+       });
+       p.setEnabled(false);
+       shell.addDisposeListener(e -> {
+               WebKitGTK.webkit_find_controller_search_finish(findController);
+       });
+       shell.pack();
+       shell.open();
+}
+
+private void searchText(long findController, Supplier<String> textGetter) {
+       String text = textGetter.get();
+       WebKitGTK.webkit_find_controller_search(findController, text.getBytes(), 0, 1000); // TODO: how to set no max count here?
+}
+
 static Object convertToJava (long ctx, long value) {
        int type = WebKitGTK.JSValueGetType (ctx, value);
        switch (type) {

Test with snippet:

package test.browser;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestBrowser {

	public static void main(String[] args) throws InterruptedException {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setSize(600, 800);
		shell.setLayout(new FillLayout());
		shell.setText("Test Browser");

		Browser b = new Browser(shell, SWT.NONE);
		b.setText(
				"""
				<html>
				 <head>
				 </head>
				 <body>
				   Hello World
				   <h1>Hello World</h1>
				   <p>Hello World</p>
				 </body>
				</html>
				""");

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

See screen recording:
gh1890_example_search_dialog.webm

We can discuss details on eclipse-platform/eclipse.platform#1890 or its PR, once available. The above is just a proof of concept, i.e. that its enough to add the native methods in the diff here, to make some basic searching work.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 9, 2025

Test Results

   545 files  ±0     545 suites  ±0   27m 32s ⏱️ -11s
 4 399 tests ±0   4 381 ✅ ±0   18 💤 ±0  0 ❌ ±0 
16 723 runs  ±0  16 583 ✅ ±0  140 💤 ±0  0 ❌ ±0 

Results for commit 898b732. ± Comparison against base commit d2cb26d.

Copy link
Member

@akurtakov akurtakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to add the bindings like that as a prep step.

@akurtakov akurtakov merged commit abfe6f0 into eclipse-platform:master Jun 9, 2025
17 checks passed
@trancexpress
Copy link
Contributor Author

Thank you @akurtakov !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants