Skip to content

Commit

Permalink
Merge pull request #6 from harry1453/cancel-detect
Browse files Browse the repository at this point in the history
Constant error for when user cancels
  • Loading branch information
harryjph committed Jun 22, 2021
2 parents 2383712 + 67c2bfa commit 5bfd608
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 17 deletions.
6 changes: 4 additions & 2 deletions _examples/notusingutil/OpenFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ func main() {
go func() {
time.Sleep(2 * time.Second)
if err := openDialog.SetFileName("hello world"); err != nil {
panic(err)
log.Fatal(err)
}
}()
if err := openDialog.Show(); err != nil {
log.Fatal(err)
}
result, err := openDialog.GetResult()
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen file: %s\n", result)
Expand Down
4 changes: 3 additions & 1 deletion _examples/notusingutil/OpenMultipleFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func main() {
log.Fatal(err)
}
results, err := openMultiDialog.GetResults()
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen file(s): %s\n", results)
Expand Down
4 changes: 3 additions & 1 deletion _examples/notusingutil/PickFolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func main() {
log.Fatal(err)
}
result, err := pickFolderDialog.GetResult()
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen folder: %s\n", result)
Expand Down
4 changes: 3 additions & 1 deletion _examples/notusingutil/SaveFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func main() {
log.Fatal(err)
}
result, err := saveDialog.GetResult()
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen file: %s\n", result)
Expand Down
4 changes: 3 additions & 1 deletion _examples/usingutil/OpenFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func main() {
FileName: "file.txt",
DefaultExtension: "txt",
})
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen file: %s\n", result)
Expand Down
4 changes: 3 additions & 1 deletion _examples/usingutil/OpenMultipleFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func main() {
FileName: "file.txt",
DefaultExtension: "txt",
})
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen file(s): %s\n", results)
Expand Down
4 changes: 3 additions & 1 deletion _examples/usingutil/PickFolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ func main() {
Role: "PickFolderExample",
Folder: "C:\\",
})
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen folder: %s\n", result)
Expand Down
4 changes: 3 additions & 1 deletion _examples/usingutil/SaveFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func main() {
FileName: "image.jpg",
DefaultExtension: "jpg",
})
if err != nil {
if err == cfd.ErrorCancelled {
log.Fatal("Dialog was cancelled by the user.")
} else if err != nil {
log.Fatal(err)
}
log.Printf("Chosen file: %s\n", result)
Expand Down
7 changes: 7 additions & 0 deletions cfd/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cfd

import "errors"

var (
ErrorCancelled = errors.New("cancelled by user")
)
26 changes: 21 additions & 5 deletions cfd/iFileOpenDialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package cfd

import (
"errors"
"fmt"
"github.com/go-ole/go-ole"
"github.com/harry1453/go-common-file-dialog/util"
"syscall"
Expand Down Expand Up @@ -50,7 +48,8 @@ func (fileOpenDialog *iFileOpenDialog) ShowAndGetResult() (string, error) {
return "", err
}
if isMultiselect {
return "", errors.New("use ShowAndGetResults for open multiple files dialog") // TODO don't repeat usages of errors.New
// We should panic as this error is caused by the developer using the library
panic("use ShowAndGetResults for open multiple files dialog")
}
if err := fileOpenDialog.Show(); err != nil {
return "", err
Expand All @@ -59,6 +58,14 @@ func (fileOpenDialog *iFileOpenDialog) ShowAndGetResult() (string, error) {
}

func (fileOpenDialog *iFileOpenDialog) ShowAndGetResults() ([]string, error) {
isMultiselect, err := fileOpenDialog.isMultiselect()
if err != nil {
return nil, err
}
if !isMultiselect {
// We should panic as this error is caused by the developer using the library
panic("use ShowAndGetResult for open single file dialog")
}
if err := fileOpenDialog.Show(); err != nil {
return nil, err
}
Expand All @@ -75,7 +82,8 @@ func (fileOpenDialog *iFileOpenDialog) GetResult() (string, error) {
return "", err
}
if isMultiselect {
return "", errors.New("use GetResults for open multiple files dialog")
// We should panic as this error is caused by the developer using the library
panic("use GetResults for open multiple files dialog")
}
return fileOpenDialog.vtbl.getResultString(unsafe.Pointer(fileOpenDialog))
}
Expand Down Expand Up @@ -103,6 +111,14 @@ func (fileOpenDialog *iFileOpenDialog) SetRole(role string) error {
// This should only be callable when the user asks for a multi select because
// otherwise they will be given the Dialog interface which does not expose this function.
func (fileOpenDialog *iFileOpenDialog) GetResults() ([]string, error) {
isMultiselect, err := fileOpenDialog.isMultiselect()
if err != nil {
return nil, err
}
if !isMultiselect {
// We should panic as this error is caused by the developer using the library
panic("use GetResult for open single file dialog")
}
return fileOpenDialog.vtbl.getResultsStrings(unsafe.Pointer(fileOpenDialog))
}

Expand Down Expand Up @@ -161,7 +177,7 @@ func (vtbl *iFileOpenDialogVtbl) getResultsStrings(objPtr unsafe.Pointer) ([]str
return nil, err
}
if shellItemArray == nil {
return nil, fmt.Errorf("cancelled by user")
return nil, ErrorCancelled
}
defer shellItemArray.vtbl.release(unsafe.Pointer(shellItemArray))
count, err := shellItemArray.vtbl.getCount(unsafe.Pointer(shellItemArray))
Expand Down
3 changes: 1 addition & 2 deletions cfd/iShellItemArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package cfd

import (
"fmt"
"github.com/go-ole/go-ole"
"syscall"
"unsafe"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (vtbl *iShellItemArrayVtbl) getItemAt(objPtr unsafe.Pointer, index uintptr)
return "", err
}
if shellItem == nil {
return "", fmt.Errorf("cancelled by user")
return "", ErrorCancelled
}
defer shellItem.vtbl.release(unsafe.Pointer(shellItem))
return shellItem.vtbl.getDisplayName(unsafe.Pointer(shellItem))
Expand Down
2 changes: 1 addition & 1 deletion cfd/vtblCommonFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (vtbl *iFileDialogVtbl) getResultString(objPtr unsafe.Pointer) (string, err
return "", err
}
if shellItem == nil {
return "", fmt.Errorf("cancelled by user")
return "", ErrorCancelled
}
defer shellItem.vtbl.release(unsafe.Pointer(shellItem))
return shellItem.vtbl.getDisplayName(unsafe.Pointer(shellItem))
Expand Down

0 comments on commit 5bfd608

Please sign in to comment.