MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:
- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
elements expire and Context methods are inert: read IsDisabled into a
local during the build, keep a ui.Handle for Focus, use c.Services()
for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
content builds, so same-frame autofocus lost focus restore on close.
It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
split button tooltip no longer reorders its open menu; disabled
menubar rows pass presses to the panel; Form defers blur-started
async checks so the growing message no longer moves Submit away
from the release; Combobox drops its disabled copy now that MyGo
blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
time.Sleep/time.Now and read element state inside the view. The web
mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.
Snapshot goldens are darwin-only and were not re-verified here.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
58 lines
1.9 KiB
Markdown
58 lines
1.9 KiB
Markdown
# 060 SegmentedControl 分段控件
|
|
|
|
## 用途
|
|
|
|
在少量互斥选项之间切换视图或范围,如“日 / 周 / 月”或“列表 / 网格”。基于 MyGo `SegmentedBase`,辅助技术看到单选组。
|
|
|
|
## 最小示例
|
|
|
|
```go
|
|
if navigation.SegmentedControl(c, &view, []navigation.Segment{
|
|
{Label: "List", Icon: icons.Must("list"), HideLabel: true},
|
|
{Label: "Grid", Icon: icons.Must("layout-grid"), HideLabel: true},
|
|
}, navigation.SegmentedControlOptions{Label: "View"}).Changed() {
|
|
fmt.Println("view", view)
|
|
}
|
|
```
|
|
|
|
签名:`func SegmentedControl(c *ui.Context, selected *int, segments []Segment, opts SegmentedControlOptions) ui.Element`
|
|
|
|
## 参数
|
|
|
|
`selected`:当前分段下标;越界时 panic。`segments` 为空时 panic。
|
|
|
|
`Segment`
|
|
|
|
| 字段 | 类型 | 默认值 | 含义 |
|
|
| --- | --- | --- | --- |
|
|
| `Label` | `string` | 必填 | 分段文字与无障碍名称;为空时 panic。 |
|
|
| `Icon` | `*ui.SVG` | nil | 文字前的图标。 |
|
|
| `HideLabel` | `bool` | false | 只显示图标,`Label` 作为名称与提示文字;没有 `Icon` 时 panic。 |
|
|
|
|
`SegmentedControlOptions`
|
|
|
|
| 字段 | 类型 | 默认值 | 含义 |
|
|
| --- | --- | --- | --- |
|
|
| `Equal` | `bool` | false | 所有分段等宽(取最宽者);否则按内容自适应。 |
|
|
| `Label` | `string` | "" | 单选组的无障碍名称。 |
|
|
| `Disabled` | `bool` | false | 整组禁用。 |
|
|
|
|
## 状态
|
|
|
|
- 选中:Accent 填充、OnAccent 文字、600 字重。
|
|
- 悬停 SurfaceHover,按下 Selection;焦点环 2 DIP。
|
|
- 禁用:整组降低不透明度,点击与方向键不改变值。
|
|
|
|
## 事件
|
|
|
|
- 返回元素的 `Changed()`:本帧用户选择了新分段。
|
|
|
|
## 键盘操作
|
|
|
|
- Tab:进入控件,落在当前分段(一个 Tab 停靠点)。
|
|
- 方向键:选择上一个/下一个分段并循环;Home / End 到两端。
|
|
|
|
## 限制
|
|
|
|
- 不支持单个分段禁用:MyGo `SegmentedBase` 的焦点组在方向键移动时直接点击目标分段。
|