Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LinearLayout serves as a wrapper for several gio layout structs and it reduces the amount of code required when applying basic attributes like padding, margin, border, etc to a layout.
- Loading branch information
Showing
4 changed files
with
178 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package decredmaterial | ||
|
||
import ( | ||
"image/color" | ||
|
||
"gioui.org/f32" | ||
"gioui.org/layout" | ||
"gioui.org/op/clip" | ||
"gioui.org/op/paint" | ||
"gioui.org/unit" | ||
) | ||
|
||
// Border lays out a widget and draws a border inside it. | ||
type Border struct { | ||
Color color.NRGBA | ||
Radius CornerRadius | ||
Width unit.Value | ||
} | ||
|
||
func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions { | ||
dims := w(gtx) | ||
sz := layout.FPt(dims.Size) | ||
|
||
width := float32(gtx.Px(b.Width)) | ||
sz.X -= width | ||
sz.Y -= width | ||
|
||
r := f32.Rectangle{Max: sz} | ||
r = r.Add(f32.Point{X: width * 0.5, Y: width * 0.5}) | ||
|
||
tr := float32(gtx.Px(unit.Dp(b.Radius.TopRight))) | ||
tl := float32(gtx.Px(unit.Dp(b.Radius.TopLeft))) | ||
br := float32(gtx.Px(unit.Dp(b.Radius.BottomRight))) | ||
bl := float32(gtx.Px(unit.Dp(b.Radius.BottomLeft))) | ||
radius := clip.RRect{ | ||
Rect: r, | ||
NW: tl, NE: tr, SE: br, SW: bl, | ||
} | ||
|
||
paint.FillShape(gtx.Ops, | ||
b.Color, | ||
clip.Stroke{ | ||
Path: radius.Path(gtx.Ops), | ||
Style: clip.StrokeStyle{Width: width}, | ||
}.Op(), | ||
) | ||
|
||
return dims | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package decredmaterial | ||
|
||
import ( | ||
"image/color" | ||
|
||
"gioui.org/f32" | ||
"gioui.org/layout" | ||
"gioui.org/op/clip" | ||
"gioui.org/unit" | ||
) | ||
|
||
const ( | ||
WRAP_CONTENT = -1 | ||
MATCH_PARENT = -2 | ||
) | ||
|
||
type LinearLayout struct { | ||
Width int | ||
Height int | ||
Orientation layout.Axis | ||
Background color.NRGBA | ||
Border Border | ||
Margin layout.Inset | ||
Padding layout.Inset | ||
Direction layout.Direction | ||
} | ||
|
||
func (ll LinearLayout) Layout(gtx C, children ...layout.FlexChild) D { | ||
// draw margin | ||
return ll.Margin.Layout(gtx, func(gtx C) D { | ||
return layout.Stack{}.Layout(gtx, | ||
layout.Expanded(func(gtx C) D { | ||
ll.applyDimension(>x) | ||
// // draw background and and clip the background to border radius | ||
tr := float32(gtx.Px(unit.Dp(ll.Border.Radius.TopRight))) | ||
tl := float32(gtx.Px(unit.Dp(ll.Border.Radius.TopLeft))) | ||
br := float32(gtx.Px(unit.Dp(ll.Border.Radius.BottomRight))) | ||
bl := float32(gtx.Px(unit.Dp(ll.Border.Radius.BottomLeft))) | ||
clip.RRect{ | ||
Rect: f32.Rectangle{Max: f32.Point{ | ||
X: float32(gtx.Constraints.Min.X), | ||
Y: float32(gtx.Constraints.Min.Y), | ||
}}, | ||
NW: tl, NE: tr, SE: br, SW: bl, | ||
}.Add(gtx.Ops) | ||
return fill(gtx, ll.Background) | ||
}), | ||
layout.Stacked(func(gtx C) D { | ||
ll.applyDimension(>x) | ||
return ll.Border.Layout(gtx, func(gtx C) D { | ||
// draw padding | ||
return ll.Padding.Layout(gtx, func(gtx C) D { | ||
// draw layout direction | ||
return ll.Direction.Layout(gtx, func(gtx C) D { | ||
return layout.Flex{Axis: ll.Orientation}.Layout(gtx, children...) | ||
}) | ||
}) | ||
}) | ||
}), | ||
) | ||
}) | ||
} | ||
|
||
func (ll LinearLayout) applyDimension(gtx *C) { | ||
if ll.Width == MATCH_PARENT { | ||
gtx.Constraints.Min.X = gtx.Constraints.Max.X | ||
} else if ll.Width != WRAP_CONTENT { | ||
gtx.Constraints.Min.X = ll.Width | ||
gtx.Constraints.Max.X = ll.Width | ||
} | ||
|
||
if ll.Height == MATCH_PARENT { | ||
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y | ||
} else if ll.Height != WRAP_CONTENT { | ||
gtx.Constraints.Min.Y = ll.Height | ||
gtx.Constraints.Max.Y = ll.Height | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters