This module provides the base view component for the terminal UI system.
::: tip TIP
To use this module, you need to import it first: import("core.ui.view")
:::
::: 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 view module is the base class for all UI components. It extends object and provides core view functionality including drawing, event handling, state management, option management, and attribute management.
::: tip API
view:new(name: <string>, bounds: <rect>, ...)
:::
| Parameter | Description |
|---|---|
| name | Required. View name string |
| bounds | Required. View bounds rectangle |
| ... | Variable arguments |
| Type | Description |
|---|---|
| view | Returns a view instance |
Create a view instance:
import("core.ui.view")
import("core.ui.rect")
local v = view:new("myview", rect{1, 1, 80, 25})
::: tip API
view:name()
:::
No parameters
| Type | Description |
|---|---|
| string | Returns the view name |
Get the view name:
local name = v:name()
print(name) -- Output: myview
::: tip API
view:bounds()
:::
No parameters
| Type | Description |
|---|---|
| rect | Returns the view bounds rectangle |
Get the view bounds:
local bounds = v:bounds()
print(bounds:sx(), bounds:sy(), bounds:ex(), bounds:ey())
::: tip API
view:bounds_set(bounds: <rect>)
:::
| Parameter | Description |
|---|---|
| bounds | Required. New bounds rectangle |
No return value
Set the view bounds:
v:bounds_set(rect{10, 5, 50, 20})
::: tip API
view:width()
:::
No parameters
| Type | Description |
|---|---|
| number | Returns the view width |
Get the view width:
local w = v:width()
::: tip API
view:height()
:::
No parameters
| Type | Description |
|---|---|
| number | Returns the view height |
Get the view height:
local h = v:height()
::: tip API
view:parent()
:::
No parameters
| Type | Description |
|---|---|
| view | Returns the parent view instance or nil |
Get the parent view:
local parent = v:parent()
::: tip API
view:application()
:::
No parameters
| Type | Description |
|---|---|
| application | Returns the application instance |
Get the application instance:
local app = v:application()
app:quit()
::: tip API
view:canvas()
:::
No parameters
| Type | Description |
|---|---|
| canvas | Returns the canvas instance |
Get the canvas for drawing operations:
local canvas = v:canvas()
canvas:move(0, 0):putstr("Hello")
::: tip API
view:state(name: <string>)
:::
| Parameter | Description |
|---|---|
| name | Required. State name |
| Type | Description |
|---|---|
| any | Returns the state value |
Get the view state. Common states include:
visible: Whether visiblecursor_visible: Whether cursor is visibleselected: Whether selectedfocused: Whether focused
local is_visible = v:state("visible")
::: tip API
view:state_set(name: <string>, enable: <boolean>)
:::
| Parameter | Description |
|---|---|
| name | Required. State name |
| enable | Required. Whether to enable |
| Type | Description |
|---|---|
| view | Returns the view instance (for chaining) |
Set the view state:
v:state_set("visible", true)
::: tip API
view:option(name: <string>)
:::
| Parameter | Description |
|---|---|
| name | Required. Option name |
| Type | Description |
|---|---|
| any | Returns the option value |
Get the view option. Common options include:
selectable: Whether selectablemouseable: Whether mouse support
local is_selectable = v:option("selectable")
::: tip API
view:option_set(name: <string>, enable: <boolean>)
:::
| Parameter | Description |
|---|---|
| name | Required. Option name |
| enable | Required. Whether to enable |
No return value
Set the view option:
v:option_set("selectable", true)
::: tip API
view:attr(name: <string>)
:::
| Parameter | Description |
|---|---|
| name | Required. Attribute name |
| Type | Description |
|---|---|
| any | Returns the attribute value |
Get the view attribute:
local bg = v:attr("background")
::: tip API
view:attr_set(name: <string>, value: <any>)
:::
| Parameter | Description |
|---|---|
| name | Required. Attribute name |
| value | Required. Attribute value |
| Type | Description |
|---|---|
| view | Returns the view instance (for chaining) |
Set the view attribute:
v:attr_set("background", "blue")
::: tip API
view:background()
:::
No parameters
| Type | Description |
|---|---|
| string | Returns the background color name |
Get the background color:
local bg = v:background()
::: tip API
view:background_set(color: <string>)
:::
| Parameter | Description |
|---|---|
| color | Required. Color name |
| Type | Description |
|---|---|
| view | Returns the view instance (for chaining) |
Set the background color:
v:background_set("blue")
::: tip API
view:action_set(name: <string>, on_action: <function>)
:::
| Parameter | Description |
|---|---|
| name | Required. Action name |
| on_action | Required. Action handler function |
| Type | Description |
|---|---|
| view | Returns the view instance (for chaining) |
Set the action handler:
import("core.ui.action")
v:action_set(action.ac_on_clicked, function (view)
print("View clicked")
end)
::: tip API
view:action_on(name: <string>, ...)
:::
| Parameter | Description |
|---|---|
| name | Required. Action name |
| ... | Variable arguments, passed to action handler |
| Type | Description |
|---|---|
| any | Returns the action handler's return value |
Trigger an action:
import("core.ui.action")
v:action_on(action.ac_on_clicked)
::: tip API
view:invalidate(bounds?: <boolean>)
:::
| Parameter | Description |
|---|---|
| bounds | Optional. Whether resize is needed |
No return value
Invalidate the view to trigger redraw:
v:invalidate() -- Mark as needing redraw
v:invalidate(true) -- Mark as needing resize
::: tip API
view:show(visible: <boolean>, opt?: <table>)
:::
| Parameter | Description |
|---|---|
| visible | Required. Whether to show |
| opt | Optional. Option table, supports: {focused = true} |
No return value
Show or hide the view:
v:show(true) -- Show
v:show(false) -- Hide
v:show(true, {focused = true}) -- Show and focus
::: tip API
view:cursor_move(x: <number>, y: <number>)
:::
| Parameter | Description |
|---|---|
| x | Required. Cursor X coordinate |
| y | Required. Cursor Y coordinate |
| Type | Description |
|---|---|
| view | Returns the view instance (for chaining) |
Move cursor position:
v:cursor_move(10, 5)
::: tip API
view:cursor_show(visible: <boolean>)
:::
| Parameter | Description |
|---|---|
| visible | Required. Whether to show cursor |
| Type | Description |
|---|---|
| view | Returns the view instance (for chaining) |
Show or hide cursor:
v:cursor_show(true) -- Show cursor
v:cursor_show(false) -- Hide cursor
::: tip API
view:on_draw(transparent: <boolean>)
:::
| Parameter | Description |
|---|---|
| transparent | Optional. Whether to draw transparently |
No return value
This method is automatically called when drawing the view. Override in subclasses to implement custom drawing:
function my_view:on_draw(transparent)
-- Call parent method to draw background
view.on_draw(self, transparent)
-- Custom drawing content
local canvas = self:canvas()
canvas:move(0, 0):putstr("My View")
end
::: tip API
view:on_event(e: <event>)
:::
| Parameter | Description |
|---|---|
| e | Required. Event object |
| Type | Description |
|---|---|
| boolean | Returns true if event was handled, otherwise returns false |
This method is automatically called when handling events. Override in subclasses to implement custom event handling:
function my_view:on_event(e)
if e.type == event.ev_keyboard and e.key_name == "Enter" then
-- Handle Enter key
return true
end
return view.on_event(self, e)
end
Here is a complete custom view example:
import("core.ui.view")
import("core.ui.window")
import("core.ui.rect")
import("core.ui.application")
import("core.ui.event")
import("core.ui.action")
-- Define custom view
local myview = myview or view()
function myview:init(name, bounds)
view.init(self, name, bounds)
self:background_set("cyan")
self:option_set("selectable", true)
end
function myview:on_draw(transparent)
view.on_draw(self, transparent)
local canvas = self:canvas()
local textattr = curses.calc_attr({"yellow", "bold"})
canvas:attr(textattr):move(0, 0):putstr("Custom View")
end
function myview:on_event(e)
if e.type == event.ev_keyboard and e.key_name == "Enter" then
print("Custom view activated")
return true
end
return view.on_event(self, e)
end
-- Use custom view
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}, "Custom View Demo")
-- Create custom view
local custom = myview:new("custom", rect{10, 5, 40, 10})
-- Add view to window panel
local panel = win:panel()
panel:insert(custom)
panel:select(custom)
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