~eliasnaur/gio#55:
updating hello_gio to latest gio

well drat. https://github.com/glycerine/hello_gio is already out of date.

hello_gio.go:29:2: cannot find package gioui.org/text/shape

How would I update it to the latest and greatest Gio ? (what do I replace text/shape and shape.Faces with?)

Thanks!

Jason

Status
REPORTED
Submitter
Jason E. Aten
Assigned to
No-one
Submitted
10 days ago
Updated
9 days ago
Labels
No labels applied.

~eliasnaur 9 days ago

On Sun Nov 3, 2019 at 4:47 PM Jason E. Aten wrote:

well drat. https://github.com/glycerine/hello_gio is already out of date.

hello_gio.go:29:2: cannot find package gioui.org/text/shape

How would I update it to the latest and greatest Gio ? (what do I replace text/shape and shape.Faces with?)

Thanks!

A few things changed in the API since your last update, sorry about that :)

The patch below makes your program run with the latest version Gio.

diff --git hello_gio.go hello_gio.go
index bf3725a..246679f 100644
--- hello_gio.go
+++ hello_gio.go
@@ -21,15 +21,14 @@ import (

    "gioui.org/app"
    "gioui.org/f32"
+   "gioui.org/font/gofont"
    "gioui.org/io/pointer"
+   "gioui.org/io/system"
    "gioui.org/layout"
    "gioui.org/op"
    "gioui.org/op/paint"
-   "gioui.org/text"
-   "gioui.org/text/shape"
    "gioui.org/unit"
-   "golang.org/x/image/font/gofont/goregular"
-   "golang.org/x/image/font/sfnt"
+   "gioui.org/widget/material"
 )

 var _ = paint.ImageOp{}
