splitview

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package splitview provides a resizable split panel container widget with a draggable divider separating two child panels.

Construction uses functional options for immutable configuration:

split := splitview.New(
    splitview.First(leftPanel),
    splitview.Second(rightPanel),
    splitview.OrientationOpt(splitview.Horizontal),
    splitview.InitialRatio(0.3),
    splitview.MinFirst(200),
)

Orientation

Two orientations are available:

  • Horizontal (default) -- first panel on the left, second on the right
  • Vertical -- first panel on top, second on the bottom

Divider

A draggable divider separates the two panels. The user can drag the divider to resize the panels. The divider width is configurable via DividerWidth. When the mouse hovers over the divider, the cursor changes to a resize cursor.

Collapse

When CollapsibleOpt is enabled, double-clicking the divider collapses the first panel. Double-clicking again restores it to the previous ratio.

Constraints

Minimum sizes for each panel can be set via MinFirst and MinSecond. The divider will stop at these limits during drag operations.

Visual Style

The divider rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) can supply its own painter to render the divider in the appropriate visual style.

If no painter is set, DefaultPainter is used, which draws a minimal divider suitable for testing and prototyping.

Signal Binding

The split ratio can be bound to a reactive signal from the state package. When a signal value changes, the split ratio automatically reflects the new state:

Example:

ratio := state.NewSignal[float32](0.5)
split := splitview.New(
    splitview.First(leftPanel),
    splitview.Second(rightPanel),
    splitview.RatioSignal(ratio),
    splitview.OnRatioChange(func(r float32) {
        fmt.Printf("ratio: %.2f\n", r)
    }),
)

Focus

SplitView does not participate in tab focus itself. Focus events are dispatched to the child panels directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultPainter

type DefaultPainter struct{}

DefaultPainter provides a minimal fallback painter with no design system styling. It draws a simple divider line with a center handle indicator.

func (DefaultPainter) PaintDivider

func (p DefaultPainter) PaintDivider(canvas widget.Canvas, ps PaintState)

PaintDivider renders a minimal divider with a center handle.

type DividerColorScheme

type DividerColorScheme struct {
	Divider      widget.Color // divider background
	DividerHover widget.Color // divider background when hovered
	DividerDrag  widget.Color // divider background when dragging
	Handle       widget.Color // center handle indicator
}

DividerColorScheme provides theme-derived colors for divider painting. Zero value means the painter should use its built-in defaults.

type Option

type Option func(*config)

Option configures a split view during construction.

func CollapsibleOpt

func CollapsibleOpt(v bool) Option

CollapsibleOpt enables double-click-to-collapse on the divider. When enabled, double-clicking the divider collapses the first panel. Double-clicking again restores it to the previous ratio.

func ColorSchemeOpt

func ColorSchemeOpt(cs DividerColorScheme) Option

ColorSchemeOpt sets the theme-derived color scheme for the divider.

func DividerWidth

func DividerWidth(w float32) Option

DividerWidth sets the divider thickness in pixels. Default is 6 pixels.

func First

func First(w widget.Widget) Option

First sets the first panel widget (left for Horizontal, top for Vertical).

func InitialRatio

func InitialRatio(r float32) Option

InitialRatio sets the initial split ratio (0.0 to 1.0). A ratio of 0.3 means the first panel takes 30% of the available space. Default is 0.5.

func MinFirst

func MinFirst(px float32) Option

MinFirst sets the minimum size (width or height) of the first panel in pixels.

func MinSecond

func MinSecond(px float32) Option

MinSecond sets the minimum size (width or height) of the second panel in pixels.

func OnRatioChange

func OnRatioChange(fn func(float32)) Option

OnRatioChange sets the callback invoked when the split ratio changes.

func OrientationOpt

func OrientationOpt(o Orientation) Option

OrientationOpt sets the split orientation. Default is Horizontal.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the divider. Each design system provides its own painter. If not set, DefaultPainter is used.

func RatioReadonlySignal

func RatioReadonlySignal(sig state.ReadonlySignal[float32]) Option

RatioReadonlySignal binds the split ratio to a read-only signal. When set, this takes highest precedence over all other ratio sources.

func RatioSignal

func RatioSignal(sig state.Signal[float32]) Option

RatioSignal binds the split ratio to a reactive signal. This is a TWO-WAY binding: the widget reads the ratio from the signal, and when the user drags the divider, the new ratio is written back.

func Second

func Second(w widget.Widget) Option

Second sets the second panel widget (right for Horizontal, bottom for Vertical).

type Orientation

type Orientation uint8

Orientation controls how the two panels are arranged.

const (
	// Horizontal arranges panels left (first) and right (second).
	Horizontal Orientation = iota

	// Vertical arranges panels top (first) and bottom (second).
	Vertical
)

Orientation constants.

func (Orientation) String

func (o Orientation) String() string

String returns a human-readable name for the orientation.

type PaintState

type PaintState struct {
	// DividerRect is the bounding rectangle of the divider area.
	DividerRect geometry.Rect

	// Orientation is the split orientation (Horizontal or Vertical).
	Orientation Orientation

	// Hovered indicates that the mouse is over the divider.
	Hovered bool

	// Dragging indicates that the divider is currently being dragged.
	Dragging bool

	// Collapsed indicates that the first panel is collapsed.
	Collapsed bool

	// ColorScheme provides theme-derived colors for divider painting.
	// Zero value means the painter should use its built-in defaults.
	ColorScheme DividerColorScheme
}

PaintState provides the current divider state to the painter.

type Painter

type Painter interface {
	PaintDivider(canvas widget.Canvas, state PaintState)
}

Painter draws the visual representation of the split view divider. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation to render the divider in its visual style.

If no Painter is set, the split view uses DefaultPainter.

type Widget

type Widget struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Widget implements a resizable split panel container with a draggable divider.

A split view is created with New using functional options:

split := splitview.New(
    splitview.First(leftPanel),
    splitview.Second(rightPanel),
    splitview.OrientationOpt(splitview.Horizontal),
    splitview.InitialRatio(0.3),
)

func New

func New(opts ...Option) *Widget

New creates a new split view Widget with the given options.

The returned widget is visible and enabled by default. The default orientation is Horizontal with a 50/50 split.

func (*Widget) Children

func (w *Widget) Children() []widget.Widget

Children returns the two panel widgets.

func (*Widget) Draw

func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders both panels and the divider.

func (*Widget) Event

func (w *Widget) Event(ctx widget.Context, e event.Event) bool

Event handles input events for divider dragging.

func (*Widget) FirstPanel

func (w *Widget) FirstPanel() widget.Widget

FirstPanel returns the first panel widget.

func (*Widget) IsCollapsed

func (w *Widget) IsCollapsed() bool

IsCollapsed reports whether the first panel is currently collapsed.

func (*Widget) Layout

func (w *Widget) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates panel sizes and positions children.

func (*Widget) Mount

func (w *Widget) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.

func (*Widget) Ratio

func (w *Widget) Ratio() float32

Ratio returns the current split ratio.

func (*Widget) SecondPanel

func (w *Widget) SecondPanel() widget.Widget

SecondPanel returns the second panel widget.

func (*Widget) Unmount

func (w *Widget) Unmount()

Unmount is called when the split view is removed from the widget tree. Implements widget.Lifecycle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL