Class PwaIcon

java.lang.Object
com.vaadin.flow.server.PwaIcon
All Implemented Interfaces:
Serializable

public class PwaIcon extends Object implements Serializable
Implementation of icons used in PWA resources. Creates the href automatically based on - baseName (the file name with path, as icons/icon.png") - width (width of icon) - height (height of icon) - (possibly) fileHash (the hashcode of image file) The href will be set as: [basename]-[width]x[height].png{?[filehash]} The trailing ?[filehash] will be added if icon cache is not controlled by service worker: cached = false Icon caching is left to the browser if it's not cached with service worker.
Since:
1.1
See Also:
  • Constructor Details

    • PwaIcon

      protected PwaIcon(PwaIcon icon)
  • Method Details

    • asElement

      public org.jsoup.nodes.Element asElement()
      Gets an Element presentation of the icon.
      Returns:
      an Element presentation of the icon
    • getWidth

      public int getWidth()
      Gets width of an icon.
      Returns:
      width of an icon
    • getHeight

      public int getHeight()
      Gets height of an icon.
      Returns:
      height of an icon
    • shouldBeCached

      public boolean shouldBeCached()
      Should the icon be cached by the Service Worker.
      Returns:
      should the icon be cached by the Service Worker.
    • getSizes

      public String getSizes()
      Gets the value of the sizes attribute.
      Returns:
      value of the sizes attribute
    • getHref

      public String getHref()
      Gets the value of the href attribute.
      Returns:
      value of the href attribute
    • getRelHref

      public String getRelHref()
      Return href with '/' -prefix and removed possible ?[fileHash]. Used in matching, when serving images.
      Returns:
      href with '/' -prefix and removed possible ?[fileHash]
    • getCacheFormat

      public String getCacheFormat()
      Gets the cache-string used in Google Workbox caching.
      Returns:
      "{ url: '[href]', revision: '[fileHash' }"
    • getType

      public String getType()
      Gets the value of the type attribute.
      Returns:
      value of the type attribute
    • getDomain

      public PwaIcon.Domain getDomain()
      Gets the icon PwaIcon.Domain.
      Returns:
      the domain of the icon
    • setRegistry

      public void setRegistry(PwaRegistry registry)
    • setImage

      public void setImage(BufferedImage image)
      Sets the image presenting the icon.
      Parameters:
      image - the image in png format
    • write

      public void write(OutputStream outputStream)
      Writes the icon image to output stream.
      Parameters:
      outputStream - output stream to write the icon image to
    • getBaseImage

      protected BufferedImage getBaseImage()
    • drawIconImage

      public static BufferedImage drawIconImage(BufferedImage baseImage, int targetWidth, int targetHeight)
      Draws a resized version of the given base image centered on a new image of the specified dimensions. The top-left pixel of the base image is used as the background fill color. The image is scaled down to fit within the target dimensions while preserving its aspect ratio; upscaling is not performed.
      Parameters:
      baseImage - the source image to resize
      targetWidth - the width of the resulting image in pixels
      targetHeight - the height of the resulting image in pixels
      Returns:
      a new BufferedImage with the resized icon drawn centered
      Since:
      25.2
    • drawDefaultBaseImage

      public static BufferedImage drawDefaultBaseImage(String appName, String themeColor) throws IOException
      Draws the base image for the PWA icons of an application that has no icon of its own: the initials of the application name on a square filled with the theme color.

      The initials are the first letters of the first two words of the name. For a single word, they are its first two capital letters when it has at least two (StarPass gives SP), otherwise its first two letters. The square uses a default blue when the theme color is the default white or not a hex color.

      Falls back to the bundled Vaadin logo when no initials can be drawn, for example when the name has no letters or digits, or no font that can display them is installed.

      Parameters:
      appName - the application name to take the initials from
      themeColor - the theme color of the application, as a CSS hex color
      Returns:
      a new BufferedImage to generate the icons from
      Throws:
      IOException - if the bundled logo is needed and can not be read