add tapped handler for changing column sort
This commit is contained in:
@@ -15,6 +15,14 @@ import (
|
|||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ColumnSort int
|
||||||
|
|
||||||
|
const (
|
||||||
|
SortNone ColumnSort = iota
|
||||||
|
SortAscending
|
||||||
|
SortDescending
|
||||||
|
)
|
||||||
|
|
||||||
type ListColumn struct {
|
type ListColumn struct {
|
||||||
Text string
|
Text string
|
||||||
AlignTrailing bool
|
AlignTrailing bool
|
||||||
@@ -24,6 +32,7 @@ type ListColumn struct {
|
|||||||
type ListHeader struct {
|
type ListHeader struct {
|
||||||
widget.BaseWidget
|
widget.BaseWidget
|
||||||
|
|
||||||
|
OnColumnSortChanged func(int, ColumnSort)
|
||||||
OnColumnVisibilityChanged func(int, bool)
|
OnColumnVisibilityChanged func(int, bool)
|
||||||
OnColumnVisibilityMenuShown func(*widget.PopUp)
|
OnColumnVisibilityMenuShown func(*widget.PopUp)
|
||||||
|
|
||||||
@@ -67,9 +76,12 @@ func (l *ListHeader) SetColumnVisible(colNum int, visible bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *ListHeader) buildColumns() {
|
func (l *ListHeader) buildColumns() {
|
||||||
for _, c := range l.columns {
|
for i, c := range l.columns {
|
||||||
hdr := newColHeader(c)
|
hdr := newColHeader(c)
|
||||||
hdr.SortVisible = true
|
hdr.OnSortChanged = func(i int) func(ColumnSort) {
|
||||||
|
return func(sort ColumnSort) { l.onSortChanged(i, sort) }
|
||||||
|
}(i)
|
||||||
|
hdr.OnTappedSecondary = l.TappedSecondary
|
||||||
l.columnsContainer.Add(
|
l.columnsContainer.Add(
|
||||||
// hdr,
|
// hdr,
|
||||||
// TODO: remove debugging background
|
// TODO: remove debugging background
|
||||||
@@ -81,6 +93,20 @@ func (l *ListHeader) buildColumns() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *ListHeader) onSortChanged(colNum int, sort ColumnSort) {
|
||||||
|
for i, c := range l.columnsContainer.Objects {
|
||||||
|
if i != colNum {
|
||||||
|
c.(*fyne.Container).Objects[1].(*colHeader).Sort = SortNone
|
||||||
|
// TODO
|
||||||
|
//c.(*colHeader).Sort = sortNone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.Refresh()
|
||||||
|
if l.OnColumnSortChanged != nil {
|
||||||
|
l.OnColumnSortChanged(colNum, sort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (l *ListHeader) CreateRenderer() fyne.WidgetRenderer {
|
func (l *ListHeader) CreateRenderer() fyne.WidgetRenderer {
|
||||||
return widget.NewSimpleRenderer(l.container)
|
return widget.NewSimpleRenderer(l.container)
|
||||||
}
|
}
|
||||||
@@ -128,8 +154,9 @@ func (l *ListHeader) createOnChangedCallbk(colNum int) func(bool) {
|
|||||||
type colHeader struct {
|
type colHeader struct {
|
||||||
widget.BaseWidget
|
widget.BaseWidget
|
||||||
|
|
||||||
SortDescending bool
|
Sort ColumnSort
|
||||||
SortVisible bool
|
OnSortChanged func(ColumnSort)
|
||||||
|
OnTappedSecondary func(*fyne.PointEvent)
|
||||||
|
|
||||||
columnCfg ListColumn
|
columnCfg ListColumn
|
||||||
|
|
||||||
@@ -158,17 +185,40 @@ func newColHeader(columnCfg ListColumn) *colHeader {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colHeader) Tapped(*fyne.PointEvent) {
|
||||||
|
switch c.Sort {
|
||||||
|
case SortNone:
|
||||||
|
c.Sort = SortAscending
|
||||||
|
case SortAscending:
|
||||||
|
c.Sort = SortDescending
|
||||||
|
case SortDescending:
|
||||||
|
c.Sort = SortNone
|
||||||
|
default:
|
||||||
|
log.Println("notReached colHeader.Tapped")
|
||||||
|
}
|
||||||
|
c.Refresh()
|
||||||
|
if c.OnSortChanged != nil {
|
||||||
|
c.OnSortChanged(c.Sort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *colHeader) TappedSecondary(e *fyne.PointEvent) {
|
||||||
|
if c.OnTappedSecondary != nil {
|
||||||
|
c.OnTappedSecondary(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colHeader) Refresh() {
|
func (c *colHeader) Refresh() {
|
||||||
if c.SortDescending {
|
if c.Sort == SortDescending {
|
||||||
c.sortIcon.Resource = theme.MenuDropDownIcon()
|
c.sortIcon.Resource = theme.MenuDropDownIcon()
|
||||||
} else {
|
} else {
|
||||||
c.sortIcon.Resource = theme.MenuDropUpIcon()
|
c.sortIcon.Resource = theme.MenuDropUpIcon()
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.SortVisible && c.sortIcon.Hidden {
|
if c.Sort > 0 && c.sortIcon.Hidden {
|
||||||
c.sortIcon.Show()
|
c.sortIcon.Show()
|
||||||
c.container.Add(c.sortIconNegSpacer)
|
c.container.Add(c.sortIconNegSpacer)
|
||||||
} else if !c.sortIcon.Hidden {
|
} else if c.Sort == SortNone && !c.sortIcon.Hidden {
|
||||||
c.sortIcon.Hide()
|
c.sortIcon.Hide()
|
||||||
c.container.Remove(c.sortIconNegSpacer)
|
c.container.Remove(c.sortIconNegSpacer)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user