github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/grouplist.go (about)

     1  package framework
     2  
     3  type GroupList struct {
     4  	w   *Window
     5  	num int
     6  
     7  	idx               int
     8  	scrollbary        int
     9  	done              bool
    10  	skippedLineHeight int
    11  }
    12  
    13  // GroupListStart starts a scrollable list of <num> rows of <height> height
    14  func GroupListStart(w *Window, num int, name string, flags WindowFlags) (GroupList, *Window) {
    15  	var gl GroupList
    16  	gl.w = w.GroupBegin(name, flags)
    17  	gl.num = num
    18  	gl.idx = -1
    19  	if gl.w != nil {
    20  		gl.scrollbary = gl.w.Scrollbar.Y
    21  	}
    22  
    23  	return gl, gl.w
    24  }
    25  
    26  func (gl *GroupList) Next() bool {
    27  	if gl.w == nil {
    28  		return false
    29  	}
    30  	if gl.skippedLineHeight > 0 && gl.idx >= 0 {
    31  		if _, below := gl.w.Invisible(); below {
    32  			n := gl.num - gl.idx
    33  			gl.idx = gl.num
    34  			gl.empty(n)
    35  		}
    36  	}
    37  	gl.idx++
    38  	if gl.idx >= gl.num {
    39  		if !gl.done {
    40  			gl.done = true
    41  			if gl.scrollbary != gl.w.Scrollbar.Y {
    42  				gl.w.Scrollbar.Y = gl.scrollbary
    43  				gl.w.Master().Changed()
    44  			}
    45  			gl.w.GroupEnd()
    46  		}
    47  		return false
    48  	}
    49  	return true
    50  }
    51  
    52  func (gl *GroupList) SkipToVisible(lineheight int) {
    53  	if gl.w == nil {
    54  		return
    55  	}
    56  	gl.SkipToVisibleScaled(gl.w.ctx.scale(lineheight))
    57  }
    58  
    59  func (gl *GroupList) SkipToVisibleScaled(lineheight int) {
    60  	if gl.w == nil {
    61  		return
    62  	}
    63  	skip := gl.w.Scrollbar.Y/(lineheight+gl.w.style().Spacing.Y) - 2
    64  	if maxskip := gl.num - 3; skip > maxskip {
    65  		skip = maxskip
    66  	}
    67  	if skip < 0 {
    68  		skip = 0
    69  	}
    70  	gl.skippedLineHeight = lineheight
    71  	gl.empty(skip)
    72  	gl.idx = skip - 1
    73  }
    74  
    75  func (gl *GroupList) empty(n int) {
    76  	if n <= 0 {
    77  		return
    78  	}
    79  	gl.w.RowScaled(n*gl.skippedLineHeight + (n-1)*gl.w.style().Spacing.Y).Dynamic(1)
    80  	gl.w.Label("More...", "LC")
    81  }
    82  
    83  func (gl *GroupList) Index() int {
    84  	return gl.idx
    85  }
    86  
    87  func (gl *GroupList) Center() {
    88  	if above, below := gl.w.Invisible(); above || below {
    89  		gl.scrollbary = gl.w.At().Y - gl.w.Bounds.H/2
    90  		if gl.scrollbary < 0 {
    91  			gl.scrollbary = 0
    92  		}
    93  	}
    94  }