2014-02-16 14:55:51 -06:00
|
|
|
// +build !windows,!darwin,!plan9
|
|
|
|
|
|
|
|
// 16 february 2014
|
2014-02-19 10:41:10 -06:00
|
|
|
package ui
|
2014-02-16 14:55:51 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var uitask chan func()
|
|
|
|
|
2014-03-01 14:18:29 -06:00
|
|
|
func ui(main func()) error {
|
2014-02-16 14:55:51 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
uitask = make(chan func())
|
|
|
|
if gtk_init() != true {
|
2014-03-01 14:18:29 -06:00
|
|
|
return fmt.Errorf("gtk_init failed (reason unknown; TODO)")
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 16:09:58 -06:00
|
|
|
// thanks to tristan in irc.gimp.net/#gtk
|
|
|
|
gdk_threads_add_idle(func() bool {
|
|
|
|
select {
|
|
|
|
case f := <-uitask:
|
|
|
|
f()
|
|
|
|
default: // do not block
|
|
|
|
}
|
|
|
|
return true // don't destroy the callback
|
|
|
|
})
|
2014-03-01 14:18:29 -06:00
|
|
|
|
2014-03-05 12:25:19 -06:00
|
|
|
go func() {
|
|
|
|
main()
|
|
|
|
uitask <- gtk_main_quit
|
|
|
|
}()
|
2014-03-01 14:18:29 -06:00
|
|
|
|
2014-02-16 14:55:51 -06:00
|
|
|
gtk_main()
|
2014-03-01 14:18:29 -06:00
|
|
|
return nil
|
2014-02-16 14:55:51 -06:00
|
|
|
}
|