Desktop.Menu behaviour (Desktop v1.3.4) View Source

Menu module used to create and handle menus in Desktop

Menues are defined similiar to Live View using a callback module an XML:

  defmodule ExampleMenuBar do
    use Desktop.Menu

    @impl true
    def mount(menu) do
      menu = assign(menu, items: ExampleRepo.all_items())
      {:ok, menu}
    end

    @impl true
    def handle_event(command, menu) do
      case command do
        <<"open">> -> :not_implemented
        <<"quit">> -> Desktop.Window.quit()
        <<"help">> -> :wx_misc.launchDefaultBrowser('https://google.com')
        <<"about">> -> :not_implemented
      end

      {:noreply, menu}
    end

    @impl true
    def render(assigns) do
      ~E"""
      <menubar>
        <menu label="<%= gettext "File" %>">
            <item onclick="open"><%= gettext "Open" %></item>
            <hr/>
            <item onclick="quit"><%= gettext "Quit" %></item>
        </menu>
        <menu label="<%= gettext "Items" %>">
          <%= for item <- @items do %>
            <item><%= item.name %></item>
          <% end %>
        </menu>
        <menu label="<%= gettext "Help" %>">
            <item onclick="help"><%= gettext "Show Documentation" %></item>
            <item onclick="about"><%= gettext "About" %></item>
        </menu>
      </menubar>
      """
    end
  end

Template

As in live view the template can either be embedded in the def render(assigns) method or it can be side loaded as a .eex file next to the menues .ex file.

XML Structure

These items are defined:

<menubar>...menues...</menubar>

For an application (window) menubar this must be the root element. When passing a menubar to Desktop.Window start parameters this has to be the root element. It has no attributes

<menu label="Label">...items...</menu>

For an icon menu menu must be the root element. Menu elements can contain multiple children of type menu, item and hr

Attributes

  • label - the label that should be displayed on the menu

<item ...>Label</item>

This is an entry in the menu with a text a type and an onclick action

Attributes

  • onclick - an event name that should be fired when this item is clicked. It will cause handle_event/2 to be called
  • type - the type of the item. The default is normal, but it can be either
    • normal - a normal text item
    • radio - a radio button
    • checkbox - a checkbox item
  • checked - whether the checkbox or radio button should be checked. nil, false and 0 are treated as false values, every other value is treated as true.
  • disabled - whether the item should be disabled. nil, false and 0 are treated as false values, every other value is treated as true.

<hr />

A separator item

Escaping

Within the XML document it will be neccesary to escape dynamic content. Within for normal text content the escape(text) function is safe to use. For xml attributes escape_attribute(value) can be used:

  <menu label="escape_attribute(@menu_label)">
      <item onclick="open"><%= escape(@user_input) %></item>
  </menu>

Link to this section Summary

Link to this section Types

Specs

t() :: %Desktop.Menu{
  __adapter__: any(),
  app: nil,
  assigns: %{},
  dom: any(),
  last_render: nil | DateTime.t(),
  module: module(),
  pid: nil | pid()
}

Link to this section Callbacks

Link to this callback

handle_event(event_name, assigns)

View Source

Specs

handle_event(event_name :: String.t(), assigns :: map()) :: {:noreply, map()}
Link to this callback

handle_info(any, assigns)

View Source

Specs

handle_info(any(), assigns :: map()) :: {:noreply, map()}

Specs

mount(assigns :: map()) :: {:ok, map()}

Specs

render(Keyword.t()) :: String.t()

Link to this section Functions

Link to this function

assign(assigns, properties)

View Source
Link to this function

assign(assigns, property, value)

View Source
Link to this function

assign_new(assigns, property, value)

View Source

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

escape_attribute(string)

View Source
Link to this function

set_icon(menu_pid, icon)

View Source
Link to this function

start!(init_opts \\ [], opts \\ [])

View Source
Link to this function

start_link(init_opts \\ [], opts \\ [])

View Source

Specs

start_link(keyword(), keyword()) :: GenServer.on_start()
Link to this function

trigger_event(menu_pid, event)

View Source