Skip to content

Commit

Permalink
Updating chapter 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonegil committed Feb 23, 2024
1 parent 09df03d commit ac497fa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion egg_timer/01_empty_window.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ has_children: false

# Chapter 1 - An empty window

Updated Feb 23rd 2024
Updated February 23rd 2024

## Goals

Expand Down
2 changes: 1 addition & 1 deletion egg_timer/02_title_and_size.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ has_children: false

# Chapter 2 - Title and size

Updated Feb 23rd 2024
Updated February 23rd 2024

## Goals

Expand Down
2 changes: 1 addition & 1 deletion egg_timer/03_button.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ has_children: false

# Chapter 3 - Button

Updated Feb 23 2024
Updated Feb ruary 23rd 2024

## Goals

Expand Down
14 changes: 8 additions & 6 deletions egg_timer/04_button_low.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ has_children: false

# Chapter 4 - Low button

Updated February 23rd 2024

## Goals

The button can't fill the screen, obviously. So let's move the button to the bottom. To do that we start using the layout concept known as [Flexbox](https://pkg.go.dev/gioui.org/layout#Flex).
Expand Down Expand Up @@ -71,30 +73,30 @@ OK, that was the high level. Now it's time to dive deep. Let's look at the whole
### Code

```go
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
// Let's try out the flexbox layout concept:
case app.FrameEvent:
gtx := app.NewContext(&ops, typ)
// Let's try out the flexbox layout:
layout.Flex{
// Vertical alignment, from top to bottom
Axis: layout.Vertical,
// Empty space is left at the start, i.e. at the top
Spacing: layout.SpaceStart,
}.Layout(gtx,
// We insert two rigid elements:
// First a button ...
// First one to hold a button ...
layout.Rigid(
func(gtx layout.Context) layout.Dimensions {
btn := material.Button(th, &startButton, "Start")
return btn.Layout(gtx)
},
),
// ... then an empty spacer
// ... then one to hold an empty spacer
layout.Rigid(
// The height of the spacer is 25 Device independent pixels
layout.Spacer{Height: unit.Dp(25)}.Layout,
),
)
e.Frame(gtx.Ops)
typ.Frame(gtx.Ops)
```

### Comments
Expand Down
16 changes: 9 additions & 7 deletions egg_timer/code/04_button_low/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"

"gioui.org/app"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
Expand All @@ -30,14 +29,15 @@ func main() {
th := material.NewTheme()

// listen for events in the window.
for e := range w.Events() {
for {
evt := w.NextEvent()

// detect what type of event
switch e := e.(type) {
switch typ := evt.(type) {

// this is sent when the application should re-render.
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
case app.FrameEvent:
gtx := app.NewContext(&ops, typ)
// Let's try out the flexbox layout:
layout.Flex{
// Vertical alignment, from top to bottom
Expand All @@ -59,10 +59,12 @@ func main() {
layout.Spacer{Height: unit.Dp(25)}.Layout,
),
)
e.Frame(gtx.Ops)
typ.Frame(gtx.Ops)

case app.DestroyEvent:
os.Exit(0)
}
}
os.Exit(0)
}()
app.Main()
}

0 comments on commit ac497fa

Please sign in to comment.