Skip to content

Commit

Permalink
Stop scrolling when last column is visible (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras authored Jun 5, 2022
1 parent b72d983 commit 6410a6a
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 172 deletions.
2 changes: 2 additions & 0 deletions table/dimensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (m *Model) recalculateWidth() {
}

updateColumnWidths(m.columns, m.targetTotalWidth)

m.recalculateLastHorizontalColumn()
}

// Updates column width in-place. This could be optimized but should be called
Expand Down
3 changes: 3 additions & 0 deletions table/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Model struct {

// How many columns to freeze when scrolling horizontally
horizontalScrollFreezeColumnsCount int

// Calculated maximum column we can scroll to before the last is displayed
maxHorizontalColumnIndex int
}

// New creates a new table ready for further modifications.
Expand Down
4 changes: 4 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ func (m Model) WithFooterVisibility(visibility bool) Model {
func (m Model) WithMaxTotalWidth(maxTotalWidth int) Model {
m.maxTotalWidth = maxTotalWidth

m.recalculateWidth()

return m
}

Expand All @@ -284,6 +286,8 @@ func (m Model) WithMaxTotalWidth(maxTotalWidth int) Model {
func (m Model) WithHorizontalFreezeColumnCount(columnsToFreeze int) Model {
m.horizontalScrollFreezeColumnsCount = columnsToFreeze

m.recalculateWidth()

return m
}

Expand Down
46 changes: 44 additions & 2 deletions table/scrolling.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package table

func (m *Model) scrollRight() {
maxCol := len(m.columns) - 1 - m.horizontalScrollFreezeColumnsCount
if m.horizontalScrollOffsetCol < maxCol {
if m.horizontalScrollOffsetCol < m.maxHorizontalColumnIndex {
m.horizontalScrollOffsetCol++
}
}
Expand All @@ -12,3 +11,46 @@ func (m *Model) scrollLeft() {
m.horizontalScrollOffsetCol--
}
}

func (m *Model) recalculateLastHorizontalColumn() {
if m.horizontalScrollFreezeColumnsCount >= len(m.columns) {
m.maxHorizontalColumnIndex = 0

return
}

if m.totalWidth <= m.maxTotalWidth {
m.maxHorizontalColumnIndex = 0

return
}

const (
leftOverflowWidth = 2
borderAdjustment = 1
)

// Always have left border
visibleWidth := borderAdjustment + leftOverflowWidth

for i := 0; i < m.horizontalScrollFreezeColumnsCount; i++ {
visibleWidth += m.columns[i].width + borderAdjustment
}

m.maxHorizontalColumnIndex = len(m.columns) - 1

// Work backwards from the right
for i := len(m.columns) - 1; i >= m.horizontalScrollFreezeColumnsCount && visibleWidth <= m.maxTotalWidth; i-- {
visibleWidth += m.columns[i].width + borderAdjustment

if visibleWidth <= m.maxTotalWidth {
m.maxHorizontalColumnIndex = i - m.horizontalScrollFreezeColumnsCount
}
}

if m.maxHorizontalColumnIndex <= m.horizontalScrollFreezeColumnsCount {
m.maxHorizontalColumnIndex = 0

return
}
}
Loading

0 comments on commit 6410a6a

Please sign in to comment.