Skip to content

Commit

Permalink
feat(reader): Remember focused feed node when defocusing
Browse files Browse the repository at this point in the history
  • Loading branch information
bow committed Feb 2, 2024
1 parent 1d8a648 commit 2e17223
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
2 changes: 0 additions & 2 deletions internal/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ func (r *Reader) feedsPaneKeyHandler() ui.KeyHandler {
ctxs, cancels := r.callCtx()
defer cancels()
r.opr.RefreshStats(r.display, r.backend.GetStatsF(ctxs))

r.display.Draw()
}()
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions internal/reader/ui/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Display struct {
handlersSet bool

focusStack tview.Primitive
counter int
}

func NewDisplay(screen tcell.Screen, theme string) (*Display, error) {
Expand Down Expand Up @@ -428,6 +429,7 @@ func (d *Display) focusPane(pane tview.Primitive) {
}

func (d *Display) focusAdjacentPane(reverse bool) {
d.counter++
if front := d.frontPageName(); front != mainPageName {
d.hidePopup(front)
}
Expand Down
40 changes: 31 additions & 9 deletions internal/reader/ui/feeds_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
type feedsPane struct {
tview.TreeView

theme *Theme
lang *Lang

store *feedStore
theme *Theme
lang *Lang
store *feedStore
focusStack *entity.ID
}

// nolint:revive
func newFeedsPane(theme *Theme, lang *Lang) *feedsPane {

fp := feedsPane{
Expand All @@ -37,20 +38,39 @@ func newFeedsPane(theme *Theme, lang *Lang) *feedsPane {
fp.SetFocusFunc(
func() {
fp.SetDrawFunc(focusf)

// TODO: Simplify by adding select/deselect based on feed IDs.
var previous, fallback *tview.TreeNode
if root := fp.GetRoot(); root != nil {
if gnodes := root.GetChildren(); len(gnodes) > 0 {
if fnodes := gnodes[0].GetChildren(); len(fnodes) > 0 {
// TODO: Store last focused item and restore instead of always
// focusing on first item.
fp.TreeView.SetCurrentNode(fnodes[0])
for i, gnode := range root.GetChildren() {
for j, fnode := range gnode.GetChildren() {
feed := fnode.GetReference().(*entity.Feed)
if i == 0 && j == 0 {
fallback = fnode
}
if top := fp.focusStack; top != nil && *top == feed.ID {
previous = fnode
break
}
}
}
}
if previous != nil {
fp.TreeView.SetCurrentNode(previous)
} else if fallback != nil {
fp.TreeView.SetCurrentNode(fallback)
}
},
)
fp.SetBlurFunc(
func() {
fp.SetDrawFunc(unfocusf)
if fnode := fp.TreeView.GetCurrentNode(); fnode != nil {
feed := fnode.GetReference().(*entity.Feed)
if feed != nil {
fp.focusStack = &feed.ID
}
}
fp.TreeView.SetCurrentNode(nil)
},
)
Expand All @@ -62,6 +82,8 @@ func newFeedsPane(theme *Theme, lang *Lang) *feedsPane {
func (fp *feedsPane) startFeedsPoll(ch <-chan *entity.Feed) {
root := fp.GetRoot()

// FIXME: This loop resets selected feed node. Update it so that
// we maintain focus.
for incoming := range ch {
fp.store.upsert(incoming)

Expand Down

0 comments on commit 2e17223

Please sign in to comment.