From 619f230b7dba3538cc1898f9f9adf368a3b31937 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Wed, 14 Dec 2022 17:44:30 +0100 Subject: [PATCH] dialog: move custom dialogs into separate file --- dialog/base.go | 55 --------------------- dialog/custom.go | 63 +++++++++++++++++++++++++ dialog/{base_test.go => custom_test.go} | 0 3 files changed, 63 insertions(+), 55 deletions(-) create mode 100644 dialog/custom.go rename dialog/{base_test.go => custom_test.go} (100%) diff --git a/dialog/base.go b/dialog/base.go index 5ff4233db0..212e7ed011 100644 --- a/dialog/base.go +++ b/dialog/base.go @@ -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" ) @@ -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) } diff --git a/dialog/custom.go b/dialog/custom.go new file mode 100644 index 0000000000..95d72aa31b --- /dev/null +++ b/dialog/custom.go @@ -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() +} diff --git a/dialog/base_test.go b/dialog/custom_test.go similarity index 100% rename from dialog/base_test.go rename to dialog/custom_test.go