Skip to content

Commit

Permalink
Merge branch 'master' into action_buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
beansgum committed Oct 4, 2021
2 parents 19b4b39 + 07df2a6 commit b1625fd
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 104 deletions.
15 changes: 15 additions & 0 deletions ui/decredmaterial/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,18 @@ func approxLuminance(c color.NRGBA) byte {
)
return byte((r*int(c.R) + g*int(c.G) + b*int(c.B)) / t)
}

func HandleEditorEvents(editors ...*widget.Editor) (bool, bool) {
var submit, changed bool
for _, editor := range editors {
for _, evt := range editor.Events() {
switch evt.(type) {
case widget.ChangeEvent:
changed = true
case widget.SubmitEvent:
submit = true
}
}
}
return submit, changed
}
60 changes: 47 additions & 13 deletions ui/modal/create_password_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ type CreatePasswordModal struct {
isCancelable bool
walletNameEnabled bool
showWalletWarnInfo bool
isEnabled bool

dialogTitle string
randomID string
serverError string

materialLoader material.LoaderStyle

Expand Down Expand Up @@ -78,11 +80,14 @@ func (cm *CreatePasswordModal) ModalID() string {
}

func (cm *CreatePasswordModal) OnResume() {
if cm.walletNameEnabled {
cm.walletName.Editor.Focus()
} else {
cm.passwordEditor.Editor.Focus()
}
}

func (cm *CreatePasswordModal) OnDismiss() {

}
func (cm *CreatePasswordModal) OnDismiss() {}

func (cm *CreatePasswordModal) Show() {
cm.ShowModal(cm)
Expand Down Expand Up @@ -132,7 +137,7 @@ func (cm *CreatePasswordModal) SetCancelable(min bool) *CreatePasswordModal {
}

func (cm *CreatePasswordModal) SetError(err string) {

cm.serverError = err
}

func (cm *CreatePasswordModal) validToCreate() bool {
Expand All @@ -146,29 +151,51 @@ func (cm *CreatePasswordModal) validToCreate() bool {
}

func (cm *CreatePasswordModal) Handle() {

if editorsNotEmpty(cm.passwordEditor.Editor) || editorsNotEmpty(cm.walletName.Editor) ||
editorsNotEmpty(cm.confirmPasswordEditor.Editor) {
cm.btnPositve.Background = cm.Theme.Color.Primary
cm.isEnabled = true
} else {
cm.btnPositve.Background = cm.Theme.Color.InactiveGray
cm.isEnabled = false
}

if cm.passwordEditor.Editor.Text() == cm.confirmPasswordEditor.Editor.Text() {
// reset error label when password and matching password fields match
isSubmit, isChanged := decredmaterial.HandleEditorEvents(cm.passwordEditor.Editor, cm.confirmPasswordEditor.Editor, cm.walletName.Editor)

if isChanged {
// reset editor errors
cm.walletName.SetError("")
cm.passwordEditor.SetError("")
cm.confirmPasswordEditor.SetError("")
}

cm.btnPositve.SetEnabled(cm.validToCreate())
if cm.btnPositve.Clicked() || handleSubmitEvent(cm.walletName.Editor, cm.passwordEditor.Editor, cm.confirmPasswordEditor.Editor) {
if (cm.btnPositve.Clicked() || isSubmit) && cm.isEnabled {

if cm.walletNameEnabled {
if !editorsNotEmpty(cm.walletName.Editor) {
cm.walletName.SetError("enter wallet name")
return
}
}

if cm.validToCreate() {
if !editorsNotEmpty(cm.passwordEditor.Editor) {
cm.passwordEditor.SetError("enter spending password")
return
}

if !editorsNotEmpty(cm.confirmPasswordEditor.Editor) {
cm.confirmPasswordEditor.SetError("confirm spending password")
return
}

if cm.passwordsMatch(cm.passwordEditor.Editor, cm.confirmPasswordEditor.Editor) {

cm.SetLoading(true)
if cm.callback(cm.walletName.Editor.Text(), cm.passwordEditor.Editor.Text(), cm) {
cm.Dismiss()
}
}

}

cm.btnNegative.SetEnabled(!cm.isLoading)
Expand All @@ -185,8 +212,8 @@ func (cm *CreatePasswordModal) Handle() {
}

computePasswordStrength(&cm.passwordStrength, cm.Theme, cm.passwordEditor.Editor)

}

func (cm *CreatePasswordModal) passwordsMatch(editors ...*widget.Editor) bool {
if len(editors) < 2 {
return false
Expand All @@ -197,12 +224,10 @@ func (cm *CreatePasswordModal) passwordsMatch(editors ...*widget.Editor) bool {

if password.Text() != matching.Text() {
cm.confirmPasswordEditor.SetError("passwords do not match")
cm.btnPositve.SetEnabled(false)
return false
}

cm.confirmPasswordEditor.SetError("")
cm.btnPositve.SetEnabled(true)
return true
}

Expand All @@ -213,6 +238,15 @@ func (cm *CreatePasswordModal) Layout(gtx layout.Context) D {
t.Font.Weight = text.Bold
return t.Layout(gtx)
},
func(gtx C) D {
if cm.serverError != "" {
t := cm.Theme.Body2(cm.serverError)
t.Color = cm.Theme.Color.Danger
return t.Layout(gtx)
}
return layout.Dimensions{}
},

func(gtx C) D {
if cm.walletNameEnabled {
return cm.walletName.Layout(gtx)
Expand Down
45 changes: 31 additions & 14 deletions ui/modal/create_watch_only_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type CreateWatchOnlyModal struct {

isLoading bool
isCancelable bool
isEnabled bool

callback func(walletName, extPubKey string, m *CreateWatchOnlyModal) bool // return true to dismiss dialog
}
Expand All @@ -47,7 +48,6 @@ func NewCreateWatchOnlyModal(l *load.Load) *CreateWatchOnlyModal {
}

cm.btnPositve.Font.Weight = text.Medium
cm.btnPositve.SetEnabled(false)

cm.btnNegative.Font.Weight = text.Medium
cm.btnNegative.Margin = layout.Inset{Right: values.MarginPadding8}
Expand Down Expand Up @@ -93,11 +93,7 @@ func (cm *CreateWatchOnlyModal) SetCancelable(min bool) *CreateWatchOnlyModal {
}

func (cm *CreateWatchOnlyModal) SetError(err string) {
if err == "" {
cm.extendedPubKey.ClearError()
} else {
cm.extendedPubKey.SetError(err)
}
cm.extendedPubKey.SetError(err)
}

func (cm *CreateWatchOnlyModal) WatchOnlyCreated(callback func(walletName, extPubKey string, m *CreateWatchOnlyModal) bool) *CreateWatchOnlyModal {
Expand All @@ -106,15 +102,36 @@ func (cm *CreateWatchOnlyModal) WatchOnlyCreated(callback func(walletName, extPu
}

func (cm *CreateWatchOnlyModal) Handle() {
if editorsNotEmpty(cm.walletName.Editor) ||
editorsNotEmpty(cm.extendedPubKey.Editor) {
cm.btnPositve.Background = cm.Theme.Color.Primary
cm.isEnabled = true
} else {
cm.btnPositve.Background = cm.Theme.Color.InactiveGray
cm.isEnabled = false
}

isSubmit, isChanged := decredmaterial.HandleEditorEvents(cm.walletName.Editor, cm.extendedPubKey.Editor)
if isChanged {
// reset editor errors
cm.walletName.SetError("")
cm.extendedPubKey.SetError("")
}

cm.btnPositve.SetEnabled(editorsNotEmpty(cm.walletName.Editor, cm.extendedPubKey.Editor))
if editorsNotEmpty(cm.walletName.Editor, cm.extendedPubKey.Editor) ||
handleSubmitEvent(cm.walletName.Editor, cm.extendedPubKey.Editor) {
for cm.btnPositve.Clicked() {
cm.SetLoading(true)
if cm.callback(cm.walletName.Editor.Text(), cm.extendedPubKey.Editor.Text(), cm) {
cm.Dismiss()
}
for (cm.btnPositve.Clicked() || isSubmit) && cm.isEnabled {
if !editorsNotEmpty(cm.walletName.Editor) {
cm.walletName.SetError("enter wallet name")
return
}

if !editorsNotEmpty(cm.extendedPubKey.Editor) {
cm.extendedPubKey.SetError("enter a valid extendedPubKey")
return
}

cm.SetLoading(true)
if cm.callback(cm.walletName.Editor.Text(), cm.extendedPubKey.Editor.Text(), cm) {
cm.Dismiss()
}
}

Expand Down
19 changes: 15 additions & 4 deletions ui/modal/password_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (pm *PasswordModal) ModalID() string {
}

func (pm *PasswordModal) OnResume() {
pm.password.Editor.Focus()
}

func (pm *PasswordModal) OnDismiss() {
Expand Down Expand Up @@ -121,10 +122,20 @@ func (pm *PasswordModal) SetError(err string) {
}

func (pm *PasswordModal) Handle() {
pm.btnPositve.SetEnabled(editorsNotEmpty(pm.password.Editor))
for pm.btnPositve.Clicked() || handleSubmitEvent(pm.password.Editor) {
if pm.isLoading || !editorsNotEmpty(pm.password.Editor) {
continue
isSubmit, isChanged := decredmaterial.HandleEditorEvents(pm.password.Editor)
if isChanged {
pm.password.SetError("")
}

if pm.btnPositve.Button.Clicked() || isSubmit {

if !editorsNotEmpty(pm.password.Editor) {
pm.password.SetError("enter spending password")
return
}

if pm.isLoading {
return
}

pm.SetLoading(true)
Expand Down
16 changes: 14 additions & 2 deletions ui/modal/text_input_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type TextInputModal struct {
IsLoading bool
showAccountWarnInfo bool
isCancelable bool
isEnabled bool

textInput decredmaterial.Editor
callback func(string, *TextInputModal) bool
Expand All @@ -44,6 +45,10 @@ func (tm *TextInputModal) Show() {
tm.ShowModal(tm)
}

func (tm *TextInputModal) OnResume() {
tm.textInput.Editor.Focus()
}

func (tm *TextInputModal) Dismiss() {
tm.DismissModal(tm)
}
Expand Down Expand Up @@ -85,13 +90,20 @@ func (tm *TextInputModal) SetCancelable(min bool) *TextInputModal {
func (tm *TextInputModal) Handle() {
if editorsNotEmpty(tm.textInput.Editor) {
tm.btnPositve.Background = tm.Theme.Color.Primary
tm.isEnabled = true
} else {
tm.btnPositve.Background = tm.Theme.Color.InactiveGray
tm.isEnabled = false
}

isSubmit, isChanged := decredmaterial.HandleEditorEvents(tm.textInput.Editor)
if isChanged {
tm.textInput.SetError("")
}

for tm.btnPositve.Clicked() || handleSubmitEvent(tm.textInput.Editor) {
if (tm.btnPositve.Clicked() || isSubmit) && tm.isEnabled {
if tm.IsLoading {
continue
return
}

tm.IsLoading = true
Expand Down
12 changes: 0 additions & 12 deletions ui/modal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ func editorsNotEmpty(editors ...*widget.Editor) bool {
return true
}

func handleSubmitEvent(editors ...*widget.Editor) bool {
var submit bool
for _, editor := range editors {
for _, e := range editor.Events() {
if _, ok := e.(widget.SubmitEvent); ok {
submit = true
}
}
}
return submit
}

func generateRandomNumber() int {
return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
}
Expand Down
36 changes: 16 additions & 20 deletions ui/page/sign_message_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ type SignMessagePage struct {
container layout.List
wallet *dcrlibwallet.Wallet

isSigningMessage bool
addressIsValid bool
messageIsValid bool
isSigningMessage bool
addressIsValid bool
messageIsValid bool
isEnabled bool

titleLabel, errorLabel, signedMessageLabel decredmaterial.Label
addressEditor, messageEditor decredmaterial.Editor
clearButton, signButton, copyButton decredmaterial.Button
Expand Down Expand Up @@ -205,7 +207,8 @@ func (pg *SignMessagePage) drawResult() layout.Widget {
}
}

func (pg *SignMessagePage) updateButtonState() {
func (pg *SignMessagePage) updateButtonColors() {
pg.isEnabled = false
if components.StringNotEmpty(pg.addressEditor.Editor.Text()) ||
components.StringNotEmpty(pg.messageEditor.Editor.Text()) {
pg.clearButton.SetEnabled(true)
Expand All @@ -214,39 +217,32 @@ func (pg *SignMessagePage) updateButtonState() {
}

if !pg.isSigningMessage && pg.messageIsValid && pg.addressIsValid {
pg.signButton.SetEnabled(true)
} else {
pg.signButton.SetEnabled(false)
pg.isEnabled = true
}

pg.signButton.SetEnabled(pg.isEnabled)
}

func (pg *SignMessagePage) Handle() {
gtx := pg.gtx
pg.updateButtonState()
pg.updateButtonColors()

for _, evt := range pg.addressEditor.Editor.Events() {
isSubmit, isChanged := decredmaterial.HandleEditorEvents(pg.addressEditor.Editor, pg.messageEditor.Editor)
if isChanged {
if pg.addressEditor.Editor.Focused() {
switch evt.(type) {
case widget.ChangeEvent:
pg.validateAddress()
}
pg.validateAddress()
}
}

for _, evt := range pg.messageEditor.Editor.Events() {
if pg.messageEditor.Editor.Focused() {
switch evt.(type) {
case widget.ChangeEvent:
pg.validateMessage()
}
pg.validateMessage()
}
}

for pg.clearButton.Clicked() {
pg.clearForm()
}

for pg.signButton.Clicked() || handleSubmitEvent(pg.addressEditor.Editor, pg.messageEditor.Editor) {
if (pg.signButton.Clicked() || isSubmit) && pg.isEnabled {
if !pg.isSigningMessage && pg.validate() {
address := pg.addressEditor.Editor.Text()
message := pg.messageEditor.Editor.Text()
Expand Down
Loading

0 comments on commit b1625fd

Please sign in to comment.