Menu

日本語

Menu creates application menu bars and native standard menu items.

Appearance

Menu on Ubuntu
Ubuntu
Menu on Windows
Windows
Menu on macOS
macOS

Runnable example

require "uing"

UIng.init

UIng::Menu.new("File") do
  append_item("Open").on_clicked do |window|
    window.try &.open_file
  end
  append_separator
  append_preferences_item.on_clicked do |window|
    window.try &.msg_box("Preferences", "Preferences clicked")
  end
  append_separator
  append_quit_item
end

UIng::Menu.new("Edit") do
  append_check_item("Check", checked: false).on_clicked do |_menu_window|
    # No-op
  end
  append_separator
  append_item("Click").on_clicked do |window|
    window.try &.msg_box("Click", "Click menu clicked")
  end
end

UIng::Menu.new("Help") do
  append_about_item.on_clicked do |window|
    window.try &.msg_box("About", "Menu example")
  end
end

# Window must be created after menu finalized.
Window = UIng::Window.new("Menu Example", 300, 50, menubar: true) do
  on_closing do
    UIng.quit
    true
  end

  show
end

UIng.on_should_quit do
  Window.destroy unless Window.released?
  true
end

{% if flag?(:darwin) %}
  label = UIng::Label.new("The Mac menu bar is at the top of the screen.")
  Window.child = label
{% end %}

UIng.main

UIng.uninit

Usage notes

  • Menu items come in the following forms:

    Type Factory method Purpose
    Regular item append_item(name) Runs a command.
    Check item append_check_item(name, checked:) Represents an on/off state. Read or change it with checked? and checked=.
    Preferences item append_preferences_item Opens application preferences.
    About item append_about_item Shows application information.
    Quit item append_quit_item Exits the application. Configure shutdown through UIng.on_should_quit.
    Separator append_separator Visually separates related groups of items.
  • Finalize all menus before creating the first Window, and set menubar: true.

  • Menu callbacks receive an optional Window because some platform-level actions may not have one.
  • Preferences, About, and Quit are standard items that follow OS menu conventions. Their label, placement, and surrounding separators vary by OS, so they may not appear in the Menu or order where you add them.
  • You can add only one append_preferences_item, append_about_item, and append_quit_item item per application.

API reference · Gallery source