gitstack

grindlemire/go-tui code browser

3.5 KB Go 146 lines 2026-04-18 · dcf2824 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Code generated by tui generate. DO NOT EDIT.
// Source: focus.gsx

package main

import (
	"fmt"

	tui "github.com/grindlemire/go-tui"
)

type panelForm struct {
	panels     []string
	panel1     *tui.State[bool]
	panel2     *tui.State[bool]
	panel3     *tui.State[bool]
	focus      *tui.FocusGroup
	clickCount *tui.State[int]
}

func PanelForm() *panelForm {
	p1 := tui.NewState(true)
	p2 := tui.NewState(false)
	p3 := tui.NewState(false)

	return &panelForm{
		panels:     []string{"Inbox", "Drafts", "Sent"},
		panel1:     p1,
		panel2:     p2,
		panel3:     p3,
		focus:      tui.MustNewFocusGroup(p1, p2, p3),
		clickCount: tui.NewState(0),
	}
}

func (p *panelForm) KeyMap() tui.KeyMap {
	return append(p.focus.KeyMap(), []tui.KeyBinding{
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune(' '), func(ke tui.KeyEvent) {
			p.clickCount.Update(func(v int) int { return v + 1 })
		}),
	}...)
}

func (p *panelForm) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithGap(1),
		tui.WithPadding(1),
	)
	__tui_1 := tui.New(
		tui.WithText("Focus Demo — Tab to switch, Space to interact"),
		tui.WithTextGradient(tui.NewGradient(tui.Cyan, tui.Magenta).WithDirection(tui.GradientHorizontal)),
		tui.WithTextStyle(tui.NewStyle().Bold()),
	)
	__tui_0.AddChild(__tui_1)
	__tui_2 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row),
		tui.WithGap(1),
	)
	for i, name := range p.panels {
		_ = i
		if i == p.focus.Current() {
			__tui_3 := tui.New(
				tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
				tui.WithBorder(tui.BorderRounded),
				tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Cyan)),
				tui.WithPadding(1),
				tui.WithWidth(20),
			)
			__tui_4 := tui.New(
				tui.WithText(name),
				tui.WithTextStyle(tui.NewStyle().Foreground(tui.Cyan).Bold()),
			)
			__tui_3.AddChild(__tui_4)
			__tui_5 := tui.New(
				tui.WithText(fmt.Sprintf("Actions: %d", p.clickCount.Get())),
				tui.WithTextStyle(tui.NewStyle().Foreground(tui.BrightWhite)),
			)
			__tui_3.AddChild(__tui_5)
			__tui_2.AddChild(__tui_3)
		} else {
			__tui_6 := tui.New(
				tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
				tui.WithBorder(tui.BorderRounded),
				tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Black)),
				tui.WithPadding(1),
				tui.WithWidth(20),
			)
			__tui_7 := tui.New(
				tui.WithText(name),
				tui.WithTextStyle(tui.NewStyle().Dim()),
			)
			__tui_6.AddChild(__tui_7)
			__tui_2.AddChild(__tui_6)
		}
	}
	__tui_0.AddChild(__tui_2)
	__tui_8 := tui.New(
		tui.WithText("Press Esc to quit"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_0.AddChild(__tui_8)

	return __tui_0
}

func (p *panelForm) UpdateProps(fresh tui.Component) {
	f, ok := fresh.(*panelForm)
	if !ok {
		return
	}
	p.panels = f.panels
	p.focus = f.focus
}

var _ tui.PropsUpdater = (*panelForm)(nil)

// bindAppFields is generated. It wires the component's *tui.App,
// State, Events, and TextArea fields to app. When you override BindApp,
// call this helper instead of hand-maintaining the delegation list.
func (p *panelForm) bindAppFields(app *tui.App) {
	if p.panel1 != nil {
		p.panel1.BindApp(app)
	}
	if p.panel2 != nil {
		p.panel2.BindApp(app)
	}
	if p.panel3 != nil {
		p.panel3.BindApp(app)
	}
	if p.clickCount != nil {
		p.clickCount.BindApp(app)
	}
}

func (p *panelForm) BindApp(app *tui.App) {
	p.bindAppFields(app)
}

var _ tui.AppBinder = (*panelForm)(nil)

// Compile-time interface satisfaction checks.
var (
	_ tui.KeyListener = (*panelForm)(nil)
)