输入与表单
本篇是 UI 教程的第 6–8 章:先学会文本输入,再认识勾选、单选、滑块和下拉选择,最后把它们组合成一个带校验的资料表单。上一篇是状态与数据流,下一篇是焦点、键盘与滚动。
本篇的示例都是单文件程序,和第一个应用一样用 //! dep: 声明依赖、用 scoop run <文件> --release 运行(Windows 写 sco)。示例在 macOS 上验证,Linux 上同样可用。
阅读代码前先记住两个写法,后面每个示例都会用到:
- 在 builder lambda 中不能隐式使用组件成员。
column { ... }、button(...) { ... }这类 lambda 有自己的接收者,直接写组件字段name会报implicit use of an outer receiver is not allowed。示例统一在render开头取局部变量(val name = this.name),再在 lambda 中使用;需要修改的状态用state(...)字段,通过.value读写。 - 根视图铺上页面背景。 根组件得到整个窗口的空间;示例用
frame(content)包住内容,以主题的背景色填充,并把内容放在左上角。
6. 文本输入
6.1 编辑模型由调用方持有
文本控件不自己保存文字。你先创建一个 TextEditorState(位于 scoop.ui.text),把它交给 input 或 textarea;控件只是“借用”这个模型来显示和编辑。这样做有几个直接的好处:
- 读取当前文字只需要
editor.text,在render中读取它,文字变化时界面会自动刷新; - 程序可以随时用
editor.replaceAll("...")改写内容,撤销历史、选区也保存在模型里; - 控件因为布局变化被重新创建时,用户输入的内容不会丢失。
通常把 TextEditorState 声明为组件的字段,一个输入框对应一个模型。
6.2 单行与多行输入
input(editor) { ... } 是单行输入框,textarea(editor) { ... } 是多行输入框。常用的配置项:
| 配置 | 作用 |
|---|---|
placeholder |
内容为空时显示的提示文字 |
onSubmit |
单行输入按 Enter、多行输入按 Primary+Enter(macOS 上是 Cmd+Enter)时调用 |
onEdited |
每次用户编辑后调用 |
enabled / readOnly / invalid |
禁用、只读、标记为无效 |
rows(仅 textarea) |
可见行数,类型是 Long,例如 4L |
触屏用户可以轻点定位光标,连续两次轻点选词,三次轻点选中逻辑行,再拖动选区把手调整范围。touch 配置文本手势,touchScroll 配置视口滚动。触摸、键盘和读屏器编辑的是同一个 TextEditorState,选区和撤销历史随模型更新。
6.3 编辑策略:长度、内容与粘贴
policy 决定“哪些编辑可以被接受”。用 textEditPolicy { ... } 构造:
maxLength = Some(20L):限制字符数;singleLine = true:不允许换行;accepts { text -> ... }:检查编辑后的完整文字,返回false时整次编辑被拒绝;paste { candidate -> ... }:只处理粘贴,可以返回TextEditDecision.Accept、Reject,或用Transform(...)改写粘贴进来的内容;transform { candidate -> ... }:对所有来源的编辑做同样的改写。
策略在编辑写入模型之前生效,所以模型中永远不会出现不合规的文字,也不需要你在事后“修正”输入。
6.4 掩码与投影:密码框
密码框设置 protectedText = true,并用 projection = textMask() 将字符显示成 •。“显示密码”只把投影切换为 textProjection(),继续保留保护标记。这样用户可以查看自己输入的文字,辅助技术仍将它识别为受保护的字段,复制和剪切默认受限。切换投影会保留正文、选区与撤销历史。
6.5 输入组与验证码
inputGroup { ... } 把一个输入框和若干附加部分(addon)拼成一个整体:addon { this.text("https://") } 加一段前缀文字,addon(InputGroupAlign.InlineEnd) { this.button(...) } 在末尾加按钮。一个输入组中只能有一个输入控件。
inputOtp(editor, 6L) 是分格的验证码输入:所有格子共享同一个 TextEditorState,默认只接受数字,粘贴时会自动去掉空格和短横线;填满时调用 onComplete。
6.6 完整示例
下面的程序把以上内容放在一个窗口里:
//! dep: scoop:scoop.window:0.1.0
//! dep: scoop:scoop.graphics:0.1.0
//! dep: scoop:scoop.ui:0.1.0
//! dep: scoop:scoop.ui.component:0.1.0
//! dep: scoop:scoop.ui.style:0.1.0
//! dep: scoop:scoop.ui.style.macos:0.1.0
import scoop.window.Utf8Range
import scoop.graphics.Paint
import scoop.ui.*
import scoop.ui.component.*
import scoop.ui.style.Insets
import scoop.ui.style.ThemeMode
import scoop.ui.style.ThemeSource
import scoop.ui.style.currentThemeSource
import scoop.ui.style.read
import scoop.ui.style.macos.macosTheme
import scoop.ui.text.*
/** Keeps only ASCII digits. */
fun digitsOnly(text: String): Boolean {
for (character in text)
if (character.code < 48 || character.code > 57)
return false
return true
}
/** Removes spaces and dashes that a paste brought in, e.g. "138-0013 8000". */
fun stripSeparators(candidate: TextEditCandidate): TextEditDecision {
val removals = ArrayList<TextEditReplacement>()
var offset = 0L
for (character in candidate.after.text) {
val width = character.toString().byteLength
if (character == ' ' || character == '-')
removals.add(TextEditReplacement(Utf8Range(offset, offset + width), ""))
offset += width
}
return TextEditDecision.Transform(removals.toArray())
}
class TextInputs(scope: ComponentScope): Component(scope) {
// The theme in effect, used to paint the page background.
private val theme = currentThemeSource()
// The caller owns every editing model; controls only borrow them.
private val name = TextEditorState()
private val phone = TextEditorState()
private val password = TextEditorState()
private val site = TextEditorState("scoop-lang.org")
private val note = TextEditorState()
private val code = TextEditorState()
private val revealed = state(false)
private val greeting = state("Press Enter in the name field.")
private val verified = state("")
public override fun render(): View {
val pageColor = this.theme.read(scope).tokens.palette.background.background
val name = this.name
val phone = this.phone
val password = this.password
val site = this.site
val note = this.note
val code = this.code
val revealed = this.revealed
val greeting = this.greeting
val verified = this.verified
val content =
column {
this.typography("Text input", TypographyRole.H3)
this.input(name) {
this.placeholder = "Your name"
this.policy = textEditPolicy { this.maxLength = Some(20L) }
this.onSubmit = { editor -> greeting.value = f"Hello, ${editor.text}!" }
}
this.label(greeting.value)
this.input(phone) {
this.placeholder = "Phone (digits only, paste may contain spaces)"
this.policy =
textEditPolicy {
this.maxLength = Some(11L)
this.paste { candidate -> stripSeparators(candidate) }
this.accepts { text -> digitsOnly(text) }
}
}
this.row {
this.input(password) {
this.placeholder = "Password"
this.protectedText = true
this.projection =
if (revealed.value)
textProjection()
else
textMask()
}
this.button(
if (revealed.value)
"Hide"
else
"Show",
{ revealed.value = !revealed.value }
) { this.variant = ButtonVariant.Outline }
this.spacing = 8.0
}
this.inputGroup {
this.addon { this.text("https://") }
this.input(site) { this.placeholder = "example.com" }
this.addon(InputGroupAlign.InlineEnd) {
this.button("Clear", { site.replaceAll("") })
}
}
this.textarea(note) {
this.rows = 4L
this.placeholder = "A longer note. Primary+Enter submits."
this.onSubmit =
{
editor -> greeting.value =
f"Note saved (${editor.text.charLength} chars)"
}
}
this.typography("Verification code", TypographyRole.Small)
this.inputOtp(code, 6L) {
this.onComplete = { editor -> verified.value = f"Code ${editor.text} complete" }
}
this.label(verified.value)
this.spacing = 12.0
this.padding = Insets.all(20.0)
this.alignment = CrossAlignment.Stretch
}
return frame(content) {
this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)
this.style { this.background(Some(Paint(pageColor))) }
}
}
}
fun main() {
val source = ThemeSource(macosTheme(ThemeMode.Light))
context(source) {
runUiApplication("Text input", { scope -> TextInputs(scope) })
}
}
运行:
scoop run ui-forms-text.scoop --release

