vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
590 stars 164 forks source link

Add support for video and audio tags #19139

Open mvysny opened 3 months ago

mvysny commented 3 months ago

Describe your motivation

Vaadin 8 used to have both video and audio elements; when migrating from Vaadin 8 it would be useful to have support for those.

mvysny commented 3 months ago
public abstract class AbstractMedia extends HtmlContainer {

    @NotNull
    private final Text alt = new Text("");

    @Tag("source")
    public static class Source extends HtmlComponent {
        public Source(@NotNull StreamResource resource) {
            getElement().setAttribute("src", resource);
            getElement().setAttribute("type", resource.getContentTypeResolver().apply(resource, Utils.getCurrentServletContext()));
        }
    }

    @Tag("video")
    public static class Video extends AbstractMedia {
    }

    @Tag("audio")
    public static class Audio extends AbstractMedia {
    }

    protected AbstractMedia() {
        add(alt);
    }

    public void addSource(@NotNull StreamResource resource) {
        add(new Source(resource));
    }

    public void setSource(@NotNull StreamResource resource) {
        clearSources();
        addSource(resource);
    }

    public void clearSources() {
        removeAll();
    }

    public void setShowControls(boolean showControls) {
        getElement().setAttribute("controls", showControls);
    }

    public boolean isShowControls() {
        return getElement().getAttribute("controls") != null;
    }

    public void setAltText(@NotNull String altText) {
        alt.setText(altText);
    }

    @NotNull
    public String getAltText() {
        return alt.getText();
    }

    public void setAutoplay(boolean autoplay) {
        getElement().setAttribute("autoplay", autoplay);
    }

    public boolean isAutoplay() {
        return getElement().getAttribute("autoplay") != null;
    }

    public void setLoop(boolean loop) {
        getElement().setAttribute("loop", loop);
    }

    public boolean isLoop() {
        return getElement().getAttribute("loop") != null;
    }

    public void setMuted(boolean muted) {
        getElement().setAttribute("muted", muted);
    }

    public boolean isMuted() {
        return getElement().getAttribute("muted") != null;
    }

    public enum PreloadMode {
        /**
         * Indicates that the whole video/audio file could be downloaded, even if
         * the user is not expected to use it. This is the default value.
         */
        AUTO,

        /**
         * Indicates that only media metadata (e.g. length) should be preloaded.
         */
        METADATA,

        /**
         * Indicates that the video/audio should not be preloaded.
         */
        NONE;

        /**
         * Returns the preload mode string used by the browser.
         *
         * @return corresponding preload attribute value string
         */
        public String getValue() {
            return name().toLowerCase(Locale.ROOT);
        }
    }

    public void setPreload(@Nullable PreloadMode preload) {
        if (preload == null) {
            getElement().removeAttribute("preload");
        } else {
            getElement().setAttribute("preload", preload.getValue());
        }
    }

    @Nullable
    public PreloadMode getPreload() {
        final String preload = getElement().getAttribute("preload");
        return preload == null ? null : PreloadMode.valueOf(preload.toUpperCase(Locale.ROOT));
    }
}