Repository navigation
Expand file tree
/
Copy pathnavigation.go
More file actions
168 lines (147 loc) · 4.01 KB
/
Copy pathnavigation.go
File metadata and controls
168 lines (147 loc) · 4.01 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
package main
import (
"strings"
"gioui.org/layout"
"loov.dev/lensm/internal/gui"
)
const navigationHistoryLimit = 256
// NavigationHistory tracks function visits independently from open tabs.
type NavigationHistory struct {
entries []string
index int
// preview marks the current entry as a preview visit, which the next
// preview visit replaces.
preview bool
}
func (h *NavigationHistory) Reset() {
h.entries = nil
h.index = -1
h.preview = false
}
// Visit records a visit to name.
func (h *NavigationHistory) Visit(name string) { h.visit(name, false) }
// VisitPreview records a preview visit. Consecutive previews, such as
// arrowing through the function list, share one entry, so Back skips
// over everything passed on the way.
func (h *NavigationHistory) VisitPreview(name string) { h.visit(name, true) }
func (h *NavigationHistory) visit(name string, preview bool) {
if name == "" {
return
}
if h.index >= 0 && h.index < len(h.entries) && h.entries[h.index] == name {
h.preview = h.preview && preview
return
}
if preview && h.preview && h.index == len(h.entries)-1 {
h.entries[h.index] = name
return
}
h.preview = preview
if h.index+1 < len(h.entries) {
h.entries = h.entries[:h.index+1]
}
h.entries = append(h.entries, name)
if len(h.entries) > navigationHistoryLimit {
drop := len(h.entries) - navigationHistoryLimit
h.entries = h.entries[drop:]
}
h.index = len(h.entries) - 1
}
func (h *NavigationHistory) CanBack() bool {
return h.index > 0
}
func (h *NavigationHistory) CanForward() bool {
return h.index >= 0 && h.index+1 < len(h.entries)
}
func (h *NavigationHistory) Back() (string, bool) {
if !h.CanBack() {
return "", false
}
h.index--
return h.entries[h.index], true
}
func (h *NavigationHistory) Forward() (string, bool) {
if !h.CanForward() {
return "", false
}
h.index++
return h.entries[h.index], true
}
// Entries returns the visits, oldest first, and the index of the
// current one.
func (h *NavigationHistory) Entries() ([]string, int) {
return h.entries, h.index
}
// Go moves to entry i, as Back or Forward repeated would.
func (h *NavigationHistory) Go(i int) (string, bool) {
if i < 0 || i >= len(h.entries) {
return "", false
}
h.index = i
return h.entries[i], true
}
func (h *NavigationHistory) Current() string {
if h.index < 0 || h.index >= len(h.entries) {
return ""
}
return h.entries[h.index]
}
func (ui *FileUI) recordNavigation(name string, preview bool) {
if ui.navigatingHistory {
return
}
if preview {
ui.Navigation.VisitPreview(name)
} else {
ui.Navigation.Visit(name)
}
}
func (ui *FileUI) navigateBack() {
for ui.Navigation.CanBack() {
name, _ := ui.Navigation.Back()
if ui.navigateHistoryEntry(name) {
return
}
}
}
func (ui *FileUI) navigateForward() {
for ui.Navigation.CanForward() {
name, _ := ui.Navigation.Forward()
if ui.navigateHistoryEntry(name) {
return
}
}
}
func (ui *FileUI) navigateHistoryEntry(name string) bool {
fn := ui.findFunc(name)
if fn == nil {
return false
}
// previewTab activates an open tab as it is, and shows a closed one
// in the preview slot, so going back doesn't pin or add tabs.
ui.navigatingHistory = true
ui.previewTab(fn)
ui.navigatingHistory = false
ui.copyStatus = ""
ui.invalidateMain()
return true
}
func (ui *FileUI) layoutHistoryPanel(gtx layout.Context) layout.Dimensions {
entries, current := ui.Navigation.Entries()
// Newest first.
at := func(i int) int { return len(entries) - 1 - i }
if i, ok := ui.HistoryList.Clicked(gtx); ok && i < len(entries) {
if name, ok := ui.Navigation.Go(at(i)); ok {
ui.navigateHistoryEntry(name)
}
}
return gui.PanelFrame{Theme: ui.Theme, Title: "HISTORY"}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
if len(entries) == 0 {
return gui.PanelNote(ui.Theme, "Functions you visit show up here.")(gtx)
}
return ui.HistoryList.Layout(gtx, ui.Theme, len(entries), at(current), func(i int) (string, string) {
pkg, name := gui.SplitPackage(entries[at(i)])
return name, strings.TrimSuffix(pkg, ".")
})
})
}