diff options
author | Alexander Neumann <alexander.neumann@picos-software.com> | 2020-09-03 10:18:20 +0200 |
---|---|---|
committer | Alexander Neumann <alexander.neumann@picos-software.com> | 2020-09-03 10:18:20 +0200 |
commit | 27a6cd99127e7bd627cc46b3484ad4a02905a536 (patch) | |
tree | 64b9639f1114a55bdf0ee20150e190131b88c995 | |
parent | Merge pull request #711 from xoviat/bugfix/issue_710 (diff) | |
download | wireguard-windows-27a6cd99127e7bd627cc46b3484ad4a02905a536.tar.xz wireguard-windows-27a6cd99127e7bd627cc46b3484ad4a02905a536.zip |
ComboBox/ListBox: Keep current selection on model reset if possible
-rw-r--r-- | combobox.go | 32 | ||||
-rw-r--r-- | listbox.go | 17 |
2 files changed, 46 insertions, 3 deletions
diff --git a/combobox.go b/combobox.go index 9a4f2529..1d5d12fb 100644 --- a/combobox.go +++ b/combobox.go @@ -32,6 +32,7 @@ type ComboBox struct { itemsInsertedHandlerHandle int itemsRemovedHandlerHandle int maxItemTextWidth int // in native pixels + currentValue interface{} prevCurIndex int selChangeIndex int maxLength int @@ -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 } @@ -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 @@ -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() } @@ -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 @@ -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 @@ -29,6 +29,7 @@ type ListBox struct { format string precision int prevCurIndex int + currentValue interface{} itemsResetHandlerHandle int itemChangedHandlerHandle int itemsInsertedHandlerHandle int @@ -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 } @@ -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() @@ -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() } @@ -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() |