cljfx

https://github.com/cljfx/cljfx
pbaille 2021-04-05T18:00:55.064900Z

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

joelkuiper 2021-04-06T07:00:32.065200Z

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)

vlaaad 2021-04-06T07:12:07.066500Z

I would suggest having a look at this: https://openjfx.io/javadoc/14/javafx.web/javafx/scene/web/WebEngine.html

vlaaad 2021-04-06T07:12:45.066800Z

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

vlaaad 2021-04-06T07:13:30.067Z

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

vlaaad 2021-04-06T07:14:38.067200Z

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

pbaille 2021-04-06T07:32:40.067700Z

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)?

vlaaad 2021-04-06T07:35:03.067900Z

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
        ...}}

pbaille 2021-04-06T07:36:21.068100Z

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

pbaille 2021-04-06T09:15:57.068400Z

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 :)

vlaaad 2021-04-06T09:17:14.068800Z

first statement — you mean this?

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

pbaille 2021-04-06T09:17:23.069Z

yes

vlaaad 2021-04-06T09:18:02.069200Z

it’s like that in clojure:

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

vlaaad 2021-04-06T09:18:38.069400Z

so it creates an instance of EventListener interface

pbaille 2021-04-06T09:18:47.069600Z

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

pbaille 2021-04-06T09:21:02.069800Z

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

pbaille 2021-04-06T09:28:46.071900Z

@vlaaad, 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 ?

vlaaad 2021-04-06T09:31:35.072100Z

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

pbaille 2021-04-06T09:34:13.072300Z

ok 🙂 thank you a lot

vlaaad 2021-04-06T09:34:15.072500Z

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)

🙏 1