mvvmtk

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

README

mvvmtk

Go Reference

Binding glue between go-widgets/mvvm (Observable / Command / ObservableList) and go-widgets/toolkit widgets.

Why a separate module

mvvm is deliberately toolkit-agnostic: it binds through a pointer to a widget's value field and a pointer to its callback slot, and never imports any widget package. toolkit, in turn, never imports mvvm. mvvmtk is the one module that knows both, so an app wires a ViewModel to a widget in a single call and never touches widget state fields directly.

unbind := mvvmtk.BindText(entry, vm.Query, win.Invalidate) // entry.Text ⇄ vm.Query
defer unbind()

Each helper is a thin, correct wrapper over the generic mvvm adapters (BindField / OneWay / BindList / BindCommand) with the widget's real field and callback names filled in — no business logic. Every helper returns an unbind func() that detaches the binding and restores any prior callback.

Helpers

Helper Widget field(s) Direction
BindText(*SearchEntry, *Observable[string], invalidate) Text / OnChange two-way
BindEntryText(*Entry, *Observable[string], invalidate) Text / OnChange two-way
BindChecked(*CheckButton, *Observable[bool], invalidate) Checked / OnToggle two-way
BindSelectedIndex(*DropDown, *Observable[int], invalidate) Selected / OnSelect two-way
BindSpin(*SpinButton, *Observable[int], invalidate) Value / OnChange two-way
BindListSelection(*ListBox, *Observable[int], invalidate) Selected / OnActivate two-way
BindViewSwitcher(*ViewSwitcher, *Observable[int], invalidate) Current / OnChange two-way
BindLabel(*Label, *Observable[string], invalidate) Text one-way
BindProgress(*ProgressBar, *Observable[float64], invalidate) Fraction one-way
BindListItems[T](*ListBox, *ObservableList[T], project, invalidate) Items list → widget
BindDropDownOptions[T](*DropDown, *ObservableList[T], project, invalidate) Options list → widget
BindViews[T](*ViewSwitcher, *ObservableList[T], project, invalidate) Views list → widget
BindCommand(*Button, *Command, invalidate) OnClick + Style greying command
BindTree[T](*TreeTable, *ObservableList[T], project, invalidate) Root forest list → widget

BindCommand reflects CanExecute by greying the button — swapping its Style to ButtonSecondary when the command cannot execute and restoring the original Style when it can — because a Button has no boolean "disabled" field.

BindTree rebuilds a TreeTable.Root ([]*TreeTableNode forest) from the list; the caller's project owns each node's Cells/Children.

Install

go get github.com/go-widgets/mvvmtk

License

BSD-3-Clause. See LICENSE. Copyright (c) 2026 the go-widgets/mvvmtk authors.

Documentation

Overview

Package mvvmtk is the binding glue between github.com/go-widgets/mvvm (Observable / Command / ObservableList) and github.com/go-widgets/toolkit widgets.

mvvm is deliberately toolkit-agnostic: it binds through a pointer to a widget's value field and a pointer to its callback slot, and never imports any widget package. toolkit, in turn, never imports mvvm. This module is the one place that knows BOTH, so an app can wire a ViewModel to a widget in a single call and never touch widget state fields directly:

unbind := mvvmtk.BindText(entry, vm.Query, win.Invalidate)

Each helper is a thin, correct wrapper over the generic mvvm adapters (BindField / OneWay / BindList / BindCommand) with the widget's real field and callback names filled in — no business logic. Every helper returns an unbind func that detaches the binding and restores any prior callback.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindChecked

func BindChecked(cb *toolkit.CheckButton, obs *mvvm.Observable[bool], invalidate func()) (unbind func())

BindChecked two-way-binds a CheckButton's Checked to a bool observable (fields CheckButton.Checked / CheckButton.OnToggle).

func BindCommand

func BindCommand(b *toolkit.Button, c *mvvm.Command, invalidate func()) (unbind func())

BindCommand wires a Command to a Button: it composes Execute into the button's OnClick and reflects executability by greying the button — swapping its Style to ButtonSecondary when the command cannot execute and restoring the original Style when it can (a Button has no boolean "disabled" field, so this backend-specific greying is how CanExecute surfaces). invalidate (may be nil) requests a repaint on each executability change. The returned unbind restores the prior OnClick and detaches.

