I am trying to write a yes/no dialog box in Go using andlabs/ui which does not seem to have this type of GUI component:
package main
import "github.com/andlabs/ui"
func confirm(msg string) bool {
var confirmWindow = ui.NewWindow("Please confirm", 300, 100, false)
var vbox = ui.NewVerticalBox()
vbox.Append(ui.NewLabel(msg), false)
var yesButton = ui.NewButton("Yes")
yesButton.OnClicked(func (*ui.Button) {
return true
})
var noButton = ui.NewButton("No")
noButton.OnClicked(func (*ui.Button) {
return false // <<< this should return out of the confirm function.
})
var hbox = ui.NewHorizontalBox()
hbox.Append(yesButton, false)
hbox.Append(noButton, false)
vbox.Append(hbox, false)
confirmWindow.SetChild(vbox)
confirmWindow.Show()
}
func main(){
var ret = confirm("Proceed?")
if ret {
println("Yes returned.")
} else {
println("No returned.")
}
}
However, in above I am not able to return true or false depending on whether yes or no button is clicked.
How can I have true/false returned from confirm function when yes/no button is clicked?
Edit: Following suggestion from answer by @Mikhail , I tried following code:
package main
import ("github.com/andlabs/ui")
func confirm(msg string) chan bool {
var confirmWindow = ui.NewWindow("Please confirm", 300, 100, false)
var vbox = ui.NewVerticalBox()
vbox.Append(ui.NewLabel(msg), false)
var yesButton = ui.NewButton("Yes")
ret := make(chan bool)
yesButton.OnClicked(func (*ui.Button){
ret <- true
})
var noButton = ui.NewButton("No")
noButton.OnClicked(func (*ui.Button){
ret <- false
})
var hbox = ui.NewHorizontalBox()
hbox.Append(yesButton, false)
hbox.Append(noButton, false)
vbox.Append(hbox, false)
confirmWindow.SetChild(vbox)
confirmWindow.Show()
confirmWindow.OnClosing(func(*ui.Window)bool{
ui.Quit(); return true
})
return ret
}
func main(){
c := make(chan bool)
c <- confirm("Proceed?")
if c {println("Yes")} else {println("No")}
}
However, it also does not work. The error is:
# command-line-arguments
./rnconfirm.go:30:4: cannot use confirm("Proceed?") (type chan bool) as type bool in send
./rnconfirm.go:31:2: non-bool c (type chan bool) used as if condition
Using following main
function also does not work:
func main(){
println(confirm("Proceed"))
}
Error is:
(process:8577): GLib-CRITICAL **: g_ptr_array_add: assertion 'rarray' failed
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fdeb996ae19]
runtime stack:
runtime.throw(0x4bd337, 0x2a)
/usr/local/go/src/runtime/panic.go:616 +0x81
runtime.sigpanic()
/usr/local/go/src/runtime/signal_unix.go:372 +0x28e
goroutine 1 [syscall, locked to thread]:
...
...
Second attempt with channels is also not working:
package main
import ("github.com/andlabs/ui")
func confirm(msg string, ret chan bool) {
var confirmWindow = ui.NewWindow("Please confirm", 300, 100, false)
var vbox = ui.NewVerticalBox()
vbox.Append(ui.NewLabel(msg), false)
var yesButton = ui.NewButton("Yes")
yesButton.OnClicked(func (*ui.Button){
ret <- true
})
var noButton = ui.NewButton("No")
noButton.OnClicked(func (*ui.Button){
ret <- false
})
var hbox = ui.NewHorizontalBox()
hbox.Append(yesButton, false)
hbox.Append(noButton, false)
vbox.Append(hbox, false)
confirmWindow.SetChild(vbox)
confirmWindow.Show()
confirmWindow.OnClosing(func(*ui.Window)bool{
ui.Quit(); return true
})
}
func main(){
c := make(chan bool)
go confirm("Proceed?", c)
ret := <- c
if ret {println("Yes")} else {println("No")}
}
It compiles and runs but crashes with error:
(process:2407): GLib-CRITICAL **: g_ptr_array_add: assertion 'rarray' failed
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f47561e5e19]