/
note_list.go
202 lines (171 loc) · 4.88 KB
/
note_list.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
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
package tui
import (
"fmt"
"sort"
"github.com/gcla/gowid"
"github.com/gcla/gowid/widgets/list"
"github.com/gdamore/tcell/v2"
)
// A NoteList is a list of Items to interract with.
// It implements gowid.IWidget by delegating to its presentation.
type NoteList struct {
ui *TUI
presentation list.IWidget
abstraction *noteListAbstraction
}
// NewNoteList returns a new NoteList.
func NewNoteList(ui *TUI) *NoteList {
abs := newNoteListAbstraction()
return &NoteList{
ui: ui,
presentation: list.New(abs),
abstraction: abs,
}
}
// Register registers an item to this list.
func (w *NoteList) Register(i *Item) {
n := w.abstraction.Add(i)
if n == 1 {
w.hackToDisplayFirstNote()
}
}
// Sort orders items by the given field.
func (w *NoteList) Sort(field string) {
if !w.abstraction.Sort(field) {
msg := "No sort as been applied"
if len(field) > 0 {
msg = fmt.Sprintf("Failed to sort on %s, fallback on the item's name", field)
}
w.ui.DisplayStatus(msg)
return
}
if w.abstraction.Length() > 0 {
w.hackToDisplayFirstNote()
}
}
// Hack to display first note content.
func (w *NoteList) hackToDisplayFirstNote() {
w.ui.editor.SetTitle(w.abstraction.ItemAt(0).Title(), w.ui.App)
w.ui.editor.SetSubWidget(w.abstraction.ItemAt(0).Editor(), w.ui.App)
}
////////////////////
// //
// Delegates //
// //
////////////////////
// Render implements gowid.IWidget
func (w *NoteList) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return w.presentation.Render(size, focus, app)
}
// RenderSize implements gowid.IWidget
func (w *NoteList) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return w.presentation.RenderSize(size, focus, app)
}
// UserInput implements gowid.IWidget
func (w *NoteList) UserInput(ev any, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
ok := w.presentation.UserInput(ev, size, focus, app)
if evm, ok := ev.(*tcell.EventMouse); !ok || evm.Buttons() != tcell.ButtonNone {
// Avoid next action on mouse hover event
if item, ok := w.abstraction.At(w.abstraction.Focus()).(*Item); ok {
// Set editor name
w.ui.editor.SetTitle(item.Title(), app)
// Display the text to edit
w.ui.editor.SetSubWidget(item.Editor(), app)
}
}
return ok
}
// Selectable implements gowid.IWidget
func (w *NoteList) Selectable() bool {
return w.presentation.Selectable()
}
////////////////////
// //
// Abstraction //
// //
////////////////////
// A noteListAbstraction is a list of Items to interract with.
// It implements list.IWalker interface.
type noteListAbstraction struct {
widgets []*Item
registred map[string]bool
focus list.ListPos
}
func newNoteListAbstraction() *noteListAbstraction {
return ¬eListAbstraction{
widgets: make([]*Item, 0),
registred: make(map[string]bool, 0),
focus: 0,
}
}
func (w *noteListAbstraction) Add(item *Item) int {
if w.registred[item.ID] {
// Won't be addressed until we want several clients to run on the same account.
// The list refreshing is done by restarting the application.
panic("TODO: update the item proprely")
}
w.widgets = append(w.widgets, item)
w.registred[item.ID] = true
return len(w.widgets)
}
func (w *noteListAbstraction) Sort(field string) bool {
switch field {
case "name":
sort.Slice(w.widgets, func(i, j int) bool {
return w.widgets[i].abstraction.Note.Title < w.widgets[j].abstraction.Note.Title
})
case "client_updated_at":
sort.Slice(w.widgets, func(i, j int) bool {
return w.widgets[i].abstraction.Note.UpdatedAt().After(w.widgets[j].abstraction.Note.UpdatedAt())
})
default:
return false
}
return true
}
func (w *noteListAbstraction) ItemAt(i int) *Item {
return w.widgets[i]
}
func (w *noteListAbstraction) First() list.IWalkerPosition {
if len(w.widgets) == 0 {
return nil
}
return list.ListPos(0)
}
func (w *noteListAbstraction) Last() list.IWalkerPosition {
if len(w.widgets) == 0 {
return nil
}
return list.ListPos(len(w.widgets) - 1)
}
func (w *noteListAbstraction) Length() int {
return len(w.widgets)
}
func (w *noteListAbstraction) At(pos list.IWalkerPosition) gowid.IWidget {
var res gowid.IWidget
ipos := int(pos.(list.ListPos))
if ipos >= 0 && ipos < w.Length() {
res = w.widgets[ipos]
}
return res
}
func (w *noteListAbstraction) Focus() list.IWalkerPosition {
return w.focus
}
func (w *noteListAbstraction) SetFocus(focus list.IWalkerPosition, app gowid.IApp) {
w.focus = focus.(list.ListPos)
}
func (w *noteListAbstraction) Next(ipos list.IWalkerPosition) list.IWalkerPosition {
pos := ipos.(list.ListPos)
if int(pos) == w.Length()-1 {
return list.ListPos(-1)
}
return pos + 1
}
func (w *noteListAbstraction) Previous(ipos list.IWalkerPosition) list.IWalkerPosition {
pos := ipos.(list.ListPos)
if pos-1 == -1 {
return list.ListPos(-1)
}
return pos - 1
}