diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-16 15:27:56 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-16 17:29:17 +1000 |
commit | e68093fe9974296fe5afb22ec8e0adb1eb2f4316 (patch) | |
tree | 040dbfdb4dc438271d9f3febae410d25698f1a38 /pkg/gui/controllers/list_controller.go | |
parent | b838b74801102b27782cc20c7396279630e77fba (diff) |
add scrollbars
Diffstat (limited to 'pkg/gui/controllers/list_controller.go')
-rw-r--r-- | pkg/gui/controllers/list_controller.go | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/pkg/gui/controllers/list_controller.go b/pkg/gui/controllers/list_controller.go index 0f64228bb..7da05710e 100644 --- a/pkg/gui/controllers/list_controller.go +++ b/pkg/gui/controllers/list_controller.go @@ -43,14 +43,42 @@ func (self *ListController) HandleNextLine() error { } func (self *ListController) HandleScrollLeft() error { - return self.scroll(self.context.GetViewTrait().ScrollLeft) + return self.scrollHorizontal(self.context.GetViewTrait().ScrollLeft) } func (self *ListController) HandleScrollRight() error { - return self.scroll(self.context.GetViewTrait().ScrollRight) + return self.scrollHorizontal(self.context.GetViewTrait().ScrollRight) } -func (self *ListController) scroll(scrollFunc func()) error { +func (self *ListController) HandleScrollUp() error { + self.context.GetViewTrait().ScrollUp() + + // we only need to do a line change if our line has been pushed out of the viewport, because + // at the moment much logic depends on the selected line always being visible + if !self.isSelectedLineInViewPort() { + return self.handleLineChange(-1) + } + + return nil +} + +func (self *ListController) HandleScrollDown() error { + self.context.GetViewTrait().ScrollDown() + + if !self.isSelectedLineInViewPort() { + return self.handleLineChange(1) + } + + return nil +} + +func (self *ListController) isSelectedLineInViewPort() bool { + selectedLineIdx := self.context.GetList().GetSelectedLineIdx() + startIdx, length := self.context.GetViewTrait().ViewPortYBounds() + return selectedLineIdx >= startIdx && selectedLineIdx < startIdx+length +} + +func (self *ListController) scrollHorizontal(scrollFunc func()) error { scrollFunc() return self.context.HandleFocus() @@ -157,7 +185,7 @@ func (self *ListController) GetMouseKeybindings(opts types.KeybindingsOpts) []*g ViewName: self.context.GetViewName(), ToContext: string(self.context.GetKey()), Key: gocui.MouseWheelUp, - Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandlePrevLine() }, + Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandleScrollUp() }, }, { ViewName: self.context.GetViewName(), @@ -169,7 +197,7 @@ func (self *ListController) GetMouseKeybindings(opts types.KeybindingsOpts) []*g ViewName: self.context.GetViewName(), ToContext: string(self.context.GetKey()), Key: gocui.MouseWheelDown, - Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandleNextLine() }, + Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandleScrollDown() }, }, } } |