-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitlist.go
More file actions
215 lines (195 loc) · 6.16 KB
/
commitlist.go
File metadata and controls
215 lines (195 loc) · 6.16 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"image"
"image/color"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
// CommitList displays a searchable list of commits.
type CommitList struct {
All []Commit
Filtered []Commit
Search widget.Editor
List SelectList
lastQuery string
}
// NewCommitList creates a new CommitList.
func NewCommitList(th *Theme) CommitList {
cl := CommitList{}
cl.Search.SingleLine = true
cl.List = NewVerticalSelectList(unit.Dp(th.TextSize) + 6)
return cl
}
// SetCommits updates the full commit list.
func (cl *CommitList) SetCommits(commits []Commit) {
cl.All = commits
cl.Filtered = commits
if len(commits) > 0 && cl.List.Selected < 0 {
cl.List.Selected = 0
}
}
// Selected returns the currently selected commit, or nil.
func (cl *CommitList) Selected() *Commit {
if cl.List.Selected >= 0 && cl.List.Selected < len(cl.Filtered) {
return &cl.Filtered[cl.List.Selected]
}
return nil
}
// Layout draws the commit list with search.
func (cl *CommitList) Layout(th *Theme, gtx layout.Context) (selectionChanged bool) {
prevSelected := cl.List.Selected
// Process search editor events.
changed := false
for {
ev, ok := cl.Search.Update(gtx)
if !ok {
break
}
if _, ok := ev.(widget.ChangeEvent); ok {
changed = true
}
}
if changed {
cl.lastQuery = cl.Search.Text()
gtx.Execute(op.InvalidateCmd{})
}
layout.Flex{
Axis: layout.Vertical,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return FocusBorder(th, gtx.Focused(&cl.Search)).Layout(gtx,
material.Editor(th.Theme, &cl.Search, "Search commits...").Layout)
}),
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
return cl.List.Layout(th, gtx, len(cl.Filtered), cl.commitItem(th))
}),
)
return cl.List.Selected != prevSelected
}
// UpdateFilter should be called when search results are available.
func (cl *CommitList) UpdateFilter(results []Commit) {
cl.Filtered = results
if cl.List.Selected >= len(cl.Filtered) {
cl.List.Selected = len(cl.Filtered) - 1
}
if cl.List.Selected < 0 && len(cl.Filtered) > 0 {
cl.List.Selected = 0
}
}
// Query returns the current search query.
func (cl *CommitList) Query() string {
return cl.lastQuery
}
func commitLabel(th *Theme, style ListItemStyle, text string) material.LabelStyle {
label := material.Body1(th.Theme, text)
label.Color = style.Foreground
label.MaxLines = 1
label.TextSize = th.TextSize * 8 / 10
return label
}
func (cl *CommitList) commitItem(th *Theme) layout.ListElement {
return func(gtx layout.Context, index int) layout.Dimensions {
commit := &cl.Filtered[index]
height := gtx.Constraints.Min.Y
dims := cl.List.LayoutItem(th, gtx, index, func(gtx layout.Context, style ListItemStyle) layout.Dimensions {
return layout.Flex{
Axis: layout.Horizontal,
Alignment: layout.Baseline,
}.Layout(gtx,
// Short hash.
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return FixedWidth(gtx, gtx.Metric.Sp(th.TextSize*6), func(gtx layout.Context) layout.Dimensions {
label := commitLabel(th, style, commit.ShortHash())
label.Font = font.Font{Typeface: th.Face}
return label.Layout(gtx)
})
}),
// Subject (fills remaining space, pushing right-aligned items).
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
label := commitLabel(th, style, commit.Subject)
label.Font.Weight = style.Weight
return label.Layout(gtx)
}),
// Decorations.
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if len(commit.Decorations) == 0 {
return layout.Dimensions{}
}
return layout.Inset{Left: 4}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layoutDecorations(th, gtx, commit.Decorations)
})
}),
// Author (right-aligned).
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: 8}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return FixedWidth(gtx, gtx.Metric.Sp(th.TextSize*10), func(gtx layout.Context) layout.Dimensions {
return commitLabel(th, style, commit.AuthorName).Layout(gtx)
})
})
}),
// Date (right-aligned).
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: 4}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return FixedWidth(gtx, gtx.Metric.Sp(th.TextSize*10), func(gtx layout.Context) layout.Dimensions {
return commitLabel(th, style, commit.RelativeDate()).Layout(gtx)
})
})
}),
)
})
dims.Size.Y = height
return dims
}
}
func layoutDecorations(th *Theme, gtx layout.Context, decs []Decoration) layout.Dimensions {
var children []layout.FlexChild
for i := range decs {
dec := &decs[i]
children = append(children,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: 2, Top: 1, Bottom: 1}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layoutBadge(th, gtx, dec)
})
}),
)
}
return layout.Flex{
Axis: layout.Horizontal,
Alignment: layout.Baseline,
}.Layout(gtx, children...)
}
func layoutBadge(th *Theme, gtx layout.Context, dec *Decoration) layout.Dimensions {
var fg, bg color.NRGBA
switch dec.Kind {
case DecorationBranch:
fg, bg = th.Colors.BranchColor, th.Colors.BranchBg
case DecorationTag:
fg, bg = th.Colors.TagColor, th.Colors.TagBg
case DecorationHead:
fg, bg = th.Colors.HeadColor, th.Colors.HeadBg
}
return layout.Background{}.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
bounds := clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, gtx.Metric.Dp(3))
paint.FillShape(gtx.Ops, bg, bounds.Op(gtx.Ops))
return layout.Dimensions{Size: gtx.Constraints.Min}
},
func(gtx layout.Context) layout.Dimensions {
inset := layout.Inset{Top: 1, Bottom: 1, Left: 3, Right: 3}
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min.Y = 0
label := material.Body1(th.Theme, dec.Name)
label.Color = fg
label.MaxLines = 1
label.TextSize = th.TextSize * 7 / 10
return label.Layout(gtx)
})
},
)
}