diff --git a/core/src/main/java/dev/webview/webview_java/Webview.java b/core/src/main/java/dev/webview/webview_java/Webview.java index 077e1b1..e49f45f 100644 --- a/core/src/main/java/dev/webview/webview_java/Webview.java +++ b/core/src/main/java/dev/webview/webview_java/Webview.java @@ -2,7 +2,7 @@ * MIT LICENSE * * Copyright (c) 2024 Alex Bowles @ Casterlabs - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -42,16 +42,14 @@ import lombok.NonNull; public class Webview implements Closeable, Runnable { - - @Deprecated - public long $pointer; + private final long $pointer; /** * Creates a new Webview.
* The default size will be set, and if the size is set again before loading the * URL, a splash will appear.
* eg: - * + * *
      * 
      *   WebView wv = new WebView(true);
@@ -59,22 +57,21 @@ public class Webview implements Closeable, Runnable {
      *   wv.loadURL("...")
      * 
      * 
- * + *

* It's recommended that setting size together: - * + * *

      * 
      *   WebView wv = new WebView(true, 1280, 720);
      *   wv.loadURL("...")
      * 
      * 
- * + * * @param debug Enables devtools/inspect element if true. - * - * @see #Webview(boolean, int, int) + * @see #Webview(boolean, int, int) */ public Webview(boolean debug) { - this(debug, (PointerByReference) null); + this(debug, 800, 600); } /** @@ -85,7 +82,7 @@ public Webview(boolean debug) { * @param height preset - height */ public Webview(boolean debug, int width, int height) { - this(debug, null, width, height); + $pointer = N.webview_create_size(debug, width, height); } /** @@ -93,7 +90,7 @@ public Webview(boolean debug, int width, int height) { * The default size will be set, and if the size is set again before loading the * URL, a splash will appear.
* eg: - * + * *
      * 
      *   WebView wv = new WebView(true);
@@ -101,22 +98,20 @@ public Webview(boolean debug, int width, int height) {
      *   wv.loadURL("...")
      * 
      * 
- * + *

* It's recommended that setting size together: - * + * *

      * 
      *   WebView wv = new WebView(true, 1280, 720);
      *   wv.loadURL("...")
      * 
      * 
- * + * * @param debug Enables devtools/inspect element if true. - * * @param target The target awt component, such as a {@link java.awt.JFrame} or * {@link java.awt.Canvas}. Must be "drawable". - * - * @see #Webview(boolean, PointerByReference, int, int) + * @see #Webview(boolean, PointerByReference, int, int) */ public Webview(boolean debug, @NonNull Component target) { this(debug, new PointerByReference(Native.getComponentPointer(target))); @@ -127,18 +122,7 @@ public Webview(boolean debug, @NonNull Component target) { */ @Deprecated public Webview(boolean debug, @Nullable PointerByReference windowPointer) { - this(debug, windowPointer, 800, 600); - } - - /** - * @deprecated Use this only if you absolutely know what you're doing. - */ - @Deprecated - public Webview(boolean debug, @Nullable PointerByReference windowPointer, int width, int height) { $pointer = N.webview_create(debug, windowPointer); - - this.loadURL(null); - this.setSize(width, height); } /** @@ -185,12 +169,10 @@ public void setFixedSize(int width, int height) { /** * Sets the script to be run on page load. Defaults to no nested access (false). - * - * @implNote This get's called AFTER window.load. - * - * @param script - * - * @see #setInitScript(String, boolean) + * + * @param script + * @implNote This get's called AFTER window.load. + * @see #setInitScript(String, boolean) */ public void setInitScript(@NonNull String script) { this.setInitScript(script, false); @@ -198,27 +180,26 @@ public void setInitScript(@NonNull String script) { /** * Sets the script to be run on page load. - * - * @implNote This get's called AFTER window.load. - * - * @param script - * @param allowNestedAccess whether or not to inject the script into nested - * iframes. + * + * @param script + * @param allowNestedAccess whether or not to inject the script into nested + * iframes. + * @implNote This get's called AFTER window.load. */ public void setInitScript(@NonNull String script, boolean allowNestedAccess) { script = String.format( - "(() => {\n" - + "try {\n" - + "if (window.top == window.self || %b) {\n" - + "%s\n" - + "}\n" - + "} catch (e) {\n" - + "console.error('[Webview]', 'An error occurred whilst evaluating init script:', %s, e);\n" - + "}\n" - + "})();", - allowNestedAccess, - script, - '"' + _WebviewUtil.jsonEscape(script) + '"' + "(() => {\n" + + "try {\n" + + "if (window.top == window.self || %b) {\n" + + "%s\n" + + "}\n" + + "} catch (e) {\n" + + "console.error('[Webview]', 'An error occurred whilst evaluating init script:', %s, e);\n" + + "}\n" + + "})();", + allowNestedAccess, + script, + '"' + _WebviewUtil.jsonEscape(script) + '"' ); N.webview_init($pointer, script); @@ -226,42 +207,40 @@ public void setInitScript(@NonNull String script, boolean allowNestedAccess) { /** * Executes the given script NOW. - * + * * @param script */ public void eval(@NonNull String script) { this.dispatch(() -> { N.webview_eval( - $pointer, - String.format( - "try {\n" - + "%s\n" - + "} catch (e) {\n" - + "console.error('[Webview]', 'An error occurred whilst evaluating script:', %s, e);\n" - + "}", - script, - '"' + _WebviewUtil.jsonEscape(script) + '"' - ) + $pointer, + String.format( + "try {\n" + + "%s\n" + + "} catch (e) {\n" + + "console.error('[Webview]', 'An error occurred whilst evaluating script:', %s, e);\n" + + "}", + script, + '"' + _WebviewUtil.jsonEscape(script) + '"' + ) ); }); } /** * Binds a function to the JavaScript environment on page load. - * - * @implNote This get's called AFTER window.load. - * - * @implSpec After calling the function in JavaScript you will get a - * Promise instead of the value. This is to prevent you from - * locking up the browser while waiting on your Java code to - * execute and generate a return value. - * - * @param name The name to be used for the function, e.g "foo" to get - * foo(). - * @param handler The callback handler, accepts a JsonArray (which are all - * arguments passed to the function()) and returns a value - * which is of type JsonElement (can be null). Exceptions are - * automatically passed back to JavaScript. + * + * @param name The name to be used for the function, e.g "foo" to get + * foo(). + * @param handler The callback handler, accepts a JsonArray (which are all + * arguments passed to the function()) and returns a value + * which is of type JsonElement (can be null). Exceptions are + * automatically passed back to JavaScript. + * @implNote This get's called AFTER window.load. + * @implSpec After calling the function in JavaScript you will get a + * Promise instead of the value. This is to prevent you from + * locking up the browser while waiting on your Java code to + * execute and generate a return value. */ public void bind(@NonNull String name, @NonNull WebviewBindCallback handler) { N.webview_bind($pointer, name, new BindCallback() { @@ -289,7 +268,7 @@ public void callback(long seq, String req, long arg) { /** * Unbinds a function, removing it from future pages. - * + * * @param name The name of the function. */ public void unbind(@NonNull String name) { @@ -298,7 +277,7 @@ public void unbind(@NonNull String name) { /** * Executes an event on the event thread. - * + * * @deprecated Use this only if you absolutely know what you're doing. */ @Deprecated @@ -310,7 +289,7 @@ public void dispatch(@NonNull Runnable handler) { /** * Executes the webview event loop until the user presses "X" on the window. - * + * * @see #close() */ @Override @@ -322,7 +301,7 @@ public void run() { /** * Executes the webview event loop asynchronously until the user presses "X" on * the window. - * + * * @see #close() */ public void runAsync() { diff --git a/core/src/main/java/dev/webview/webview_java/WebviewNative.java b/core/src/main/java/dev/webview/webview_java/WebviewNative.java index f7f9b59..584e934 100644 --- a/core/src/main/java/dev/webview/webview_java/WebviewNative.java +++ b/core/src/main/java/dev/webview/webview_java/WebviewNative.java @@ -2,17 +2,17 @@ * MIT LICENSE * * Copyright (c) 2024 Alex Bowles @ Casterlabs - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -43,10 +43,10 @@ import lombok.SneakyThrows; interface WebviewNative extends Library { - static final WebviewNative N = runSetup(); + WebviewNative N = runSetup(); @SneakyThrows - private static WebviewNative runSetup() { + static WebviewNative runSetup() { String[] libraries = null; switch (Platform.osDistribution) { @@ -72,7 +72,7 @@ private static WebviewNative runSetup() { case WINDOWS_NT: { libraries = new String[] { -// "/dev/webview/webview_java/natives/" + Platform.archTarget + "/windows_nt/WebView2Loader.dll", + "/dev/webview/webview_java/natives/" + Platform.archTarget + "/windows_nt/webview2loader.dll", "/dev/webview/webview_java/natives/" + Platform.archTarget + "/windows_nt/webview.dll" }; break; @@ -152,18 +152,20 @@ static interface DispatchCallback extends Callback { * the native window handle. If it's non-null - then child WebView is embedded * into the given parent window. Otherwise a new window is created. Depending on * the platform, a GtkWindow, NSWindow or HWND pointer can be passed here. - * + * * @param debug Enables developer tools if true (if supported) * @param $window A pointer to a native window handle, for embedding the webview * in a window. (Either a GtkWindow, NSWindow, or HWND pointer) */ long webview_create(boolean debug, PointerByReference window); + long webview_create_size(boolean debug, int width, int height); + /** * @return a native window handle pointer. - * + * * @param $pointer The instance pointer of the webview - * + * * @implNote This is either a pointer to a GtkWindow, NSWindow, or * HWND. */ @@ -179,7 +181,7 @@ static interface DispatchCallback extends Callback { /** * Navigates to the given URL. - * + * * @param $pointer The instance pointer of the webview * @param url The target url, can be a data uri. */ @@ -187,7 +189,7 @@ static interface DispatchCallback extends Callback { /** * Sets the title of the webview window. - * + * * @param $pointer The instance pointer of the webview * @param title */ @@ -196,7 +198,7 @@ static interface DispatchCallback extends Callback { /** * Updates the webview's window size, see {@link WV_HINT_NONE}, * {@link WV_HINT_MIN}, {@link WV_HINT_MAX}, and {@link WV_HINT_FIXED} - * + * * @param $pointer The instance pointer of the webview * @param width * @param height @@ -207,28 +209,28 @@ static interface DispatchCallback extends Callback { /** * Runs the main loop until it's terminated. You must destroy the webview after * this method returns. - * + * * @param $pointer The instance pointer of the webview */ void webview_run(long $pointer); /** * Destroys a webview and closes the native window. - * + * * @param $pointer The instance pointer of the webview */ void webview_destroy(long $pointer); /** * Stops the webview loop, which causes {@link #webview_run(long)} to return. - * + * * @param $pointer The instance pointer of the webview */ void webview_terminate(long $pointer); /** * Evaluates arbitrary JavaScript code asynchronously. - * + * * @param $pointer The instance pointer of the webview * @param js The script to execute */ @@ -236,9 +238,9 @@ static interface DispatchCallback extends Callback { /** * Injects JavaScript code at the initialization of the new page. - * + * * @implSpec It is guaranteed to be called before window.onload. - * + * * @param $pointer The instance pointer of the webview * @param js The script to execute */ @@ -247,7 +249,7 @@ static interface DispatchCallback extends Callback { /** * Binds a native callback so that it will appear under the given name as a * global JavaScript function. Internally it uses webview_init(). - * + * * @param $pointer The instance pointer of the webview * @param name The name of the function to be exposed in Javascript * @param callback The callback to be called @@ -257,7 +259,7 @@ static interface DispatchCallback extends Callback { /** * Remove the native callback specified. - * + * * @param $pointer The instance pointer of the webview * @param name The name of the callback */ @@ -266,7 +268,7 @@ static interface DispatchCallback extends Callback { /** * Allows to return a value from the native binding. Original request pointer * must be provided to help internal RPC engine match requests with responses. - * + * * @param $pointer The instance pointer of the webview * @param name The name of the callback * @param isError Whether or not `result` should be thrown as an exception @@ -277,7 +279,7 @@ static interface DispatchCallback extends Callback { /** * Dispatches the callback on the UI thread, only effective while * {@link #webview_run(long)} is blocking. - * + * * @param $pointer The instance pointer of the webview * @param callback The callback to be called * @param arg Unused diff --git a/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview.dll b/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview.dll index 2eb5523..faf6728 100644 Binary files a/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview.dll and b/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview.dll differ diff --git a/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview2loader.dll b/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview2loader.dll new file mode 100644 index 0000000..28f7ab0 Binary files /dev/null and b/core/src/main/resources/dev/webview/webview_java/natives/x86_64/windows_nt/webview2loader.dll differ diff --git a/examples/src/main/java/dev/webview/webview_java/example/fx/FxExample.java b/examples/src/main/java/dev/webview/webview_java/example/fx/FxExample.java index 6d1cafc..ae9b91a 100644 --- a/examples/src/main/java/dev/webview/webview_java/example/fx/FxExample.java +++ b/examples/src/main/java/dev/webview/webview_java/example/fx/FxExample.java @@ -39,8 +39,7 @@ public void start(Stage stage) throws Exception { private void initWv() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { Scene s = stage.getScene(); - wv = new Webview(true, new PointerByReference(FXStageDetect.getWindowPointer(stage)), - (int)s.getWidth(), (int)s.getHeight()); + wv = new Webview(true, new PointerByReference(FXStageDetect.getWindowPointer(stage))); wv.setTitle("TestWindow"); // note: not allow to resize wv.setFixedSize((int)s.getWidth(), (int)s.getHeight());