forked from mdouchement/standardfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
192 lines (167 loc) · 5.41 KB
/
item.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
package tui
import (
"time"
"github.com/bep/debounce"
"github.com/gcla/gowid"
"github.com/gcla/gowid/gwutil"
"github.com/gcla/gowid/widgets/columns"
"github.com/gcla/gowid/widgets/edit"
"github.com/gcla/gowid/widgets/selectable"
"github.com/gcla/gowid/widgets/styled"
"github.com/gcla/gowid/widgets/text"
"github.com/gcla/gowid/widgets/vscroll"
"github.com/gdamore/tcell"
"github.com/cherya/standardfile/pkg/libsf"
)
// An Item is the graphical representation of an libsf.Item.
type Item struct {
ID string
presentation gowid.IWidget
abstraction *libsf.Item
editorPresentation *ItemEditor
}
// NewItem returns a new Item.
func NewItem(item *libsf.Item, sync func(item *libsf.Item) *time.Time) *Item {
editor := edit.New(edit.Options{Text: item.Note.Text})
debounced := debounce.New(500 * time.Millisecond)
editor.OnTextSet(gowid.WidgetCallback{Name: "cb", WidgetChangedFunction: func(app gowid.IApp, iw gowid.IWidget) {
debounced(func() {
item.Note.Text = editor.Text()
item.UpdatedAt = sync(item)
})
}})
return &Item{
ID: item.ID,
presentation: selectable.New(
styled.NewExt(
text.New(item.Note.Title),
gowid.MakePaletteRef("normal"), gowid.MakePaletteRef("focused"),
),
),
editorPresentation: newItemEditor(editor),
abstraction: item,
}
}
// Title returns the name of the editor.
func (w *Item) Title() string {
return w.abstraction.Note.Title
}
// Editor returns the ItemContent of the Item.
func (w *Item) Editor() *ItemEditor {
return w.editorPresentation
}
////////////////////
// //
// Delegates //
// //
////////////////////
// Render implements gowid.IWidget
func (w *Item) 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 *Item) 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 *Item) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
return w.presentation.UserInput(ev, size, focus, app)
}
// Selectable implements gowid.IWidget
func (w *Item) Selectable() bool {
return w.presentation.Selectable()
}
//
//
//
//
//
//
//
//
//
//
//
//
// An ItemEditor is the graphical representation of editable libsf.Item text.
type ItemEditor struct {
*columns.Widget
e *edit.Widget
sb *vscroll.Widget
goUpDown int // positive means down
pgUpDown int // positive means down
}
func newItemEditor(e *edit.Widget) *ItemEditor {
sb := vscroll.NewExt(vscroll.VerticalScrollbarUnicodeRunes)
ie := &ItemEditor{
Widget: columns.New([]gowid.IContainerWidget{
&gowid.ContainerWidget{IWidget: e, D: gowid.RenderWithWeight{W: 1}},
&gowid.ContainerWidget{IWidget: sb, D: gowid.RenderWithUnits{U: 1}},
}),
e: e,
sb: sb,
goUpDown: 0,
pgUpDown: 0,
}
sb.OnClickAbove(gowid.WidgetCallback{Name: "cb", WidgetChangedFunction: ie.clickUp})
sb.OnClickBelow(gowid.WidgetCallback{Name: "cb", WidgetChangedFunction: ie.clickDown})
sb.OnClickUpArrow(gowid.WidgetCallback{Name: "cb", WidgetChangedFunction: ie.clickUpArrow})
sb.OnClickDownArrow(gowid.WidgetCallback{Name: "cb", WidgetChangedFunction: ie.clickDownArrow})
return ie
}
func (w *ItemEditor) clickUp(app gowid.IApp, iw gowid.IWidget) {
w.pgUpDown--
}
func (w *ItemEditor) clickDown(app gowid.IApp, iw gowid.IWidget) {
w.pgUpDown++
}
func (w *ItemEditor) clickUpArrow(app gowid.IApp, iw gowid.IWidget) {
w.goUpDown--
}
func (w *ItemEditor) clickDownArrow(app gowid.IApp, iw gowid.IWidget) {
w.goUpDown++
}
// UserInput implements gowid.IWidget
func (w *ItemEditor) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
box, _ := size.(gowid.IRenderBox)
w.sb.Top, w.sb.Middle, w.sb.Bottom = w.e.CalculateTopMiddleBottom(gowid.MakeRenderBox(box.BoxColumns()-1, box.BoxRows()))
// Remap events
if k, ok := ev.(*tcell.EventKey); ok {
switch k.Key() {
case tcell.KeyHome:
ev = tcell.NewEventKey(tcell.KeyCtrlA, ' ', tcell.ModNone) // Start of line defined by edit widget
case tcell.KeyEnd:
ev = tcell.NewEventKey(tcell.KeyCtrlE, ' ', tcell.ModNone) // End of line defined by edit widget
}
}
handled := w.Widget.UserInput(ev, size, focus, app)
if handled {
w.Widget.SetFocus(app, 0)
}
return handled
}
// Render implements gowid.IWidget
func (w *ItemEditor) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
box, _ := size.(gowid.IRenderBox)
ecols := box.BoxColumns() - 1
ebox := gowid.MakeRenderBox(ecols, box.BoxRows())
if w.goUpDown != 0 || w.pgUpDown != 0 {
w.e.SetLinesFromTop(gwutil.Max(0, w.e.LinesFromTop()+w.goUpDown+(w.pgUpDown*box.BoxRows())), app)
txt := w.e.MakeText()
layout := text.MakeTextLayout(txt.Content(), ecols, txt.Wrap(), gowid.HAlignLeft{})
_, y := text.GetCoordsFromCursorPos(w.e.CursorPos(), ecols, layout, w.e)
if y < w.e.LinesFromTop() {
for i := y; i < w.e.LinesFromTop(); i++ {
w.e.DownLines(ebox, false, app)
}
} else if y >= w.e.LinesFromTop()+box.BoxRows() {
for i := w.e.LinesFromTop() + box.BoxRows(); i <= y; i++ {
w.e.UpLines(ebox, false, app)
}
}
}
w.goUpDown = 0
w.pgUpDown = 0
w.sb.Top, w.sb.Middle, w.sb.Bottom = w.e.CalculateTopMiddleBottom(ebox)
return w.Widget.Render(size, focus, app)
}