| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"log" |
| 5 |
|
| 6 |
"git.sr.ht/~rockorager/vaxis" |
| 7 |
"git.sr.ht/~rockorager/vaxis/vxfw" |
| 8 |
"git.sr.ht/~rockorager/vaxis/vxfw/button" |
| 9 |
) |
| 10 |
|
| 11 |
type App struct { |
| 12 |
b *button.Button |
| 13 |
} |
| 14 |
|
| 15 |
func (a *App) CaptureEvent(ev vaxis.Event) (vxfw.Command, error) { |
| 16 |
switch ev := ev.(type) { |
| 17 |
case vaxis.Key: |
| 18 |
if ev.Matches('c', vaxis.ModCtrl) { |
| 19 |
return vxfw.QuitCmd{}, nil |
| 20 |
} |
| 21 |
if ev.Matches('l', vaxis.ModCtrl) { |
| 22 |
return []vxfw.Command{vxfw.DebugCmd{}, vxfw.RedrawCmd{}}, nil |
| 23 |
} |
| 24 |
} |
| 25 |
return nil, nil |
| 26 |
} |
| 27 |
|
| 28 |
func (a *App) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) { |
| 29 |
switch ev.(type) { |
| 30 |
case vxfw.Init: |
| 31 |
return vxfw.FocusWidgetCmd(a.b), nil |
| 32 |
} |
| 33 |
return nil, nil |
| 34 |
} |
| 35 |
|
| 36 |
func (a *App) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) { |
| 37 |
chCtx := ctx.WithMax(vxfw.Size{ |
| 38 |
Width: ctx.Max.Width / 2, |
| 39 |
Height: ctx.Max.Height / 2, |
| 40 |
}) |
| 41 |
s, err := a.b.Draw(chCtx) |
| 42 |
if err != nil { |
| 43 |
return vxfw.Surface{}, err |
| 44 |
} |
| 45 |
|
| 46 |
root := vxfw.NewSurface(s.Size.Width, s.Size.Height, a) |
| 47 |
root.AddChild(0, 0, s) |
| 48 |
|
| 49 |
return root, nil |
| 50 |
} |
| 51 |
|
| 52 |
func (a *App) onClick() (vxfw.Command, error) { |
| 53 |
return vxfw.QuitCmd{}, nil |
| 54 |
} |
| 55 |
|
| 56 |
func main() { |
| 57 |
app, err := vxfw.NewApp(vaxis.Options{}) |
| 58 |
if err != nil { |
| 59 |
log.Fatalf("Couldn't create a new app: %v", err) |
| 60 |
} |
| 61 |
|
| 62 |
root := &App{} |
| 63 |
root.b = button.New("Click me!", root.onClick) |
| 64 |
|
| 65 |
app.Run(root) |
| 66 |
} |