Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve preview loading message flickering #1154

Merged
merged 8 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ func (app *app) loop() {
app.nav.renew()
app.ui.loadFile(app, false)
app.ui.draw(app.nav)
case <-app.nav.previewTimer.C:
app.nav.previewLoading = true
app.ui.draw(app.nav)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,15 @@ type nav struct {
searchPos int
prevFilter []string
volatilePreview bool
previewTimer *time.Timer
previewLoading bool
jumpList []string
jumpListInd int
}

func (nav *nav) loadDirInternal(path string) *dir {
nav.startPreview()

d := &dir{
loading: true,
loadTime: time.Now(),
Expand Down Expand Up @@ -448,6 +452,7 @@ func (nav *nav) checkDir(dir *dir) {
return
}

nav.startPreview()
dir.loading = true
dir.loadTime = now
go func() {
Expand Down Expand Up @@ -511,6 +516,7 @@ func newNav(height int) *nav {
tags: make(map[string]string),
selectionInd: 0,
height: height,
previewTimer: time.NewTimer(0),
jumpList: make([]string, 0),
jumpListInd: -1,
}
Expand Down Expand Up @@ -813,6 +819,7 @@ func (nav *nav) loadReg(path string, volatile bool) *reg {
if !ok || (volatile && r.volatile) {
r := &reg{loading: true, loadTime: time.Now(), path: path, volatile: true}
nav.regCache[path] = r
nav.startPreview()
nav.previewChan <- path
return r
}
Expand All @@ -838,10 +845,17 @@ func (nav *nav) checkReg(reg *reg) {

if s.ModTime().After(reg.loadTime) {
reg.loadTime = now
nav.startPreview()
nav.previewChan <- reg.path
}
}

func (nav *nav) startPreview() {
nav.previewTimer.Stop()
nav.previewLoading = false
nav.previewTimer.Reset(100 * time.Millisecond)
}

func (nav *nav) sort() {
for _, d := range nav.dirs {
name := d.name()
Expand Down
22 changes: 14 additions & 8 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,18 @@ func (win *win) printRight(screen tcell.Screen, y int, st tcell.Style, s string)
win.print(screen, win.w-printLength(s), y, st, s)
}

func (win *win) printReg(screen tcell.Screen, reg *reg) {
func (win *win) printReg(screen tcell.Screen, reg *reg, previewLoading bool) {
if reg == nil {
return
}

st := tcell.StyleDefault

if reg.loading {
st = st.Reverse(true)
win.print(screen, 2, 0, st, "loading...")
if previewLoading {
st = st.Reverse(true)
win.print(screen, 2, 0, st, "loading...")
}
return
}

Expand Down Expand Up @@ -350,7 +352,7 @@ const SelectionColor = tcell.ColorPurple
const YankColor = tcell.ColorOlive
const CutColor = tcell.ColorMaroon

func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dirStyle *dirStyle) {
func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dirStyle *dirStyle, previewLoading bool) {
if win.w < 5 || dir == nil {
return
}
Expand All @@ -362,7 +364,9 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir
return
}
if (dir.loading && len(dir.files) == 0) || (dirStyle.role == Preview && dir.loading && gOpts.dirpreviews) {
win.print(screen, 2, 0, messageStyle, "loading...")
if dirStyle.role != Preview || previewLoading {
win.print(screen, 2, 0, messageStyle, "loading...")
}
return
}

Expand Down Expand Up @@ -930,7 +934,8 @@ func (ui *ui) draw(nav *nav) {
}
if dir := ui.dirOfWin(nav, i); dir != nil {
ui.wins[i].printDir(ui.screen, dir, &context,
&dirStyle{colors: ui.styles, icons: ui.icons, role: role})
&dirStyle{colors: ui.styles, icons: ui.icons, role: role},
nav.previewLoading)
}
}

Expand Down Expand Up @@ -966,9 +971,10 @@ func (ui *ui) draw(nav *nav) {

if curr.IsDir() {
preview.printDir(ui.screen, ui.dirPrev, &context,
&dirStyle{colors: ui.styles, icons: ui.icons, role: Preview})
&dirStyle{colors: ui.styles, icons: ui.icons, role: Preview},
nav.previewLoading)
} else if curr.Mode().IsRegular() {
preview.printReg(ui.screen, ui.regPrev)
preview.printReg(ui.screen, ui.regPrev, nav.previewLoading)
}
}
}
Expand Down