你会看到一列输入框。在姓名框中输入名字并按 Enter,下方提示变为 Hello, <名字>!;姓名最多 20 个字符。手机号框只接受数字,最多 11 位,粘贴 138-0013 8000 会得到 13800138000,输入字母则不会产生任何变化。点击 Show 显示密码明文,再点 Hide 恢复为圆点。输入组前面是固定的 https://,末尾的 Clear 清空网址。验证码填满 6 位时,下方显示 Code ... complete。
stripSeparators 展示了粘贴改写的写法:candidate.after.text 是粘贴后的完整文字,TextEditReplacement 用 UTF-8 字节范围描述要替换的片段(因此用 byteLength 累加偏移,Utf8Range 来自 scoop.window,需要声明对应依赖)。粘贴结果先经过 paste,再经过 accepts 和长度限制。
7. 选择类控件
7.1 值通过 Binding 传入
勾选框、开关、单选、滑块和下拉选择都不自己保存值,而是接收一个 Binding:控件读取它来显示,用户操作时写回它。最常见的来源是状态的 .binding():
val darkMode = state(false)
// ...
this.switchControl(darkMode.binding())
也可以用 binding({ 读取 }, { 新值 -> 写入 }) 构造“派生”绑定,读写时换算成别的数据——下面的“全选”勾选框就是这样实现的。这种方式让业务代码完全掌握数据:例如在 setter 中忽略新值,就能拒绝某次修改。
7.2 勾选、开关与切换按钮
checkbox(binding):绑定Boolean时是普通勾选框;绑定CheckState时支持三态Unchecked、Checked、Indeterminate(部分选中)。switchControl(binding):开关,绑定Boolean。toggle("Bold", binding):可以按下/弹起的按钮,绑定Boolean。
勾选框本身没有文字。要让点击文字也能切换,用 FieldTarget 把控件和 fieldLabel 关联起来:控件设置 labelTarget = Some(target),旁边放 fieldLabel("文字", target)。第 8 章会详细介绍这个机制。
7.3 单选组
radioGroup(binding) { item(...) } 绑定一个可选值 T?(None 表示还没有选择),每个选项是一个 Choice(key, value, label, enabled):key 是稳定的视图身份,value 是写回 binding 的值,最后的 enabled 可省略。axis = FlexAxis.Horizontal 让选项横向排列。键盘上,Tab 进入当前选中项,方向键在选项间移动并选中。
同一模块还提供 toggleGroup,用一组切换按钮做单选或多选,用法与 radioGroup 相同。
7.4 滑块与多个滑块头
slider(values, RangeSpec(...)) 绑定的是 Array<Double>:数组有几个元素就有几个滑块头(thumb)。一个元素是普通滑块,两个元素就是区间选择。RangeSpec(minimum, maximum, step, minimumGap) 描述取值范围、步长(Some(5.0),None 表示连续)和相邻滑块头之间的最小间距。
拖动时 binding 会连续更新;如果只关心最终值,设置 onValueCommit。
7.5 下拉选择与组合框
select(binding) { item(Choice(...)) } 是从固定列表中选一项的下拉框,用 placeholder 设置未选择时的文字,group(key, title) { ... } 和 separator(key) 可以给选项分组。
combobox(value, choices, query) 是可以输入搜索的组合框,多出来的 query 是另一个 TextEditorState,保存搜索框中的文字(与选中值互相独立)。它有两种选择模式,由 binding 的类型决定:
- 绑定
T?:单选,可设showClear = true显示清除按钮; - 绑定
Array<T>:多选,选中项显示为输入框中的小标签(chip),在空输入框按 Backspace 删除最后一个;valueText决定标签上的文字。
choices 可以是 Array<Choice<T>>,也可以用 choiceItems<T> { group(...) { item(...) } } 构造带分组和搜索关键词(keywords)的列表。数据量很大时,实现 ChoiceSource<T> 接口按需提供条目,并配合 filter = ComboboxFilter.Provided 由你自己决定显示哪些结果。
7.6 完整示例
//! dep: scoop:scoop.graphics:0.1.0
//! dep: scoop:scoop.ui:0.1.0
//! dep: scoop:scoop.ui.component:0.1.0
//! dep: scoop:scoop.ui.style:0.1.0
//! dep: scoop:scoop.ui.style.macos:0.1.0
import scoop.graphics.Paint
import scoop.ui.*
import scoop.ui.component.*
import scoop.ui.style.Insets
import scoop.ui.style.ThemeMode
import scoop.ui.style.ThemeSource
import scoop.ui.style.currentThemeSource
import scoop.ui.style.macos.macosTheme
import scoop.ui.text.TextEditorState
class Preferences {
val topics = [state(true), state(false), state(false)]
val topicTargets = [FieldTarget(), FieldTarget(), FieldTarget(), FieldTarget()]
val darkMode = state(false)
val darkTarget = FieldTarget()
val bold = state(false)
val density = state<Int?>(Some(1))
val volume = state<Array<Double>>([40.0])
val price = state<Array<Double>>([20.0, 80.0])
val country = state<String?>(None)
val language = state<String?>(None)
val tags = state<Array<String>>(["Scoop"])
val languageQuery = TextEditorState()
val tagQuery = TextEditorState()
val languages =
[
Choice(ViewKey.Text("scoop"), "Scoop", "Scoop"),
Choice(ViewKey.Text("kotlin"), "Kotlin", "Kotlin"),
Choice(ViewKey.Text("rust"), "Rust", "Rust"),
Choice(ViewKey.Text("swift"), "Swift", "Swift")
]
/** A derived binding: reads summarize the children, writes update all of them. */
fun allTopics(): Binding<CheckState> =
binding(
{
var selected = 0
for (topic in topics)
if (topic.value)
selected += 1
if (selected == 0)
CheckState.Unchecked
else
if (selected == topics.size.toInt())
CheckState.Checked
else
CheckState.Indeterminate
},
{
next -> for (topic in topics)
topic.value = next == CheckState.Checked
}
)
}
class ChoiceDemo(scope: ComponentScope): Component(scope) {
// The theme in effect, used to paint the page background.
private val theme = currentThemeSource()
private val model = Preferences()
public override fun render(): View {
val pageColor = this.theme.current.value.tokens.palette.background.background
val model = this.model
val titles = ["Releases", "Security", "Weekly digest"]
val content =
column {
this.typography("Notifications", TypographyRole.H4)
this.row {
this.checkbox(model.allTopics()) {
this.labelTarget = Some(model.topicTargets[0L])
}
this.fieldLabel("All topics", model.topicTargets[0L])
this.spacing = 8.0
this.alignment = CrossAlignment.Center
}
for (index in 0L ..< 3L)
this.row {
this.checkbox(model.topics[index].binding()) {
this.labelTarget = Some(model.topicTargets[index + 1L])
}
this.fieldLabel(titles[index], model.topicTargets[index + 1L])
this.spacing = 8.0
this.alignment = CrossAlignment.Center
this.padding = Insets(24.0, 0.0, 0.0, 0.0)
}
this.row {
this.switchControl(model.darkMode.binding()) {
this.labelTarget = Some(model.darkTarget)
}
this.fieldLabel("Dark mode", model.darkTarget)
this.toggle("Bold", model.bold.binding())
this.spacing = 12.0
this.alignment = CrossAlignment.Center
}
this.typography("Density", TypographyRole.H4)
this.radioGroup(model.density.binding()) {
this.axis = FlexAxis.Horizontal
this.item(Choice(ViewKey.Number(0L), 0, "Compact"))
this.item(Choice(ViewKey.Number(1L), 1, "Default"))
this.item(Choice(ViewKey.Number(2L), 2, "Roomy"))
}
this.label(f"Volume: ${model.volume.value[0L].toInt()}")
this.slider(model.volume.binding(), RangeSpec(0.0, 100.0, Some(1.0)))
this.label(
f"Price: ${model.price.value[0L].toInt()} – ${model.price.value[1L].toInt()}"
)
this.slider(
model.price.binding(),
RangeSpec(0.0, 100.0, Some(5.0), minimumGap = 10.0)
)
this.row {
this.select(model.country.binding()) {
this.placeholder = "Country"
this.width = Some(180.0)
this.item(Choice(ViewKey.Text("cn"), "cn", "China"))
this.item(Choice(ViewKey.Text("de"), "de", "Germany"))
this.item(Choice(ViewKey.Text("jp"), "jp", "Japan"))
}
this.combobox(model.language.binding(), model.languages, model.languageQuery) {
this.placeholder = "Language"
this.showClear = true
this.width = 200.0
}
this.spacing = 12.0
}
this.combobox(model.tags.binding(), model.languages, model.tagQuery) {
this.placeholder = "Add languages"
this.width = 420.0
this.valueText = Some({ value -> value })
}
this.label(
f"country=${model.country.value ?: "-"} language=${
model.language.value ?: "-"
} tags=${model.tags.value.size}"
)
this.spacing = 12.0
this.padding = Insets.all(20.0)
this.alignment = CrossAlignment.Stretch
}
return frame(content) {
this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)
this.style { this.background(Some(Paint(pageColor))) }
}
}
}
fun main() {
val source = ThemeSource(macosTheme(ThemeMode.Light))
context(source) {
runUiApplication("Choices", { scope -> ChoiceDemo(scope) })
}
}
运行:
scoop run ui-forms-choices.scoop --release

