Decided to make dialogs code-modal; will figure out how to get the behavior I want on Mac OS X - http://stackoverflow.com/questions/24468620/how-do-i-create-a-nested-run-loop-after-nsalert-beginsheetmodalforwindow-tha - Now to see if Windows works...

This commit is contained in:
Pietro Gagliardi 2014-06-28 16:02:50 -04:00
parent cd116033a3
commit 02d6a03ba3
4 changed files with 16 additions and 43 deletions

View File

@ -2,12 +2,6 @@
package ui
// Dialog is an interface adopted by all dialogs.
type Dialog struct {
Response() Response // response code from the dialog
Selection() interface{} // currently nil
}
// Response denotes a response from the user to a Dialog.
type Response uint
const (
@ -15,14 +9,6 @@ const (
OK
)
// basic return
type dialogret struct {
res Response
sel interface{}
}
func (d *dialogret) Response() Response { return d.res }
func (d *dialogret) Selection() interface{} { return d.sel }
// sentinel (not nil so programmer errors don't go undetected)
// this window is invalid and cannot be used directly
// notice the support it uses
@ -31,11 +17,6 @@ var dialogWindow = &Window{
winhandler: dh,
}
}
type dhandler chan *dialogret
func (d dhandler) Event(e Event, dat interface{}) {
d <- dat.(*dialogret)
}
var dh = make(dhandler)
// MsgBox displays an informational message box to the user with just an OK button.
// primaryText should be a short string describing the message, and will be displayed with additional emphasis on platforms that support it.
@ -44,31 +25,32 @@ var dh = make(dhandler)
// On platforms that allow for the message box window to have a title, os.Args[0] is used.
//
// See "On Dialogs" in the package overview for behavioral information.
func MsgBox(primaryText string, secondaryText string) Response {
func MsgBox(primaryText string, secondaryText string) {
dialogWindow.msgBox(primaryText, secondaryText)
return (<-dh).res
}
// MsgBox is the Window method version of the package-scope function MsgBox.
// See that function's documentation and "On Dialogs" in the package overview for more information.
func (w *Window) MsgBox(primaryText string, secondaryText string) Dialog {
func (w *Window) MsgBox(primaryText string, secondaryText string) {
if !w.created {
panic("parent window passed to Window.MsgBox() before it was created")
}
return w.msgBox(primaryText, secondaryText)
w.msgBox(primaryText, secondaryText)
}
// MsgBoxError displays a message box to the user with just an OK button and an icon indicating an error.
// Otherwise, it behaves like MsgBox.
//
// See "On Dialogs" in the package overview for more information.
func MsgBoxError(primaryText string, secondaryText string) Dialog {
func MsgBoxError(primaryText string, secondaryText string) {
dialogWindow.msgBoxError(primaryText, secondaryText)
return (<-dh).res
}
// MsgBoxError is the Window method version of the package-scope function MsgBoxError.
// See that function's documentation and "On Dialogs" in the package overview for more information.
func (w *Window) MsgBoxError(primaryText string, secondaryText string) Dialog {
return w.msgBoxError(primaryText, secondaryText)
func (w *Window) MsgBoxError(primaryText string, secondaryText string) {
if !w.created {
panic("parent window passed to Window.MsgBox() before it was created")
}
w.msgBoxError(primaryText, secondaryText)
}

View File

@ -38,20 +38,13 @@ func _msgBox(parent *Window, primarytext string, secondarytext string, uType uin
if r1 == 0 { // failure
panic(fmt.Sprintf("error displaying message box to user: %v\nstyle: 0x%08X\ntitle: %q\ntext:\n%s", err, uType, os.Args[0], text))
}
w.sysData.winhandler.Event(Dismissed, &dialogret{
res: dialogResponses[r1],
})
return dialogResponses[r1]
}
func (w *Window) msgBox(primarytext string, secondarytext string) {
// send to uitask so the function can return immediately
touitask(func() {
_msgBox(w, primarytext, secondarytext, _MB_OK)
})
_msgBox(w, primarytext, secondarytext, _MB_OK)
}
func (w *Window) msgBoxError(primarytext string, secondarytext string) {
touitask(func() {
_msgBox(w, primarytext, secondarytext, _MB_OK|_MB_ICONERROR)
})
_msgBox(w, primarytext, secondarytext, _MB_OK|_MB_ICONERROR)
}

View File

@ -559,14 +559,13 @@ func (handler *dialoghandler) Event(e Event, d Data) {
if e == Clicked {
switch d {
case handler.bMsgBox:
// dialog_sret = handler.w.MsgBox("Message Box", "Dismiss")
// handler.send(CustomEvent, "DIALOG")
handler.w.MsgBox("Message Box", "Dismiss")
// handler.send(CustomEvent, nil)
case handler.bMsgBoxError:
// dialog_sret = handler.w.MsgBoxError("Message Box", "Dismiss")
// handler.send(CustomEvent, "DIALOG")
handler.w.MsgBoxError("Message Box", "Dismiss")
// handler.send(CustomEvent, nil)
// case <-dialog_sret:
// dialog_sret = nil
// handler.send(CustomEvent + 1, nil)
case handler.bCenter:
handler.w.Center()
}

View File

@ -37,7 +37,6 @@ type Event int
const (
Closing Event = iota // Window close
Clicked // Button click
Dismissed // Dialog closed
CustomEvent = 5000 // very high number; higher than the package would ever need, anyway
)