Skip to content

Commit

Permalink
Merge pull request #851 from obsti8383/master
Browse files Browse the repository at this point in the history
Callbacks for Information and Error dialogs
  • Loading branch information
andydotxyz authored Apr 19, 2020
2 parents 048b8e2 + 8874a16 commit b3b172d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
15 changes: 15 additions & 0 deletions dialog/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Dialog interface {
Show()
Hide()
SetDismissText(label string)
SetOnClosed(closed func())
}

// Declare conformity to Dialog interface
Expand All @@ -39,6 +40,20 @@ type dialog struct {
parent fyne.Window
}

// SetOnClosed allows to set a callback function that is called when
// the dialog is closed
func (d *dialog) SetOnClosed(closed func()) {
// if there is already a callback set, remember it and call both
originalCallback := d.callback

d.callback = func(response bool) {
closed()
if originalCallback != nil {
originalCallback(response)
}
}
}

func (d *dialog) setButtons(buttons fyne.CanvasObject) {
d.bg = canvas.NewRectangle(theme.BackgroundColor())
d.label = widget.NewLabelWithStyle(d.title, fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
Expand Down
59 changes: 59 additions & 0 deletions dialog/confirm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dialog

import (
"testing"

"fyne.io/fyne/test"

"github.com/stretchr/testify/assert"
)

func TestDialog_ConfirmDoubleCallback(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", func(_ bool) {
ch <- 42
}, test.NewWindow(nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.SetOnClosed(func() {
ch <- 43
})
cnf.Show()

assert.False(t, cnf.win.Hidden)
go test.Tap(cnf.dismiss)
assert.EqualValues(t, <-ch, 43)
assert.EqualValues(t, <-ch, 42)
assert.True(t, cnf.win.Hidden)
}

func TestDialog_ConfirmCallbackOnlyOnClosed(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", nil, test.NewWindow(nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.SetOnClosed(func() {
ch <- 43
})
cnf.Show()

assert.False(t, cnf.win.Hidden)
go test.Tap(cnf.dismiss)
assert.EqualValues(t, <-ch, 43)
assert.True(t, cnf.win.Hidden)
}

func TestDialog_ConfirmCallbackOnlyOnConfirm(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", func(_ bool) {
ch <- 42
}, test.NewWindow(nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.Show()

assert.False(t, cnf.win.Hidden)
go test.Tap(cnf.dismiss)
assert.EqualValues(t, <-ch, 42)
assert.True(t, cnf.win.Hidden)
}
9 changes: 8 additions & 1 deletion dialog/information.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ func ShowInformation(title, message string, parent fyne.Window) {
NewInformation(title, message, parent).Show()
}

// NewError creates a dialog over the specified window for an application
// error. The title and message are extracted from the provided error.
// After creation you should call Show().
func NewError(err error, parent fyne.Window) Dialog {
return createTextDialog("Error", err.Error(), theme.WarningIcon(), parent)
}

// ShowError shows a dialog over the specified window for an application
// error. The title and message are extracted from the provided error.
func ShowError(err error, parent fyne.Window) {
createTextDialog("Error", err.Error(), theme.WarningIcon(), parent).Show()
NewError(err, parent).Show()
}
41 changes: 41 additions & 0 deletions dialog/information_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dialog

import (
"errors"
"testing"
"time"

"fyne.io/fyne/test"
"github.com/stretchr/testify/assert"
Expand All @@ -16,3 +18,42 @@ func TestDialog_MinSize(t *testing.T) {

assert.Less(t, label.Width, dialogContent.Width)
}

func TestDialog_InformationCallback(t *testing.T) {
d := NewInformation("Information", "Hello World", test.NewWindow(nil))
tapped := make(chan bool)
d.SetOnClosed(func() { tapped <- true })
d.Show()

information := d.(*dialog)
assert.False(t, information.win.Hidden)
go test.Tap(information.dismiss)
func() {
select {
case <-tapped:
case <-time.After(1 * time.Second):
assert.Fail(t, "Timed out waiting for button tap")
}
}()
assert.True(t, information.win.Hidden)
}

func TestDialog_ErrorCallback(t *testing.T) {
err := errors.New("Error message")
d := NewError(err, test.NewWindow(nil))
tapped := make(chan bool)
d.SetOnClosed(func() { tapped <- true })
d.Show()

information := d.(*dialog)
assert.False(t, information.win.Hidden)
go test.Tap(information.dismiss)
func() {
select {
case <-tapped:
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "Timed out waiting for button tap")
}
}()
assert.True(t, information.win.Hidden)
}

0 comments on commit b3b172d

Please sign in to comment.