Thursday, July 31, 2025

Re: Mixing Elemental2 with GWT Widgets

Excellent!  Will be good to have some company on the Elemental2 journey.  🙂

While I'm sharing...  I wrote a method to load an HTMLImageElement from a base64 encoded string:

public static void loadImage(String imgBase64, Consumer<HTMLImageElement> loaded) {
  HTMLImageElement img = (HTMLImageElement)DomGlobal.document.createElement("img");

  img.onload = e -> {
    loaded.accept(img);
    return null;
  };

  img.onerror = e -> {
    DomGlobal.console.error("Failed to load image.");
    return null;
  };

  img.src = imgBase64;
}

What I don't understand is why onload and onerror need a return value?  I just return null as I've no clue what the return value should be.

On Thursday, 31 July 2025 at 4:20:40 pm UTC+10 Ralph Fiergolla wrote:
Craig! 
Don't worry - i am following your Elemtal2 Experience closely. It is on my to-do list for quite some time now. Thanks for sharing! 
Ralph 

Craig Mitchell <ma...@craig-mitchell.com> schrieb am Do. 31. Juli 2025 um 02:37:
On the off chance someone reads the above post and thinks Elemental2 isn't very good.  Just to make it clear, Elemental2 is awesome.  Just now I needed a WeakHashMap.  GWT said no.  Elemental2 said sure can, with JsWeakMap.  Love it!

On Wednesday, 30 July 2025 at 2:34:49 pm UTC+10 Craig Mitchell wrote:
I've switched to use Elemental2 for a few things.  The rest of my app still uses the standard GWT widgets.

I can bind directly to the Elemental2 elements from the GWT UI Binder, however, if I programatically want to add an Elemental2 element to a GWT Widget, there doesn't seem to be a direct way to do this.

Eg: How would you add the Elemental2 HTMLCanvasElement, to a GWT SimplePanel using its setWidget method?

To work around this, I created an "Elemental2Widget":

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;
import jsinterop.base.Js;

public class Elemental2Widget<T> extends Widget {
  public Elemental2Widget(T elemental2Widget) {
    Element element = Js.cast(elemental2Widget);
    setElement(element);
  }
  public T getElemental2Widget() {
    return Js.cast(getElement());
  }
}

Now I can call it like this:

simplePanel.setWidget(new Elemental2Widget<>(htmlCanvasElement));

Is this how people mix Elemental2 with GWT widgets?  Or is there a better way?

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/7a9446c7-32b5-4a1a-84ba-2c7200901122n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/c1d31ce2-446c-4e2e-b228-e2215c67a5dcn%40googlegroups.com.

No comments:

Post a Comment