layout,widget: transpose Constraints to use image.Points for limits

Instead of

    type Contraints struct {
	    Width, Height Constraint
    }

use

    type Constraints struct {
	    Min, Max image.Point
    }

which leads to simpler use. For example, the Min method is trivally replaced by
the field, and the RigidConstraints constructor is no longer a net win.

API Change. Rewrites:

    gofmt -r 'gtx.Constraints.Min() -> gtx.Constraints.Min'
    gofmt -r 'gtx.Constraints.Width.Min -> gtx.Constraints.Min.X'
    gofmt -r 'gtx.Constraints.Height.Min -> gtx.Constraints.Min.Y'
    gofmt -r 'gtx.Constraints.Height.Max -> gtx.Constraints.Max.Y'
    gofmt -r 'gtx.Constraints.Width.Max -> gtx.Constraints.Max.X'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-19 09:50:37 +02:00
parent 67a9d9e9d7
commit 7bf3265ccd
16 changed files with 99 additions and 122 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ func (b *Clickable) Layout(gtx *layout.Context) {
b.Update(gtx)
var st op.StackOp
st.Push(gtx.Ops)
pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min()}).Add(gtx.Ops)
pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
b.click.Add(gtx.Ops)
st.Pop()
for len(b.history) > 0 {
+1 -1
View File
@@ -244,7 +244,7 @@ func (e *Editor) Layout(gtx *layout.Context, sh text.Shaper, font text.Font, siz
e.font = font
e.textSize = textSize
}
maxWidth := gtx.Constraints.Width.Max
maxWidth := gtx.Constraints.Max.X
if e.SingleLine {
maxWidth = inf
}
+1 -1
View File
@@ -32,7 +32,7 @@ func (im Image) Layout(gtx *layout.Context) {
wf, hf := float32(size.X), float32(size.Y)
w, h := gtx.Px(unit.Dp(wf*scale)), gtx.Px(unit.Dp(hf*scale))
cs := gtx.Constraints
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
d := cs.Constrain(image.Pt(w, h))
var s op.StackOp
s.Push(gtx.Ops)
clip.Rect{Rect: f32.Rectangle{Max: toPointF(d)}}.Op(gtx.Ops).Add(gtx.Ops)
+1 -1
View File
@@ -87,7 +87,7 @@ func (l *lineIterator) Next() (int, int, []text.Glyph, f32.Point, bool) {
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, size unit.Value, txt string) {
cs := gtx.Constraints
textSize := fixed.I(gtx.Px(size))
lines := s.LayoutString(font, textSize, cs.Width.Max, txt)
lines := s.LayoutString(font, textSize, cs.Max.X, txt)
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
}
+8 -10
View File
@@ -87,8 +87,8 @@ func Clickable(gtx *layout.Context, button *widget.Clickable, w layout.Widget) {
layout.Expanded(func() {
clip.Rect{
Rect: f32.Rectangle{Max: f32.Point{
X: float32(gtx.Constraints.Width.Min),
Y: float32(gtx.Constraints.Height.Min),
X: float32(gtx.Constraints.Min.X),
Y: float32(gtx.Constraints.Min.Y),
}},
}.Op(gtx.Ops).Add(gtx.Ops)
for _, c := range button.History() {
@@ -111,15 +111,14 @@ func (b ButtonStyle) Layout(gtx *layout.Context, button *widget.Clickable) {
}
func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Clickable, w layout.Widget) {
hmin := gtx.Constraints.Width.Min
vmin := gtx.Constraints.Height.Min
min := gtx.Constraints.Min
layout.Stack{Alignment: layout.Center}.Layout(gtx,
layout.Expanded(func() {
rr := float32(gtx.Px(b.CornerRadius))
clip.Rect{
Rect: f32.Rectangle{Max: f32.Point{
X: float32(gtx.Constraints.Width.Min),
Y: float32(gtx.Constraints.Height.Min),
X: float32(gtx.Constraints.Min.X),
Y: float32(gtx.Constraints.Min.Y),
}},
NE: rr, NW: rr, SE: rr, SW: rr,
}.Op(gtx.Ops).Add(gtx.Ops)
@@ -129,8 +128,7 @@ func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Clickable,
}
}),
layout.Stacked(func() {
gtx.Constraints.Width.Min = hmin
gtx.Constraints.Height.Min = vmin
gtx.Constraints.Min = min
layout.Center.Layout(gtx, func() {
b.Inset.Layout(gtx, func() {
w()
@@ -146,7 +144,7 @@ func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Clickable,
func (b IconButtonStyle) Layout(gtx *layout.Context, button *widget.Clickable) {
layout.Stack{Alignment: layout.Center}.Layout(gtx,
layout.Expanded(func() {
size := gtx.Constraints.Width.Min
size := gtx.Constraints.Min.X
sizef := float32(size)
rr := sizef * .5
clip.Rect{
@@ -171,7 +169,7 @@ func (b IconButtonStyle) Layout(gtx *layout.Context, button *widget.Clickable) {
})
}),
layout.Expanded(func() {
pointer.Ellipse(image.Rectangle{Max: gtx.Constraints.Min()}).Add(gtx.Ops)
pointer.Ellipse(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
button.Layout(gtx)
}),
)
+2 -4
View File
@@ -34,8 +34,7 @@ func (c *checkable) layout(gtx *layout.Context, checked bool) {
icon = c.uncheckedStateIcon
}
hmin := gtx.Constraints.Width.Min
vmin := gtx.Constraints.Height.Min
min := gtx.Constraints.Min
layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func() {
layout.Center.Layout(gtx, func() {
@@ -51,8 +50,7 @@ func (c *checkable) layout(gtx *layout.Context, checked bool) {
}),
layout.Rigid(func() {
gtx.Constraints.Width.Min = hmin
gtx.Constraints.Height.Min = vmin
gtx.Constraints.Min = min
layout.W.Layout(gtx, func() {
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
+4 -4
View File
@@ -45,11 +45,11 @@ func (e EditorStyle) Layout(gtx *layout.Context, editor *widget.Editor) {
tl := widget.Label{Alignment: editor.Alignment}
tl.Layout(gtx, e.shaper, e.Font, e.TextSize, e.Hint)
macro.Stop()
if w := gtx.Dimensions.Size.X; gtx.Constraints.Width.Min < w {
gtx.Constraints.Width.Min = w
if w := gtx.Dimensions.Size.X; gtx.Constraints.Min.X < w {
gtx.Constraints.Min.X = w
}
if h := gtx.Dimensions.Size.Y; gtx.Constraints.Height.Min < h {
gtx.Constraints.Height.Min = h
if h := gtx.Dimensions.Size.Y; gtx.Constraints.Min.Y < h {
gtx.Constraints.Min.Y = h
}
editor.Layout(gtx, e.shaper, e.Font, e.TextSize)
if editor.Len() > 0 {
+1 -1
View File
@@ -50,7 +50,7 @@ func (b ProgressBarStyle) Layout(gtx *layout.Context, progress int) {
progress = 0
}
progressBarWidth := float32(gtx.Constraints.Width.Max)
progressBarWidth := float32(gtx.Constraints.Max.X)
layout.Stack{Alignment: layout.W}.Layout(gtx,
layout.Stacked(func() {
+1 -2
View File
@@ -3,7 +3,6 @@
package material
import (
"image"
"image/color"
"gioui.org/f32"
@@ -66,7 +65,7 @@ func argb(c uint32) color.RGBA {
func fill(gtx *layout.Context, col color.RGBA) {
cs := gtx.Constraints
d := image.Point{X: cs.Width.Min, Y: cs.Height.Min}
d := cs.Min
dr := f32.Rectangle{
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
}