Skip to content

Commit

Permalink
dialog: move custom dialogs into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 14, 2022
1 parent 739d415 commit 619f230
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
55 changes: 0 additions & 55 deletions dialog/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
col "fyne.io/fyne/v2/internal/color"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand Down Expand Up @@ -48,60 +47,6 @@ type dialog struct {
layout *dialogLayout
}

// NewCustom creates and returns a dialog over the specified application using custom
// content. The button will have the dismiss text set.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func NewCustom(title, dismiss string, content fyne.CanvasObject, parent fyne.Window) Dialog {
d := &dialog{content: content, title: title, parent: parent}
d.layout = &dialogLayout{d: d}

d.dismiss = &widget.Button{Text: dismiss,
OnTapped: d.Hide,
}
d.create(container.NewHBox(layout.NewSpacer(), d.dismiss, layout.NewSpacer()))

return d
}

// NewCustomConfirm creates and returns a dialog over the specified application using
// custom content. The cancel button will have the dismiss text set and the "OK" will
// use the confirm text. The response callback is called on user action.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func NewCustomConfirm(title, confirm, dismiss string, content fyne.CanvasObject,
callback func(bool), parent fyne.Window) Dialog {
d := &dialog{content: content, title: title, icon: nil, parent: parent}
d.layout = &dialogLayout{d: d}
d.callback = callback

d.dismiss = &widget.Button{Text: dismiss, Icon: theme.CancelIcon(),
OnTapped: d.Hide,
}
ok := &widget.Button{Text: confirm, Icon: theme.ConfirmIcon(), Importance: widget.HighImportance,
OnTapped: func() {
d.hideWithResponse(true)
},
}
d.create(container.NewHBox(layout.NewSpacer(), d.dismiss, ok, layout.NewSpacer()))

return d
}

// ShowCustom shows a dialog over the specified application using custom
// content. The button will have the 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) {
NewCustom(title, dismiss, content, parent).Show()
}

// ShowCustomConfirm shows a dialog over the specified application using custom
// content. The cancel button will have the dismiss text set and the "OK" will use
// the confirm text. The response callback is called on user action.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func ShowCustomConfirm(title, confirm, dismiss string, content fyne.CanvasObject,
callback func(bool), parent fyne.Window) {
NewCustomConfirm(title, confirm, dismiss, content, callback, parent).Show()
}

func (d *dialog) Hide() {
d.hideWithResponse(false)
}
Expand Down
63 changes: 63 additions & 0 deletions dialog/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dialog

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)

// NewCustom creates and returns a dialog over the specified application using custom
// content. The button will have the dismiss text set.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func NewCustom(title, dismiss string, content fyne.CanvasObject, parent fyne.Window) Dialog {
d := &dialog{content: content, title: title, parent: parent}
d.layout = &dialogLayout{d: d}

d.dismiss = &widget.Button{Text: dismiss,
OnTapped: d.Hide,
}
d.create(container.NewHBox(layout.NewSpacer(), d.dismiss, layout.NewSpacer()))

return d
}

// NewCustomConfirm creates and returns a dialog over the specified application using
// custom content. The cancel button will have the dismiss text set and the "OK" will
// use the confirm text. The response callback is called on user action.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func NewCustomConfirm(title, confirm, dismiss string, content fyne.CanvasObject,
callback func(bool), parent fyne.Window) Dialog {
d := &dialog{content: content, title: title, icon: nil, parent: parent}
d.layout = &dialogLayout{d: d}
d.callback = callback

d.dismiss = &widget.Button{Text: dismiss, Icon: theme.CancelIcon(),
OnTapped: d.Hide,
}
ok := &widget.Button{Text: confirm, Icon: theme.ConfirmIcon(), Importance: widget.HighImportance,
OnTapped: func() {
d.hideWithResponse(true)
},
}
d.create(container.NewHBox(layout.NewSpacer(), d.dismiss, ok, layout.NewSpacer()))

return d
}

// ShowCustom shows a dialog over the specified application using custom
// content. The button will have the 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) {
NewCustom(title, dismiss, content, parent).Show()
}

// ShowCustomConfirm shows a dialog over the specified application using custom
// content. The cancel button will have the dismiss text set and the "OK" will use
// the confirm text. The response callback is called on user action.
// The MinSize() of the CanvasObject passed will be used to set the size of the window.
func ShowCustomConfirm(title, confirm, dismiss string, content fyne.CanvasObject,
callback func(bool), parent fyne.Window) {
NewCustomConfirm(title, confirm, dismiss, content, callback, parent).Show()
}
File renamed without changes.

0 comments on commit 619f230

Please sign in to comment.