Skip to content

Commit

Permalink
add validate() which is without refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorbmon committed Dec 17, 2023
1 parent f9a112a commit 0fda397
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
13 changes: 4 additions & 9 deletions widget/entry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package widget

import (
"errors"
"image/color"
"math"
"runtime"
Expand Down Expand Up @@ -894,10 +893,8 @@ func (e *Entry) TypedRune(r rune) {
})
e.propertyLock.Unlock()

if err := e.Validate(); err == nil || errors.Is(err, e.validationError) {
// Under this situation, the validate function won't call refresh, so do it here
e.Refresh()
}
e.validate()
e.Refresh()
if cb != nil {
cb(content)
}
Expand Down Expand Up @@ -1042,10 +1039,8 @@ func (e *Entry) pasteFromClipboard(clipboard fyne.Clipboard) {
e.propertyLock.Unlock()

e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos + len(runes))
if err := e.Validate(); err == nil || errors.Is(err, e.validationError) {
// the validate() won't call refresh under this condition, so we need to do it manually
e.Refresh() // placing the cursor (and refreshing) happens last
}
e.validate()
e.Refresh()
if cb != nil { // we have made sure that the paste content is not empty, so we always need to call the callback
cb(content)
}
Expand Down
28 changes: 19 additions & 9 deletions widget/entry_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ var _ fyne.Validatable = (*Entry)(nil)

// Validate validates the current text in the widget
func (e *Entry) Validate() error {
err, needRefresh := e.validate()
if needRefresh {
e.Refresh()
}
return err
}

// validate validates the current text in the widget but not refresh
func (e *Entry) validate() (err error, needRefresh bool) {
if e.Validator == nil {
return nil
return nil, false
}

err := e.Validator(e.Text)
e.SetValidationError(err)
return err
err = e.Validator(e.Text)
updated := e.SetValidationError(err)
return err, updated
}

// SetOnValidationChanged is intended for parent widgets or containers to hook into the validation.
Expand All @@ -27,13 +36,13 @@ func (e *Entry) SetOnValidationChanged(callback func(error)) {
e.onValidationChanged = callback
}

// SetValidationError manually updates the validation status until the next input change
func (e *Entry) SetValidationError(err error) {
// SetValidationError manually updates the validation status until the next input change. Returns if the validation error is really set.
func (e *Entry) SetValidationError(err error) bool {
if e.Validator == nil {
return
return false
}
if err == nil && e.validationError == nil {
return
return false
}

if !errors.Is(err, e.validationError) {
Expand All @@ -43,8 +52,9 @@ func (e *Entry) SetValidationError(err error) {
e.onValidationChanged(err)
}

e.Refresh()
return true
}
return false
}

var _ fyne.Widget = (*validationStatus)(nil)
Expand Down

0 comments on commit 0fda397

Please sign in to comment.