窗口从上到下依次是:
- “All topics” 和三个子项。只勾选部分子项时,“All topics” 显示为横杠(
Indeterminate);点击它会一次勾选或清空全部子项。点击子项的文字同样会切换勾选。 - 开关 “Dark mode” 和切换按钮 “Bold”。
- 横向的单选组,初始选中 “Default”。
- 音量滑块和价格区间滑块;区间的两个滑块头至少相隔 10。
- 国家下拉框和语言组合框(单选),下面是多选的语言组合框,初始带一个
Scoop标签。 - 最底下一行实时显示当前的选择。
注意 Preferences 是一个普通的类:所有状态都放在它里面,组件只持有一个实例,并在 render 开头用 val model = this.model 取出来。值较多时,这比在组件上声明一长串字段更清楚。
8. 表单组合
8.1 标签和控件的关联:FieldTarget
FieldTarget 是一个“关联点”,把标签和控件连接起来:
val emailTarget = FieldTarget()
// The control registers itself as the target...
input(email) { this.labelTarget = Some(emailTarget) }
// ...and the label points to it.
fieldLabel("Email", emailTarget)
关联之后,点击标签会聚焦输入框,或切换勾选框、开关。FieldTarget 和 FocusHandle 一样,需要在多次 render 之间保持同一个对象,所以放在组件或模型的字段里,不要在 render 中新建。
读屏器也通过同一个 FieldTarget 取得控件的标签、说明和错误信息。用 label("Email", target) 时,field 会采用标签的目标。如果表单行只有 title,可在 field 中显式设置 target = Some(emailTarget)。见触摸、手势与无障碍。
8.2 field:标签、说明与错误
field(control) { ... } 把一个控件和它周围的文字排成标准的表单行:
label("文字", target):带关联的标签;不需要关联时用title("文字");description("..."):灰色的说明文字;errors = [...]:错误信息数组,为空时不显示;required = true:在标签旁标出“必填”;orientation = FieldOrientation.Horizontal加controlPlacement = FieldControlPlacement.Start:控件在左、文字在右,适合勾选框和开关。
多个 field 放进 fieldGroup { ... } 获得统一的间距;fieldSet(content) { legend("...") } 给一组字段加上标题;fieldSeparator() 画一条分隔线(也可以带文字)。
8.3 校验:业务决定错误,控件只负责呈现
field 和控件不会自己判断输入是否合法。它们只呈现你给出的结果:errors 中有什么就显示什么,invalid = true 时控件显示为错误样式。判断规则写在你自己的代码里,通常是模型上的一个计算属性:
val nameErrors: Array<String> get() =
if (attempted.value && name.text.charLength == 0L)
["Name is required."]
else
[]
因为这个属性在 render 中读取了 name.text 和 attempted.value,用户每次编辑或点击保存,界面都会重新计算并更新错误信息。attempted 用来实现常见的交互:用户第一次点击保存之前不显示错误,之后错误随输入实时更新。
保存时如果有错误,用 FocusHandle.requestFocus() 把焦点移到第一个出错的输入框——FocusHandle 通过控件的 focus = FocusOptions(Some(handle)) 关联。焦点的更多用法见下一篇。
8.4 实战:资料表单
//! dep: scoop:scoop.graphics:0.1.0
//! dep: scoop:scoop.ui:0.1.0
//! dep: scoop:scoop.ui.component:0.1.0
//! dep: scoop:scoop.ui.style:0.1.0
//! dep: scoop:scoop.ui.style.macos:0.1.0
import scoop.text.contains
import scoop.graphics.Paint
import scoop.ui.*
import scoop.ui.component.*
import scoop.ui.style.Insets
import scoop.ui.style.ThemeMode
import scoop.ui.style.ThemeSource
import scoop.ui.style.currentThemeSource
import scoop.ui.style.macos.macosTheme
import scoop.ui.text.*
/** Application data, validation rules and actions; the UI only presents them. */
class ProfileForm {
val name = TextEditorState()
val email = TextEditorState()
val bio = TextEditorState()
val plan = state<Int?>(Some(0))
val newsletter = state(true)
val nameTarget = FieldTarget()
val emailTarget = FieldTarget()
val bioTarget = FieldTarget()
val newsletterTarget = FieldTarget()
val nameFocus = FocusHandle()
val emailFocus = FocusHandle()
// Errors stay hidden until the first save attempt.
val attempted = state(false)
val saved = state("")
val nameErrors: Array<String> get() =
if (attempted.value && name.text.charLength == 0L)
["Name is required."]
else
[]
val emailErrors: Array<String> get() {
if (!attempted.value)
return []
if (email.text.charLength == 0L)
return ["Email is required."]
if (!email.text.contains("@"))
return ["Include an @ sign."]
return []
}
fun save() {
attempted.value = true
if (nameErrors.size > 0L)
nameFocus.requestFocus()
else
if (emailErrors.size > 0L)
emailFocus.requestFocus()
else
saved.value = f"Saved ${name.text} <${email.text}>"
}
fun reset() {
name.replaceAll("")
email.replaceAll("")
bio.replaceAll("")
plan.value = Some(0)
newsletter.value = true
attempted.value = false
saved.value = ""
}
}
class ProfilePage(scope: ComponentScope): Component(scope) {
// The theme in effect, used to paint the page background.
private val theme = currentThemeSource()
private val form = ProfileForm()
public override fun render(): View {
val form = this.form
val pageColor = this.theme.current.value.tokens.palette.background.background
val bioLimit = 160L
// Build controls outside the builder lambdas: inside them, `input(...)`
// would resolve to the builder's own member that adds a child.
val nameInput =
input(form.name) {
this.labelTarget = Some(form.nameTarget)
this.focus = FocusOptions(Some(form.nameFocus))
this.invalid = form.nameErrors.size > 0L
}
val emailInput =
input(form.email) {
this.placeholder = "[email protected]"
this.labelTarget = Some(form.emailTarget)
this.focus = FocusOptions(Some(form.emailFocus))
this.invalid = form.emailErrors.size > 0L
}
val bioInput =
textarea(form.bio) {
this.rows = 3L
this.labelTarget = Some(form.bioTarget)
this.policy = textEditPolicy { this.maxLength = Some(bioLimit) }
}
val planChoice =
radioGroup(form.plan.binding()) {
this.item(Choice(ViewKey.Text("personal"), 0, "Personal"))
this.item(Choice(ViewKey.Text("team"), 1, "Team"))
}
val newsletterBox =
checkbox(form.newsletter.binding()) { this.labelTarget = Some(form.newsletterTarget) }
val account =
fieldGroup {
this.field(nameInput) {
this.label("Display name", form.nameTarget)
this.required = true
this.errors = form.nameErrors
}
this.field(emailInput) {
this.label("Email", form.emailTarget)
this.required = true
this.description("We only use it for sign-in.")
this.errors = form.emailErrors
}
this.field(bioInput) {
this.label("Bio", form.bioTarget)
this.description(f"${form.bio.text.charLength} / ${bioLimit} characters")
}
}
val preferences =
fieldGroup {
this.field(planChoice) { this.title("Plan") }
this.field(newsletterBox) {
this.orientation = FieldOrientation.Horizontal
this.controlPlacement = FieldControlPlacement.Start
this.label("Monthly newsletter", form.newsletterTarget)
}
}
val page =
scrollArea(
column {
this.typography("Profile", TypographyRole.H3)
this.fieldSet(account) { this.legend("Account") }
this.fieldSeparator()
this.fieldSet(preferences) { this.legend("Preferences") }
this.row {
this.button("Save", { form.save() })
this.button("Reset", { form.reset() }) {
this.variant = ButtonVariant.Outline
}
this.spacing = 8.0
}
this.label(form.saved.value)
this.spacing = 16.0
this.padding = Insets.all(20.0)
this.alignment = CrossAlignment.Stretch
}
)
return frame(page) {
this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)
this.style { this.background(Some(Paint(pageColor))) }
}
}
}
fun main() {
val source = ThemeSource(macosTheme(ThemeMode.Light))
context(source) {
runUiApplication("Profile", { scope -> ProfilePage(scope) })
}
}
运行:
scoop run ui-forms-profile.scoop --release

