mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 18:05:35 +00:00
layout: replace Align with a Layout method on Direction
It's one less type (Align) and shorter: Before: layout.Align(layout.Center).Layout(...) After layout.Center.Layout(...) It is also safer: since `layout.Align(...)` was a casting operation, the Go compiler would not complain about an incompatible constant. For example, the widget/material package contained a wrong cast: layout.Align(layout.Start) which should have been layout.Align(layout.W) After this change, attempting `layout.Start.Layout(...)` result in a compile error. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -30,13 +30,12 @@ func ExampleInset() {
|
|||||||
// (70,70)
|
// (70,70)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleAlign() {
|
func ExampleDirection() {
|
||||||
gtx := new(layout.Context)
|
gtx := new(layout.Context)
|
||||||
// Rigid constraints with both minimum and maximum set.
|
// Rigid constraints with both minimum and maximum set.
|
||||||
gtx.Reset(nil, image.Point{X: 100, Y: 100})
|
gtx.Reset(nil, image.Point{X: 100, Y: 100})
|
||||||
|
|
||||||
align := layout.Align(layout.Center)
|
layout.Center.Layout(gtx, func() {
|
||||||
align.Layout(gtx, func() {
|
|
||||||
// Lay out a 50x50 sized widget.
|
// Lay out a 50x50 sized widget.
|
||||||
layoutWidget(gtx, 50, 50)
|
layoutWidget(gtx, 50, 50)
|
||||||
fmt.Println(gtx.Dimensions.Size)
|
fmt.Println(gtx.Dimensions.Size)
|
||||||
|
|||||||
+2
-5
@@ -95,9 +95,6 @@ type Inset struct {
|
|||||||
Top, Right, Bottom, Left unit.Value
|
Top, Right, Bottom, Left unit.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Align aligns a widget in the available space.
|
|
||||||
type Align Direction
|
|
||||||
|
|
||||||
// Layout a widget.
|
// Layout a widget.
|
||||||
func (in Inset) Layout(gtx *Context, w Widget) {
|
func (in Inset) Layout(gtx *Context, w Widget) {
|
||||||
top := gtx.Px(in.Top)
|
top := gtx.Px(in.Top)
|
||||||
@@ -140,8 +137,8 @@ func UniformInset(v unit.Value) Inset {
|
|||||||
return Inset{Top: v, Right: v, Bottom: v, Left: v}
|
return Inset{Top: v, Right: v, Bottom: v, Left: v}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Layout a widget.
|
// Layout a widget according to the direction.
|
||||||
func (a Align) Layout(gtx *Context, w Widget) {
|
func (a Direction) Layout(gtx *Context, w Widget) {
|
||||||
var macro op.MacroOp
|
var macro op.MacroOp
|
||||||
macro.Record(gtx.Ops)
|
macro.Record(gtx.Ops)
|
||||||
cs := gtx.Constraints
|
cs := gtx.Constraints
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
|
|||||||
layout.Stacked(func() {
|
layout.Stacked(func() {
|
||||||
gtx.Constraints.Width.Min = hmin
|
gtx.Constraints.Width.Min = hmin
|
||||||
gtx.Constraints.Height.Min = vmin
|
gtx.Constraints.Height.Min = vmin
|
||||||
layout.Align(layout.Center).Layout(gtx, func() {
|
layout.Center.Layout(gtx, func() {
|
||||||
layout.Inset{Top: unit.Dp(10), Bottom: unit.Dp(10), Left: unit.Dp(12), Right: unit.Dp(12)}.Layout(gtx, func() {
|
layout.Inset{Top: unit.Dp(10), Bottom: unit.Dp(10), Left: unit.Dp(12), Right: unit.Dp(12)}.Layout(gtx, func() {
|
||||||
paint.ColorOp{Color: col}.Add(gtx.Ops)
|
paint.ColorOp{Color: col}.Add(gtx.Ops)
|
||||||
widget.Label{}.Layout(gtx, b.shaper, b.Font, b.Text)
|
widget.Label{}.Layout(gtx, b.shaper, b.Font, b.Text)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
|||||||
vmin := gtx.Constraints.Height.Min
|
vmin := gtx.Constraints.Height.Min
|
||||||
layout.Flex{Alignment: layout.Middle}.Layout(gtx,
|
layout.Flex{Alignment: layout.Middle}.Layout(gtx,
|
||||||
layout.Rigid(func() {
|
layout.Rigid(func() {
|
||||||
layout.Align(layout.Center).Layout(gtx, func() {
|
layout.Center.Layout(gtx, func() {
|
||||||
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
||||||
size := gtx.Px(c.Size)
|
size := gtx.Px(c.Size)
|
||||||
icon.Color = c.IconColor
|
icon.Color = c.IconColor
|
||||||
@@ -53,7 +53,7 @@ func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
|||||||
layout.Rigid(func() {
|
layout.Rigid(func() {
|
||||||
gtx.Constraints.Width.Min = hmin
|
gtx.Constraints.Width.Min = hmin
|
||||||
gtx.Constraints.Height.Min = vmin
|
gtx.Constraints.Height.Min = vmin
|
||||||
layout.Align(layout.Start).Layout(gtx, func() {
|
layout.W.Layout(gtx, func() {
|
||||||
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
|
||||||
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
|
paint.ColorOp{Color: c.Color}.Add(gtx.Ops)
|
||||||
widget.Label{}.Layout(gtx, c.shaper, c.Font, c.Label)
|
widget.Label{}.Layout(gtx, c.shaper, c.Font, c.Label)
|
||||||
|
|||||||
Reference in New Issue
Block a user