forked from jojomi/gonsole
-
Notifications
You must be signed in to change notification settings - Fork 4
/
selectiondialog.go
66 lines (53 loc) · 1.37 KB
/
selectiondialog.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
package gonsole
import (
"fmt"
"strconv"
"github.com/huandu/xstrings"
)
type SelectionDialog struct {
BaseWindow
list *List
buttonIndex int
}
func (d *SelectionDialog) SelectedItem() int {
return d.list.SelectedItem()
}
func (d *SelectionDialog) SelectedButton() int {
return d.buttonIndex
}
func NewSelectionDialog(app *App, id, title, message string, buttons []string, items []string) *SelectionDialog {
d := &SelectionDialog{}
d.Init(app, id)
d.App().addWindow(d)
d.SetTitle(title)
d.SetPadding(Sides{1, 1, 1, 1})
label := NewLabel(d, d, fmt.Sprintf("%s__message", id))
label.SetPosition(Position{"0", "0", "100%", "20%"})
label.SetText(message)
d.list = NewList(d, d, "list")
d.list.SetPosition(Position{"10%", "20%", "80%", "60%"})
d.list.SetOptions(items)
d.list.Focus()
d.list.OnSumbit(func(selected int) {
d.App().removeWindow(d)
if d.onClose != nil {
d.onClose()
}
})
buttonCount := len(buttons)
for i, button := range buttons {
textLen := xstrings.Len(button)
btn := NewButton(d, d, fmt.Sprintf("%s__button%d", id, i))
btn.SetPosition(Position{fmt.Sprintf("%d%%-%d", (i*buttonCount+1)*100/(buttonCount*2), textLen/2), "90%", strconv.Itoa(textLen), "1"})
btn.SetText(button)
btnIndex := i
btn.OnClick(func() {
d.buttonIndex = btnIndex
d.App().removeWindow(d)
if d.onClose != nil {
d.onClose()
}
})
}
return d
}