func BindDropDownOptions

func BindDropDownOptions[T any](d *toolkit.DropDown, l *mvvm.ObservableList[T], project func(T) string, invalidate func()) (unbind func())

BindDropDownOptions binds an ObservableList to a DropDown's Options slice (field DropDown.Options), projecting each element to its option string.

func BindEntryText

func BindEntryText(e *toolkit.Entry, obs *mvvm.Observable[string], invalidate func()) (unbind func())

BindEntryText two-way-binds an Entry's Text to a string observable (fields Entry.Text / Entry.OnChange).

func BindLabel

func BindLabel(l *toolkit.Label, obs *mvvm.Observable[string], invalidate func()) (unbind func())

BindLabel one-way-binds a string observable to a Label's Text (field Label.Text; a Label has no edit callback).

func BindListItems

func BindListItems[T any](lb *toolkit.ListBox, l *mvvm.ObservableList[T], project func(T) string, invalidate func()) (unbind func())

BindListItems binds an ObservableList to a ListBox's Items slice (field ListBox.Items), projecting each element to its row string.

func BindListSelection

func BindListSelection(lb *toolkit.ListBox, obs *mvvm.Observable[int], invalidate func()) (unbind func())

BindListSelection two-way-binds a ListBox's Selected row to an int observable (fields ListBox.Selected / ListBox.OnActivate). Note OnActivate fires on row activation (its only value/index callback slot), so the view→VM edge updates the observable when a row is activated; the VM→view edge sets Selected on any observable change.

func BindProgress

func BindProgress(p *toolkit.ProgressBar, obs *mvvm.Observable[float64], invalidate func()) (unbind func())

BindProgress one-way-binds a float64 observable to a ProgressBar's Fraction (field ProgressBar.Fraction; a ProgressBar has no edit callback). Callers keep the value in the widget's [0,1] domain.

func BindSelectedIndex

func BindSelectedIndex(d *toolkit.DropDown, obs *mvvm.Observable[int], invalidate func()) (unbind func())

BindSelectedIndex two-way-binds a DropDown's Selected index to an int observable (fields DropDown.Selected / DropDown.OnSelect).

func BindSpin

func BindSpin(s *toolkit.SpinButton, obs *mvvm.Observable[int], invalidate func()) (unbind func())

BindSpin two-way-binds a SpinButton's Value to an int observable (fields SpinButton.Value / SpinButton.OnChange).

func BindText

func BindText(e *toolkit.SearchEntry, obs *mvvm.Observable[string], invalidate func()) (unbind func())

BindText two-way-binds a SearchEntry's Text to a string observable (fields SearchEntry.Text / SearchEntry.OnChange).

func BindTree

func BindTree[T any](tt *toolkit.TreeTable, l *mvvm.ObservableList[T], project func(T) *toolkit.TreeTableNode, invalidate func()) (unbind func())

BindTree binds an ObservableList to a TreeTable's Root forest: on every list change it rebuilds Root (field TreeTable.Root, a []*TreeTableNode forest — not a flat []string, so mvvm.BindList can't drive it) by projecting each element to a node, then calls invalidate. project owns building each node's Cells and Children; this helper only wires the rebuild-on-change. The returned unbind detaches the list subscription.

func BindViewSwitcher

func BindViewSwitcher(v *toolkit.ViewSwitcher, obs *mvvm.Observable[int], invalidate func()) (unbind func())

BindViewSwitcher two-way-binds a ViewSwitcher's Current segment to an int observable (fields ViewSwitcher.Current / ViewSwitcher.OnChange).

func BindViews

func BindViews[T any](v *toolkit.ViewSwitcher, l *mvvm.ObservableList[T], project func(T) string, invalidate func()) (unbind func())

BindViews binds an ObservableList to a ViewSwitcher's Views slice (field ViewSwitcher.Views), projecting each element to its segment label.

Types

This section is empty.

Jump to

Keyboard shortcuts

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