From 509c70a97ca6b870c49dbea5c1f71bf995fc6335 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 26 Jun 2021 18:20:24 +1000 Subject: [PATCH] [v2] Dialog fixes --- v2/go.mod | 2 +- v2/go.sum | 4 ++-- .../ffenestri/ffenestri_client_windows.go | 22 +++++-------------- .../messagedispatcher/message/dialog.go | 14 ++---------- v2/internal/runtime/dialog.go | 9 +------- 5 files changed, 12 insertions(+), 39 deletions(-) diff --git a/v2/go.mod b/v2/go.mod index ecd6131a..59706e3d 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -12,7 +12,7 @@ require ( github.com/leaanthony/clir v1.0.4 github.com/leaanthony/debme v1.2.1 github.com/leaanthony/go-ansi-parser v1.0.1 - github.com/leaanthony/go-common-file-dialog v1.0.2 + github.com/leaanthony/go-common-file-dialog v1.0.3 github.com/leaanthony/gosod v1.0.1 github.com/leaanthony/slicer v1.5.0 github.com/leaanthony/webview2runtime v1.1.0 diff --git a/v2/go.sum b/v2/go.sum index a7256917..0a642f06 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -50,8 +50,8 @@ github.com/leaanthony/debme v1.2.1 h1:9Tgwf+kjcrbMQ4WnPcEIUcQuIZYqdWftzZkBr+i/oO github.com/leaanthony/debme v1.2.1/go.mod h1:3V+sCm5tYAgQymvSOfYQ5Xx2JCr+OXiD9Jkw3otUjiA= github.com/leaanthony/go-ansi-parser v1.0.1 h1:97v6c5kYppVsbScf4r/VZdXyQ21KQIfeQOk2DgKxGG4= github.com/leaanthony/go-ansi-parser v1.0.1/go.mod h1:7arTzgVI47srICYhvgUV4CGd063sGEeoSlych5yeSPM= -github.com/leaanthony/go-common-file-dialog v1.0.2 h1:uJ2zXCxP4GwbpR/k/y6unuNbSJn3WTjt4rvHolX3Eys= -github.com/leaanthony/go-common-file-dialog v1.0.2/go.mod h1:TGhEc9eSJgRsupZ+iH1ZgAOnEo9zp05cRH2j08RPrF0= +github.com/leaanthony/go-common-file-dialog v1.0.3 h1:O0uGjKnWtdEADGrkg+TyAAbZylykMwwx/MNEXn9fp+Y= +github.com/leaanthony/go-common-file-dialog v1.0.3/go.mod h1:TGhEc9eSJgRsupZ+iH1ZgAOnEo9zp05cRH2j08RPrF0= github.com/leaanthony/gosod v1.0.1 h1:F+4c3DmEBfigi7oAswCV2RpQ+k4DcNbhuCZUGdBHacQ= github.com/leaanthony/gosod v1.0.1/go.mod h1:W8RyeSFBXu7RpIxPGEJfW4moSyGGEjlJMLV25wEbAdU= github.com/leaanthony/slicer v1.5.0 h1:aHYTN8xbCCLxJmkNKiLB6tgcMARl4eWmH9/F+S/0HtY= diff --git a/v2/internal/ffenestri/ffenestri_client_windows.go b/v2/internal/ffenestri/ffenestri_client_windows.go index 804cc704..5316fe61 100644 --- a/v2/internal/ffenestri/ffenestri_client_windows.go +++ b/v2/internal/ffenestri/ffenestri_client_windows.go @@ -139,10 +139,6 @@ func convertFilters(filters []dialog.FileFilter) []cfd.FileFilter { return result } -func userCancelled(err error) bool { - return err.Error() == "cancelled by user" -} - // OpenFileDialog will open a dialog with the given title and filter func (c *Client) OpenFileDialog(options *dialog.OpenDialog, callbackID string) { config := cfd.DialogConfig{ @@ -162,16 +158,11 @@ func (c *Client) OpenFileDialog(options *dialog.OpenDialog, callbackID string) { } }(thisdialog) result, err := thisdialog.ShowAndGetResult() - if err != nil && !userCancelled(err) { + if err != nil && err != cfd.ErrorCancelled { log.Fatal(err) } - resultJSON, err := json.Marshal([]string{result}) - if err != nil { - log.Fatal(err) - } - dispatcher.DispatchMessage("DO" + callbackID + "|" + string(resultJSON)) - + dispatcher.DispatchMessage("DO" + callbackID + "|" + result) } // OpenDirectoryDialog will open a dialog with the given title and filter @@ -193,11 +184,10 @@ func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackI } }(thisDialog) result, err := thisDialog.ShowAndGetResult() - if err != nil && !userCancelled(err) { + if err != nil && err != cfd.ErrorCancelled { log.Fatal(err) } - resultJSON, err := json.Marshal(result) - dispatcher.DispatchMessage("DD" + callbackID + "|" + string(resultJSON)) + dispatcher.DispatchMessage("DD" + callbackID + "|" + result) } // OpenMultipleFilesDialog will open a dialog with the given title and filter @@ -221,7 +211,7 @@ func (c *Client) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callb } }(thisdialog) result, err := thisdialog.ShowAndGetResults() - if err != nil && !userCancelled(err) { + if err != nil && err != cfd.ErrorCancelled { log.Fatal(err) } resultJSON, err := json.Marshal(result) @@ -249,7 +239,7 @@ func (c *Client) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) log.Fatal(err) } result, err := saveDialog.GetResult() - if err != nil && !userCancelled(err) { + if err != nil && err != cfd.ErrorCancelled { log.Fatal(err) } dispatcher.DispatchMessage("DS" + callbackID + "|" + result) diff --git a/v2/internal/messagedispatcher/message/dialog.go b/v2/internal/messagedispatcher/message/dialog.go index 66643656..5fcc63d3 100644 --- a/v2/internal/messagedispatcher/message/dialog.go +++ b/v2/internal/messagedispatcher/message/dialog.go @@ -32,21 +32,11 @@ func dialogMessageParser(message string) (*parsedMessage, error) { switch dialogType { case 'O': - var data string topic = "dialog:openselected:" + callbackID - err := json.Unmarshal([]byte(payloadData), &data) - if err != nil { - return nil, err - } - responseMessage = &parsedMessage{Topic: topic, Data: data} + responseMessage = &parsedMessage{Topic: topic, Data: payloadData} case 'D': - var data string topic = "dialog:opendirectoryselected:" + callbackID - err := json.Unmarshal([]byte(payloadData), &data) - if err != nil { - return nil, err - } - responseMessage = &parsedMessage{Topic: topic, Data: data} + responseMessage = &parsedMessage{Topic: topic, Data: payloadData} case '*': var data []string topic = "dialog:openmultipleselected:" + callbackID diff --git a/v2/internal/runtime/dialog.go b/v2/internal/runtime/dialog.go index 47bf025b..9141dd6a 100644 --- a/v2/internal/runtime/dialog.go +++ b/v2/internal/runtime/dialog.go @@ -29,7 +29,6 @@ func newDialog(bus *servicebus.ServiceBus) Dialog { } } - // processTitleAndFilter return the title and filter from the given params. // title is the first string, filter is the second func (r *dialog) processTitleAndFilter(params ...string) (string, string) { @@ -94,13 +93,7 @@ func (r *dialog) OpenFile(dialogOptions *dialogoptions.OpenDialog) (string, erro // Delete subscription to response topic r.bus.UnSubscribe(responseTopic) - files := result.Data().([]string) - res := "" - if len(files) > 0 { - res = files[0] - } - - return res, nil + return result.Data().(string), nil } // OpenMultipleFiles prompts the user to select a file