Skip to Content
Onevra

Split

Two resizable panes with a draggable divider, side by side or stacked.

Loading the live example…

split.go
import (
	"fmt"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type editorLayout struct {
	files   float32 // the file list's width in DIPs
	console float32 // the console's share of the height, in percent
}

func (e *editorLayout) View(c *ui.Context) {
	t := c.Theme()
	pane := func(title, body string, background ui.Color) func() {
		return func() {
			ui.Column(c).Fill().Padding(t.Space(4)).Gap(t.Space(1)).Background(background).Children(func() {
				components.Text(c, title).Bold()
				components.Text(c, body).TextColor(t.TextMuted)
			})
		}
	}
	editor := func() {
		components.SplitPercent(c, &e.console,
			pane("Console", fmt.Sprintf("%.0f%% of the height", e.console), t.Surface),
			pane("Editor", "Drag the dividers, or focus one and use the arrow keys.", t.Background),
			components.SplitOptions{Orientation: components.Vertical, Default: 30}).
			Label("Console divider").Fill()
	}
	components.Split(c, &e.files,
		pane("Files", fmt.Sprintf("%.0f points wide", e.files), t.Surface),
		editor,
		components.SplitOptions{Default: 180, FirstMin: 120}).
		Label("Sidebar divider").Fill()
}
Source

components.Split binds the first pane's size in points; components.SplitPercent binds its share, so both panes grow with the window. Dragging the divider or focusing it and pressing the arrow keys resizes the panes, and double-clicking restores Default. Minimum sizes keep both panes usable.

API

Split

func Split(c *ui.Context, size *float32, first, second func(), options ...SplitOptions) *SplitControl

Split arranges two clipped panes separated by a one-DIP line. size is the application-owned preferred width (Horizontal) or height (Vertical) of the first pane and must be nonnil and finite. Give the split a bounded size. Changing orientation reinterprets size in the new axis; applications that need independent sizes should keep separate bindings.

Split(c, &width, files, editor).Fill()
Split(c, &height, preview, console, SplitOptions{Orientation: Vertical}).Fill()

A transparent, theme-scaled handle captures pointer drags. The line keeps Theme.Border on hover and while dragging; the cursor and keyboard focus ring provide feedback. Tab focuses the divider between the panes; axis arrow keys move it by Theme.Space(2.5), and Home/End move to the effective limits. Label defaults to "Divider". Nil builders leave their panes empty. Double-clicking the divider restores SplitOptions.Default when set.

pkg.go.dev

SplitPercent

func SplitPercent(c *ui.Context, percent *float32, first, second func(), options ...SplitOptions) *SplitControl

SplitPercent is Split with *percent the share of the room, from 0 to 100, that the first pane takes, so both panes grow and shrink with the split in proportion. Values outside that range are clamped when shown. The minima still apply and are not written back, so the preferred share returns when there is room again. The divider reports its position to assistive technology as a percentage, and SplitOptions.Default is a share.

SplitPercent(c, &percent, files, editor).Fill()
SplitPercent(c, &percent, preview, console, SplitOptions{Orientation: Vertical}).Fill()
pkg.go.dev

SplitOptions

SplitOptions configures a two-pane resizable layout.

Prop

Type

pkg.go.dev

SplitControl

SplitControl exposes the layout element. Keep this handle to query Changed after Element layout methods, which return ui.Element.

Prop

Type

Methods
func (s *SplitControl) AutoFocus() *SplitControl

AutoFocus focuses the divider.

func (s *SplitControl) Changed() bool

Changed reports an input-driven size change, not a layout constraint.

func (s *SplitControl) Label(label string) *SplitControl

Label sets the accessible name of the keyboard-focusable divider.

pkg.go.dev
Edit this page on GitHub

On this page