@@ -44,7 +43,7 @@ func main() {
    //return

    go func() {
-       w := app.NewWindow(app.WithTitle("hello_gio!"))
+       w := app.NewWindow(app.Title("hello_gio!"))
        if err := loop(w); err != nil {
            log.Fatal(err)
        }
@@ -53,14 +52,8 @@ func main() {
 }

 func loop(w *app.Window) error {
-   regular, err := sfnt.Parse(goregular.TTF)
-   if err != nil {
-       panic("failed to load font")
-   }
-   _ = regular
-   var faces shape.Faces
-   face := faces.For(regular, unit.Sp(20))
-   _ = face
+   gofont.Register()
+   theme := material.NewTheme()
    m := setupDrawState(w)
    _ = m
    yellowBkg := true
@@ -68,25 +61,24 @@ func loop(w *app.Window) error {
    for {
        e := <-w.Events()
        switch e := e.(type) {
-       case app.DestroyEvent:
+       case system.DestroyEvent:
            return e.Err
-       case app.UpdateEvent:
-           m.gtx.Reset(&e.Config, e.Size)
-           faces.Reset(m.gtx.Config)
+       case system.FrameEvent:
+           m.gtx.Reset(e.Config, e.Size)

            // draw a pre-rendered png plot on the screen.
            showImage(e, m, yellowBkg)

            // draw some boxes with labels directly.
-           direct(m.gtx, w, e, face)
+           direct(m.gtx, theme, w, e)

            // Submit operations to the window.
-           w.Update(m.gtx.Ops)
+           e.Frame(m.gtx.Ops)
        }
    }
 }

-func direct(gtx *layout.Context, w *app.Window, e app.UpdateEvent, face text.Face) {
+func direct(gtx *layout.Context, theme *material.Theme, w *app.Window, e system.FrameEvent) {
    aqua := color.RGBA{A: 0xff, G: 0xcc, B: 200}
    const borderPix = 5

@@ -95,7 +87,6 @@ func direct(gtx *layout.Context, w *app.Window, e app.UpdateEvent, face text.Fac
        w:         50,
        color:     aqua,
        borderPix: borderPix,
-       face:      face,
    }

    // draws 5 squares
@@ -108,7 +99,7 @@ func direct(gtx *layout.Context, w *app.Window, e app.UpdateEvent, face text.Fac
        ci := 50 * byte(i) // color increment

        // add _0123 to the end so we can see the clipping in action.
-       m.drawc(gtx, x, y, color.RGBA{A: 0xff, G: 0xcc, B: ci, R: 255 - ci}, fmt.Sprintf("%v_0123", i))
+       m.drawc(gtx, theme, x, y, color.RGBA{A: 0xff, G: 0xcc, B: ci, R: 255 - ci}, fmt.Sprintf("%v_0123", i))
    }
 }

@@ -117,11 +108,10 @@ type box struct {
    w         int        //width
    color     color.RGBA // default
    borderPix int        // number of pixels to inset the label from the box edge.
-   face      text.Face  // for label
 }

 // draw a rectangle at x0,y0 with given color; adding it to the gtx.Ops chain.
-func (e *box) drawc(gtx *layout.Context, x0, y0 int, color color.RGBA, label string) {
+func (e *box) drawc(gtx *layout.Context, theme *material.Theme, x0, y0 int, color color.RGBA, label string) {
    ops := gtx.Ops
    paint.ColorOp{Color: color}.Add(ops)
    re := f32.Rectangle{
@@ -144,7 +134,7 @@ func (e *box) drawc(gtx *layout.Context, x0, y0 int, color color.RGBA, label str
    // extend beyond or overflow outside the box.
    e.inPlaceClip(e.borderPix).Add(ops)

-   text.Label{Face: e.face, Text: label}.Layout(gtx)
+   theme.Label(unit.Sp(20), label).Layout(gtx)
    stack.Pop() // ops)

    // Elias comments:
diff --git showimg.go showimg.go
index 088630f..0e8136c 100644
--- showimg.go
+++ showimg.go
@@ -13,6 +13,7 @@ import (

    "gioui.org/app"
    "gioui.org/f32"
+   "gioui.org/io/system"
    "gioui.org/layout"
    "gioui.org/op/paint"
 )
@@ -29,7 +30,7 @@ type myDrawState struct {
    w   *app.Window
    gtx *layout.Context

-   pngPlot     image.Image
+   pngPlotOp   paint.ImageOp
    pngPlotRect image.Rectangle
 }

@@ -41,10 +42,11 @@ func setupDrawState(w *app.Window) *myDrawState {
        Queue: w.Queue(),
    }
    var err error
-   m.pngPlot, _, err = LoadImage("points.png")
+   pngPlot, _, err := LoadImage("points.png")
    panicOn(err)
-   m.pngPlotRect = m.pngPlot.(*image.NRGBA).Rect
-   vv("m.pngPlot.Rect = '%#v'", m.pngPlotRect)
+   m.pngPlotRect = pngPlot.(*image.NRGBA).Rect
+   m.pngPlotOp = paint.NewImageOp(pngPlot)
+   vv("pngPlot.Rect = '%#v'", m.pngPlotRect)
    return m
 }

@@ -61,13 +63,13 @@ func showImageMain() {
        for {
            e := <-w.Events()
            switch e := e.(type) {
-           case app.DestroyEvent:
+           case system.DestroyEvent:
                err = e.Err
                break mainLoop
-           case app.UpdateEvent:
+           case system.FrameEvent:
                //vv("e is '%#v'", e)
                showImage(e, m, yellowBkg)
-               m.w.Update(m.gtx.Ops)
+               e.Frame(m.gtx.Ops)
            }
        }
        panicOn(err)
@@ -75,8 +77,8 @@ func showImageMain() {
    app.Main()
 }

-func showImage(e app.UpdateEvent, m *myDrawState, yellowBkg bool) {
-   m.gtx.Reset(&e.Config, e.Size)
+func showImage(e system.FrameEvent, m *myDrawState, yellowBkg bool) {
+   m.gtx.Reset(e.Config, e.Size)
    ops := m.gtx.Ops

    // choose how big to show the png
@@ -120,8 +122,8 @@ func showImage(e app.UpdateEvent, m *myDrawState, yellowBkg bool) {
    // The ImageOp.Rect specifies the source rectangle.
    // The PaintOp.Rect field specifies the destination rectangle.
    // Scale the PaintOp.Rect to change the size of the rendered png.
-   paint.ImageOp{Src: m.pngPlot, Rect: m.pngPlotRect}.Add(ops) // set the source for the png.
-   paint.PaintOp{Rect: toRectF(imgPos)}.Add(ops)               // set the destination.
+   m.pngPlotOp.Add(ops)
+   paint.PaintOp{Rect: toRectF(imgPos)}.Add(ops) // set the destination.
 }

 func LoadImage(filename string) (image.Image, string, error) {

-- elias

Register here or Log in to comment, or comment via email.