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

Fix/scrolling contents #520

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 5 additions & 26 deletions runtime/ui/view/cursor.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
package view

import (
"errors"

"github.com/awesome-gocui/gocui"
)

// CursorDown moves the cursor down in the currently selected gocui pane, scrolling the screen as needed.
func CursorDown(g *gocui.Gui, v *gocui.View) error {
return CursorStep(g, v, 1)
func CursorDown(v *gocui.View, step uint) error {
v.MoveCursor(0, int(step))
return nil
}

// CursorUp moves the cursor up in the currently selected gocui pane, scrolling the screen as needed.
func CursorUp(g *gocui.Gui, v *gocui.View) error {
return CursorStep(g, v, -1)
}

// Moves the cursor the given step distance, setting the origin to the new cursor line
func CursorStep(g *gocui.Gui, v *gocui.View, step int) error {
cx, cy := v.Cursor()

// if there isn't a next line
line, err := v.Line(cy + step)
if err != nil {
return err
}
if len(line) == 0 {
return errors.New("unable to move the cursor, empty line")
}
if err := v.SetCursor(cx, cy+step); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+step); err != nil {
return err
}
}
func CursorUp(v *gocui.View, step uint) error {
v.MoveCursor(0, int(-step))
return nil
}
20 changes: 4 additions & 16 deletions runtime/ui/view/image_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,20 @@ func (v *ImageDetails) IsVisible() bool {

func (v *ImageDetails) PageUp() error {
_, height := v.body.Size()
if err := CursorStep(v.gui, v.body, -height); err != nil {
logrus.Debugf("Couldn't move the cursor up by %d steps", height)
}
return nil
return CursorUp(v.body, uint(height))
}

func (v *ImageDetails) PageDown() error {
_, height := v.body.Size()
if err := CursorStep(v.gui, v.body, height); err != nil {
logrus.Debugf("Couldn't move the cursor down by %d steps", height)
}
return nil
return CursorDown(v.body, uint(height))
}

func (v *ImageDetails) CursorUp() error {
if err := CursorUp(v.gui, v.body); err != nil {
logrus.Debug("Couldn't move the cursor up")
}
return nil
return CursorUp(v.body, 1)
}

func (v *ImageDetails) CursorDown() error {
if err := CursorDown(v.gui, v.body); err != nil {
logrus.Debug("Couldn't move the cursor down")
}
return nil
return CursorDown(v.body, 1)
}

// KeyHelp indicates all the possible actions a user can take while the current pane is selected (currently does nothing).
Expand Down
116 changes: 38 additions & 78 deletions runtime/ui/view/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (v *Layer) Setup(body *gocui.View, header *gocui.View) error {
v.header.Wrap = false
v.header.Frame = false

var infos = []key.BindingInfo{
infos := []key.BindingInfo{
{
ConfigKeys: []string{"keybinding.compare-layer"},
OnAction: func() error { return v.setCompareMode(viewmodel.CompareSingleLayer) },
Expand Down Expand Up @@ -141,15 +141,15 @@ func (v *Layer) Setup(body *gocui.View, header *gocui.View) error {
}
v.helpKeys = helpKeys

return v.Render()
}

// height obtains the height of the current pane (taking into account the lost space due to the header).
func (v *Layer) height() uint {
_, height := v.body.Size()
return uint(height - 1)
v.vm.Setup(0, height)
_ = v.Update()
_ = v.Render()

return nil
}


func (v *Layer) CompareMode() viewmodel.LayerCompareMode {
return v.vm.CompareMode
}
Expand All @@ -161,62 +161,48 @@ func (v *Layer) IsVisible() bool {

// PageDown moves to next page putting the cursor on top
func (v *Layer) PageDown() error {
step := int(v.height()) + 1
targetLayerIndex := v.vm.LayerIndex + step

if targetLayerIndex > len(v.vm.Layers) {
step -= targetLayerIndex - (len(v.vm.Layers) - 1)
}

if step > 0 {
// err := CursorStep(v.gui, v.body, step)
err := error(nil)
if err == nil {
return v.SetCursor(v.vm.LayerIndex + step)
if v.vm.PageDown() {
err := v.notifyLayerChangeListeners()
if err != nil {
return err
}
return v.Render()
}
return nil
}

// PageUp moves to previous page putting the cursor on top
func (v *Layer) PageUp() error {
step := int(v.height()) + 1
targetLayerIndex := v.vm.LayerIndex - step

if targetLayerIndex < 0 {
step += targetLayerIndex
}

if step > 0 {
// err := CursorStep(v.gui, v.body, -step)
err := error(nil)
if err == nil {
return v.SetCursor(v.vm.LayerIndex - step)
if v.vm.PageUp() {
err := v.notifyLayerChangeListeners()
if err != nil {
return err
}
return v.Render()
}
return nil
}

// CursorDown moves the cursor down in the layer pane (selecting a higher layer).
func (v *Layer) CursorDown() error {
if v.vm.LayerIndex < len(v.vm.Layers)-1 {
// err := CursorDown(v.gui, v.body)
err := error(nil)
if err == nil {
return v.SetCursor(v.vm.LayerIndex + 1)
if v.vm.CursorDown() {
err := v.notifyLayerChangeListeners()
if err != nil {
return err
}
return v.Render()
}
return nil
}

// CursorUp moves the cursor up in the layer pane (selecting a lower layer).
func (v *Layer) CursorUp() error {
if v.vm.LayerIndex > 0 {
// err := CursorUp(v.gui, v.body)
err := error(nil)
if err == nil {
return v.SetCursor(v.vm.LayerIndex - 1)
if v.vm.CursorUp() {
err := v.notifyLayerChangeListeners()
if err != nil {
return err
}
return v.Render()
}
return nil
}
Expand All @@ -243,21 +229,6 @@ func (v *Layer) setCompareMode(compareMode viewmodel.LayerCompareMode) error {
return v.notifyLayerChangeListeners()
}

// renderCompareBar returns the formatted string for the given layer.
func (v *Layer) renderCompareBar(layerIdx int) string {
bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop := v.vm.GetCompareIndexes()
result := " "

if layerIdx >= bottomTreeStart && layerIdx <= bottomTreeStop {
result = format.CompareBottom(" ")
}
if layerIdx >= topTreeStart && layerIdx <= topTreeStop {
result = format.CompareTop(" ")
}

return result
}

func (v *Layer) ConstrainLayout() {
if !v.constrainedRealEstate {
logrus.Debugf("constraining layer layout")
Expand Down Expand Up @@ -293,7 +264,7 @@ func (v *Layer) Render() error {
logrus.Tracef("view.Render() %s", v.Name())

// indicate when selected
title := "Layers"
title := fmt.Sprintf("Layers (%d / %d)", v.vm.LayerIndex+1, len(v.vm.Layers))
isSelected := v.gui.CurrentView() == v.body

v.gui.Update(func(g *gocui.Gui) error {
Expand All @@ -319,28 +290,17 @@ func (v *Layer) Render() error {

// update contents
v.body.Clear()
for idx, layer := range v.vm.Layers {
var layerStr string
if v.constrainedRealEstate {
layerStr = fmt.Sprintf("%-4d", layer.Index)
} else {
layerStr = layer.String()
}

compareBar := v.renderCompareBar(idx)

if idx == v.vm.LayerIndex {
_, err = fmt.Fprintln(v.body, compareBar+" "+format.Selected(layerStr))
} else {
_, err = fmt.Fprintln(v.body, compareBar+" "+layerStr)
}

if err != nil {
logrus.Debug("unable to write to buffer: ", err)
return err
}
err = v.vm.Update(v.constrainedRealEstate)
if err != nil {
return err
}
return nil
err = v.vm.Render()
if err != nil {
return err
}
_, err = fmt.Fprint(v.body, v.vm.Buffer.String())

return err
})
return nil
}
Expand Down
10 changes: 2 additions & 8 deletions runtime/ui/view/layer_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,12 @@ func (v *LayerDetails) IsVisible() bool {

// CursorUp moves the cursor up in the details pane
func (v *LayerDetails) CursorUp() error {
if err := CursorUp(v.gui, v.body); err != nil {
logrus.Debug("Couldn't move the cursor up")
}
return nil
return CursorUp(v.body, 1)
}

// CursorDown moves the cursor up in the details pane
func (v *LayerDetails) CursorDown() error {
if err := CursorDown(v.gui, v.body); err != nil {
logrus.Debug("Couldn't move the cursor down")
}
return nil
return CursorDown(v.body, 1)
}

// KeyHelp indicates all the possible actions a user can take while the current pane is selected (currently does nothing).
Expand Down
Loading