aboutsummaryrefslogtreecommitdiffstats
path: root/app/scrollable.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/scrollable.go')
-rw-r--r--app/scrollable.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/app/scrollable.go b/app/scrollable.go
new file mode 100644
index 00000000..bca20232
--- /dev/null
+++ b/app/scrollable.go
@@ -0,0 +1,67 @@
+package app
+
+// Scrollable implements vertical scrolling
+type Scrollable struct {
+ scroll int
+ height int
+ elems int
+}
+
+func (s *Scrollable) Scroll() int {
+ return s.scroll
+}
+
+func (s *Scrollable) PercentVisible() float64 {
+ if s.elems <= 0 {
+ return 1.0
+ }
+ return float64(s.height) / float64(s.elems)
+}
+
+func (s *Scrollable) PercentScrolled() float64 {
+ if s.elems <= 0 {
+ return 1.0
+ }
+ return float64(s.scroll) / float64(s.elems)
+}
+
+func (s *Scrollable) NeedScrollbar() bool {
+ needScrollbar := true
+ if s.PercentVisible() >= 1.0 {
+ needScrollbar = false
+ }
+ return needScrollbar
+}
+
+func (s *Scrollable) UpdateScroller(height, elems int) {
+ s.height = height
+ s.elems = elems
+}
+
+func (s *Scrollable) EnsureScroll(selectingIdx int) {
+ if selectingIdx < 0 {
+ return
+ }
+
+ maxScroll := s.elems - s.height
+ if maxScroll < 0 {
+ maxScroll = 0
+ }
+
+ if selectingIdx >= s.scroll && selectingIdx < s.scroll+s.height {
+ if s.scroll > maxScroll {
+ s.scroll = maxScroll
+ }
+ return
+ }
+
+ if selectingIdx >= s.scroll+s.height {
+ s.scroll = selectingIdx - s.height + 1
+ } else if selectingIdx < s.scroll {
+ s.scroll = selectingIdx
+ }
+
+ if s.scroll > maxScroll {
+ s.scroll = maxScroll
+ }
+}