constraint

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

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

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 Const

func Const(f float64) *Anchor

Const returns a new Anchor with a constant value f.

func Notifier

func Notifier(n comm.Float64Notifier) *Anchor

Notifier returns a new Anchor whose value is equal to n.Value().

func (*Anchor) Add

func (a *Anchor) Add(v float64) *Anchor

Add returns a new Anchor that is offset by v.

func (*Anchor) Mul

func (a *Anchor) Mul(v float64) *Anchor

Mul returns a new anchor that is multiplied by v.

type Guide

type Guide struct {
	// contains filtered or unexported fields
}

Guide represents a layout.Guide that is materialized during the layout phase.

func (*Guide) Bottom

func (g *Guide) Bottom() *Anchor

Bottom returns the maximum Y coordinate as an Anchor.

func (*Guide) CenterX

func (g *Guide) CenterX() *Anchor

CenterX returns the center of g along the X axis as an Anchor.

func (*Guide) CenterY

func (g *Guide) CenterY() *Anchor

CenterY returns the center of g along the Y axis as an Anchor.

func (*Guide) Height

func (g *Guide) Height() *Anchor

Height returns the height of g as an Anchor.

func (*Guide) Left

func (g *Guide) Left() *Anchor

Left returns the minimum X coordinate as an Anchor.

func (*Guide) Right

func (g *Guide) Right() *Anchor

Right returns the maximum X coordinate as an Anchor.

func (*Guide) Solve

func (g *Guide) Solve(solveFunc func(*Solver))

Solve immediately calls solveFunc to update the constraints for g.

func (*Guide) Top

func (g *Guide) Top() *Anchor

Top returns the minimum Y coordinate as an Anchor.

func (*Guide) Width

func (g *Guide) Width() *Anchor

Width returns the width of g as an Anchor.

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

func (l *Layouter) Add(v view.View, solveFunc func(*Solver)) *Guide

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

func (l *Layouter) Layout(ctx *layout.Context) (layout.Guide, map[matcha.Id]layout.Guide)

Layout evaluates the constraints and returns the calculated guide and child guides.

func (*Layouter) MaxGuide

func (l *Layouter) MaxGuide() *Guide

MaxGuide returns a guide representing the largest allowed size for the view.

func (*Layouter) MinGuide

func (l *Layouter) MinGuide() *Guide

MinGuide returns a guide representing the smallest allowed size for the view.

func (*Layouter) Notify

func (l *Layouter) Notify(f func()) comm.Id

Notify calls f anytime a Notifier anchor changes. Other updates to the layouter, such as adding guides will not trigger the notification.

func (*Layouter) Solve

func (l *Layouter) Solve(solveFunc func(*Solver))

func (*Layouter) Unnotify

func (l *Layouter) Unnotify(id comm.Id)

Unnotify stops notifications for id.

func (*Layouter) Views

func (l *Layouter) Views() []view.View

View returns a list of all views added to l.

type Solver

type Solver struct {
	// contains filtered or unexported fields
}

Solver is a list of constraints to be applied to a view.

func (*Solver) Bottom

func (s *Solver) Bottom(v float64)

func (*Solver) BottomEqual

func (s *Solver) BottomEqual(a *Anchor)

func (*Solver) BottomGreater

func (s *Solver) BottomGreater(a *Anchor)

func (*Solver) BottomLess

func (s *Solver) BottomLess(a *Anchor)

func (*Solver) CenterX

func (s *Solver) CenterX(v float64)

func (*Solver) CenterXEqual

func (s *Solver) CenterXEqual(a *Anchor)

func (*Solver) CenterXGreater

func (s *Solver) CenterXGreater(a *Anchor)

func (*Solver) CenterXLess

func (s *Solver) CenterXLess(a *Anchor)

func (*Solver) CenterY

func (s *Solver) CenterY(v float64)

func (*Solver) CenterYEqual

func (s *Solver) CenterYEqual(a *Anchor)

func (*Solver) CenterYGreater

func (s *Solver) CenterYGreater(a *Anchor)

func (*Solver) CenterYLess

func (s *Solver) CenterYLess(a *Anchor)

func (*Solver) Debug

func (s *Solver) Debug()

Debug adds debug logging for the solver.

func (*Solver) Height

func (s *Solver) Height(v float64)

func (*Solver) HeightEqual

func (s *Solver) HeightEqual(a *Anchor)

func (*Solver) HeightGreater

func (s *Solver) HeightGreater(a *Anchor)

func (*Solver) HeightLess

func (s *Solver) HeightLess(a *Anchor)

func (*Solver) Left

func (s *Solver) Left(v float64)

func (*Solver) LeftEqual

func (s *Solver) LeftEqual(a *Anchor)

func (*Solver) LeftGreater

func (s *Solver) LeftGreater(a *Anchor)

func (*Solver) LeftLess

func (s *Solver) LeftLess(a *Anchor)

func (*Solver) Right

func (s *Solver) Right(v float64)

func (*Solver) RightEqual

func (s *Solver) RightEqual(a *Anchor)

func (*Solver) RightGreater

func (s *Solver) RightGreater(a *Anchor)

func (*Solver) RightLess

func (s *Solver) RightLess(a *Anchor)

func (*Solver) String

func (s *Solver) String() string

func (*Solver) Top

func (s *Solver) Top(v float64)

func (*Solver) TopEqual

func (s *Solver) TopEqual(a *Anchor)

func (*Solver) TopGreater

func (s *Solver) TopGreater(a *Anchor)

func (*Solver) TopLess

func (s *Solver) TopLess(a *Anchor)

func (*Solver) Width

func (s *Solver) Width(v float64)

func (*Solver) WidthEqual

func (s *Solver) WidthEqual(a *Anchor)

func (*Solver) WidthGreater

func (s *Solver) WidthGreater(a *Anchor)

func (*Solver) WidthLess

func (s *Solver) WidthLess(a *Anchor)

Jump to

Keyboard shortcuts

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