mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-26 13:49:01 +08:00
#7 - added confirmation dialog
This commit is contained in:
parent
6e94c635ab
commit
c96af91308
15
consts.go
15
consts.go
@ -215,8 +215,15 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
DialogClosed = -1
|
||||
DialogAlive = 0
|
||||
DialogOK = 1
|
||||
DialogCancel = 2
|
||||
DialogClosed = -1
|
||||
DialogAlive = 0
|
||||
DialogButton1 = 1
|
||||
DialogButton2 = 2
|
||||
DialogButton3 = 3
|
||||
)
|
||||
|
||||
var (
|
||||
ButtonsOK = []string{"OK"}
|
||||
ButtonsYesNo = []string{"Yes", "No"}
|
||||
ButtonsYesNoCancel = []string{"Yes", "No", "Cancel"}
|
||||
)
|
||||
|
81
dialog.go
81
dialog.go
@ -4,15 +4,19 @@ import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
type OkCancelDialog struct {
|
||||
type ConfirmationDialog struct {
|
||||
view View
|
||||
parent *Composer
|
||||
result int
|
||||
onClose func()
|
||||
}
|
||||
|
||||
func NewOkCancelDialog(c *Composer, title, question string, buttonTitles []string) *OkCancelDialog {
|
||||
dlg := new(OkCancelDialog)
|
||||
func NewConfirmationDialog(c *Composer, title, question string, buttons []string, defaultButton int) *ConfirmationDialog {
|
||||
dlg := new(ConfirmationDialog)
|
||||
|
||||
if len(buttons) == 0 {
|
||||
buttons = []string{"OK"}
|
||||
}
|
||||
|
||||
cw, ch := term.Size()
|
||||
|
||||
@ -31,20 +35,51 @@ func NewOkCancelDialog(c *Composer, title, question string, buttonTitles []strin
|
||||
NewFrame(dlg.view, dlg.view, 1, 1, BorderNone, DoNotScale)
|
||||
frm1 := NewFrame(dlg.view, dlg.view, 16, 4, BorderNone, DoNotScale)
|
||||
NewFrame(dlg.view, frm1, 1, 1, BorderNone, 1)
|
||||
bText := "OK"
|
||||
if len(buttonTitles) > 0 {
|
||||
bText = buttonTitles[0]
|
||||
|
||||
bText := buttons[0]
|
||||
btn1 := NewButton(dlg.view, frm1, AutoSize, AutoSize, bText, DoNotScale)
|
||||
btn1.OnClick(func(ev Event) {
|
||||
dlg.result = DialogButton1
|
||||
c.DestroyView(dlg.view)
|
||||
if dlg.onClose != nil {
|
||||
go dlg.onClose()
|
||||
}
|
||||
})
|
||||
if defaultButton == DialogButton1 {
|
||||
dlg.view.ActivateControl(btn1)
|
||||
}
|
||||
btnOk := NewButton(dlg.view, frm1, AutoSize, AutoSize, bText, DoNotScale)
|
||||
if len(buttonTitles) > 1 {
|
||||
bText = buttonTitles[1]
|
||||
} else {
|
||||
bText = "Cancel"
|
||||
var btn2, btn3 *Button
|
||||
|
||||
if len(buttons) > 1 {
|
||||
NewFrame(dlg.view, frm1, 1, 1, BorderNone, 1)
|
||||
btn2 = NewButton(dlg.view, frm1, AutoSize, AutoSize, buttons[1], DoNotScale)
|
||||
btn2.OnClick(func(ev Event) {
|
||||
dlg.result = DialogButton2
|
||||
c.DestroyView(dlg.view)
|
||||
if dlg.onClose != nil {
|
||||
go dlg.onClose()
|
||||
}
|
||||
})
|
||||
if defaultButton == DialogButton2 {
|
||||
dlg.view.ActivateControl(btn2)
|
||||
}
|
||||
}
|
||||
if len(buttons) > 2 {
|
||||
NewFrame(dlg.view, frm1, 1, 1, BorderNone, 1)
|
||||
btn3 = NewButton(dlg.view, frm1, AutoSize, AutoSize, buttons[2], DoNotScale)
|
||||
btn3.OnClick(func(ev Event) {
|
||||
dlg.result = DialogButton3
|
||||
c.DestroyView(dlg.view)
|
||||
if dlg.onClose != nil {
|
||||
go dlg.onClose()
|
||||
}
|
||||
})
|
||||
if defaultButton == DialogButton3 {
|
||||
dlg.view.ActivateControl(btn3)
|
||||
}
|
||||
}
|
||||
|
||||
NewFrame(dlg.view, frm1, 1, 1, BorderNone, 1)
|
||||
btnCancel := NewButton(dlg.view, frm1, AutoSize, AutoSize, bText, DoNotScale)
|
||||
NewFrame(dlg.view, frm1, 1, 1, BorderNone, 1)
|
||||
dlg.view.ActivateControl(btnCancel)
|
||||
|
||||
dlg.view.OnClose(func(ev Event) {
|
||||
if dlg.result == DialogAlive {
|
||||
@ -55,28 +90,14 @@ func NewOkCancelDialog(c *Composer, title, question string, buttonTitles []strin
|
||||
}
|
||||
}
|
||||
})
|
||||
btnOk.OnClick(func(ev Event) {
|
||||
dlg.result = DialogOK
|
||||
c.DestroyView(dlg.view)
|
||||
if dlg.onClose != nil {
|
||||
go dlg.onClose()
|
||||
}
|
||||
})
|
||||
btnCancel.OnClick(func(ev Event) {
|
||||
dlg.result = DialogCancel
|
||||
c.DestroyView(dlg.view)
|
||||
if dlg.onClose != nil {
|
||||
go dlg.onClose()
|
||||
}
|
||||
})
|
||||
|
||||
return dlg
|
||||
}
|
||||
|
||||
func (d *OkCancelDialog) OnClose(fn func()) {
|
||||
func (d *ConfirmationDialog) OnClose(fn func()) {
|
||||
d.onClose = fn
|
||||
}
|
||||
|
||||
func (d *OkCancelDialog) Result() int {
|
||||
func (d *ConfirmationDialog) Result() int {
|
||||
return d.result
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user