diff --git a/docs/components/005-stack.md b/docs/components/005-stack.md index bb2e7c9..55278e5 100644 --- a/docs/components/005-stack.md +++ b/docs/components/005-stack.md @@ -2,14 +2,16 @@ ## 用途 -按横向或纵向排列子元素,统一间距、对齐与换行。用 `StackItem` 包裹可隐藏的子项,隐藏时不占位置也不留间距。 +按横向或纵向排列子元素,统一间距、对齐与换行。要隐藏子项就不构建它(`if !hidden { build() }`),未构建的子项不占位置也不留间距。 ## 最小示例 ```go mujica.Stack(c, mujica.StackOptions{Horizontal: true, Gap: mujica.SpaceM}, func() { mujica.Button(c, "Save", mujica.ButtonOptions{}) - mujica.StackItem(!showDraft, func() { mujica.Button(c, "Draft", mujica.ButtonOptions{Variant: mujica.Secondary}) }) + if showDraft { + mujica.Button(c, "Draft", mujica.ButtonOptions{Variant: mujica.Secondary}) + } mujica.Button(c, "Cancel", mujica.ButtonOptions{Variant: mujica.Ghost}) }) ``` @@ -17,7 +19,6 @@ mujica.Stack(c, mujica.StackOptions{Horizontal: true, Gap: mujica.SpaceM}, func( 签名: - `func Stack(c *ui.Context, opts StackOptions, children func()) *ui.Element` -- `func StackItem(hidden bool, build func())` ## 参数 @@ -29,8 +30,6 @@ mujica.Stack(c, mujica.StackOptions{Horizontal: true, Gap: mujica.SpaceM}, func( | `Justify` | `ui.Align` | `ui.Start` | 主轴分布:Start、Center、End、SpaceBetween、SpaceAround、SpaceEvenly。 | | `Wrap` | `bool` | false | 横向换行;纵向时设置会 panic。 | -`StackItem`:`hidden` 为 true 时不构建 `build`,因此既不占空间也不产生间距。`build` 为 nil 时 panic。 - ## 状态 布局组件,无交互状态。 diff --git a/example_layout_test.go b/example_layout_test.go index e3e4b19..0f01e58 100644 --- a/example_layout_test.go +++ b/example_layout_test.go @@ -61,7 +61,9 @@ func ExampleStack() { mujica.Use(c, mujica.Settings{}) mujica.Stack(c, mujica.StackOptions{Horizontal: true, Gap: mujica.SpaceM}, func() { mujica.Button(c, "Save", mujica.ButtonOptions{}) - mujica.StackItem(!showDraft, func() { mujica.Button(c, "Draft", mujica.ButtonOptions{Variant: mujica.Secondary}) }) + if showDraft { + mujica.Button(c, "Draft", mujica.ButtonOptions{Variant: mujica.Secondary}) + } mujica.Button(c, "Cancel", mujica.ButtonOptions{Variant: mujica.Ghost}) }) } diff --git a/examples/gallery/pages_layout.go b/examples/gallery/pages_layout.go index 04d90a2..6c0eb71 100644 --- a/examples/gallery/pages_layout.go +++ b/examples/gallery/pages_layout.go @@ -245,7 +245,9 @@ func layoutContainerDemo(c *ui.Context, log func(string)) { const layoutStackCode = `mujica.Stack(c, mujica.StackOptions{Horizontal: true, Gap: mujica.SpaceM}, func() { mujica.Button(c, "Save", mujica.ButtonOptions{}) - mujica.StackItem(!showDraft, func() { mujica.Button(c, "Draft", mujica.ButtonOptions{Variant: mujica.Secondary}) }) + if showDraft { + mujica.Button(c, "Draft", mujica.ButtonOptions{Variant: mujica.Secondary}) + } mujica.Button(c, "Cancel", mujica.ButtonOptions{Variant: mujica.Ghost}) })` @@ -276,11 +278,9 @@ func layoutStackDemo(c *ui.Context, log func(string)) { mujica.Stack(c, mujica.StackOptions{Horizontal: s.horizontal, Wrap: s.wrap, Gap: layoutSpace(s.gap), Align: align, Justify: justifies[s.justify]}, func() { for _, n := range []string{"Save", "Draft", "Publish", "Archive", "Cancel"} { - mujica.StackItem(s.hide && n == "Draft", func() { - if mujica.Button(c, n, mujica.ButtonOptions{Variant: mujica.Secondary}).Clicked() { - log(n + " clicked") - } - }) + if (!s.hide || n != "Draft") && mujica.Button(c, n, mujica.ButtonOptions{Variant: mujica.Secondary}).Clicked() { + log(n + " clicked") + } } }).Grow(1).Padding(12) }) diff --git a/stack.go b/stack.go index 1914350..6e4b593 100644 --- a/stack.go +++ b/stack.go @@ -26,7 +26,7 @@ type StackOptions struct { Wrap bool } -// Stack arranges children in a row or column. Build children with StackItem to hide them without leaving a gap. +// Stack arranges children in a row or column. A child not built takes no room and no gap. func Stack(c *ui.Context, opts StackOptions, children func()) *ui.Element { if children == nil { panic("mujica: Stack needs children") @@ -57,13 +57,3 @@ func Stack(c *ui.Context, opts StackOptions, children func()) *ui.Element { } return e.Children(children) } - -// StackItem builds one child of a Stack unless hidden; a hidden item takes no room and no gap. -func StackItem(hidden bool, build func()) { - if build == nil { - panic("mujica: StackItem needs build") - } - if !hidden { - build() - } -} diff --git a/stack_test.go b/stack_test.go index d9c2053..e208555 100644 --- a/stack_test.go +++ b/stack_test.go @@ -30,7 +30,9 @@ func TestStackHiddenItemTakesNoGap(t *testing.T) { Use(c, Settings{}) Stack(c, StackOptions{Horizontal: true, Gap: SpaceL}, func() { a = ui.Box(c).Size(30, 10).Bounds() - StackItem(hide, func() { ui.Box(c).Size(30, 10) }) + if !hide { + ui.Box(c).Size(30, 10) + } last = ui.Box(c).Size(30, 10).Bounds() }) }, 300, 300).Frame() @@ -105,7 +107,6 @@ func TestStackSnapshot(t *testing.T) { }) Stack(c, StackOptions{Horizontal: true, Justify: ui.SpaceBetween}, func() { chip("Start") - StackItem(true, func() { chip("Hidden") }) chip("Between") chip("End") })