Skip to content

Splitter 分割面板 ​

用可拖动的分隔条把工作区拆成多个可调整大小的面板。

何时使用 ​

  • IDE、文件管理器、属性检查器等需要用户调整两栏或多栏宽度的工作区。
  • 需要上下分栏或嵌套工作区时使用 Vertical() 和嵌套 Splitter。
  • 只是排列固定宽度内容用 Grid 栅格 或 Flex 弹性布局,不要用 Splitter。
  • 移动端窄屏优先使用单栏或抽屉,避免强迫用户拖动很窄的面板。

在线体验 ​

Splitter 分隔面板 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
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。

go
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) 会锁住该面板旁的分隔条。

go
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,用于“上方预览、下方左右分栏”等复杂工作区。

go
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 可记录交互或触发松手后的计算。

go
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 入门。

Splitter 分隔面板

gallery 演示 Splitter 的具名 Render 函数,源码 page_nav_layout.go#L91。

加载中…

Splitter 方向、百分比限制与把手

gallery 演示 SplitterAlign 的 Render 函数,源码 page_b3j.go#L61。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Splitter ​

go
func Splitter(c *Context) *SplitterW

Splitter 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 ​

go
func SplitterPanel(c *Context) *SplitterPanelW

SplitterPanel adds a pane; call it inside Splitter.Body.

返回 *SplitterPanelW,可链式调用的方法见 SplitterPanelW 方法。

SplitterW 方法 ​

ui.SplitterW:SplitterW lays panes side by side with draggable bars.

方法说明签名
BodyBody builds the panes.Body(fn func()) *Box
ChangedChanged reports a resize or collapse this frame.Changed() bool
DragEndedDragEnded reports the bar whose pointer drag ended this frame (released or cancelled).DragEnded() (bar int, ok bool)
DragStartedDragStarted reports the bar (1-based: bar i sits before pane i) whose pointer drag began this frame.DragStarted() (bar int, ok bool)
DraggingDragging reports whether a bar is being dragged with the pointer.Dragging() bool
KeyboardMinimumCapKeyboardMinimumCap caps each pane's keyboard-only minimum to the given fraction of the adjacent pair's total size.KeyboardMinimumCap(fraction float32) *SplitterW
KeyboardStepKeyboardStep sets arrow and Shift+arrow increments as fractions of the two adjacent panes' total size.KeyboardStep(step, shiftStep float32) *SplitterW
KeyboardStepPixelsKeyboardStepPixels sets arrow and Shift+arrow increments in logical pixels.KeyboardStepPixels(step, shiftStep float32) *SplitterW
LazyLazy 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
OrientationOrientation lays the panes out "horizontal" (default, side by side) or "vertical" (antd 6 orientation; layout deprecated).Orientation(o string) *SplitterW
PixelSizesPixelSizes returns the effective pane sizes in logical pixels along the splitter's axis, excluding the bars.PixelSizes() []float32
SizesSizes returns the pane sizes as fractions.Sizes() []float32
StyleStyle applies layout options (give the splitter a size, e.g. layout.H(200)).Style(opts ...layout.Option) *SplitterW
VerticalVertical stacks panes top to bottom.Vertical() *SplitterW
WithHandleWithHandle draws a grip on every resizable bar (shadcn ResizableHandle withHandle).WithHandle() *SplitterW

SplitterPanelW 方法 ​

ui.SplitterPanelW:SplitterPanelW is one pane of a Splitter.

方法说明签名
AlwaysShowCollapseAlwaysShowCollapse keeps the adjacent bar's collapse controls visible without hover, like antd's collapsible.showCollapsibleIcon=true.AlwaysShowCollapse(on bool) *SplitterPanelW
BodyBody sets the pane content.Body(fn func()) *SplitterPanelW
CollapseOffsetsCollapseOffsets sets the toggle center's distance from the bar in logical pixels.CollapseOffsets(expanded, collapsed float32) *SplitterPanelW
CollapseToggleCollapseToggle 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
CollapsedCollapsed controls whether this pane is folded.Collapsed(on bool) *SplitterPanelW
CollapsibleCollapsible shows collapse arrows on the adjacent bars.Collapsible() *SplitterPanelW
DefaultPercentDefaultPercent sets the initial size as a fraction (0.3 = 30%).DefaultPercent(f float32) *SplitterPanelW
DefaultSizeDefaultSize sets the initial size in pixels.DefaultSize(px float32) *SplitterPanelW
MaxMax sets the maximum size in pixels.Max(px float32) *SplitterPanelW
MaxPercentMaxPercent(f float32) *SplitterPanelW
MinMin sets the minimum size in pixels.Min(px float32) *SplitterPanelW
MinPercentMinPercent / 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
OnCollapseOnCollapse reports whether a bar action folded or restored this pane.OnCollapse(fn func(bool)) *SplitterPanelW
OnResizeOnResize reports an expanded pane's new size in logical pixels when a bar is moved by the user.OnResize(fn func(float32)) *SplitterPanelW
ResizableResizable(false) locks the bars next to this pane.Resizable(on bool) *SplitterPanelW
SizeSize controls the expanded pane size in logical pixels along the splitter's axis.Size(px float32) *SplitterPanelW

相关链接 ​

以 MIT 许可发布