Support presenting GUI dialogs using Zenity

This snapcraft.yaml part definition and corresponding changes enable the usage of Zenity, allowing packagers to present GUI dialogs to the users.

This is normally used in GTK application snaps to present a message or require user input in a relatively friendly user interface:

Also, see Inform users with custom dialogs for the generic usage of dialogs in snaps.

Prerequisites

This section documents the prerequisites for using this Snapcraft part:

Usage

Follow the following instructions to incorporate Zenity into your snaps:

  1. Merge the following part definition into the parts stanza of your snapcraft.yaml file:

    parts:
      # Integrate custom dialogs in your snap - doc - snapcraft.io
      # https://forum.snapcraft.io/t/integrate-custom-dialogs-in-your-snap/10825
      zenity:
        plugin: nil
        stage-packages:
          - zenity
        prime:
          - usr/bin/zenity
          - usr/share/zenity/*
          - usr/share/doc/*/copyright*
    
  2. Merge the following layout definition into the layout stanza of your snapcraft.yaml file:

    layout:
      # Fix resource relocation problem of zenity part
      /usr/share/zenity:
        symlink: $SNAP/usr/share/zenity
    
  3. In your launcher or application, invoke the zenity command as in the following example:

    zenity_opts=(
        # Specify to use the information dialog type
        --info
        # Specify the text of the dialog window's title bar
        --title='Example text dialog'
        # Specify the text of the dialog's body
        --text='This is an example text dialog.'
    )
    if ! zenity "${zenity_opts[@]}"; then
        printf \
            'Error: Unable to present the security confinement warning(home).\n.' \
            1>&2
        exit 1
    fi
    

    This example code snippet should present a dialog similar to the following screenshot:

    Screenshot of example dialog

Refer to the Zenity Manual for more info on how to invoke the zenity command.

Limitations

This implementation uses whatever version of Zenity that’s distributed by the Ubuntu release on which your base is based. It may not provide all the features(including but not limited to, HTML support) you want.

Examples

This section enumerates snaps that make use of this part:

Reference

The following material is referenced during the writing of this article:

1 Like