Skip to content

Commit

Permalink
ComboBox/ListBox: Keep current selection on model reset if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Sep 3, 2020
1 parent 5e32bf2 commit 27a6cd9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
32 changes: 30 additions & 2 deletions combobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ComboBox struct {
itemsInsertedHandlerHandle int
itemsRemovedHandlerHandle int
maxItemTextWidth int // in native pixels
currentValue interface{}
prevCurIndex int
selChangeIndex int
maxLength int
Expand Down Expand Up @@ -296,9 +297,10 @@ func (cb *ComboBox) resetItems() error {

cb.maxItemTextWidth = 0

cb.SetCurrentIndex(-1)
oldValue := cb.currentValue

if cb.model == nil {
cb.SetCurrentIndex(-1)
return nil
}

Expand All @@ -310,6 +312,12 @@ func (cb *ComboBox) resetItems() error {
}
}

if oldValue != nil {
cb.Property("Value").Set(oldValue)
} else {
cb.SetCurrentIndex(-1)
}

cb.RequestLayout()

return nil
Expand Down Expand Up @@ -556,6 +564,12 @@ func (cb *ComboBox) SetCurrentIndex(value int) error {
}

if value != cb.prevCurIndex {
if value == -1 {
cb.currentValue = nil
} else {
cb.currentValue = cb.Property("Value").Get()
}

cb.prevCurIndex = value
cb.currentIndexChangedPublisher.Publish()
}
Expand All @@ -572,10 +586,21 @@ func (cb *ComboBox) Text() string {
}

func (cb *ComboBox) SetText(value string) error {
var oldText string
oldText, _ = cb.currentValue.(string)

if err := cb.setText(value); err != nil {
return err
}

if value == oldText {
return nil
}

if cb.Editable() {
cb.currentValue = value
}

cb.textChangedPublisher.Publish()

return nil
Expand Down Expand Up @@ -654,8 +679,11 @@ func (cb *ComboBox) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) u

case win.CBN_SELENDOK:
if editable := cb.Editable(); editable || selIndex != cb.prevCurIndex {
valueProp := cb.Property("Value")
if editable && selIndex > -1 {
cb.Property("Value").Set(cb.model.Value(selIndex))
valueProp.Set(cb.model.Value(selIndex))
} else {
cb.currentValue = valueProp.Get()
}
cb.currentIndexChangedPublisher.Publish()
cb.prevCurIndex = selIndex
Expand Down
17 changes: 16 additions & 1 deletion listbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ListBox struct {
format string
precision int
prevCurIndex int
currentValue interface{}
itemsResetHandlerHandle int
itemChangedHandlerHandle int
itemsInsertedHandlerHandle int
Expand Down Expand Up @@ -231,9 +232,10 @@ func (lb *ListBox) resetItems() error {

lb.maxItemTextWidth = 0

lb.SetCurrentIndex(-1)
oldValue := lb.currentValue

if lb.model == nil {
lb.SetCurrentIndex(-1)
return nil
}

Expand All @@ -247,6 +249,12 @@ func (lb *ListBox) resetItems() error {
}
}

if oldValue != nil {
lb.Property("Value").Set(oldValue)
} else {
lb.SetCurrentIndex(-1)
}

if lb.styler == nil {
// Update the listbox width (this sets the correct horizontal scrollbar).
sh := lb.idealSize()
Expand Down Expand Up @@ -580,6 +588,12 @@ func (lb *ListBox) SetCurrentIndex(value int) error {
}

if value != lb.prevCurIndex {
if value == -1 {
lb.currentValue = nil
} else {
lb.currentValue = lb.Property("Value").Get()
}

lb.prevCurIndex = value
lb.currentIndexChangedPublisher.Publish()
}
Expand Down Expand Up @@ -795,6 +809,7 @@ func (lb *ListBox) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) ui
case win.LBN_SELCHANGE:
lb.ensureVisibleItemsHeightUpToDate()
lb.prevCurIndex = lb.CurrentIndex()
lb.currentValue = lb.Property("Value").Get()
lb.currentIndexChangedPublisher.Publish()
lb.selectedIndexesChangedPublisher.Publish()

Expand Down

0 comments on commit 27a6cd9

Please sign in to comment.