Skip to content

Commit

Permalink
fix js code not getting executed, this also fixes dev tools being blank
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Jan 29, 2023
1 parent 8d74bd5 commit 83c11dc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/osiris/desku/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class App {
AL.info("workingDir = " + workingDir);
AL.info("tempDir = " + tempDir);
AL.info("userDir = " + userDir);
// Create styles file
styles.getParentFile().mkdirs();
if (styles.exists()) styles.delete();
styles.createNewFile();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/osiris/desku/swing/DevToolsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void componentHidden(ComponentEvent e) {
dispose();
}
});

setVisible(true);
}

@Override
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/osiris/desku/swing/NativeWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.callback.CefQueryCallback;
import org.cef.callback.CefURLRequestClient;
import org.cef.handler.CefLoadHandler;
import org.cef.handler.CefLoadHandlerAdapter;
import org.cef.handler.CefMessageRouterHandlerAdapter;
import org.cef.handler.CefPrintHandlerAdapter;
import org.cef.network.CefRequest;
import org.cef.network.CefResponse;
import org.cef.network.CefURLRequest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -94,6 +103,17 @@ public void windowClosing(WindowEvent e) {
dispose();
}
});

AtomicBoolean isLoaded = new AtomicBoolean(false);
App.cefClient.addLoadHandler(new CefLoadHandlerAdapter() {
@Override
public void onLoadEnd(CefBrowser b, CefFrame frame, int httpStatusCode) {
if(b == browser){
isLoaded.set(true);
}
}
});
while (!isLoaded.get()) Thread.yield();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -207,12 +227,16 @@ public NativeWindow onClick(com.osiris.desku.ui.Component<?> comp, Runnable code
String jsTriggerCallback = addCallback("", (message) -> {
code.run();
});
String jsNow = "var comp = document.querySelectorAll('[javaId=\"" + comp.id + "\"]')[0];\n" +
String jsNow = "var comp = document.querySelectorAll('[java-id=\"" + comp.id + "\"]')[0];\n" +
"comp.addEventListener(\"click\", () => {\n" +
"" + jsTriggerCallback + // JS code that triggers Java function gets executed on a click event for this component
"});\n";

browser.executeJavaScript(jsNow, "internal", 0);
return this;
}

public DevToolsDialog openDevTools(){
return new DevToolsDialog("DevTools", browser);
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/osiris/desku/ui/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class Component<T> {
public final ConcurrentHashMap<String, String> style = new ConcurrentHashMap<>();
public final CopyOnWriteArrayList<Component<?>> children = new CopyOnWriteArrayList<>();
/**
* Equals the attribute "javaId" inside HTML and thus useful for finding this object via JavaScript. <br>
* Example: The code below will return the object with the javaId = 5.
* Equals the attribute "java-id" inside HTML and thus useful for finding this object via JavaScript. <br>
* Example: The code below will return the object with the java-id = 5.
* <pre>
* var element = document.querySelectorAll('[javaId="5"]')[0];
* var element = document.querySelectorAll('[java-id="5"]')[0];
* </pre>
*/
public final int id = idCounter.getAndIncrement();
Expand All @@ -38,7 +38,7 @@ public class Component<T> {
public void init(T target, String tag) {
this.target = target;
this.element = new Element(tag);
element.attr("javaId", "" + id);
element.attr("java-id", "" + id);
}

/**
Expand All @@ -49,7 +49,7 @@ public void init(T target, String tag) {
public void init(T target, Tag tag, String baseUri, Attributes attributes) {
this.target = target;
this.element = new Element(tag, baseUri, attributes);
element.attr("javaId", "" + id);
element.attr("java-id", "" + id);
}

/**
Expand All @@ -60,7 +60,7 @@ public void init(T target, Tag tag, String baseUri, Attributes attributes) {
public void init(T target, Tag tag, String baseUri) {
this.target = target;
this.element = new Element(tag, baseUri);
element.attr("javaId", "" + id);
element.attr("java-id", "" + id);
}

public T add(Collection<Component<?>> comp) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/osiris/desku/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class AppTest {
void main() throws IOException, InterruptedException {
// Setup details
App.name = "My-App";
// before loading the page

// Create routes
Route home = new Home();
Route about = new About();

// Create windows
NativeWindow winHome = new NativeWindow(home);
winHome.openDevTools();
//new NativeWindow(about).plusX(20).plusY(20);


Expand Down

0 comments on commit 83c11dc

Please sign in to comment.