焦点、键盘与滚动
本篇是 UI 教程的第 9–10 章:先让界面能完全用键盘操作,再处理放不下的内容——从普通滚动区域到一亿行的虚拟列表。上一篇是输入与表单,下一篇是弹层与导航。
示例沿用上一篇的约定:单文件程序、scoop run <文件> --release 运行(Windows 写 sco),在 render 开头取局部变量后再在 builder lambda 中使用,根视图用 frame 铺上主题背景。
9. 焦点与键盘
9.1 默认的 Tab 行为
标准控件默认都可以获得焦点并加入 Tab 顺序:按钮、输入框、勾选框、滑块、滚动区域等。不写任何代码时,Tab 按控件挂载的先后(大致就是代码中出现的顺序)前进,Shift+Tab 后退,到窗口末尾后回到开头。按钮在获得焦点时响应 Space 和 Enter;多行输入默认用 Tab 移动焦点,设置 tab = TextTabBehavior.Insert 后才会插入制表符。
用鼠标点击也会移动焦点,但只有键盘操作时才画出焦点环,这是平台惯例。
9.2 FocusOptions:句柄、Tab 停靠与顺序
每个标准控件都有 focus 配置,类型是 FocusOptions(handle, tabStop, order),三个参数都可省略:
| 参数 | 默认值 | 作用 |
|---|---|---|
handle |
None |
传入 Some(FocusHandle()),以便程序查询或请求焦点 |
tabStop |
true |
false 时 Tab 跳过该控件,鼠标和程序聚焦仍然有效 |
order |
0 |
Tab 顺序按 order 从小到大排列,相同值保持挂载顺序 |
order 在整个焦点范围(默认是整个窗口)内排序,而不是只在父容器内。所以 order = -1 能让一个控件成为窗口中第一个 Tab 停靠点;反过来,给某个控件设置正数,它会排到所有默认控件之后。需要局部调整时,优先调整代码顺序,或用 9.4 节的焦点范围把一组控件圈起来。
9.3 程序聚焦:FocusHandle.requestFocus
FocusHandle 是对某个控件的稳定引用。把它放在组件字段中,通过 FocusOptions(Some(handle)) 交给控件,之后就可以:
handle.requestFocus():请求聚焦,返回是否接受了请求;handle.hasFocus:查询当前是否拥有焦点;handle.isPending:请求是否还在等待兑现。
请求并不总是立刻生效。目标在滚动区域中被滚出可见范围时,框架会先把它滚动到可见位置再聚焦;目标还没有完成第一次布局时,请求会保留到该帧被系统接受(Accepted)之后再兑现。这段时间内 isPending 为 true。新的焦点请求会取消尚未兑现的旧请求,所以连续调用不会产生“焦点乱跳”。
焦点只能在事件回调中改变,例如按钮的点击回调、快捷键动作或 onMount;在 render 期间请求焦点会报错。
9.4 焦点范围与方向键导航
下面两项能力是 ComponentScope 提供的,每个组件在 onMount 中最多登记一次,并在组件卸载时自动释放:
scope.focusScope(FocusScopeOptions(loop = true))把当前组件的子树变成一个焦点范围:Tab 在组内循环,不会离开这一组。对话框之类的弹层通常需要这种行为。scope.keyNavigation(...)把一组FocusHandle接入方向键和 Home/End。默认是 roving 模式:整组只占一个 Tab 停靠点,进入组之后用方向键在成员间移动。layout可选Horizontal、Vertical或按行列定位的Grid(用item(handle, row, column)登记)。它只移动焦点,激活仍由控件自己处理。
一个循环焦点范围的最小写法:
class LoopGroup(scope: ComponentScope): Component(scope) {
private val a = TextEditorState()
private val b = TextEditorState()
public override fun onMount() {
// Tab and Shift+Tab cycle inside this component instead of leaving it.
scope.focusScope(FocusScopeOptions(loop = true))
}
public override fun render(): View {
val a = this.a
val b = this.b
return column {
this.input(a)
this.input(b)
}
}
}
把它作为子组件挂载:this.add(component({ mount -> LoopGroup(mount) }))。自绘控件如何登记焦点目标,见自绘元素。
9.5 快捷键
窗口内的快捷键用 ShortcutScope(来自 scoop.surface)登记。在组件的 onMount 中用 scope.resource { ... } 创建它,组件卸载时快捷键自动注销:
scope.resource {
ShortcutScope(
scope.input,
[shortcutBinding(primaryChord("i"), { count.value += 1 })]
)
}
ShortcutChord 由按键和修饰键组成,ShortcutModifiers(primary = true) 表示“主修饰键”:macOS 上是 Cmd,Linux 和 Windows 上是 Ctrl,同一份代码在各平台都符合习惯。快捷键在控件的默认行为之后执行,因此输入框自己的 Cmd+C、Cmd+Z 等编辑操作不会被它截走。
多窗口应用如果需要一组与具体窗口无关的应用级快捷键,可以在创建窗口前用 UiHost.shortcutScope(bindings) 登记;窗口内的 ShortcutScope 优先于它。完整的 UiHost 用法见桌面集成与异步。
要在界面上展示快捷键,用 kbdGroup(chord):它直接使用与绑定相同的 ShortcutChord,按当前平台的标签分成几个键帽显示(例如 macOS 上的 ⌘ 和 K)。
9.6 完整示例
//! dep: scoop:scoop.surface:0.1.0
//! 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.surface.ShortcutScope
import scoop.surface.shortcutBinding
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
import scoop.window.LogicalKey
import scoop.window.ShortcutChord
import scoop.window.ShortcutKey
import scoop.window.ShortcutModifiers
fun primaryChord(character: String): ShortcutChord =
ShortcutChord(
ShortcutKey.Logical(LogicalKey.Character(character)),
ShortcutModifiers(primary = true)
)
class FocusDemo(scope: ComponentScope): Component(scope) {
// The theme in effect, used to paint the page background.
private val theme = currentThemeSource()
private val search = TextEditorState()
private val note = TextEditorState()
private val searchFocus = FocusHandle()
private val tools = Array<FocusHandle>(3L) { _ -> FocusHandle() }
private val tool = state("none")
private val count = state(0)
public override fun onMount() {
// Arrow keys move between the toolbar buttons; Tab enters the group once.
val items = Array<NavigationItem>(tools.size) { index -> NavigationItem(tools[index]) }
scope.keyNavigation(
navigationOptions {
this.addAll(items)
this.layout = NavigationLayout.Horizontal
this.loop = true
}
)
// Window-local shortcuts, released together with this component.
val searchFocus = this.searchFocus
val count = this.count
scope.resource {
ShortcutScope(
scope.input,
[
shortcutBinding(
primaryChord("k"),
{
searchFocus.requestFocus();
Unit
}
),
shortcutBinding(primaryChord("i"), { count.value += 1 })
]
)
}
}
public override fun render(): View {
val pageColor = this.theme.current.value.tokens.palette.background.background
val search = this.search
val note = this.note
val searchFocus = this.searchFocus
val tools = this.tools
val tool = this.tool
val count = this.count
val names = ["Bold", "Italic", "Underline"]
val content =
column {
this.row {
for (index in 0L ..< 3L)
this.button(names[index], { tool.value = names[index] }) {
this.variant = ButtonVariant.Outline
this.focus = FocusOptions(Some(tools[index]))
}
this.spacing = 8.0
}
this.label(f"Last tool: ${tool.value}")
this.row {
this.input(search) {
this.placeholder = "Search"
// order -1 sorts before every default (0) target: first Tab stop.
this.focus = FocusOptions(Some(searchFocus), order = -1)
}
this.kbdGroup(primaryChord("k"))
this.spacing = 8.0
this.alignment = CrossAlignment.Center
}
this.input(note) { this.placeholder = "Note" }
this.row {
this.button(
"Focus search",
{
searchFocus.requestFocus();
Unit
}
)
this.button("Not in Tab order", { count.value += 1 }) {
this.variant = ButtonVariant.Ghost
this.focus = FocusOptions(tabStop = false)
}
this.spacing = 8.0
}
this.label(f"Primary+I pressed ${count.value} times")
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("Focus", { scope -> FocusDemo(scope) })
}
}
运行:
scoop run ui-focus-scroll-keyboard.scoop --release

操作一下:
- 窗口获得焦点后按 Tab,焦点首先落在 Search 输入框上,因为它的
order是-1。 - 继续按 Tab 进入工具栏,此时用左右方向键在 Bold、Italic、Underline 之间移动(到末尾会绕回开头);再按 Tab 直接离开工具栏,而不是逐个经过三个按钮。
- “Not in Tab order” 按钮不会被 Tab 选中,但仍可以用鼠标点击。
- 点击 “Focus search”,或在任意位置按 Primary+K(macOS 上是 Cmd+K),焦点回到搜索框。
- 按 Primary+I,最后一行的计数加一。
10. 滚动与长列表
10.1 滚动区域
scrollArea(content) { ... } 支持鼠标滚轮、触控板、手指或笔尖拖动,以及拖动滚动条。视口获得焦点后也可以用方向键、Page Up/Down、Home/End 滚动。直接接触先按轻点处理,移动越过阈值后视口才接管,所以在列表按钮上起手也能滚动。
| 配置 | 作用 |
|---|---|
height / width |
视口大小,例如 Some(320.0);不设置时使用父级给出的空间 |
axes |
滚动方向,默认 ScrollAxes.Vertical |
state |
传入自己持有的 ScrollState,以便读取和控制滚动位置 |
scrollbars |
ScrollbarVisibility.Auto、Always 或 Hidden |
touchScroll |
TouchScrollOptions 配置触点的移动阈值、是否锁定主轴。嵌套视口默认沿 ScrollChain.Chain 把剩余位移交给外层。 |
scrollPhysics |
ScrollPhysicsOptions 配置惯性和边界。平台默认在 macOS 使用弹性边界,在 Windows 和 Linux 使用硬边界。启用“减少动画”时关闭回弹。 |
10.2 ScrollState:读取与控制位置
和 TextEditorState 一样,ScrollState 由调用方持有、交给控件借用。在 render 中读取它的属性,滚动时界面会自动刷新:
offset:当前滚动位置;maximum:最大可滚动位置;viewport和content:视口和内容的大小。
控制位置的方法要在事件回调中调用:
scrollTo(Point(x, y)):跳到指定位置;scrollBy(Point(dx, dy)):相对滚动,返回实际滚动的距离;animateTo(Point(x, y)):平滑滚动,默认 300 毫秒;reveal(rect, horizontal, vertical):滚动到让内容坐标中的矩形可见,对齐方式为ScrollAlignment.Nearest、Start、Center或End。
还有更简单的“滚动到某个控件”:对滚动区域内的控件调用 FocusHandle.requestFocus(),框架会先把它滚入视野再聚焦(9.3 节)。
//! 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.graphics.Point
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 ScrollDemo(scope: ComponentScope): Component(scope) {
// The theme in effect, used to paint the page background.
private val theme = currentThemeSource()
private val scroll = ScrollState()
private val reply = TextEditorState()
private val replyFocus = FocusHandle()
public override fun render(): View {
val pageColor = this.theme.current.value.tokens.palette.background.background
val scroll = this.scroll
val reply = this.reply
val replyFocus = this.replyFocus
val messages =
column {
for (index in 1 .. 40) {
this.label(f"Message ${index}")
this.separator()
}
this.input(reply) {
this.placeholder = "Reply at the very bottom"
this.focus = FocusOptions(Some(replyFocus))
}
this.spacing = 8.0
this.padding = Insets.all(12.0)
this.alignment = CrossAlignment.Stretch
}
val content =
column {
this.row {
this.button("Top", { scroll.scrollTo(Point(0.0, 0.0)) })
this.button(
"Bottom (animated)",
{ scroll.animateTo(Point(0.0, scroll.maximum.y)) }
)
this.button(
"Down 100",
{
scroll.scrollBy(Point(0.0, 100.0));
Unit
}
)
this.button(
"Focus reply",
{
replyFocus.requestFocus();
Unit
}
)
this.spacing = 8.0
}
this.scrollArea(messages) {
this.state = Some(scroll)
this.height = Some(320.0)
}
this.label(
f"offset ${scroll.offset.y.toInt()} / ${scroll.maximum.y.toInt()}, viewport ${
scroll.viewport.height.toInt()
}"
)
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("Scroll", { scope -> ScrollDemo(scope) })
}
}
运行:
scoop run ui-focus-scroll-area.scoop --release

窗口顶部有四个按钮,下面是一个 320 高的滚动区域,包含 40 条消息和底部的回复框,最下一行实时显示滚动位置。滚动时数字随之变化;Top 立即回到顶部;“Bottom (animated)” 平滑滚到底部;“Down 100” 每次向下 100;“Focus reply” 把回复框滚入视野并聚焦,可以直接输入。
10.3 虚拟列表:只构建看得见的行
scrollArea 会构建全部内容,适合几十到几百个子视图。数据更多时用 virtualList(source, extent):它只为当前可见(加上少量预留)的行构建视图,滚动时复用,内存和布局开销与总行数无关。
数据通过实现 ItemSource 接口提供:
class NumberRows(private val picked: State<Long>): ItemSource {
public override val count: Long get() = 100000000L
public override fun keyAt(index: Long): ViewKey = ViewKey.Number(index)
public override fun indexOfKey(key: ViewKey): Long? =
when (key) {
case Number(val index) -> if (index >= 0L && index < count)
Some(index)
else
None
else -> None
}
public override fun build(index: Long): View {
val picked = this.picked
return button(f"Row ${index} · square = ${index * index}", { picked.value = index }) {
this.variant = ButtonVariant.Ghost
}
}
}
这个数据源来自 10.6 节的示例。count 是总行数;keyAt 和 indexOfKey 在下标与稳定的 key 之间互相转换,滚动定位、聚焦和保留状态都靠 key 识别行,所以同一行的 key 不能随滚动变化;build 为一行创建视图——这里每行是一个按钮,点击时把行号写进状态 picked。
第二个参数 ItemExtent 说明行高:
ItemExtent.Fixed(32.0):每行同样高,定位最快,适合大部分列表;ItemExtent.Variable(56.0):行高不同,参数是估计值,实际高度在行第一次布局后记录下来。
读屏器也可以查询屏幕外的条目。在 virtualList 的 builder 中用 itemSemantics 提供与同一 ItemSource 绑定的名称、状态等数据。查询本身只读数据,不执行 build、滚动或移动焦点。读屏器明确请求显露某一行时,列表才按稳定 key 定位并呈现它。UI 无障碍规范
10.4 VirtualListHandle:定位、聚焦与阅读位置
虚拟列表的大部分行并不存在,所以不能用普通的 FocusHandle 或像素位置去找它们。改用 VirtualListHandle,通过 handle = Some(list) 交给列表:
scrollToItem(key, ScrollAlignment.Center):滚动到指定行,即使它离当前位置有几千万行;focusItem(key):滚动到该行并聚焦行内第一个可聚焦控件;reading:当前的阅读快照(VirtualReadingSnapshot?),其中visible是可见行的下标范围,atEnd表示是否已滚到末尾;在render中读取它,滚动时自动刷新。
设置 navigation = ItemNavigation.Roving() 后,整个列表只占一个 Tab 停靠点,进入后用上下方向键和 Home/End 在行之间移动——同样不需要预先构建所有行。
10.5 保留行状态与绘制缓存
默认情况下,滚出视野的行会被销毁(ItemRetention.Dispose),滚回来时重新构建。如果行内有自己的状态(比如一个子组件的动画或展开状态)需要保留,用 retention = ItemRetention.KeepAlive(20L) 让最多 20 个离屏行保持挂载。行内输入框的文字放在 TextEditorState 中、由数据源持有时,即使行被销毁也不会丢失:
class Notes(private val editors: Array<TextEditorState>): ItemSource {
public override val count: Long get() = editors.size
public override fun keyAt(index: Long): ViewKey = ViewKey.Number(index)
public override fun indexOfKey(key: ViewKey): Long? =
when (key) {
case Number(val index) -> if (index >= 0L && index < count) Some(index) else None
else -> None
}
public override fun build(index: Long): View = textarea(editors[index]) { this.rows = 2L }
}
this.virtualList(notes, ItemExtent.Variable(56.0)) {
this.height = Some(300.0)
this.retention = ItemRetention.KeepAlive(20L)
this.paintCache = PaintCachePolicy.Tiles()
}
paintCache = PaintCachePolicy.Tiles() 把已绘制的内容按图块缓存,滚动时只需重绘新露出的部分。内容绘制较重(大量文字或图形)时可以打开它;普通列表保持默认的 None 即可。scrollArea 也有同名配置。
10.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.text.toLongOrNone
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.*
/** One hundred million rows, computed on demand: nothing is stored per row. */
class NumberRows(private val picked: State<Long>): ItemSource {
public override val count: Long get() = 100000000L
public override fun keyAt(index: Long): ViewKey = ViewKey.Number(index)
public override fun indexOfKey(key: ViewKey): Long? =
when (key) {
case Number(val index) -> if (index >= 0L && index < count)
Some(index)
else
None
else -> None
}
public override fun build(index: Long): View {
val picked = this.picked
return button(f"Row ${index} · square = ${index * index}", { picked.value = index }) {
this.variant = ButtonVariant.Ghost
}
}
}
class HugeList(scope: ComponentScope): Component(scope) {
// The theme in effect, used to paint the page background.
private val theme = currentThemeSource()
private val picked = state(-1L)
private val rows = NumberRows(picked)
private val list = VirtualListHandle()
private val target = TextEditorState("50000000")
public override fun render(): View {
val pageColor = this.theme.current.value.tokens.palette.background.background
val picked = this.picked
val rows = this.rows
val list = this.list
val target = this.target
val visible =
when (list.reading) {
case Some(val snapshot) -> f"${snapshot.visible.start} ..< ${snapshot.visible.end}"
case None -> "not laid out yet"
}
val content =
column {
this.row {
this.input(target) {
this.placeholder = "Row number"
this.policy = textEditPolicy { this.maxLength = Some(9L) }
}
this.button(
"Scroll to",
{
val index = target.text.toLongOrNone() ?: 0L
list.scrollToItem(ViewKey.Number(index), ScrollAlignment.Center)
Unit
}
)
this.button(
"Focus it",
{
val index = target.text.toLongOrNone() ?: 0L
list.focusItem(ViewKey.Number(index))
Unit
}
) { this.variant = ButtonVariant.Outline }
this.spacing = 8.0
}
this.virtualList(rows, ItemExtent.Fixed(32.0)) {
this.handle = Some(list)
this.height = Some(400.0)
this.overscan = 64.0
this.navigation = ItemNavigation.Roving()
}
this.label(f"Visible: ${visible} Picked: ${picked.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("100M rows", { scope -> HugeList(scope) })
}
}
运行:
scoop run ui-focus-scroll-virtual.scoop --release

窗口启动和滚动都和普通列表一样快,尽管列表有一亿行:每一行的文字在 build 中即时计算,程序没有为任何一行预先分配数据。拖动滚动条可以快速到达任意位置;底部一行显示当前可见的行号范围和最后点击的行。在输入框中填入行号(默认 50000000)后点击 “Scroll to”,列表把该行滚动到中央;“Focus it” 则聚焦这一行的按钮,此时可以用上下方向键逐行移动,按 Space 点击当前行。
常见错误与继续阅读
| 现象 | 原因与解决 |
|---|---|
给控件设了 order = 1,它跑到了窗口最后 |
order 在整个焦点范围内排序,所有默认控件都是 0。用负数提前,或调整代码顺序 |
requestFocus() 之后 hasFocus 仍是 false |
目标需要先滚入视野或完成布局,请求处于 pending 状态,下一帧才兑现;用 isPending 查询 |
在 render 中调用 requestFocus 或 scrollTo 报错 |
焦点和滚动位置只能在事件回调或 onMount 中修改 |
mount already has a focus scope / key navigation |
一个组件只能登记一次;需要多组时拆成多个子组件 |
| 快捷键没有反应 | ShortcutScope 没有用 scope.resource 保存,被提前释放;或窗口当前没有焦点 |
animateTo 报 smooth scrolling requires an attached viewport |
ScrollState 还没有交给任何已显示的滚动区域 |
| 虚拟列表滚动后行内状态丢失 | 默认 ItemRetention.Dispose 会销毁离屏行;把数据放进数据源,或使用 KeepAlive |
scrollToItem 返回 false |
列表还没有挂载,或 key 不在数据源中(检查 indexOfKey) |
下一篇弹层与导航介绍对话框、菜单和标签页,它们会用到本篇的焦点范围与焦点恢复。想自己实现可聚焦、可滚动的控件,请阅读自绘元素。