fixup layout origin/cursor changing

This commit is contained in:
Jesse Duffield 2019-06-16 10:24:37 +10:00
parent 55894b4a5e
commit 1bf9c47438
3 changed files with 44 additions and 18 deletions

View file

@ -267,9 +267,10 @@ func (gui *Gui) layout(g *gocui.Gui) error {
// TODO: find out if I should add the services view to this.
listViews := map[*gocui.View]listViewState{
containersView: {selectedLine: gui.State.Panels.Containers.SelectedLine, lineCount: len(gui.DockerCommand.Containers)},
containersView: {selectedLine: gui.State.Panels.Containers.SelectedLine, lineCount: len(gui.DockerCommand.DisplayContainers)},
imagesView: {selectedLine: gui.State.Panels.Images.SelectedLine, lineCount: len(gui.DockerCommand.Images)},
volumesView: {selectedLine: gui.State.Panels.Volumes.SelectedLine, lineCount: len(gui.DockerCommand.Volumes)},
servicesView: {selectedLine: gui.State.Panels.Services.SelectedLine, lineCount: len(gui.DockerCommand.Services)},
}
// menu view might not exist so we check to be safe

View file

@ -146,32 +146,49 @@ func (gui *Gui) resetOrigin(v *gocui.View) error {
}
// if the cursor down past the last item, move it to the last line
func (gui *Gui) focusPoint(cx int, cy int, lineCount int, v *gocui.View) error {
if cy < 0 || cy > lineCount {
func (gui *Gui) focusPoint(selectedX int, selectedY int, lineCount int, v *gocui.View) error {
if selectedY < 0 || selectedY > lineCount {
return nil
}
ox, oy := v.Origin()
originalOy := oy
cx, cy := v.Cursor()
originalCy := cy
_, height := v.Size()
ly := height - 1
if ly == -1 {
ly = 0
}
ly := utils.Max(height-1, 0)
// if line is above origin, move origin and set cursor to zero
// if line is below origin + height, move origin and set cursor to max
// otherwise set cursor to value - origin
if ly > lineCount {
_ = v.SetCursor(cx, cy)
_ = v.SetOrigin(ox, 0)
} else if cy < oy {
_ = v.SetCursor(cx, 0)
_ = v.SetOrigin(ox, cy)
} else if cy > oy+ly {
_ = v.SetCursor(cx, ly)
_ = v.SetOrigin(ox, cy-ly)
} else {
_ = v.SetCursor(cx, cy-oy)
gui.Log.Warn("ly: ", ly)
gui.Log.Warn("lineCount: ", lineCount)
gui.Log.Warn("selectedY: ", selectedY)
gui.Log.Warn("oy: ", oy)
gui.Log.Warn("cy: ", cy)
windowStart := oy
windowEnd := oy + ly
if selectedY < windowStart {
gui.Log.Warn("one")
oy = utils.Max(oy-(windowStart-selectedY), 0)
} else if selectedY > windowEnd {
gui.Log.Warn("two")
oy += (selectedY - windowEnd)
}
if windowEnd > lineCount-1 {
gui.Log.Warn("three")
shiftAmount := (windowEnd - (lineCount - 1))
oy = utils.Max(oy-shiftAmount, 0)
}
if originalOy != oy {
_ = v.SetOrigin(ox, oy)
}
cy = selectedY - oy
if originalCy != cy {
_ = v.SetCursor(cx, selectedY-oy)
}
return nil
}

View file

@ -95,6 +95,14 @@ func Min(x, y int) int {
return y
}
// Max returns the maximum of two integers
func Max(x, y int) int {
if x > y {
return x
}
return y
}
type Displayable interface {
GetDisplayStrings(bool) []string
}