Skip to content

Overrides

Kevin edited this page Jun 12, 2026 · 4 revisions

Overrides

Some properties, methods, and attributes of GObject.Objects are overridden to make declarative UI construction easier and reduce boilerplate.

Gio.Menu

local button = Gtk.MenuButton()
local menu = Gio.Menu()

local item = Gio.MenuItem()
item:set_label('option 1')
item:set_detailed_action('action()')

local submenu = Gio.Menu()

local submenu_item = Gio.MenuItem()
submenu_item:set_label('submenu')
submenu_item:set_submenu(submenu)

menu:append_item(item)
menu:append_item(submenu_item)
button:set_menu_model(menu)
local menubutton = MenuButton {
    Gio.Menu {
        Gio.MenuItem { label = 'option 1', action = 'action()' },
        Gio.MenuItem {
            label = 'submenu',

            -- Shorthand for:
            -- submenu = Gio.Menu { ... }
            Gio.Menu {},
        },
    },
}

Gio.SimpleActionGroup

Gio.Actions can be added directly to a Gio.SimpleActionGroup.

local action_group = Gio.SimpleActionGroup()

local action = Gio.SimpleAction {
    name = 'hello',
}

action_group:insert(action)
local action_group = Gio.SimpleActionGroup {
    Gio.SimpleAction {
        name = 'hello',
        on_activate = function()
            print('Hello!')
        end,
    },
    --- Gio.ActionEntrys are also supported.
    Gio.ActionEntry {
        name = 'hello',
        activate = function()
            print('Hello!')
        end,
    },
}

GLib.Variant

A convenience :decode() method is provided for converting a GLib.Variant into native Lua values.

local variant = GLib.Variant(
    '(a{o(oayays)})',
    {
        {
            ['/path/to/object1'] = {
                '/path/to/object2',
                'bytestring1',
                'bytestring2',
                'string',
            },
        },
    }
)
local bytestring2 = tuple
    :get_child_value(0)
    :get_child_value(0)
    :get_child_value(1)
    :get_child_value(2).value

local bytestring2 = tuple:decode()[1]['/path/to/object1'][3]

Clone this wiki locally