Skip to content

Commit

Permalink
update gio
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasnaur committed Sep 25, 2019
1 parent 27cd4aa commit 49d01e8
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 138 deletions.
36 changes: 18 additions & 18 deletions centering.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ func main() {
go func() {
w := app.NewWindow()
regular, _ := sfnt.Parse(goregular.TTF)
var cfg ui.Config
var faces measure.Faces
ops := new(ui.Ops)
gtx := &layout.Context{
Queue: w.Queue(),
}
for e := range w.Events() {
if e, ok := e.(app.UpdateEvent); ok {
cfg = &e.Config
cs := layout.RigidConstraints(e.Size)
ops.Reset()
faces.Reset(cfg)
gtx.Reset(&e.Config, layout.RigidConstraints(e.Size))
faces.Reset(gtx.Config)
f := faces.For(regular, ui.Sp(72))
drawLabels(f, ops, cs)
w.Update(ops)
drawLabels(gtx, f)
w.Update(gtx.Ops)
}
}
}()
app.Main()
}

// START OMIT
func drawLabels(face text.Face, ops *ui.Ops, cs layout.Constraints) {
cs.Width.Min = 0
cs.Height.Min = 0
func drawLabels(gtx *layout.Context, face text.Face) {
gtx.Constraints.Width.Min = 0
gtx.Constraints.Height.Min = 0
lbl := text.Label{Face: face, Text: "I'm centered!"}
var macro ui.MacroOp // HLcenter
macro.Record(ops) // Start recording // HLcenter
dimensions := lbl.Layout(ops, cs)
var macro ui.MacroOp // HLcenter
macro.Record(gtx.Ops) // Start recording // HLcenter
lbl.Layout(gtx)
dimensions := gtx.Dimensions
macro.Stop() // End recording // HLcenter
ui.TransformOp{}.Offset(f32.Point{
X: float32(cs.Width.Max-dimensions.Size.X) / 2,
Y: float32(cs.Height.Max-dimensions.Size.Y) / 2,
}).Add(ops)
macro.Add(ops) // Replay operations // HLcenter
X: float32(gtx.Constraints.Width.Max-dimensions.Size.X) / 2,
Y: float32(gtx.Constraints.Height.Max-dimensions.Size.Y) / 2,
}).Add(gtx.Ops)
macro.Add(gtx.Ops) // Replay operations // HLcenter
}

