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>
42 lines
1.3 KiB
Markdown
42 lines
1.3 KiB
Markdown
# 009 AspectRatio 宽高比
|
|
|
|
## 用途
|
|
|
|
在父容器的宽高约束内放置一个保持指定宽高比的框,尽可能大并居中;子内容在框内居中,不被拉伸,超出部分裁剪。适合预览、视频占位、封面图。
|
|
|
|
## 最小示例
|
|
|
|
```go
|
|
ui.Box(c).Size(320, 240).Children(func() {
|
|
layout.AspectRatio(c, layout.AspectRatioOptions{Ratio: 16.0 / 9}, func() {
|
|
ui.Text(c, "16 : 9")
|
|
})
|
|
})
|
|
```
|
|
|
|
签名:`func AspectRatio(c *ui.Context, opts AspectRatioOptions, child func()) ui.Element`
|
|
|
|
## 参数
|
|
|
|
| 字段 | 类型 | 默认值 | 含义 |
|
|
| --- | --- | --- | --- |
|
|
| `Ratio` | `float32` | 1 | 宽 / 高;负数 panic。 |
|
|
| `child` | `func()` | 必填 | 框内内容;nil 时 panic。需要铺满框时让子元素 `Fill()`。 |
|
|
|
|
## 状态
|
|
|
|
布局组件,无交互状态。返回的元素占满父级内容区,比例框位于其中央。
|
|
|
|
## 事件
|
|
|
|
无。
|
|
|
|
## 键盘操作
|
|
|
|
不获取焦点。
|
|
|
|
## 限制
|
|
|
|
- 父级高度不确定(随内容增长)时只按宽度计算。
|
|
- 框尺寸依据上一帧的父级尺寸计算,窗口缩放后组件请求下一帧完成调整(MyGo 布局的 `AspectRatio` 由已定的宽推出高,或由固定的高推出宽,见 `ui/layout.go:345` 与 `:364`;但不会在父级宽高同时约束时取两者中较小的一边,因此组件自行计算)。
|