Passed
Push — main ( 375dae...05b53a )
by Rafael
01:12
created

ui/custom_dialogs.go   A

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 44
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ui.NewCustomDialog 0 26 3
A ui.ShowConfirm 0 11 3
A ui.ShowFatalError 0 6 2
1
package ui
2
3
import (
4
	"fyne.io/fyne/v2"
5
	"fyne.io/fyne/v2/container"
6
	"fyne.io/fyne/v2/dialog"
7
	"fyne.io/fyne/v2/layout"
8
	"fyne.io/fyne/v2/widget"
9
	"github.com/rchargel/hdfs-explorer/log"
10
)
11
12
func ShowFatalError(err error) {
13
	errorDialog := dialog.NewError(err, Window)
14
	errorDialog.SetOnClosed(func() {
15
		log.Error.Fatal(err)
16
	})
17
	errorDialog.Show()
18
}
19
20
func ShowConfirm(title, text string, callback func()) {
21
	dialog.NewConfirm(
22
		title,
23
		text,
24
		func(b bool) {
25
			if b {
26
				callback()
27
			}
28
		},
29
		Window,
30
	).Show()
31
}
32
33
func NewCustomDialog(title string, content fyne.CanvasObject, buttons ...*widget.Button) fyne.Window {
34
	var win fyne.Window
35
36
	closeButton := widget.NewButton(
37
		"Close",
38
		func() { win.Hide() },
39
	)
40
	buttonList := make([]fyne.CanvasObject, len(buttons)+2)
41
	buttonList[0] = layout.NewSpacer()
42
	buttonList[1] = closeButton
43
	idx := 2
44
	for _, button := range buttons {
45
		buttonList[idx] = button
46
		idx++
47
	}
48
	buttonPanel := container.NewHBox(buttonList...)
49
	container := container.NewBorder(
50
		nil,
51
		buttonPanel,
52
		nil,
53
		nil,
54
		content,
55
	)
56
	win = Application.NewWindow(title)
57
	win.SetContent(container)
58
	return win
59
}
60