($&Screen) button
pub fn (s $&Screen) button(text string) int用途
在 Screen 中创建 Button,返回组件节点 ID;创建失败时抛出错误。
参数
s$&Screen- 本次调用使用的
Screen实例;以独占借用传入,方法可修改它,调用方仍持有实例。 textstring- 文本(string)
返回值
int — 组件节点 ID,供 place 和其他 Screen 方法使用。
触发一项明确的操作
在演练场运行 Xu 示例示例由浏览器中的 Xu/WASM 运行器执行。
点击按钮,或按 Tab 聚焦后按 Enter,观察计数变化。
操作使用明确的动词命名。通过 ACTION_ACTIVATE 处理激活;禁用时不应执行操作,图标与文字均受样式控制。
下面的 Screen 构造方法返回节点 ID。将 ID 交给 Screen 的布局、状态与事件接口;它不是下方独立控件类型的实例。
原生 Xu 创建片段:在已有 Screen 的初始化阶段调用 configure;窗口创建、事件循环与资源释放参见完整原生示例。
#ui/widgets
fn configure(screen $&widgets.Screen) int {
id := screen.button("Save") or >>
screen.place(id,24.0,24.0,320.0,80.0) or >>
<< id
}
($&Screen) buttonpub fn (s $&Screen) button(text string) int在 Screen 中创建 Button,返回组件节点 ID;创建失败时抛出错误。
s $&ScreenScreen 实例;以独占借用传入,方法可修改它,调用方仍持有实例。text stringint — 组件节点 ID,供 place 和其他 Screen 方法使用。
组件由应用拥有。Screen.place 放置节点,Screen.set_enabled 和 Screen.set_visible 管理可用性与显示。状态变化后重新呈现;创建和修改时处理 or 错误,不在绘制回调中修改拓扑。ToastHost 使用自己的队列接口。
Action · Styles · 界面生命周期与样式指南
Screen.place · Screen.set_enabled · Screen.set_visible
以下类型与方法用于自行管理控件、输入和绘制的进阶用法。接收者是控件对象,不是 Screen 返回的节点 ID;普通 Screen 用法请使用上面的创建接口及 Widgets 模块中的 Screen 方法。
struct Buttonpub struct Button {
}Button 上下文或状态结构体。
此类型没有公开字段。
(&Button) idpub fn (button &Button) id() int返回当前控件、元素或节点的唯一标识 ID。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Button) is_closedpub fn (button &Button) is_closed() bool&Button 上的操作:判断是否 closed。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
($&Button) closepub fn (button $&Button) close()$&Button 上的操作:关闭资源。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
(&Button) identitypub fn (button &Button) identity() ui.CanvasEvent在 &Button 上执行 identity 操作。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。ui.CanvasEvent — 该类型的值
(&Button) needs_paintpub fn (button &Button) needs_paint() bool返回控件是否需要重新绘制;绘制完成后调用 painted 清除标记。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_focusedpub fn (button &Button) is_focused() bool返回控件当前是否拥有输入焦点。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_enabledpub fn (button &Button) is_enabled() bool返回控件当前是否可用。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_selectablepub fn (button &Button) is_selectable() bool&Button 上的操作:判断是否 selectable。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
($&Button) set_selectablepub fn (button $&Button) set_selectable(selectable bool)$&Button 上的操作:设置 selectable。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。selectable bool不返回值。
(&Button) boundspub fn (button &Button) bounds() layout.Rect返回当前控件或几何对象的有效外接矩形边界(Rectangle)。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。layout.Rect — 该类型的值
($&Button) paintedpub fn (button $&Button) painted()确认当前帧已绘制,清除重绘标记;不是状态查询。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
($&Button) invalidatepub fn (button $&Button) invalidate()标记控件区域为脏,请求系统在下一帧重新绘制。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
($&Button) cancelpub fn (button $&Button) cancel()$&Button 上的操作:取消。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
($&Button) set_enabledpub fn (button $&Button) set_enabled(enabled bool)$&Button 上的操作:设置 enabled。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。enabled bool不返回值。
($&Button) set_focuspub fn (button $&Button) set_focus(focused bool)$&Button 上的操作:设置 focus。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。focused bool不返回值。
($&Button) set_boundspub fn (button $&Button) set_bounds(bounds &layout.Rect) int$&Button 上的操作:设置 bounds。失败时抛出错误,调用方需处理或继续传播。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。bounds &layout.Rectint — 该类型的值
($&Button) inputpub fn (button $&Button) input(input &ui.InputEvent) Action处理按钮的按下、释放和键盘激活,只有一次完整的激活才触发业务动作。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。input &ui.InputEventAction 描述本次交互:kind 是动作类型,consumed 表示事件是否已处理,request_focus 请求焦点,needs_paint 表示需要重绘。
ACTION_ACTIVATE;只读状态不会激活。($&Button) eventpub fn (button $&Button) event(event &ui.Event) Action处理并响应窗口与系统的交互及生命周期事件。失败时抛出错误,调用方需处理或继续传播。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。event &ui.EventAction — 输入处理结果,包含 kind、id、重绘与焦点请求。
(&Button) paintpub fn (button &Button) paint(frame $&draw.Frame,label &draw.TextLayout,theme &Theme) int在目标画布或渲染目标上执行控件的视觉绘制。失败时抛出错误,调用方需处理或继续传播。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。frame $&draw.Framelabel &draw.TextLayouttheme &Themeint — 该类型的值
(&Button) measure_icon_styledpub fn (button &Button) measure_icon_styled(label $&StyledLabel,styles &Styles) layout.Size测量图标、标签、间距与内边距;icon_size=0 时不保留图标间距。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。label $&StyledLabelstyles &Styles固有逻辑尺寸
(&Button) paint_icon_styledpub fn (button &Button) paint_icon_styled(frame $&draw.Frame,label $&StyledLabel,image &draw.Image,styles &Styles) int按样式绘制图片图标和标签;图像保持纵横比,调用者拥有 Image。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。frame $&draw.Framelabel $&StyledLabelimage &draw.Imagestyles &Styles成功为 0;失败抛出错误
(&Button) clonepub fn (b &Button) clone() Button复制控件或样式快照,返回独立的 Button 值。
b &ButtonButton 实例;以只读借用传入,调用方仍持有实例。Button — 该类型的值
(&Button) visual_statepub fn (button &Button) visual_state() int&Button 上的操作:执行 visual state 操作。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Button) has_stylepub fn (button &Button) has_style() bool&Button 上的操作:判断是否具有 style。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) style_orpub fn (button &Button) style_or(styles &Styles) ControlStyle&Button 上的操作:执行 style or 操作。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。styles &StylesControlStyle — 该类型的值
($&Button) set_stylepub fn (button $&Button) set_style(style &ControlStyle) int$&Button 上的操作:设置 style。失败时抛出错误,调用方需处理或继续传播。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。style &ControlStyleint — 该类型的值
($&Button) clear_stylepub fn (button $&Button) clear_style()$&Button 上的操作:执行 clear style 操作。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
(&Button) is_invalidpub fn (button &Button) is_invalid() bool&Button 上的操作:判断是否 invalid。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_selectedpub fn (button &Button) is_selected() bool&Button 上的操作:判断是否 selected。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_read_onlypub fn (button &Button) is_read_only() bool&Button 上的操作:判断是否 read only。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_hoveredpub fn (button &Button) is_hovered() bool&Button 上的操作:判断是否 hovered。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Button) is_pressedpub fn (button &Button) is_pressed() bool&Button 上的操作:判断是否 pressed。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
($&Button) set_read_onlypub fn (button $&Button) set_read_only(read_only bool)$&Button 上的操作:设置 read only。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。read_only bool不返回值。
(&Button) measure_styledpub fn (button &Button) measure_styled(label $&StyledLabel,styles &Styles) layout.Size按有效字体、密度、内容和最小尺寸测量Button。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。label $&StyledLabelstyles &Styles固有逻辑尺寸;原生排版失败时抛出错误
(&Button) paint_styledpub fn (button &Button) paint_styled(frame $&draw.Frame,label $&StyledLabel,styles &Styles) int使用当前完整主题绘制Button;控件边界与 Frame 使用相同的局部坐标。
button &ButtonButton 实例;以只读借用传入,调用方仍持有实例。frame $&draw.Framelabel $&StyledLabelstyles &Styles成功为 0;失败抛出错误
($&Button) set_invalidpub fn (button $&Button) set_invalid(invalid bool)$&Button 上的操作:设置 invalid。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。invalid bool不返回值。
($&Button) set_selectedpub fn (button $&Button) set_selected(selected bool)设置按钮的持续选中外观,适合工具栏模式开关;选中与短暂按下是不同状态。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。selected bool不返回值;状态改变时标记需要重绘。
($&Button) routepub fn (button $&Button) route(kind int,input &ui.InputEvent) Action路由并分发交互事件至命中的子节点或处理器。失败时抛出错误,调用方需处理或继续传播。
button $&ButtonButton 实例;以独占借用传入,方法可修改它,调用方仍持有实例。kind intinput &ui.InputEventAction — 输入处理结果,包含 kind、id、重绘与焦点请求。