Fork me on GitHub
#cljfx
<
2021-04-05
>
pbaille18:04:55

Hi, I'm trying to use web-views and wondering how can I send an event from javascript to cljfx. any idea ?

joelkuiper07:04:32

what I ended up doing was using WebSockets via Sente (https://github.com/ptaoussanis/sente) to create an event bus, this is a little bit involved but ultimately has a nice property: you can transparently dispatch events between the webview and javafx bi-directionally using the same re-frame esque conventions (if you also plan on using ClojureScript in the Webview)

vlaaad07:04:45

this doc describes how one can setup communication with the web page

vlaaad07:04:30

you can access the web engine by creating custom props, for example

vlaaad07:04:38

here are some examples of built-in custom props that access WebEngine: https://github.com/cljfx/cljfx/blob/master/src/cljfx/ext/web_view.clj

pbaille07:04:40

thank you a lot, I was hoping for something builtin to cljfx, but it seems doable. So if I understand well, I will have to create my own lifecycle, which will setup the communication in its create method (by using WebEngine)?

vlaaad07:04:03

you will create extension lifecycle that wraps web-view lifecycle:

(def ext-with-my-stuff 
  (fx/make-ext-with-props 
    {:my-custom-prop-that-establishes-communication ...}))

{:fx/type ext-with-my-stuff
 :props {:my-custom-prop-that-establishes-communication ...}
 :desc {:fx/type :web-view
        ...}}

pbaille07:04:21

nice 🙂 I will start like this. thank you a lot for the help

pbaille09:04:57

I've got a java question (I'm not so much familiar with clj/java interop). I'm trying to make this snippet work in the context of my cljfx app: (its from the https://openjfx.io/javadoc/14/javafx.web/javafx/scene/web/WebEngine.html you've linked)

EventListener listener = new EventListener() {
        public void handleEvent(Event ev) {
            Platform.exit();
        }
    };

    Document doc = webEngine.getDocument();
    Element el = doc.getElementById("exit-app");
    ((EventTarget) el).addEventListener("click", listener, false);
To be honest I don't even understand the first statement... And I don't know how can I translate it to clojure... sorry for the noobyness :)

vlaaad09:04:14

first statement — you mean this?

EventListener listener = new EventListener() {
        public void handleEvent(Event ev) {
            Platform.exit();
        }
    }

vlaaad09:04:02

it’s like that in clojure:

(reify EventListener
  (handleEvent [this ev]
    (Platform/exit)))

vlaaad09:04:38

so it creates an instance of EventListener interface

pbaille09:04:47

@U0508P8EK seems appealing to be able to use clojurescript in a webview, i've tryed without success. do you have some code to show ?

pbaille09:04:02

@U47G49KHQ, ho! I'm surprised that it is so simple! thank you (looking at the javadoc I taught that I would need a proxy)

pbaille09:04:46

@U47G49KHQ, when I try to evaluate your expression I get this error

Syntax error (IllegalArgumentException) compiling reify* at (codemirror.clj:75:1).
Can't define method not in interfaces: handleEvent
Maybe EventListener is not what I taught it was. I'm using java.util.EventListener Am I missing something ?

vlaaad09:04:35

yep, it’s org.w3c.dom.events.EventListener

pbaille09:04:13

ok 🙂 thank you a lot

vlaaad09:04:15

I found it like that: WebEngine’s document property is defined here: https://openjfx.io/javadoc/14/javafx.web/javafx/scene/web/WebEngine.html#documentProperty() it returns a value of type “Document”, and it’s has a link leading here: https://docs.oracle.com/en/java/javase/12/docs/api/java.xml/org/w3c/dom/Document.html?is-external=true So this is something from the JDK, and at that site I searched for addEventListener and found this: https://docs.oracle.com/en/java/javase/12/docs/api/java.xml/org/w3c/dom/events/EventTarget.html#addEventListener(java.lang.String,org.w3c.dom.events.EventListener,boolean)

🙏 3