Splitter 分割面板
用可拖动的分隔条把工作区拆成多个可调整大小的面板。
何时使用
- IDE、文件管理器、属性检查器等需要用户调整两栏或多栏宽度的工作区。
- 需要上下分栏或嵌套工作区时使用
Vertical()和嵌套Splitter。 - 只是排列固定宽度内容用 Grid 栅格 或 Flex 弹性布局,不要用 Splitter。
- 移动端窄屏优先使用单栏或抽屉,避免强迫用户拖动很窄的面板。
在线体验
基本用法
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func pane(c *ui.Context, label string) func() {
return func() { ui.Column(c, func() { ui.Text(c, label) }, layout.Center()) }
}
func page(c *ui.Context) {
ui.Splitter(c).Style(layout.H(240)).Body(func() {
ui.SplitterPanel(c).DefaultPercent(0.3).Min(160).Body(pane(c, "导航"))
ui.SplitterPanel(c).Body(pane(c, "编辑区"))
})
}SplitterPanel 必须在 Splitter.Body 中创建。第一个面板初始占 30%,并限制最小宽度;未设置比例的面板会分配剩余空间。
常见用法
可折叠侧栏
Collapsible() 显示相邻分隔条的折叠箭头;CollapseToggle 只显示一个带提示的箭头,适合 IDE 侧栏。
在可折叠面板上调用 AlwaysShowCollapse(true) 可让相邻分隔条始终显示折叠按钮。对应 antd 6.7 的 collapsible.showCollapsibleIcon: true,在 Tango 中也可用于单箭头的 CollapseToggle。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.Splitter(c).Style(layout.H(200)).Body(func() {
ui.SplitterPanel(c).DefaultPercent(0.28).Min(120).
CollapseToggle("折叠工程侧栏", "展开工程侧栏").Body(func() { ui.Text(c, "工程") })
ui.SplitterPanel(c).Body(func() { ui.Text(c, "编辑器") })
})
}固定尺寸与范围
DefaultSize 以像素指定初始尺寸,Min 和 Max 限制拖动范围;Resizable(false) 会锁住该面板旁的分隔条。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.Splitter(c).Style(layout.H(220)).Body(func() {
ui.SplitterPanel(c).DefaultSize(240).Min(180).Max(360).Body(func() { ui.Text(c, "属性") })
ui.SplitterPanel(c).Resizable(false).Body(func() { ui.Text(c, "预览") })
})
}竖向与嵌套
Vertical() 将面板上下排列。面板 Body 内可以再创建一个 Splitter,用于“上方预览、下方左右分栏”等复杂工作区。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.Splitter(c).Vertical().Style(layout.H(300)).Body(func() {
ui.SplitterPanel(c).DefaultPercent(0.4).Body(func() { ui.Text(c, "上方") })
ui.SplitterPanel(c).Body(func() {
ui.Splitter(c).Style(layout.Grow(1)).Body(func() {
ui.SplitterPanel(c).Body(func() { ui.Text(c, "左下") })
ui.SplitterPanel(c).Body(func() { ui.Text(c, "右下") })
})
})
})
}延迟提交与拖拽事件
Lazy() 拖动时只显示预览线,松手才提交比例。DragStarted、DragEnded 可记录交互或触发松手后的计算。
import (
"fmt"
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
var dragMessage = "拖动分隔条试试"
func page(c *ui.Context) {
sp := ui.Splitter(c).Lazy().Style(layout.H(160))
sp.Body(func() {
ui.SplitterPanel(c).DefaultPercent(0.4).Min(80).Body(func() { ui.Text(c, "预览") })
ui.SplitterPanel(c).Min(80).Body(func() { ui.Text(c, "提交") })
})
if _, ok := sp.DragEnded(); ok {
sizes := sp.Sizes()
dragMessage = fmt.Sprintf("已提交:%.0f%% / %.0f%%", sizes[0]*100, sizes[1]*100)
}
ui.Text(c, dragMessage)
}主题与样式
分隔条宽度、悬停色、焦点色和折叠箭头由 theme.Components.Splitter 控制。Ant 风格强调可拖拽的中性分隔条,Element Plus 更轻,shadcn 使用低对比边框;面板内容本身的背景和内边距应由面板 Body 设置。
注意事项
- 外层必须有确定的尺寸(例如
layout.H(240)或layout.Grow(1)),否则拖动比例没有可用空间。 Sizes()返回各面板的比例;它是当前帧的结果,不要在每帧把它重新写回DefaultPercent。- 给每个面板设置合理的
Min,避免键盘调整或窄窗口下内容不可读;键盘可聚焦分隔条并调整尺寸。 - 折叠箭头的提示文本同时用于屏幕阅读器名称;自定义
CollapseToggle时应传入有意义的折叠与展开文案。 - RTL 下分隔条方向和箭头会自动镜像。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Splitter
func Splitter(c *Context) *SplitterWSplitter starts a split view; chain options, then Body with SplitterPanel calls.
ConfigProvider.Defaults("Splitter", ...) keys: "orientation" ("vertical"; antd 6), "layout" (its deprecated name), "lazy", "withHandle" (bool).
Under shadcn/ui this is Resizable: a 1px border line, WithHandle adds the grip box of ResizableHandle withHandle.
返回 *SplitterW,可链式调用的方法见 SplitterW 方法。
ui.SplitterPanel
func SplitterPanel(c *Context) *SplitterPanelWSplitterPanel adds a pane; call it inside Splitter.Body.
返回 *SplitterPanelW,可链式调用的方法见 SplitterPanelW 方法。
SplitterW 方法
ui.SplitterW:SplitterW lays panes side by side with draggable bars.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body builds the panes. | Body(fn func()) *Box |
Changed | Changed reports a resize or collapse this frame. | Changed() bool |
DragEnded | DragEnded reports the bar whose pointer drag ended this frame (released or cancelled). | DragEnded() (bar int, ok bool) |
DragStarted | DragStarted reports the bar (1-based: bar i sits before pane i) whose pointer drag began this frame. | DragStarted() (bar int, ok bool) |
Dragging | Dragging reports whether a bar is being dragged with the pointer. | Dragging() bool |
KeyboardMinimumCap | KeyboardMinimumCap caps each pane's keyboard-only minimum to the given fraction of the adjacent pair's total size. | KeyboardMinimumCap(fraction float32) *SplitterW |
KeyboardStep | KeyboardStep sets arrow and Shift+arrow increments as fractions of the two adjacent panes' total size. | KeyboardStep(step, shiftStep float32) *SplitterW |
KeyboardStepPixels | KeyboardStepPixels sets arrow and Shift+arrow increments in logical pixels. | KeyboardStepPixels(step, shiftStep float32) *SplitterW |
Lazy | Lazy makes a drag show only a preview line; the panes keep their sizes until the pointer is released, then the new ratio is committed at once (Changed reports it on that frame). | Lazy() *SplitterW |
Orientation | Orientation lays the panes out "horizontal" (default, side by side) or "vertical" (antd 6 orientation; layout deprecated). | Orientation(o string) *SplitterW |
PixelSizes | PixelSizes returns the effective pane sizes in logical pixels along the splitter's axis, excluding the bars. | PixelSizes() []float32 |
Sizes | Sizes returns the pane sizes as fractions. | Sizes() []float32 |
Style | Style applies layout options (give the splitter a size, e.g. layout.H(200)). | Style(opts ...layout.Option) *SplitterW |
Vertical | Vertical stacks panes top to bottom. | Vertical() *SplitterW |
WithHandle | WithHandle draws a grip on every resizable bar (shadcn ResizableHandle withHandle). | WithHandle() *SplitterW |
SplitterPanelW 方法
ui.SplitterPanelW:SplitterPanelW is one pane of a Splitter.
| 方法 | 说明 | 签名 |
|---|---|---|
AlwaysShowCollapse | AlwaysShowCollapse keeps the adjacent bar's collapse controls visible without hover, like antd's collapsible.showCollapsibleIcon=true. | AlwaysShowCollapse(on bool) *SplitterPanelW |
Body | Body sets the pane content. | Body(fn func()) *SplitterPanelW |
CollapseOffsets | CollapseOffsets sets the toggle center's distance from the bar in logical pixels. | CollapseOffsets(expanded, collapsed float32) *SplitterPanelW |
CollapseToggle | CollapseToggle shows a single arrow on the adjacent bar, like an IDE side bar: it points at this pane and collapses it, then flips and restores the saved size. | CollapseToggle(collapseTip, expandTip string) *SplitterPanelW |
Collapsed | Collapsed controls whether this pane is folded. | Collapsed(on bool) *SplitterPanelW |
Collapsible | Collapsible shows collapse arrows on the adjacent bars. | Collapsible() *SplitterPanelW |
DefaultPercent | DefaultPercent sets the initial size as a fraction (0.3 = 30%). | DefaultPercent(f float32) *SplitterPanelW |
DefaultSize | DefaultSize sets the initial size in pixels. | DefaultSize(px float32) *SplitterPanelW |
Max | Max sets the maximum size in pixels. | Max(px float32) *SplitterPanelW |
MaxPercent | MaxPercent(f float32) *SplitterPanelW | |
Min | Min sets the minimum size in pixels. | Min(px float32) *SplitterPanelW |
MinPercent | MinPercent / MaxPercent limit the size as a fraction of the splitter (antd min / max "20%"); with Min / Max too, the tighter limit wins. | MinPercent(f float32) *SplitterPanelW |
OnCollapse | OnCollapse reports whether a bar action folded or restored this pane. | OnCollapse(fn func(bool)) *SplitterPanelW |
OnResize | OnResize reports an expanded pane's new size in logical pixels when a bar is moved by the user. | OnResize(fn func(float32)) *SplitterPanelW |
Resizable | Resizable(false) locks the bars next to this pane. | Resizable(on bool) *SplitterPanelW |
Size | Size controls the expanded pane size in logical pixels along the splitter's axis. | Size(px float32) *SplitterPanelW |