mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-28 13:48:50 +08:00
working dialog with edit box
This commit is contained in:
parent
77ee1b7531
commit
c83e8ea28f
44
demos/editdialog/editdialog.go
Normal file
44
demos/editdialog/editdialog.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
ui "vivek-go/clui"
|
||||||
|
|
||||||
|
termbox "github.com/nsf/termbox-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateBox() {
|
||||||
|
dlg := ui.CreateConfirmationEditDialog(
|
||||||
|
"<c:blue>"+"SearchBox:",
|
||||||
|
"Enter search text. Pressing enter would print your input in debug.log")
|
||||||
|
|
||||||
|
dlg.OnClose(func() {
|
||||||
|
// write input test to debug.log only when enter is pressed
|
||||||
|
if dlg.Result() == ui.DialogButton1 {
|
||||||
|
ui.Logger().Println("result", dlg.EditResult())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func mainLoop() {
|
||||||
|
ui.InitLibrary()
|
||||||
|
defer ui.DeinitLibrary()
|
||||||
|
|
||||||
|
// BUG, if you don't provide any window and create a dialog box
|
||||||
|
// app crashes on pressing any button of dialog
|
||||||
|
window := ui.AddWindow(0, 0, 10, 7, "EditDialog Demo")
|
||||||
|
window.OnKeyDown(func(event ui.Event) bool {
|
||||||
|
switch event.Key {
|
||||||
|
case termbox.KeySpace:
|
||||||
|
CreateBox()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
// CreateBox()
|
||||||
|
|
||||||
|
ui.MainLoop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mainLoop()
|
||||||
|
}
|
@ -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) {
|
||||||
|
79
dialog.go
79
dialog.go
@ -12,7 +12,8 @@ import (
|
|||||||
// Views until the user closes the dialog
|
// Views until the user closes the dialog
|
||||||
type ConfirmationDialog struct {
|
type ConfirmationDialog struct {
|
||||||
View *Window
|
View *Window
|
||||||
result int
|
btnResult int
|
||||||
|
edtResult string
|
||||||
onClose func()
|
onClose func()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,7 +24,7 @@ type ConfirmationDialog struct {
|
|||||||
// 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
|
btnResult int
|
||||||
value int
|
value int
|
||||||
rg *RadioGroup
|
rg *RadioGroup
|
||||||
list *ListBox
|
list *ListBox
|
||||||
@ -31,6 +32,9 @@ type SelectDialog struct {
|
|||||||
onClose func()
|
onClose func()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// a separate function to avoid api changes; assign to this function to create an editbox
|
||||||
|
var createEditbox func(parent Control, width int, text string, scale int, dlg *ConfirmationDialog) *EditField
|
||||||
|
|
||||||
// CreateAlertDialog creates a new alert dialog.
|
// CreateAlertDialog creates a new alert dialog.
|
||||||
// title is a dialog title
|
// title is a dialog title
|
||||||
// message is a text inside dialog for user to be notified of a fact
|
// message is a text inside dialog for user to be notified of a fact
|
||||||
@ -39,6 +43,32 @@ func CreateAlertDialog(title, message string, button string) *ConfirmationDialog
|
|||||||
return CreateConfirmationDialog(title, message, []string{button}, 0)
|
return CreateConfirmationDialog(title, message, []string{button}, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateConfirmationEditDialog(title, message string) *ConfirmationDialog {
|
||||||
|
// lambda function to create editbox
|
||||||
|
createEditbox = func(parent Control, width int, text string, scale int, dlg *ConfirmationDialog) *EditField {
|
||||||
|
edit := CreateEditField(parent, width, text, scale)
|
||||||
|
|
||||||
|
edit.OnKeyPress(func(key term.Key) bool {
|
||||||
|
var input string
|
||||||
|
if key == term.KeyEnter {
|
||||||
|
input = edit.Title()
|
||||||
|
dlg.edtResult = input
|
||||||
|
dlg.btnResult = DialogButton1
|
||||||
|
|
||||||
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
|
if dlg.onClose != nil {
|
||||||
|
dlg.onClose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// returning false so that other keypresses work as usual
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return edit
|
||||||
|
}
|
||||||
|
|
||||||
|
return CreateConfirmationDialog(title, message, []string{"Enter", "Cancel"}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
// CreateConfirmationDialog creates new confirmation dialog.
|
// CreateConfirmationDialog creates new confirmation dialog.
|
||||||
// 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
|
||||||
@ -72,6 +102,15 @@ func CreateConfirmationDialog(title, question string, buttons []string, defaultB
|
|||||||
lb.SetMultiline(true)
|
lb.SetMultiline(true)
|
||||||
CreateFrame(fbtn, 1, 1, BorderNone, Fixed)
|
CreateFrame(fbtn, 1, 1, BorderNone, Fixed)
|
||||||
|
|
||||||
|
// create editbox if editbox fn is not nil
|
||||||
|
var editbox *EditField
|
||||||
|
if createEditbox != nil {
|
||||||
|
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
|
||||||
|
frmedit := CreateFrame(dlg.View, 1, 1, BorderNone, 1)
|
||||||
|
frmwidth, _ := frmedit.Size()
|
||||||
|
editbox = createEditbox(frmedit, frmwidth, "", AutoSize, dlg)
|
||||||
|
}
|
||||||
|
|
||||||
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
|
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
|
||||||
frm1 := CreateFrame(dlg.View, 16, 4, BorderNone, Fixed)
|
frm1 := CreateFrame(dlg.View, 16, 4, BorderNone, Fixed)
|
||||||
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
||||||
@ -79,7 +118,9 @@ func CreateConfirmationDialog(title, question string, buttons []string, defaultB
|
|||||||
bText := buttons[0]
|
bText := buttons[0]
|
||||||
btn1 := CreateButton(frm1, AutoSize, AutoSize, bText, Fixed)
|
btn1 := CreateButton(frm1, AutoSize, AutoSize, bText, Fixed)
|
||||||
btn1.OnClick(func(ev Event) {
|
btn1.OnClick(func(ev Event) {
|
||||||
dlg.result = DialogButton1
|
// only click on first button stores editbox result
|
||||||
|
dlg.btnResult = DialogButton1
|
||||||
|
dlg.edtResult = editbox.Title()
|
||||||
|
|
||||||
WindowManager().DestroyWindow(dlg.View)
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
WindowManager().BeginUpdate()
|
WindowManager().BeginUpdate()
|
||||||
@ -95,7 +136,9 @@ func CreateConfirmationDialog(title, question string, buttons []string, defaultB
|
|||||||
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
||||||
btn2 = CreateButton(frm1, AutoSize, AutoSize, buttons[1], Fixed)
|
btn2 = CreateButton(frm1, AutoSize, AutoSize, buttons[1], Fixed)
|
||||||
btn2.OnClick(func(ev Event) {
|
btn2.OnClick(func(ev Event) {
|
||||||
dlg.result = DialogButton2
|
dlg.btnResult = DialogButton2
|
||||||
|
dlg.edtResult = ""
|
||||||
|
|
||||||
WindowManager().DestroyWindow(dlg.View)
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
if dlg.onClose != nil {
|
if dlg.onClose != nil {
|
||||||
dlg.onClose()
|
dlg.onClose()
|
||||||
@ -106,7 +149,9 @@ func CreateConfirmationDialog(title, question string, buttons []string, defaultB
|
|||||||
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
||||||
btn3 = CreateButton(frm1, AutoSize, AutoSize, buttons[2], Fixed)
|
btn3 = CreateButton(frm1, AutoSize, AutoSize, buttons[2], Fixed)
|
||||||
btn3.OnClick(func(ev Event) {
|
btn3.OnClick(func(ev Event) {
|
||||||
dlg.result = DialogButton3
|
dlg.btnResult = DialogButton3
|
||||||
|
dlg.edtResult = ""
|
||||||
|
|
||||||
WindowManager().DestroyWindow(dlg.View)
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
if dlg.onClose != nil {
|
if dlg.onClose != nil {
|
||||||
dlg.onClose()
|
dlg.onClose()
|
||||||
@ -116,7 +161,9 @@ func CreateConfirmationDialog(title, question string, buttons []string, defaultB
|
|||||||
|
|
||||||
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
||||||
|
|
||||||
if defaultButton == DialogButton2 && len(buttons) > 1 {
|
if editbox != nil {
|
||||||
|
ActivateControl(dlg.View, editbox)
|
||||||
|
} else if defaultButton == DialogButton2 && len(buttons) > 1 {
|
||||||
ActivateControl(dlg.View, btn2)
|
ActivateControl(dlg.View, btn2)
|
||||||
} else if defaultButton == DialogButton3 && len(buttons) > 2 {
|
} else if defaultButton == DialogButton3 && len(buttons) > 2 {
|
||||||
ActivateControl(dlg.View, btn3)
|
ActivateControl(dlg.View, btn3)
|
||||||
@ -125,8 +172,8 @@ func CreateConfirmationDialog(title, question string, buttons []string, defaultB
|
|||||||
}
|
}
|
||||||
|
|
||||||
dlg.View.OnClose(func(ev Event) bool {
|
dlg.View.OnClose(func(ev Event) bool {
|
||||||
if dlg.result == DialogAlive {
|
if dlg.btnResult == DialogAlive {
|
||||||
dlg.result = DialogClosed
|
dlg.btnResult = DialogClosed
|
||||||
if ev.X != 1 {
|
if ev.X != 1 {
|
||||||
WindowManager().DestroyWindow(dlg.View)
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
}
|
}
|
||||||
@ -153,7 +200,11 @@ func (d *ConfirmationDialog) OnClose(fn func()) {
|
|||||||
// that means that the dialog is still visible and a
|
// that means that the dialog is still visible and a
|
||||||
// user still does not click any button
|
// user still does not click any button
|
||||||
func (d *ConfirmationDialog) Result() int {
|
func (d *ConfirmationDialog) Result() int {
|
||||||
return d.result
|
return d.btnResult
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ConfirmationDialog) EditResult() string {
|
||||||
|
return d.edtResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------ Selection Dialog ---------------------
|
// ------------------------ Selection Dialog ---------------------
|
||||||
@ -213,7 +264,7 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ Sele
|
|||||||
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
||||||
btn1 := CreateButton(frm1, AutoSize, AutoSize, "OK", Fixed)
|
btn1 := CreateButton(frm1, AutoSize, AutoSize, "OK", Fixed)
|
||||||
btn1.OnClick(func(ev Event) {
|
btn1.OnClick(func(ev Event) {
|
||||||
dlg.result = DialogButton1
|
dlg.btnResult = DialogButton1
|
||||||
if dlg.typ == SelectDialogList {
|
if dlg.typ == SelectDialogList {
|
||||||
dlg.value = dlg.list.SelectedItem()
|
dlg.value = dlg.list.SelectedItem()
|
||||||
} else {
|
} else {
|
||||||
@ -228,7 +279,7 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ Sele
|
|||||||
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
CreateFrame(frm1, 1, 1, BorderNone, 1)
|
||||||
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.btnResult = DialogButton2
|
||||||
dlg.value = -1
|
dlg.value = -1
|
||||||
WindowManager().DestroyWindow(dlg.View)
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
if dlg.onClose != nil {
|
if dlg.onClose != nil {
|
||||||
@ -239,8 +290,8 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ Sele
|
|||||||
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 {
|
||||||
if dlg.result == DialogAlive {
|
if dlg.btnResult == DialogAlive {
|
||||||
dlg.result = DialogClosed
|
dlg.btnResult = DialogClosed
|
||||||
if ev.X != 1 {
|
if ev.X != 1 {
|
||||||
WindowManager().DestroyWindow(dlg.View)
|
WindowManager().DestroyWindow(dlg.View)
|
||||||
}
|
}
|
||||||
@ -268,7 +319,7 @@ func (d *SelectDialog) OnClose(fn func()) {
|
|||||||
// that means that the dialog is still visible and a
|
// that means that the dialog is still visible and a
|
||||||
// user still does not click any button
|
// user still does not click any button
|
||||||
func (d *SelectDialog) Result() int {
|
func (d *SelectDialog) Result() int {
|
||||||
return d.result
|
return d.btnResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the number of the selected item or
|
// Value returns the number of the selected item or
|
||||||
|
Loading…
x
Reference in New Issue
Block a user