This module provides a clickable button component for the terminal UI.
::: tip TIP
To use this module, you need to import it first: import("core.ui.button")
:::
::: tip NOTE
The UI module is primarily used for xmake's internal xmake f --menu menu-based visual configuration. It provides basic UI components that can also be used by users to implement their own terminal UIs.
:::
The button module extends label and provides a clickable button with:
::: tip API
button:new(name: <string>, bounds: <rect>, text: <string>, on_action?: <function>)
:::
| Parameter | Description |
|---|---|
| name | Required. Button name string |
| bounds | Required. Button bounds rectangle |
| text | Required. Button text to display |
| on_action | Optional. Function to call when button is activated |
| Type | Description |
|---|---|
| button | Returns a button instance |
Create a button with action handler:
import("core.ui.button")
import("core.ui.rect")
local btn = button:new("ok", rect{10, 5, 20, 1}, "Ok", function (v)
print("Button clicked!")
v:parent():quit()
end)
::: tip API
button:text()
:::
No parameters
| Type | Description |
|---|---|
| string | Returns the button text |
Get the button text:
local text = btn:text()
print(text) -- Output: Ok
::: tip API
button:text_set(text: <string>)
:::
| Parameter | Description |
|---|---|
| text | Required. Button text to display |
| Type | Description |
|---|---|
| button | Returns the button instance (for method chaining) |
Set or update the button text:
btn:text_set("Cancel")
btn:text_set("< OK >") -- Can use brackets for visual styling
::: tip API
button:textattr()
:::
No parameters
| Type | Description |
|---|---|
| string | Returns the text attribute string |
Get the current text attribute:
local attr = btn:textattr()
::: tip API
button:textattr_set(attr: <string>)
:::
| Parameter | Description |
|---|---|
| attr | Required. Text attribute string (e.g., "red bold") |
| Type | Description |
|---|---|
| button | Returns the button instance (for method chaining) |
Set button text color and style:
btn:textattr_set("green") -- Green text
btn:textattr_set("yellow bold") -- Yellow bold text
btn:textattr_set("cyan onblack") -- Cyan text on black background
::: tip API
button:action_set(action_type: <string>, handler: <function>)
:::
| Parameter | Description |
|---|---|
| action_type | Required. Action type (e.g., action.ac_on_enter) |
| handler | Required. Function to call when action is triggered |
| Type | Description |
|---|---|
| button | Returns the button instance (for method chaining) |
Set custom action handler:
import("core.ui.action")
btn:action_set(action.ac_on_enter, function (v)
print("Button activated!")
end)
::: tip API
button:on_event(e: <event>)
:::
| Parameter | Description |
|---|---|
| e | Required. Event object |
| Type | Description |
|---|---|
| boolean | Returns true if Enter key was handled, nil otherwise |
This is automatically called when the button receives keyboard events. When selected and Enter is pressed, it triggers the button's action.
::: tip API
button:on_draw(transparent: <boolean>)
:::
| Parameter | Description |
|---|---|
| transparent | Optional. Draw with transparency |
No return value
This is automatically called when the button needs to be drawn. The button is displayed with reverse video (inverted colors) when selected and focused. Here's a complete example:
import("core.ui.button")
import("core.ui.rect")
import("core.ui.application")
import("core.ui.window")
import("core.ui.action")
local demo = application()
function demo:init()
application.init(self, "demo")
self:background_set("blue")
-- Create window
local win = window:new("main", rect{1, 1, self:width() - 1, self:height() - 1}, "Button Demo")
-- Create buttons
local ok_btn = button:new("ok", rect{5, 5, 15, 1}, "< OK >", function (v)
print("OK button clicked!")
end)
local cancel_btn = button:new("cancel", rect{25, 5, 20, 1}, "< Cancel >", function (v)
print("Cancel button clicked!")
self:quit()
end)
local apply_btn = button:new("apply", rect{50, 5, 18, 1}, "< Apply >", function (v)
print("Apply button clicked!")
end)
-- Set different text attributes
ok_btn:textattr_set("green")
cancel_btn:textattr_set("red")
apply_btn:textattr_set("cyan")
-- Add buttons to window panel
local panel = win:panel()
panel:insert(ok_btn)
panel:insert(cancel_btn)
panel:insert(apply_btn)
-- Select first button
panel:select(ok_btn)
self:insert(win)
self._win = win
end
function demo:on_resize()
self._win:bounds_set(rect{1, 1, self:width() - 1, self:height() - 1})
application.on_resize(self)
end
function main(...)
demo:run(...)
end