-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdialog.go
127 lines (110 loc) · 2.52 KB
/
dialog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fynetailscale
import (
"context"
"image/color"
"io"
"net/url"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"tailscale.com/client/tailscale"
)
type login struct {
d dialog.Dialog
cancel func()
}
var _ io.Closer = (*login)(nil)
// NewLogin will show a dialog that will allow the user to login to tailscale if necessary.
func NewLogin(ctx context.Context, win fyne.Window, lc *tailscale.LocalClient, done func(succeeded bool)) io.Closer {
cancellable, cancel := context.WithCancel(ctx)
connecting := container.NewVBox(layout.NewSpacer(), container.NewBorder(nil, nil, widget.NewLabel("Connecting"), nil, widget.NewProgressBarInfinite()), layout.NewSpacer())
info, _ := NewQRCode(nil)
info.Hide()
minSizeRect := canvas.NewRectangle(color.Transparent)
minSizeRect.SetMinSize(fyne.NewSize(255, 255))
content := container.NewMax(minSizeRect, connecting, container.NewHBox(layout.NewSpacer(), info, layout.NewSpacer()))
d := dialog.NewCustom("Login", "Cancel", content, win)
d.SetOnClosed(func() {
status, err := lc.Status(context.Background())
if err != nil {
done(false)
} else {
done(status.BackendState == "Running")
}
})
d.Show()
go func() {
displayURL := func(targetURL string) error {
u, err := url.Parse(targetURL)
if err != nil {
d.Hide()
return err
}
err = info.SetURL(u)
if err != nil {
d.Hide()
return err
}
info.Show()
connecting.Hide()
return nil
}
defer cancel()
oldState := ""
for {
select {
case <-cancellable.Done():
return
case <-time.After(100 * time.Millisecond):
status, err := lc.Status(cancellable)
if err != nil {
done(false)
return
}
if oldState == status.BackendState {
continue
}
switch status.BackendState {
case "Running":
d.Hide()
return
case "NeedsLogin":
if status.AuthURL == "" {
continue
}
err := displayURL(status.AuthURL)
if err != nil {
return
}
case "NeedsMachineAuth":
pref, err := lc.GetPrefs(cancellable)
if err != nil {
d.Hide()
return
}
if pref.AdminPageURL() == "" {
continue
}
err = displayURL(pref.AdminPageURL())
if err != nil {
return
}
}
oldState = status.BackendState
}
}
}()
return &login{
d: d,
cancel: cancel,
}
}
// Close will close the dialog.
func (d *login) Close() error {
d.cancel()
return nil
}