-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
panel.go
128 lines (110 loc) · 2.78 KB
/
panel.go
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
package theme
import (
"context"
rtrace "runtime/trace"
"honnef.co/go/gotraceui/color"
"honnef.co/go/gotraceui/layout"
"honnef.co/go/gotraceui/widget"
)
type ComponentButtons struct {
close widget.PrimaryClickable
back widget.PrimaryClickable
detach widget.PrimaryClickable
attach widget.PrimaryClickable
state ComponentState
}
func (pb *ComponentButtons) Transition(state ComponentState) {
pb.state = state
}
func (pb *ComponentButtons) WantsTransition(gtx layout.Context) ComponentState {
if pb.detach.Clicked(gtx) {
return ComponentStateTab
} else if pb.attach.Clicked(gtx) {
return ComponentStatePanel
} else if pb.close.Clicked(gtx) {
return ComponentStateClosed
} else {
return ComponentStateNone
}
}
func (pb *ComponentButtons) Backed(gtx layout.Context) bool {
return pb.back.Clicked(gtx)
}
func (pb *ComponentButtons) Layout(win *Window, gtx layout.Context) layout.Dimensions {
defer rtrace.StartRegion(context.Background(), "theme.ComponentButtons.Layout").End()
type button struct {
w *widget.PrimaryClickable
label string
cmd NormalCommand
}
var buttons []button
switch pb.state {
case ComponentStatePanel:
buttons = []button{
{
&pb.back,
"Back",
NormalCommand{
PrimaryLabel: "Go to previous panel",
Aliases: []string{"back"},
},
},
{
&pb.detach,
"Tabify",
NormalCommand{
PrimaryLabel: "Turn panel into tab",
},
},
}
case ComponentStateTab:
case ComponentStateWindow:
buttons = []button{
{
&pb.attach,
"Attach",
NormalCommand{
PrimaryLabel: "Attach panel",
},
},
{
&pb.close,
"Close",
NormalCommand{
PrimaryLabel: "Close panel",
},
},
}
}
var cmds CommandSlice
children := make([]layout.Widget, 0, 3)
for _, btn := range buttons {
btn := btn
children = append(children,
func(gtx layout.Context) layout.Dimensions {
return Button(win.Theme, &btn.w.Clickable, btn.label).Layout(win, gtx)
},
layout.Spacer{Width: 5}.Layout,
)
cmd := btn.cmd
cmd.Category = "Panel"
cmd.Color = color.Oklch{L: 0.7862, C: 0.104, H: 140, A: 1}
cmd.Fn = func() Action {
return ExecuteAction(func(gtx layout.Context) {
btn.w.Click()
})
}
cmds = append(cmds, cmd)
}
win.AddCommandProvider(CommandSlice(cmds))
return layout.Rigids(gtx, layout.Horizontal, children...)
}
// WidgetComponent turns any widget into a Component. You are responsible for handling component button events.
type WidgetComponent struct {
w Widget
ComponentButtons
}
func (wp *WidgetComponent) Layout(win *Window, gtx layout.Context) layout.Dimensions {
defer rtrace.StartRegion(context.Background(), "theme.WidgetComponent.Layout").End()
return layout.Rigids(gtx, layout.Horizontal, Dumb(win, wp.ComponentButtons.Layout), Dumb(win, wp.w))
}