// END OMIT
16 changes: 7 additions & 9 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@ func main() {
go func() {
w := app.NewWindow()
regular, _ := sfnt.Parse(goregular.TTF) // HLdraw
var cfg ui.Config // HLdraw
ops := new(ui.Ops) // HLdraw
// START INIT OMIT
var faces measure.Faces // HLdraw
editor := &text.Editor{
Face: faces.For(regular, ui.Sp(52)),
}
editor.SetText("Hello, Gophercon! Edit me.")
gtx := &layout.Context{
Queue: w.Queue(),
}
// END INIT OMIT
for e := range w.Events() {
if e, ok := e.(app.UpdateEvent); ok {
cfg = &e.Config // HLdraw
cs := layout.RigidConstraints(e.Size) // HLdraw
ops.Reset() // HLdraw
faces.Reset(cfg) // HLdraw
queue := w.Queue()
gtx.Reset(&e.Config, layout.RigidConstraints(e.Size)) // HLdraw
faces.Reset(gtx.Config) // HLdraw
// START OMIT
editor.Layout(cfg, queue, ops, cs)
editor.Layout(gtx)
// END OMIT
w.Update(ops) // HLdraw
w.Update(gtx.Ops) // HLdraw
}
} // HLeventloop
}()
Expand Down
39 changes: 19 additions & 20 deletions flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"image"
"image/color"

"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/f32"
"gioui.org/ui/layout"
Expand All @@ -14,51 +13,51 @@ import (
func main() {
go func() {
w := app.NewWindow()
var cfg app.Config
ops := new(ui.Ops)
gtx := &layout.Context{
Queue: w.Queue(),
}
for e := range w.Events() {
if e, ok := e.(app.UpdateEvent); ok {
cfg = e.Config
cs := layout.RigidConstraints(e.Size)
ops.Reset()
drawRects(&cfg, ops, cs)
w.Update(ops)
gtx.Reset(&e.Config, layout.RigidConstraints(e.Size))
drawRects(gtx)
w.Update(gtx.Ops)
}
}
}()
app.Main()
}

// START OMIT
func drawRects(c ui.Config, ops *ui.Ops, cs layout.Constraints) {
func drawRects(gtx *layout.Context) {
flex := layout.Flex{}
flex.Init(ops, cs)
flex.Init(gtx)

red := flex.Flexible(0.5, func(cs layout.Constraints) layout.Dimensions {
return drawRect(c, ops, color.RGBA{A: 0xff, R: 0xff}, cs)
red := flex.Flexible(0.5, func() {
drawRect(gtx, color.RGBA{A: 0xff, R: 0xff})
})

green := flex.Flexible(0.25, func(cs layout.Constraints) layout.Dimensions {
return drawRect(c, ops, color.RGBA{A: 0xff, G: 0xff}, cs)
green := flex.Flexible(0.25, func() {
drawRect(gtx, color.RGBA{A: 0xff, G: 0xff})
})

blue := flex.Flexible(0.25, func(cs layout.Constraints) layout.Dimensions {
return drawRect(c, ops, color.RGBA{A: 0xff, B: 0xff}, cs)
blue := flex.Flexible(0.25, func() {
drawRect(gtx, color.RGBA{A: 0xff, B: 0xff})
})

flex.Layout(red, green, blue)
}

// END OMIT

func drawRect(c ui.Config, ops *ui.Ops, color color.RGBA, cs layout.Constraints) layout.Dimensions {
func drawRect(gtx *layout.Context, color color.RGBA) {
cs := gtx.Constraints
square := f32.Rectangle{
Max: f32.Point{
X: float32(cs.Width.Max),
Y: float32(cs.Height.Max),
},
}
paint.ColorOp{Color: color}.Add(ops)
paint.PaintOp{Rect: square}.Add(ops)
return layout.Dimensions{Size: image.Point{X: cs.Width.Max, Y: cs.Height.Max}}
paint.ColorOp{Color: color}.Add(gtx.Ops)
paint.PaintOp{Rect: square}.Add(gtx.Ops)
gtx.Dimensions = layout.Dimensions{Size: image.Point{X: cs.Width.Max, Y: cs.Height.Max}}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module gophercon2019
go 1.13

require (
gioui.org/ui v0.0.0-20190918182616-7ca1d7a3fee0
gioui.org/ui v0.0.0-20190925074315-9c33550644a7
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gioui.org/ui v0.0.0-20190918182616-7ca1d7a3fee0 h1:1popnecUCpLbiELuAfx0id4JtlN2xehC6QRLITvug30=
gioui.org/ui v0.0.0-20190918182616-7ca1d7a3fee0/go.mod h1:PssKPKlqVIeyaed+0w492Xc2NgX5M3n6oZKOAj5rxoE=
gioui.org/ui v0.0.0-20190925074315-9c33550644a7 h1:l/WdLfRQnDRjipqy7NP9iJcq/zXABhsCd7vGX8oapYo=
gioui.org/ui v0.0.0-20190925074315-9c33550644a7/go.mod h1:PssKPKlqVIeyaed+0w492Xc2NgX5M3n6oZKOAj5rxoE=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9 h1:uc17S921SPw5F2gJo7slQ3aqvr2RwpL7eb3+DZncu3s=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
Expand Down
40 changes: 18 additions & 22 deletions gophercon-2019.slide
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,17 @@ Dimensions are output

* Constraints and dimensions

Widgets accept constraints, output dimensions
Widgets accept constraints, output dimensions, stored in
layout.Context.

package text // import gioui.org/ui/text

func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimensions
func (e *Editor) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimensions
func (l Label) Layout(gtx *layout.Context)
func (e *Editor) Layout(gtx *layout.Context)

package widget // import gioui.org/ui/widget

func (im Image) Layout(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimensions
func (im Image) Layout(gtx *layout.Context)


: Gio borrows Constraints and Dimensions from the Flutter framework.
Expand Down Expand Up @@ -424,25 +425,21 @@ Widgets accept constraints, output dimensions

Aligning

var ops *ui.Ops
var cs layout.Constraints
var gtx *layout.Context

align := layout.Align{Alignment: layout.Center}
cs = align.Begin(ops, cs)
...
dimensions := someWidget.Layout(..., cs) // Draw widget
...
dimensions = align.End(dimensions)
align := layout.Align(layout.Center)
align.Layout(gtx, func() {
someWidget.Layout(gtx, ...) // Draw widget
})
dimensions := gtx.Dimensions

Insetting

var c ui.Config
inset := layout.Inset{Top: ui.Dp(8), ...} // 8dp top inset
cs = inset.Begin(c, ops, cs)
...
dimensions := anotherWidget.Layout(..., cs) // Draw widget
...
dimensions = inset.End(dimensions)
inset.Layout(gtx, func() {
anotherWidget.Layout(gtx, ...) // Draw widget
})
dimensions := gtx.Dimensions

: Centering is a special case of aligning widgets. Aligning and insetting widgets are so common that Gio provides two helper
: types in the layout package, Align and Inset.
Expand Down Expand Up @@ -551,9 +548,9 @@ Lay out widgets on an axis.



* Window input queue
* Window event queue

The Window's Queue method returns an input.Queue for OS events.
The Window's Queue method returns an ui.Queue for OS events.

package app // import gioui.org/ui/app

Expand All @@ -565,11 +562,10 @@ The Window's Queue method returns an input.Queue for OS events.

import "gioui.org/ui"
import "gioui.org/ui/gesture"
import "gioui.org/ui/input"

Detect clicks

var queue input.Queue
var queue ui.Queue
var c gesture.Click
for event, ok := c.Next(queue); ok; event, ok = c.Next(queue) {
// event is a gesture.ClickEvent, not a raw pointer.Event.
Expand Down
15 changes: 7 additions & 8 deletions helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ func main() {
go func() {
w := app.NewWindow()
regular, _ := sfnt.Parse(goregular.TTF)
var cfg ui.Config
var faces measure.Faces
ops := new(ui.Ops)
gtx := &layout.Context{
Queue: w.Queue(),
}
for e := range w.Events() {
if e, ok := e.(app.UpdateEvent); ok {
cfg = &e.Config
cs := layout.RigidConstraints(e.Size)
ops.Reset()
faces.Reset(cfg)
gtx.Reset(&e.Config, layout.RigidConstraints(e.Size))
faces.Reset(gtx.Config)

lbl := text.Label{Face: faces.For(regular, ui.Sp(72)), Text: "Hello, World!"} // HLdraw
lbl.Layout(ops, cs) // HLdraw
lbl.Layout(gtx) // HLdraw

w.Update(ops)
w.Update(gtx.Ops)
}
} // HLeventloop
}()
Expand Down
22 changes: 10 additions & 12 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/input"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
"gioui.org/ui/text"
Expand All @@ -18,37 +17,36 @@ func main() {
go func() {
w := app.NewWindow()
regular, _ := sfnt.Parse(goregular.TTF)
var cfg ui.Config
var faces measure.Faces
ops := new(ui.Ops)
// START INIT OMIT
list := &layout.List{
Axis: layout.Vertical,
}
gtx := &layout.Context{
Queue: w.Queue(),
}
// END INIT OMIT
for e := range w.Events() {
if e, ok := e.(app.UpdateEvent); ok {
cfg = &e.Config
cs := layout.RigidConstraints(e.Size)
ops.Reset()
faces.Reset(cfg)
gtx.Reset(&e.Config, layout.RigidConstraints(e.Size))
faces.Reset(gtx.Config)
f := faces.For(regular, ui.Sp(42))
drawList(cfg, w.Queue(), list, f, ops, cs)
w.Update(ops)
drawList(gtx, list, f)
w.Update(gtx.Ops)
}
}
}()
app.Main()
}

// START OMIT
func drawList(c ui.Config, q input.Queue, list *layout.List, face text.Face, ops *ui.Ops, cs layout.Constraints) {
func drawList(gtx *layout.Context, list *layout.List, face text.Face) {
const n = 1e6
list.Layout(c, q, ops, cs, n, func(cs layout.Constraints, i int) layout.Dimensions {
list.Layout(gtx, n, func(i int) {
txt := fmt.Sprintf("List element #%d", i)

lbl := text.Label{Face: face, Text: txt}
return lbl.Layout(ops, cs)
lbl.Layout(gtx)
})
}

Expand Down
3 changes: 1 addition & 2 deletions pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/f32"
"gioui.org/ui/input"
"gioui.org/ui/paint"
"gioui.org/ui/pointer"
)
Expand Down Expand Up @@ -37,7 +36,7 @@ type Button struct {
}

// START OMIT
func (b *Button) Layout(queue input.Queue, ops *ui.Ops) {
func (b *Button) Layout(queue ui.Queue, ops *ui.Ops) {
for e, ok := queue.Next(b); ok; e, ok = queue.Next(b) { // HLevent
if e, ok := e.(pointer.Event); ok { // HLevent
switch e.Type { // HLevent
Expand Down
Loading

0 comments on commit 49d01e8

Please sign in to comment.