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...))
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)

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)
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
}

View file

@ -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