UI / WIDGETS / 文本输入
PasswordInput
在掩码显示下编辑密码
在演练场运行 Xu 示例 ↗示例由浏览器中的 Xu/WASM 运行器执行。
体验什么
输入示例文字,切换显隐。
使用要点
掩码显示不等于内存加密。原生显隐切换保留值与选择,复制与剪切仍被禁止;应用不得把实际密码写入日志。
通过 Screen 创建
下面的 Screen 构造方法返回节点 ID。将 ID 交给 Screen 的布局、状态与事件接口;它不是下方独立控件类型的实例。
原生 Xu 创建片段:在已有 Screen 的初始化阶段调用 configure;窗口创建、事件循环与资源释放参见完整原生示例。
#ui/widgets
fn configure(screen $&widgets.Screen) int {
id := screen.password_input("Xu-demo-123") or >>
screen.place(id,24.0,24.0,320.0,80.0) or >>
<< id
}
($&Screen) password_input
pub fn (s $&Screen) password_input(value string) int
用途
在 Screen 中创建 PasswordInput,返回组件节点 ID;创建失败时抛出错误。
参数
s $&Screen- 本次调用使用的
Screen 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
value string- value(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 Editor
用途
文本编辑核心控制器,提供光标移动、选区、撤销重做与排版。
公开字段
此类型没有公开字段。
查看用法示例
(&Editor) id
pub fn (editor &Editor) id() int
用途
返回当前控件、元素或节点的唯一标识 ID。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
int — 该类型的值
(&Editor) value
pub fn (editor &Editor) value() string
用途
&Editor 上的操作:值。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
string — 结果字符串
(&Editor) has_selection
pub fn (editor &Editor) has_selection() bool
用途
&Editor 上的操作:判断是否具有 selection。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) selection_range
pub fn (editor &Editor) selection_range() TextRange
用途
&Editor 上的操作:执行 selection range 操作。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
TextRange — 该类型的值
(&Editor) selected_text
pub fn (editor &Editor) selected_text() string
用途
&Editor 上的操作:执行 selected text 操作。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
string — 结果字符串
(&Editor) is_password
pub fn (editor &Editor) is_password() bool
用途
&Editor 上的操作:判断是否 password。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) is_password_visible
pub fn (editor &Editor) is_password_visible() bool
用途
&Editor 上的操作:判断是否 password visible。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Editor) set_password
pub fn (editor $&Editor) set_password(on bool) int
用途
$&Editor 上的操作:设置 password。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
on bool- on(bool)
返回值
int — 该类型的值
($&Editor) set_password_visible
pub fn (editor $&Editor) set_password_visible(visible bool) int
用途
$&Editor 上的操作:设置 password visible。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
visible bool- visible(bool)
返回值
int — 该类型的值
(&Editor) is_focused
pub fn (editor &Editor) is_focused() bool
用途
返回控件当前是否拥有输入焦点。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) is_enabled
pub fn (editor &Editor) is_enabled() bool
用途
返回控件当前是否可用。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) is_selectable
pub fn (editor &Editor) is_selectable() bool
用途
&Editor 上的操作:判断是否 selectable。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) cursor
pub fn (editor &Editor) cursor() int
用途
&Editor 上的操作:游标。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
int — 该类型的值
(&Editor) needs_paint
pub fn (editor &Editor) needs_paint() bool
用途
返回控件是否需要重新绘制;绘制完成后调用 painted 清除标记。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Editor) painted
pub fn (editor $&Editor) painted()
用途
确认当前帧已绘制,清除重绘标记;不是状态查询。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Editor) set_locale
pub fn (editor $&Editor) set_locale(locale &Locale) int
用途
$&Editor 上的操作:设置 locale。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
locale &Locale- locale(只读借用,调用方仍持有原值;类型 &Locale)
返回值
int — 该类型的值
($&Editor) set_viewport
pub fn (editor $&Editor) set_viewport(viewport &layout.Rect)
用途
$&Editor 上的操作:设置 viewport。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
viewport &layout.Rect- viewport(只读借用,调用方仍持有原值;类型 &layout.Rect)
返回值
不返回值。
($&Editor) set_selectable
pub fn (editor $&Editor) set_selectable(selectable bool) int
用途
$&Editor 上的操作:设置 selectable。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
selectable bool- selectable(bool)
返回值
int — 该类型的值
($&Editor) set_placeholder
pub fn (editor $&Editor) set_placeholder(value &string) int
用途
$&Editor 上的操作:设置 placeholder。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
value &string- value(只读借用,调用方仍持有原值;类型 &string)
返回值
int — 该类型的值
($&Editor) focus_session
pub fn (editor $&Editor) focus_session(on bool) int
用途
切换编辑焦点并管理原生输入会话;失焦保留提交值。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
on bool- on(bool)
返回值
成功为 0;失败抛出错误
($&Editor) enable
pub fn (editor $&Editor) enable(on bool) int
用途
在 $&Editor 上执行 enable 操作。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
on bool- on(bool)
返回值
int — 该类型的值
($&Editor) place
pub fn (editor $&Editor) place(bounds &layout.Rect) int
用途
设置画布全局矩形;布局或祖先滚动后更新,以同步原生 IME 几何。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bounds &layout.Rect- bounds(只读借用,调用方仍持有原值;类型 &layout.Rect)
返回值
成功为 0;失败抛出错误
($&Editor) set_value
pub fn (editor $&Editor) set_value(value &string) int
用途
$&Editor 上的操作:设置 value。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
value &string- value(只读借用,调用方仍持有原值;类型 &string)
返回值
int — 该类型的值
($&Editor) route
pub fn (editor $&Editor) route(kind int,event &ui.InputEvent) Action
用途
路由并分发交互事件至命中的子节点或处理器。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
kind int- 类型标签(int)
event &ui.InputEvent- event(只读借用,调用方仍持有原值;类型 &ui.InputEvent)
返回值
Action — 输入处理结果,包含 kind、id、重绘与焦点请求。
($&Editor) event
pub fn (editor $&Editor) event(event &ui.Event) Action
用途
处理并响应窗口与系统的交互及生命周期事件。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
event &ui.Event- event(只读借用,调用方仍持有原值;类型 &ui.Event)
返回值
Action — 输入处理结果,包含 kind、id、重绘与焦点请求。
($&Editor) paint
pub fn (editor $&Editor) paint(frame $&draw.Frame,theme &Theme) int
用途
在目标画布或渲染目标上执行控件的视觉绘制。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
frame $&draw.Frame- frame(独占借用,可修改原值;类型 $&draw.Frame)
theme &Theme- theme(只读借用,调用方仍持有原值;类型 &Theme)
返回值
int — 该类型的值
($&Editor) dispose
pub fn (editor $&Editor) dispose()
用途
释放并清理对象所持有的系统原生资源与底层句柄。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
(&Editor) caret_opacity
pub fn (editor &Editor) caret_opacity() f64
用途
&Editor 上的操作:执行 caret opacity 操作。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
f64 — 该类型的值
(&Editor) has_caret_animation
pub fn (editor &Editor) has_caret_animation() bool
用途
&Editor 上的操作:判断是否具有 caret animation。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) next_caret_tick
pub fn (editor &Editor) next_caret_tick(now time.Instant,reduced_motion bool) time.Instant
用途
&Editor 上的操作:执行 next caret tick 操作。失败时抛出错误,调用方需处理或继续传播。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
now time.Instant- now(time.Instant)
reduced_motion bool- reduced_motion(bool)
返回值
time.Instant — 该类型的值
($&Editor) tick
pub fn (editor $&Editor) tick(now time.Instant,reduced_motion bool) bool
用途
推进动画时间戳与状态过渡动画步进。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
now time.Instant- now(time.Instant)
reduced_motion bool- reduced_motion(bool)
返回值
bool — 是否成立
(&Editor) bounds
pub fn (editor &Editor) bounds() layout.Rect
用途
返回当前控件或几何对象的有效外接矩形边界(Rectangle)。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
layout.Rect — 该类型的值
(&Editor) is_invalid
pub fn (editor &Editor) is_invalid() bool
用途
&Editor 上的操作:判断是否 invalid。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) is_hovered
pub fn (editor &Editor) is_hovered() bool
用途
&Editor 上的操作:判断是否 hovered。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) visual_state
pub fn (editor &Editor) visual_state() int
用途
&Editor 上的操作:执行 visual state 操作。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
int — 该类型的值
(&Editor) has_style
pub fn (editor &Editor) has_style() bool
用途
&Editor 上的操作:判断是否具有 style。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Editor) style_or
pub fn (editor &Editor) style_or(styles &Styles) ControlStyle
用途
&Editor 上的操作:执行 style or 操作。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
ControlStyle — 该类型的值
($&Editor) set_style
pub fn (editor $&Editor) set_style(style &ControlStyle) int
用途
$&Editor 上的操作:设置 style。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
style &ControlStyle- style(只读借用,调用方仍持有原值;类型 &ControlStyle)
返回值
int — 该类型的值
($&Editor) clear_style
pub fn (editor $&Editor) clear_style()
用途
$&Editor 上的操作:执行 clear style 操作。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Editor) apply_styles
pub fn (editor $&Editor) apply_styles(styles &Styles) int
用途
应用并验证主题;指标变化时更新布局 / 几何并标记重绘。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
成功为 0;失败抛出错误
($&Editor) paint_motion_styled
pub fn (editor $&Editor) paint_motion_styled(frame $&draw.Frame,styles &Styles,motion $&PaintTransition,now time.Instant,reduced_motion bool) int
用途
$&Editor 上的操作:执行 paint motion styled 操作。失败时抛出错误,调用方需处理或继续传播。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
frame $&draw.Frame- frame(独占借用,可修改原值;类型 $&draw.Frame)
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
motion $&PaintTransition- motion(独占借用,可修改原值;类型 $&PaintTransition)
now time.Instant- now(time.Instant)
reduced_motion bool- reduced_motion(bool)
返回值
int — 该类型的值
($&Editor) set_invalid
pub fn (editor $&Editor) set_invalid(invalid bool)
用途
$&Editor 上的操作:设置 invalid。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
invalid bool- invalid(bool)
返回值
不返回值。
(&Editor) is_read_only
pub fn (editor &Editor) is_read_only() bool
用途
&Editor 上的操作:判断是否 read only。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Editor) set_read_only
pub fn (editor $&Editor) set_read_only(on bool) int
用途
切换只读并释放或重启原生输入会话;保留选择、导航、复制,拒绝只读期间的写入与迟到 IME 提交。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
on bool- on(bool)
返回值
成功为 0;失败抛出错误
(&Editor) is_copy_enabled
pub fn (editor &Editor) is_copy_enabled() bool
用途
&Editor 上的操作:判断是否 copy enabled。
参数
editor &Editor- 本次调用使用的
Editor 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Editor) set_copy_enabled
pub fn (editor $&Editor) set_copy_enabled(on bool)
用途
$&Editor 上的操作:设置 copy enabled。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
on bool- on(bool)
返回值
不返回值。
($&Editor) measure_styled
pub fn (editor $&Editor) measure_styled(styles &Styles) layout.Size
用途
按有效字体、密度、内容和最小尺寸测量Editor。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
固有逻辑尺寸;原生排版失败时抛出错误
($&Editor) paint_styled
pub fn (editor $&Editor) paint_styled(frame $&draw.Frame,styles &Styles) int
用途
使用当前完整主题绘制Editor;控件边界与 Frame 使用相同的局部坐标。
参数
editor $&Editor- 本次调用使用的
Editor 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
frame $&draw.Frame- frame(独占借用,可修改原值;类型 $&draw.Frame)
styles &Styles- styles(只读借用,调用方仍持有原值;类型 &Styles)
返回值
成功为 0;失败抛出错误
完整原生示例
Component Workshop · 返回 Widgets 目录