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
endTemplate
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 causehandle_event/2to be calledtype- the type of the item. The default isnormal, but it can be eithernormal- a normal text itemradio- a radio buttoncheckbox- a checkbox item
checked- whether thecheckboxorradiobutton should be checked.nil,falseand0are treated as false values, every other value is treated as true.disabled- whether the item should be disabled.nil,falseand0are 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
Functions
Returns a specification to start this module under a supervisor.
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
Specs
Specs
Specs
Specs
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Specs
start_link(keyword(), keyword()) :: GenServer.on_start()