Debian Codegraph @

_examples/vxfw/input/main.go

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