From 74b785e109675a5564a7ce89d17bb1aae7097319 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 31 Jan 2020 01:31:54 +0000 Subject: [PATCH] Timeouts, cancellation (windows). --- cmd/zenity/main.go | 37 ++++++++++++++++--------------------- file_windows.go | 22 ++++++++++++++++++++++ notify_windows.go | 4 ++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index a274567..b4752c0 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -84,13 +84,13 @@ func main() { errResult(zenity.Notify(text, opts...)) case errorDlg: - msgResult(zenity.Error(text, opts...)) + okResult(zenity.Error(text, opts...)) case infoDlg: - msgResult(zenity.Info(text, opts...)) + okResult(zenity.Info(text, opts...)) case warningDlg: - msgResult(zenity.Warning(text, opts...)) + okResult(zenity.Warning(text, opts...)) case questionDlg: - msgResult(zenity.Question(text, opts...)) + okResult(zenity.Question(text, opts...)) case fileSelectionDlg: switch { @@ -253,15 +253,9 @@ func loadFlags() []zenity.Option { } func errResult(err error) { - if err != nil { - os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(zenutil.LineBreak) - os.Exit(-1) + if os.IsTimeout(err) { + os.Exit(5) } - os.Exit(0) -} - -func msgResult(ok bool, err error) { if err == zenity.ErrExtraButton { os.Stdout.WriteString(extraButton) os.Stdout.WriteString(zenutil.LineBreak) @@ -272,6 +266,13 @@ func msgResult(ok bool, err error) { os.Stderr.WriteString(zenutil.LineBreak) os.Exit(-1) } + os.Exit(0) +} + +func okResult(ok bool, err error) { + if err != nil { + errResult(err) + } if ok { os.Exit(0) } @@ -280,9 +281,7 @@ func msgResult(ok bool, err error) { func strResult(s string, err error) { if err != nil { - os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(zenutil.LineBreak) - os.Exit(-1) + errResult(err) } if s == "" { os.Exit(1) @@ -294,9 +293,7 @@ func strResult(s string, err error) { func listResult(l []string, err error) { if err != nil { - os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(zenutil.LineBreak) - os.Exit(-1) + errResult(err) } os.Stdout.WriteString(strings.Join(l, zenutil.Separator)) os.Stdout.WriteString(zenutil.LineBreak) @@ -308,9 +305,7 @@ func listResult(l []string, err error) { func colorResult(c color.Color, err error) { if err != nil { - os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(zenutil.LineBreak) - os.Exit(-1) + errResult(err) } if c == nil { os.Exit(1) diff --git a/file_windows.go b/file_windows.go index b1551e9..16843eb 100644 --- a/file_windows.go +++ b/file_windows.go @@ -245,7 +245,18 @@ func pickFolders(opts options, multi bool) (str string, lst []string, err error) } } + if opts.ctx != nil { + unhook, err := hookDialog(opts.ctx, nil) + if err != nil { + return "", nil, err + } + defer unhook() + } + hr, _, _ = dialog.Call(dialog.vtbl.Show, 0) + if opts.ctx != nil && opts.ctx.Err() != nil { + return "", nil, opts.ctx.Err() + } if hr == 0x800704c7 { // ERROR_CANCELLED return "", nil, nil } @@ -316,7 +327,18 @@ func browseForFolder(opts options) (string, []string, error) { }) } + if opts.ctx != nil { + unhook, err := hookDialog(opts.ctx, nil) + if err != nil { + return "", nil, err + } + defer unhook() + } + ptr, _, _ := shBrowseForFolder.Call(uintptr(unsafe.Pointer(&args))) + if opts.ctx != nil && opts.ctx.Err() != nil { + return "", nil, opts.ctx.Err() + } if ptr == 0 { return "", nil, nil } diff --git a/notify_windows.go b/notify_windows.go index 8ab0b25..fe37067 100644 --- a/notify_windows.go +++ b/notify_windows.go @@ -13,6 +13,10 @@ var ( func notify(text string, options []Option) error { opts := applyOptions(options) + if opts.ctx != nil && opts.ctx.Err() != nil { + return opts.ctx.Err() + } + var args _NOTIFYICONDATA args.StructSize = uint32(unsafe.Sizeof(args)) args.ID = 0x378eb49c // random