WFavicon

A WFavicon is an object that represents a favicon that can be set for the application. Changing the favicon can be used to notify the user that something happened on the application, even when the user is looking at another tab. This is less intrusive than a WNotification, and does not require permission from the user.

Changing Favicon

You can change the favicon to any image that a browser accepts as a favicon (e.g. PNG, JPEG, ICO). This will only modify the favicon for the application, so it will only affect the user. You can go back to the default favicon of the application by setting the favicon of the application to nullptr.

Tips: You can set the default favicon for an application when adding the entry point for the application, or you can configure the default favicon in thewt_config.xml, by setting the property named favicon.

Example
source
  void ChangeFavicon() {
    WContainerWidget container = new WContainerWidget();
    WPushButton sunFaviconButton = new WPushButton("Change the favicon to a sun");
    container.addWidget(sunFaviconButton);
    WPushButton cloudFaviconButton = new WPushButton("Change the favicon to a cloud");
    container.addWidget(cloudFaviconButton);
    WPushButton snowFaviconButton = new WPushButton("Change the favicon to a snowflake");
    container.addWidget(snowFaviconButton);
    WPushButton defaultFaviconButton = new WPushButton("Go back to the default favicon");
    container.addWidget(defaultFaviconButton);
    sunFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WResourceFavicon favicon =
                  new WResourceFavicon(new WWebRootDataInfo("icons/sun01.png").getFilePath());
              WApplication.getInstance().setFavicon(favicon);
            });
    cloudFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WResourceFavicon favicon =
                  new WResourceFavicon(new WWebRootDataInfo("icons/w_cloud.png").getFilePath());
              WApplication.getInstance().setFavicon(favicon);
            });
    snowFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WResourceFavicon favicon =
                  new WResourceFavicon(new WWebRootDataInfo("icons/snow.png").getFilePath());
              WApplication.getInstance().setFavicon(favicon);
            });
    defaultFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WApplication.getInstance().setFavicon((WFavicon) null);
            });
  }

Favicon Pair

A WFavicon has 2 states: the default state and the updated state. This state can be changed using the update() and reset() methods. Basic WFavicon are the same image in both states, but a WFaviconPair is a favicon that shows a different WFavicon in each state.

Example
source
  void FaviconPair() {
    WContainerWidget container = new WContainerWidget();
    WPushButton setCustomFaviconButton = new WPushButton("Set custom favicon");
    container.addWidget(setCustomFaviconButton);
    WPushButton updateFaviconButton = new WPushButton("Update favicon");
    container.addWidget(updateFaviconButton);
    WPushButton resetFaviconButton = new WPushButton("Reset favicon");
    container.addWidget(resetFaviconButton);
    setCustomFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WResourceFavicon defaultFavicon =
                  new WResourceFavicon(new WWebRootDataInfo("icons/jwt.png").getFilePath());
              WResourceFavicon updatedFavicon =
                  new WResourceFavicon(new WWebRootDataInfo("icons/jwt-update.png").getFilePath());
              WFaviconPair faviconPair = new WFaviconPair(defaultFavicon, updatedFavicon);
              WApplication.getInstance().setFavicon(faviconPair);
            });
    updateFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WApplication.getInstance().getFavicon().update();
            });
    resetFaviconButton
        .clicked()
        .addListener(
            this,
            () -> {
              WApplication.getInstance().getFavicon().reset();
            });
  }