Merge pull request #85 from Viv1k/master

Confirmation Dialog with editbox input
This commit is contained in:
Vladimir Markelov 2018-04-07 14:08:40 -07:00 committed by GitHub
commit 143171225b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 10 deletions

View File

@ -318,8 +318,10 @@ var (
const ( const (
// SelectDialogList - all items are displayed in a ListBox // SelectDialogList - all items are displayed in a ListBox
SelectDialogList SelectDialogType = iota SelectDialogList SelectDialogType = iota
// SelectDialogList - all items are displayed in a RadioGroup // SelectDialogRadio - all items are displayed in a RadioGroup
SelectDialogRadio SelectDialogRadio
// SelectDialogEdit - Creates an editbox for user input
SelectDialogEdit
) )
// TableAction constants // TableAction constants

View File

@ -12,9 +12,10 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
ui "github.com/VladimirMarkelov/clui" ui "github.com/VladimirMarkelov/clui"
term "github.com/nsf/termbox-go" term "github.com/nsf/termbox-go"
"strconv"
) )
func updateProgress(value string, pb *ui.ProgressBar) { func updateProgress(value string, pb *ui.ProgressBar) {

View File

@ -22,13 +22,15 @@ type ConfirmationDialog struct {
// The dialog is modal, so a user cannot interact other // The dialog is modal, so a user cannot interact other
// Views until the user closes the dialog // Views until the user closes the dialog
type SelectDialog struct { type SelectDialog struct {
View *Window View *Window
result int result int
value int value int
rg *RadioGroup edtResult string
list *ListBox rg *RadioGroup
typ SelectDialogType list *ListBox
onClose func() edit *EditField
typ SelectDialogType
onClose func()
} }
// CreateAlertDialog creates a new alert dialog. // CreateAlertDialog creates a new alert dialog.
@ -158,6 +160,10 @@ func (d *ConfirmationDialog) Result() int {
// ------------------------ Selection Dialog --------------------- // ------------------------ Selection Dialog ---------------------
func CreateEditDialog(title, message, initialText string) *SelectDialog {
return CreateSelectDialog(title, []string{message, initialText}, 0, SelectDialogEdit)
}
// NewSelectDialog creates new dialog to select an item from list. // NewSelectDialog creates new dialog to select an item from list.
// c is a composer that manages the dialog // c is a composer that manages the dialog
// title is a dialog title // title is a dialog title
@ -194,6 +200,31 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ Sele
if selectedItem >= 0 && selectedItem < len(items) { if selectedItem >= 0 && selectedItem < len(items) {
dlg.list.SelectItem(selectedItem) dlg.list.SelectItem(selectedItem)
} }
} else if typ == SelectDialogEdit {
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
lb := CreateLabel(dlg.View, 10, 3, items[0], 1)
lb.SetMultiline(true)
fWidth, _ := dlg.View.Size()
dlg.edit = CreateEditField(dlg.View, fWidth-2, items[1], AutoSize)
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
dlg.edit.OnKeyPress(func(key term.Key) bool {
var input string
if key == term.KeyEnter {
input = dlg.edit.Title()
dlg.edtResult = input
dlg.value = -1
dlg.result = DialogButton1
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
dlg.onClose()
}
}
// returning false so that other keypresses work as usual
return false
})
} else { } else {
fRadio := CreateFrame(dlg.View, 1, 1, BorderNone, Fixed) fRadio := CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
fRadio.SetPaddings(1, 1) fRadio.SetPaddings(1, 1)
@ -216,6 +247,9 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ Sele
dlg.result = DialogButton1 dlg.result = DialogButton1
if dlg.typ == SelectDialogList { if dlg.typ == SelectDialogList {
dlg.value = dlg.list.SelectedItem() dlg.value = dlg.list.SelectedItem()
} else if dlg.typ == SelectDialogEdit {
dlg.edtResult = dlg.edit.Title()
dlg.value = -1
} else { } else {
dlg.value = dlg.rg.Selected() dlg.value = dlg.rg.Selected()
} }
@ -229,13 +263,14 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ Sele
btn2 := CreateButton(frm1, AutoSize, AutoSize, "Cancel", Fixed) btn2 := CreateButton(frm1, AutoSize, AutoSize, "Cancel", Fixed)
btn2.OnClick(func(ev Event) { btn2.OnClick(func(ev Event) {
dlg.result = DialogButton2 dlg.result = DialogButton2
dlg.edtResult = ""
dlg.value = -1 dlg.value = -1
WindowManager().DestroyWindow(dlg.View) WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil { if dlg.onClose != nil {
dlg.onClose() dlg.onClose()
} }
}) })
ActivateControl(dlg.View, btn2) ActivateControl(dlg.View, dlg.edit)
CreateFrame(frm1, 1, 1, BorderNone, 1) CreateFrame(frm1, 1, 1, BorderNone, 1)
dlg.View.OnClose(func(ev Event) bool { dlg.View.OnClose(func(ev Event) bool {
@ -276,3 +311,8 @@ func (d *SelectDialog) Result() int {
func (d *SelectDialog) Value() int { func (d *SelectDialog) Value() int {
return d.value return d.value
} }
// EditResult returns the text from editfield
func (d *SelectDialog) EditResult() string {
return d.edtResult
}