Skip to content

Commit

Permalink
Add customisation for dialog button text.
Browse files Browse the repository at this point in the history
Needed a small addition to set button text as well
  • Loading branch information
andydotxyz committed Oct 11, 2018
1 parent 3579856 commit 34e5b6f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
28 changes: 17 additions & 11 deletions dialog/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/fyne-io/fyne/widget"
)

// Dialog is the common API for any dialog window
// Dialog is the common API for any dialog window with a single dismiss button
type Dialog interface {
Show()
SetDismissText(label string)
}

type dialog struct {
Expand All @@ -20,6 +21,8 @@ type dialog struct {
content fyne.CanvasObject
icon fyne.Resource

dismiss *widget.Button

response chan bool
responded bool
}
Expand Down Expand Up @@ -126,25 +129,28 @@ func (d *dialog) Show() {
d.win.Show()
}

// SetDismissText allows custom text to be set in the confirmation button
func (d *dialog) SetDismissText(label string) {
d.dismiss.SetText(label)
}

// ShowCustom shows a dialog over the specified application using custom
// content. The MinSize() of the CanvasObject passed will be used to set
// the size of the window.
func ShowCustom(title, confirm string, content fyne.CanvasObject, parent fyne.Window) {
// content. The button will have th dismiss text set.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func ShowCustom(title, dismiss string, content fyne.CanvasObject, parent fyne.Window) {
d := &dialog{content: content, icon: nil}

win := newDialogWin(title, parent)
win.SetOnClosed(d.closed)
d.win = win
d.response = make(chan bool, 1)

d.setButtons(widget.NewHBox(layout.NewSpacer(),
&widget.Button{Text: confirm, Style: widget.PrimaryButton,
OnTapped: func() {
d.response <- false
},
d.dismiss = &widget.Button{Text: dismiss,
OnTapped: func() {
d.response <- false
},
layout.NewSpacer()),
)
}
d.setButtons(widget.NewHBox(layout.NewSpacer(), d.dismiss, layout.NewSpacer()))

d.Show()
}
36 changes: 24 additions & 12 deletions dialog/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@ import (
"github.com/fyne-io/fyne/widget"
)

// ConfirmDialog is like the standard Dialog but with an additional confirmation button
type ConfirmDialog struct {
*dialog

confirm *widget.Button
}

// SetConfirmText allows custom text to be set in the confirmation button
func (d *ConfirmDialog) SetConfirmText(label string) {
d.confirm.SetText(label)
}

// NewConfirm creates a dialog over the specified window for user confirmation.
// The title is used for the ialog window and message is the content.
// The callback is executed when the user decides. After creation you should call Show().
func NewConfirm(title, message string, callback func(bool), parent fyne.Window) Dialog {
func NewConfirm(title, message string, callback func(bool), parent fyne.Window) *ConfirmDialog {
d := newDialog(title, message, theme.QuestionIcon(), callback, parent)
d.setButtons(newButtonList(
&widget.Button{Text: "No",
OnTapped: func() {
d.response <- false
},

d.dismiss = &widget.Button{Text: "No",
OnTapped: func() {
d.response <- false
},
&widget.Button{Text: "Yes", Style: widget.PrimaryButton,
OnTapped: func() {
d.response <- true
},
}
confirm := &widget.Button{Text: "Yes", Style: widget.PrimaryButton,
OnTapped: func() {
d.response <- true
},
))
}
d.setButtons(newButtonList(d.dismiss, confirm))

return d
return &ConfirmDialog{d, confirm}
}

// ShowConfirm shows a dialog over the specified window for a user
Expand Down
6 changes: 4 additions & 2 deletions dialog/information.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
// After creation you should call Show().
func NewInformation(title, message string, parent fyne.Window) Dialog {
d := newDialog(title, message, theme.InfoIcon(), nil, parent)
d.setButtons(newButtonList(&widget.Button{Text: "OK", Style: widget.PrimaryButton,

d.dismiss = &widget.Button{Text: "OK",
OnTapped: func() {
d.response <- false
},
}))
}
d.setButtons(newButtonList(d.dismiss))

return d
}
Expand Down
5 changes: 4 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func main() {
dialog.ShowError(err, w)
}},
&W.Button{Text: "Confirm", OnTapped: func() {
dialog.ShowConfirm("Confirmation", "Do you want to confirm?", confirmCallback, w)
cnf := dialog.NewConfirm("Confirmation", "Are you enjoying this demo?", confirmCallback, w)
cnf.SetDismissText("Nah")
cnf.SetConfirmText("Oh Yes!")
cnf.Show()
}},
&W.Button{Text: "Custom", OnTapped: func() {
dialog.ShowCustom("MyDialog", "Nice", &W.Check{Text: "Inside a dialog"}, w)
Expand Down
8 changes: 8 additions & 0 deletions widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ func (b *Button) Renderer() fyne.WidgetRenderer {
return b.renderer
}

// SetText allows the button label to be changed
func (b *Button) SetText(text string) {
b.Text = text
b.Renderer().(*buttonRenderer).label.Text = text

b.Renderer().Refresh()
}

// NewButton creates a new button widget with the set label and tap handler
func NewButton(label string, tapped func()) *Button {
button := &Button{baseWidget{}, label, DefaultButton, nil, tapped}
Expand Down

0 comments on commit 34e5b6f

Please sign in to comment.