on main help sign in
feat(ui): invalidate rendered items on focus/blur
8d7e64fAyman Bagabas
internal/ui/lazylist/list.go+21
@@ -102,6 +102,12 @@ func (l *List) getItem(idx int) renderedItem {
102 return ri
102 return ri
103}
103}
104
104
105+// invalidateItem invalidates the cached rendered content of the item at the
106+// given index.
107+func (l *List) invalidateItem(idx int) {
108+ delete(l.renderedItems, idx)
109+}
110+
105// ScrollToIndex scrolls the list to the given item index.
111// ScrollToIndex scrolls the list to the given item index.
106func (l *List) ScrollToIndex(index int) {
112func (l *List) ScrollToIndex(index int) {
107 if index < 0 {
113 if index < 0 {
@@ -314,11 +320,26 @@ func (l *List) Focus() {
314 if l.selectedIdx < 0 || l.selectedIdx > len(l.items)-1 {
320 if l.selectedIdx < 0 || l.selectedIdx > len(l.items)-1 {
315 return
321 return
316 }
322 }
323+
324+ item := l.items[l.selectedIdx]
325+ if focusable, ok := item.(Focusable); ok {
326+ focusable.Focus()
327+ l.invalidateItem(l.selectedIdx)
328+ }
317}
329}
318
330
319// Blur removes the focus state from the list.
331// Blur removes the focus state from the list.
320func (l *List) Blur() {
332func (l *List) Blur() {
321 l.focused = false
333 l.focused = false
334+ if l.selectedIdx < 0 || l.selectedIdx > len(l.items)-1 {
335+ return
336+ }
337+
338+ item := l.items[l.selectedIdx]
339+ if focusable, ok := item.(Focusable); ok {
340+ focusable.Blur()
341+ l.invalidateItem(l.selectedIdx)
342+ }
322}
343}
323
344
324// ScrollToTop scrolls the list to the top.
345// ScrollToTop scrolls the list to the top.