Timeouts, cancellation (windows).

This commit is contained in:
Nuno Cruces 2020-01-31 01:31:54 +00:00
parent 975c82db2a
commit 74b785e109
3 changed files with 42 additions and 21 deletions

View file

@ -84,13 +84,13 @@ func main() {
errResult(zenity.Notify(text, opts...)) errResult(zenity.Notify(text, opts...))
case errorDlg: case errorDlg:
msgResult(zenity.Error(text, opts...)) okResult(zenity.Error(text, opts...))
case infoDlg: case infoDlg:
msgResult(zenity.Info(text, opts...)) okResult(zenity.Info(text, opts...))
case warningDlg: case warningDlg:
msgResult(zenity.Warning(text, opts...)) okResult(zenity.Warning(text, opts...))
case questionDlg: case questionDlg:
msgResult(zenity.Question(text, opts...)) okResult(zenity.Question(text, opts...))
case fileSelectionDlg: case fileSelectionDlg:
switch { switch {
@ -253,15 +253,9 @@ func loadFlags() []zenity.Option {
} }
func errResult(err error) { func errResult(err error) {
if err != nil { if os.IsTimeout(err) {
os.Stderr.WriteString(err.Error()) os.Exit(5)
os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1)
} }
os.Exit(0)
}
func msgResult(ok bool, err error) {
if err == zenity.ErrExtraButton { if err == zenity.ErrExtraButton {
os.Stdout.WriteString(extraButton) os.Stdout.WriteString(extraButton)
os.Stdout.WriteString(zenutil.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
@ -272,6 +266,13 @@ func msgResult(ok bool, err error) {
os.Stderr.WriteString(zenutil.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1) os.Exit(-1)
} }
os.Exit(0)
}
func okResult(ok bool, err error) {
if err != nil {
errResult(err)
}
if ok { if ok {
os.Exit(0) os.Exit(0)
} }
@ -280,9 +281,7 @@ func msgResult(ok bool, err error) {
func strResult(s string, err error) { func strResult(s string, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) errResult(err)
os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1)
} }
if s == "" { if s == "" {
os.Exit(1) os.Exit(1)
@ -294,9 +293,7 @@ func strResult(s string, err error) {
func listResult(l []string, err error) { func listResult(l []string, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) errResult(err)
os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1)
} }
os.Stdout.WriteString(strings.Join(l, zenutil.Separator)) os.Stdout.WriteString(strings.Join(l, zenutil.Separator))
os.Stdout.WriteString(zenutil.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
@ -308,9 +305,7 @@ func listResult(l []string, err error) {
func colorResult(c color.Color, err error) { func colorResult(c color.Color, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) errResult(err)
os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1)
} }
if c == nil { if c == nil {
os.Exit(1) os.Exit(1)

View file

@ -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) 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 if hr == 0x800704c7 { // ERROR_CANCELLED
return "", nil, nil 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))) ptr, _, _ := shBrowseForFolder.Call(uintptr(unsafe.Pointer(&args)))
if opts.ctx != nil && opts.ctx.Err() != nil {
return "", nil, opts.ctx.Err()
}
if ptr == 0 { if ptr == 0 {
return "", nil, nil return "", nil, nil
} }

View file

@ -13,6 +13,10 @@ var (
func notify(text string, options []Option) error { func notify(text string, options []Option) error {
opts := applyOptions(options) opts := applyOptions(options)
if opts.ctx != nil && opts.ctx.Err() != nil {
return opts.ctx.Err()
}
var args _NOTIFYICONDATA var args _NOTIFYICONDATA
args.StructSize = uint32(unsafe.Sizeof(args)) args.StructSize = uint32(unsafe.Sizeof(args))
args.ID = 0x378eb49c // random args.ID = 0x378eb49c // random