From 02d6a03ba3a657aa7b50d072771361236bb207a3 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 28 Jun 2014 16:02:50 -0400 Subject: [PATCH] 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... --- dialog.go | 36 +++++++++--------------------------- dialog_windows.go | 13 +++---------- test/main.go | 9 ++++----- window.go | 1 - 4 files changed, 16 insertions(+), 43 deletions(-) diff --git a/dialog.go b/dialog.go index a275a64..e676127 100644 --- a/dialog.go +++ b/dialog.go @@ -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) } diff --git a/dialog_windows.go b/dialog_windows.go index 5422f1c..18de661 100644 --- a/dialog_windows.go +++ b/dialog_windows.go @@ -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) } diff --git a/test/main.go b/test/main.go index 93ee5fc..7d3ddd9 100644 --- a/test/main.go +++ b/test/main.go @@ -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() } diff --git a/window.go b/window.go index 8d0e2f4..b348ae8 100644 --- a/window.go +++ b/window.go @@ -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 )