UI / WIDGETS / 操作
IconButton
用图标承载紧凑操作
在演练场运行 Xu 示例 ↗示例由浏览器中的 Xu/WASM 运行器执行。
体验什么
切换收藏,保留可理解的文字名称。
使用要点
图标按钮仍需可理解的操作名称,必要时配合 Tooltip。图标大小与点击区域分开设置。
通过 Screen 创建
下面的 Screen 构造方法返回节点 ID。将 ID 交给 Screen 的布局、状态与事件接口;它不是下方独立控件类型的实例。
原生 Xu 创建片段:在已有 Screen 的初始化阶段调用 configure;窗口创建、事件循环与资源释放参见完整原生示例。
#ui/widgets
fn configure(screen $&widgets.Screen) int {
id := screen.icon_button("www/logo.png","Favorite") or >>
screen.place(id,24.0,24.0,320.0,80.0) or >>
<< id
}
($&Screen) icon_button
pub fn (s $&Screen) icon_button(asset string,text string) int
用途
在 Screen 中创建 IconButton,返回组件节点 ID;创建失败时抛出错误。
参数
s $&Screen- 本次调用使用的
Screen 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
asset string- asset(string)
text string- 文本(string)
返回值
int — 组件节点 ID,供 place 和其他 Screen 方法使用。
放置、状态与事件
组件由应用拥有。Screen.place 放置节点,Screen.set_enabled 和 Screen.set_visible 管理可用性与显示。状态变化后重新呈现;创建和修改时处理 or 错误,不在绘制回调中修改拓扑。ToastHost 使用自己的队列接口。
Action · Styles · 界面生命周期与样式指南
Screen.place · Screen.set_enabled · Screen.set_visible
独立控件对象 API
以下类型与方法用于自行管理控件、输入和绘制的进阶用法。接收者是控件对象,不是 Screen 返回的节点 ID;普通 Screen 用法请使用上面的创建接口及 Widgets 模块中的 Screen 方法。
struct Button
用途
Button 上下文或状态结构体。
公开字段
此类型没有公开字段。
查看用法示例
(&Button) id
pub fn (button &Button) id() int
用途
返回当前控件、元素或节点的唯一标识 ID。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
int — 该类型的值
(&Button) is_closed
pub fn (button &Button) is_closed() bool
用途
&Button 上的操作:判断是否 closed。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Button) close
pub fn (button $&Button) close()
用途
$&Button 上的操作:关闭资源。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
(&Button) identity
pub fn (button &Button) identity() ui.CanvasEvent
用途
在 &Button 上执行 identity 操作。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
ui.CanvasEvent — 该类型的值
(&Button) needs_paint
pub fn (button &Button) needs_paint() bool
用途
返回控件是否需要重新绘制;绘制完成后调用 painted 清除标记。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_focused
pub fn (button &Button) is_focused() bool
用途
返回控件当前是否拥有输入焦点。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_enabled
pub fn (button &Button) is_enabled() bool
用途
返回控件当前是否可用。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_selectable
pub fn (button &Button) is_selectable() bool
用途
&Button 上的操作:判断是否 selectable。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Button) set_selectable
pub fn (button $&Button) set_selectable(selectable bool)
用途
$&Button 上的操作:设置 selectable。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
selectable bool- selectable(bool)
返回值
不返回值。
(&Button) bounds
pub fn (button &Button) bounds() layout.Rect
用途
返回当前控件或几何对象的有效外接矩形边界(Rectangle)。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
layout.Rect — 该类型的值
($&Button) painted
pub fn (button $&Button) painted()
用途
确认当前帧已绘制,清除重绘标记;不是状态查询。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Button) invalidate
pub fn (button $&Button) invalidate()
用途
标记控件区域为脏,请求系统在下一帧重新绘制。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Button) cancel
pub fn (button $&Button) cancel()
用途
$&Button 上的操作:取消。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Button) set_enabled
pub fn (button $&Button) set_enabled(enabled bool)
用途
$&Button 上的操作:设置 enabled。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
enabled bool- enabled(bool)
返回值
不返回值。
($&Button) set_focus
pub fn (button $&Button) set_focus(focused bool)
用途
$&Button 上的操作:设置 focus。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
focused bool- focused(bool)
返回值
不返回值。
($&Button) set_bounds
pub fn (button $&Button) set_bounds(bounds &layout.Rect) int
用途
$&Button 上的操作:设置 bounds。失败时抛出错误,调用方需处理或继续传播。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bounds &layout.Rect- bounds(只读借用,调用方仍持有原值;类型 &layout.Rect)
返回值
int — 该类型的值
($&Button) input
pub fn (button $&Button) input(input &ui.InputEvent) Action
用途
处理按钮的按下、释放和键盘激活,只有一次完整的激活才触发业务动作。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
input &ui.InputEvent- 同一窗口、画布和代次的输入事件;坐标按控件所在的画布空间传入。
返回值
Action 描述本次交互:kind 是动作类型,consumed 表示事件是否已处理,request_focus 请求焦点,needs_paint 表示需要重绘。
使用时注意
- 在按钮内按下并在内部释放,或聚焦后按下并释放 Enter/空格,产生
ACTION_ACTIVATE;只读状态不会激活。
($&Button) event
pub fn (button $&Button) event(event &ui.Event) Action
用途
处理并响应窗口与系统的交互及生命周期事件。失败时抛出错误,调用方需处理或继续传播。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
event &ui.Event- event(只读借用,调用方仍持有原值;类型 &ui.Event)
返回值
Action — 输入处理结果,包含 kind、id、重绘与焦点请求。
(&Button) paint
pub fn (button &Button) paint(frame $&draw.Frame,label &draw.TextLayout,theme &Theme) int
用途
在目标画布或渲染目标上执行控件的视觉绘制。失败时抛出错误,调用方需处理或继续传播。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
frame $&draw.Frame- frame(独占借用,可修改原值;类型 $&draw.Frame)
label &draw.TextLayout- label(只读借用,调用方仍持有原值;类型 &draw.TextLayout)
theme &Theme- theme(只读借用,调用方仍持有原值;类型 &Theme)
返回值
int — 该类型的值
(&Button) measure_icon_styled
pub fn (button &Button) measure_icon_styled(label $&StyledLabel,styles &Styles) layout.Size
用途
测量图标、标签、间距与内边距;icon_size=0 时不保留图标间距。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
label $&StyledLabel- label(独占借用,可修改原值;类型 $&StyledLabel)
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
固有逻辑尺寸
(&Button) paint_icon_styled
pub fn (button &Button) paint_icon_styled(frame $&draw.Frame,label $&StyledLabel,image &draw.Image,styles &Styles) int
用途
按样式绘制图片图标和标签;图像保持纵横比,调用者拥有 Image。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
frame $&draw.Frame- frame(独占借用,可修改原值;类型 $&draw.Frame)
label $&StyledLabel- label(独占借用,可修改原值;类型 $&StyledLabel)
image &draw.Image- image(只读借用,调用方仍持有原值;类型 &draw.Image)
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
成功为 0;失败抛出错误
(&Button) clone
pub fn (b &Button) clone() Button
用途
复制控件或样式快照,返回独立的 Button 值。
参数
b &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
Button — 该类型的值
(&Button) visual_state
pub fn (button &Button) visual_state() int
用途
&Button 上的操作:执行 visual state 操作。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
int — 该类型的值
(&Button) has_style
pub fn (button &Button) has_style() bool
用途
&Button 上的操作:判断是否具有 style。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) style_or
pub fn (button &Button) style_or(styles &Styles) ControlStyle
用途
&Button 上的操作:执行 style or 操作。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
ControlStyle — 该类型的值
($&Button) set_style
pub fn (button $&Button) set_style(style &ControlStyle) int
用途
$&Button 上的操作:设置 style。失败时抛出错误,调用方需处理或继续传播。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
style &ControlStyle- style(只读借用,调用方仍持有原值;类型 &ControlStyle)
返回值
int — 该类型的值
($&Button) clear_style
pub fn (button $&Button) clear_style()
用途
$&Button 上的操作:执行 clear style 操作。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
(&Button) is_invalid
pub fn (button &Button) is_invalid() bool
用途
&Button 上的操作:判断是否 invalid。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_selected
pub fn (button &Button) is_selected() bool
用途
&Button 上的操作:判断是否 selected。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_read_only
pub fn (button &Button) is_read_only() bool
用途
&Button 上的操作:判断是否 read only。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_hovered
pub fn (button &Button) is_hovered() bool
用途
&Button 上的操作:判断是否 hovered。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Button) is_pressed
pub fn (button &Button) is_pressed() bool
用途
&Button 上的操作:判断是否 pressed。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Button) set_read_only
pub fn (button $&Button) set_read_only(read_only bool)
用途
$&Button 上的操作:设置 read only。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
read_only bool- read_only(bool)
返回值
不返回值。
(&Button) measure_styled
pub fn (button &Button) measure_styled(label $&StyledLabel,styles &Styles) layout.Size
用途
按有效字体、密度、内容和最小尺寸测量Button。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
label $&StyledLabel- label(独占借用,可修改原值;类型 $&StyledLabel)
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
固有逻辑尺寸;原生排版失败时抛出错误
(&Button) paint_styled
pub fn (button &Button) paint_styled(frame $&draw.Frame,label $&StyledLabel,styles &Styles) int
用途
使用当前完整主题绘制Button;控件边界与 Frame 使用相同的局部坐标。
参数
button &Button- 本次调用使用的
Button 实例;以只读借用传入,调用方仍持有实例。
frame $&draw.Frame- frame(独占借用,可修改原值;类型 $&draw.Frame)
label $&StyledLabel- label(独占借用,可修改原值;类型 $&StyledLabel)
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
成功为 0;失败抛出错误
($&Button) set_invalid
pub fn (button $&Button) set_invalid(invalid bool)
用途
$&Button 上的操作:设置 invalid。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
invalid bool- invalid(bool)
返回值
不返回值。
($&Button) set_selected
pub fn (button $&Button) set_selected(selected bool)
用途
设置按钮的持续选中外观,适合工具栏模式开关;选中与短暂按下是不同状态。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
selected bool- 是否显示为选中;这不会模拟一次点击或发出激活动作。
返回值
不返回值;状态改变时标记需要重绘。
($&Button) route
pub fn (button $&Button) route(kind int,input &ui.InputEvent) Action
用途
路由并分发交互事件至命中的子节点或处理器。失败时抛出错误,调用方需处理或继续传播。
参数
button $&Button- 本次调用使用的
Button 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
kind int- 类型标签(int)
input &ui.InputEvent- input(只读借用,调用方仍持有原值;类型 &ui.InputEvent)
返回值
Action — 输入处理结果,包含 kind、id、重绘与焦点请求。
完整原生示例
Component Workshop · 返回 Widgets 目录