Documentation
¶
Overview ¶
Package constraint implements a constraint-based layout system.
func (v *View) Build(ctx *view.Context) view.Model { // Create a new constraint system. l := &constraint.Layouter{} // Solves for the position of v, given the constraints on s. The result is a 400x100 frame. l.Solve(func(s *constraint.Solver) { s.Width(400) s.Width(200) // If two constraints conflict, the earlier one is preferred. s.Height(100) } // Adds a child view and solves for its position relative to v. The result is a 5x10 frame pinned to the lower right corner of v. child1 := basicview.New(ctx, "child1") guide1 := l.Add(child1, func(s *constraint.Solver) { s.Width(5) // Left(), Top(), CenterX()... methods support constraining to floats. s.Height(10) s.TopEqual(l.Bottom()) // LeftEqual(), TopLess(), CenterXGreater()... methods support constraining to anchors. s.LeftEqual(l.Right()) }) // Anchors can be manipulated outside of the solver function. verticalCenter := l.CenterX().Add(10) // Adds a child view that is twice as large as child1 and 10 points above the center v. child2 := basicview.New(ctx, "child2") _ = l.Add(child1, func(s *constraint.Solver) { s.WidthEqual(guide1.Width().Mul(2)) // Anchors can be added to and multiplied by constants. s.HeightEqual(guide1.Height().Mul(2)) s.CenterXEqual(l.CenterX()) s.CenterYEqual(verticalCenter.Add(10)) }) // Recalulates the constraints for child1. guide1.Solve(func(s *constraint.Solver) { s.Width(40) s.Height(30) s.TopEqual(l.Bottom()) // The top and left position must be respecified, even though only the width and height have been updated. s.LeftEqual(l.Right()) }) // Solvers do not run simultaneously! Child2 is still 10x20 since at the time it was added Child1 was 5x10. return view.Model{ Views: l.Views(), Layouter:l, } }
If a child view is unconstrained in x or y, it will try to move as close to the center of the parent as possible. If the view is unconstrained in width or height, it will try to match the minGuide as close as possible.
Index ¶
- type Anchor
- type Guide
- func (g *Guide) Bottom() *Anchor
- func (g *Guide) CenterX() *Anchor
- func (g *Guide) CenterY() *Anchor
- func (g *Guide) Height() *Anchor
- func (g *Guide) Left() *Anchor
- func (g *Guide) Right() *Anchor
- func (g *Guide) Solve(solveFunc func(*Solver))
- func (g *Guide) Top() *Anchor
- func (g *Guide) Width() *Anchor
- type Layouter
- func (l *Layouter) Add(v view.View, solveFunc func(*Solver)) *Guide
- func (l *Layouter) Layout(ctx *layout.Context) (layout.Guide, map[matcha.Id]layout.Guide)
- func (l *Layouter) MaxGuide() *Guide
- func (l *Layouter) MinGuide() *Guide
- func (l *Layouter) Notify(f func()) comm.Id
- func (l *Layouter) Solve(solveFunc func(*Solver))
- func (l *Layouter) Unnotify(id comm.Id)
- func (l *Layouter) Views() []view.View
- type Solver
- func (s *Solver) Bottom(v float64)
- func (s *Solver) BottomEqual(a *Anchor)
- func (s *Solver) BottomGreater(a *Anchor)
- func (s *Solver) BottomLess(a *Anchor)
- func (s *Solver) CenterX(v float64)
- func (s *Solver) CenterXEqual(a *Anchor)
- func (s *Solver) CenterXGreater(a *Anchor)
- func (s *Solver) CenterXLess(a *Anchor)
- func (s *Solver) CenterY(v float64)
- func (s *Solver) CenterYEqual(a *Anchor)
- func (s *Solver) CenterYGreater(a *Anchor)
- func (s *Solver) CenterYLess(a *Anchor)
- func (s *Solver) Debug()
- func (s *Solver) Height(v float64)
- func (s *Solver) HeightEqual(a *Anchor)
- func (s *Solver) HeightGreater(a *Anchor)
- func (s *Solver) HeightLess(a *Anchor)
- func (s *Solver) Left(v float64)
- func (s *Solver) LeftEqual(a *Anchor)
- func (s *Solver) LeftGreater(a *Anchor)
- func (s *Solver) LeftLess(a *Anchor)
- func (s *Solver) Right(v float64)
- func (s *Solver) RightEqual(a *Anchor)
- func (s *Solver) RightGreater(a *Anchor)
- func (s *Solver) RightLess(a *Anchor)
- func (s *Solver) String() string
- func (s *Solver) Top(v float64)
- func (s *Solver) TopEqual(a *Anchor)
- func (s *Solver) TopGreater(a *Anchor)
- func (s *Solver) TopLess(a *Anchor)
- func (s *Solver) Width(v float64)
- func (s *Solver) WidthEqual(a *Anchor)
- func (s *Solver) WidthGreater(a *Anchor)
- func (s *Solver) WidthLess(a *Anchor)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Anchor ¶
type Anchor struct {
// contains filtered or unexported fields
}
Anchor represents a float64 value that is materialized during the layout phase.
func Notifier ¶
func Notifier(n comm.Float64Notifier) *Anchor
Notifier returns a new Anchor whose value is equal to n.Value().
type Guide ¶
type Guide struct {
// contains filtered or unexported fields
}
Guide represents a layout.Guide that is materialized during the layout phase.
type Layouter ¶
type Layouter struct { // Guide represents the size of the view that the layouter is attached to. By default Guide is the same size as MinGuide. Guide // contains filtered or unexported fields }
func (*Layouter) Add ¶
Add immediately calls solveFunc to generate the constraints for v. These constraints are solved by l during the layout phase. A corresponding guide is returned, which can be used to position other views or reposition v. If the view is not fully constrained it will try to match the MinGuide in dimension and center. If the child view is not fully constrained it will try to match the parent in center.
func (*Layouter) Layout ¶
Layout evaluates the constraints and returns the calculated guide and child guides.
func (*Layouter) MaxGuide ¶
MaxGuide returns a guide representing the largest allowed size for the view.
func (*Layouter) MinGuide ¶
MinGuide returns a guide representing the smallest allowed size for the view.
func (*Layouter) Notify ¶
Notify calls f anytime a Notifier anchor changes. Other updates to the layouter, such as adding guides will not trigger the notification.
type Solver ¶
type Solver struct {
// contains filtered or unexported fields
}
Solver is a list of constraints to be applied to a view.