窗口中有 “Account” 和 “Preferences” 两组字段。直接点击 Save:Display name 和 Email 下方出现错误,输入框变为错误样式,焦点移到 Display name。填好名字再保存,错误转移到 Email;输入不带 @ 的地址会提示 Include an @ sign.。两项都合法后,底部显示 Saved <名字> <<邮箱>>。Bio 下方实时显示字数,超过 160 个字符的输入不会被接受。点击 “Monthly newsletter” 文字可以切换勾选框。Reset 清空所有内容,错误也随之隐藏。
这个例子有几处值得模仿:
- 数据、规则和动作都在
ProfileForm里,组件只负责把它们摆成界面。规则可以单独测试,界面也可以换一种布局而不改规则。 - 先在 builder 外创建控件,再交给
field。在fieldGroup { ... }内部直接写input(...)会解析成 builder 自己的成员方法(它把子视图加入列表并返回Unit),而field需要的是一个View。另一种写法是导入别名,例如import scoop.ui.component.input as makeInput。 - 整页放进
scrollArea,窗口变矮时仍能滚动到底部的按钮。
常见错误与继续阅读
| 现象 | 原因与解决 |
|---|---|
implicit use of an outer receiver is not allowed |
在 builder lambda 中直接使用了组件字段。在 render 开头取局部变量后再使用 |
dependency function argument must be of type View, found Unit |
在 builder lambda 内调用了同名的成员方法(如 input、checkbox),它返回 Unit。在 lambda 外创建控件,或使用导入别名 |
| 输入框的内容在界面刷新后被清空 | 在 render 中新建了 TextEditorState。把它放在组件或模型的字段里 |
| 点击标签没有反应 | 控件没有设置 labelTarget,或标签和控件用的不是同一个 FieldTarget |
| 粘贴改写后文字错位 | TextEditReplacement 的范围是 UTF-8 字节偏移,按 byteLength 累加,不要用字符下标 |
import scoop.window.Utf8Range 报 import target is not available |
单文件程序需要为直接导入的 Cone 声明依赖://! dep: scoop:scoop.window:0.1.0 |
| 期望控件自动检查“必填”“邮箱格式” | 控件只呈现结果;规则写在业务代码中,通过 errors 和 invalid 交给控件 |
下一篇焦点、键盘与滚动介绍 Tab 顺序、程序聚焦、快捷键、滚动区域和可以承载上亿条数据的虚拟列表。完整的控件清单见 scoop.ui.component 的 README 与 shadcn 示例(安装后位于 <Root>/src/scoop.ui.component/(<Root> 是 scoop-install show 显示的工具链根目录))。