|
25 | 25 | import java.util.function.*; |
26 | 26 |
|
27 | 27 | import org.eclipse.swt.*; |
| 28 | +import org.eclipse.swt.events.*; |
28 | 29 | import org.eclipse.swt.graphics.*; |
29 | 30 | import org.eclipse.swt.internal.*; |
30 | 31 | import org.eclipse.swt.internal.gtk.*; |
@@ -96,6 +97,13 @@ class WebKit extends WebBrowser { |
96 | 97 | URI tlsErrorUri; |
97 | 98 | String tlsErrorType; |
98 | 99 |
|
| 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 | + |
99 | 107 | boolean firstLoad = true; |
100 | 108 | static boolean FirstCreate = true; |
101 | 109 |
|
@@ -784,13 +792,33 @@ public void create (Composite parent, int style) { |
784 | 792 | onResize (event); |
785 | 793 | break; |
786 | 794 | } |
| 795 | + case SWT.KeyDown: { |
| 796 | + if (event.keyCode == 'f' && (event.stateMask & SWT.CTRL) == SWT.CTRL) { |
| 797 | + openSearchDialog(); |
| 798 | + } |
| 799 | + break; |
| 800 | + } |
787 | 801 | } |
788 | 802 | }; |
789 | 803 | browser.addListener (SWT.Dispose, listener); |
790 | 804 | browser.addListener (SWT.FocusIn, listener); |
791 | 805 | browser.addListener (SWT.KeyDown, listener); |
792 | 806 | browser.addListener (SWT.Resize, listener); |
793 | 807 |
|
| 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 | + |
794 | 822 | /* |
795 | 823 | * Bug in WebKitGTK. MouseOver/MouseLeave events are not consistently sent from |
796 | 824 | * the DOM when the mouse enters and exits the browser control, see |
@@ -835,6 +863,9 @@ public boolean close () { |
835 | 863 | // false = blocks disposal. In Browser.java, user is told widget was not disposed. |
836 | 864 | // See Snippet326. |
837 | 865 | boolean close (boolean showPrompters) { |
| 866 | + if (browser != null && !browser.isDisposed()) { |
| 867 | + browser.getShell().removeControlListener(browserMoveListener); |
| 868 | + } |
838 | 869 | // don't execute any JavaScript if it's disabled or requested to get disabled |
839 | 870 | // we need to check jsEnabledOnNextPage here because jsEnabled is updated asynchronously |
840 | 871 | // 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) { |
2665 | 2696 | OS.g_object_set(settings, property, value, 0); |
2666 | 2697 | } |
2667 | 2698 |
|
| 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 | + |
2668 | 2812 | static Object convertToJava (long ctx, long value) { |
2669 | 2813 | int type = WebKitGTK.JSValueGetType (ctx, value); |
2670 | 2814 | switch (type) { |
|
0 commit comments