Use options struct for dialogs

This commit is contained in:
Lea Anthony
2020-09-27 10:17:30 +10:00
parent 02fd4ec477
commit cd99376da9
7 changed files with 45 additions and 52 deletions

View File

@@ -29,33 +29,6 @@ import "C"
// TODO: move to compile time.
var DEBUG bool = true
// // Config defines how our application should be configured
// type Config struct {
// Title string
// Width int
// Height int
// MinWidth int
// MinHeight int
// MaxWidth int
// MaxHeight int
// DevTools bool
// Resizable bool
// Fullscreen bool
// Frameless bool
// StartHidden bool
// }
// var defaultConfig = &Config{
// Title: "My Wails App",
// Width: 800,
// Height: 600,
// DevTools: true,
// Resizable: true,
// Fullscreen: false,
// Frameless: false,
// StartHidden: false,
// }
// Application is our main application object
type Application struct {
config *options.App

View File

@@ -19,6 +19,7 @@ import (
"unsafe"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
)
// Client is our implentation of messageDispatcher.Client
@@ -123,11 +124,11 @@ func (c *Client) WindowSetColour(colour int) {
}
// OpenDialog will open a dialog with the given title and filter
func (c *Client) OpenDialog(title string, filter string) []string {
func (c *Client) OpenDialog(dialogOptions *options.OpenDialog) []string {
var result []string
cstring := C.OpenDialog(c.app.app, c.app.string2CString(title), c.app.string2CString(filter))
cstring := C.OpenDialog(c.app.app, c.app.string2CString(dialogOptions.Title), c.app.string2CString(dialogOptions.Filter))
if cstring == nil {
return result
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher/message"
"github.com/wailsapp/wails/v2/internal/servicebus"
"github.com/wailsapp/wails/v2/pkg/options"
)
// Client defines what a frontend client can do
@@ -13,7 +14,7 @@ type Client interface {
Quit()
NotifyEvent(message string)
CallResult(message string)
OpenDialog(title string, filter string) []string
OpenDialog(*options.OpenDialog) []string
WindowSetTitle(title string)
WindowShow()
WindowHide()

View File

@@ -10,6 +10,7 @@ import (
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher/message"
"github.com/wailsapp/wails/v2/internal/servicebus"
"github.com/wailsapp/wails/v2/pkg/options"
)
// Dispatcher translates messages received from the frontend
@@ -317,7 +318,6 @@ func (d *Dispatcher) processWindowMessage(result *servicebus.Message) {
// processDialogMessage processes dialog messages
func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
splitTopic := strings.Split(result.Topic(), ":")
if len(splitTopic) < 4 {
d.logger.Error("Invalid dialog message : %#v", result.Data())
return
@@ -327,25 +327,23 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
switch command {
case "select":
dialogType := splitTopic[2]
title := splitTopic[3]
filter := ""
if len(splitTopic) > 4 {
filter = splitTopic[4]
}
switch dialogType {
case "open":
responseTopic, ok := result.Data().(string)
dialogOptions, ok := result.Data().(*options.OpenDialog)
if !ok {
d.logger.Error("Invalid responseTopic for 'dialog:select:open' : %#v", result.Data())
d.logger.Error("Invalid data for 'dialog:select:open' : %#v", result.Data())
return
}
// This is hardcoded in the sender too
responseTopic := "dialog:openselected:" + splitTopic[3]
d.logger.Info("Opening File dialog! responseTopic = %s", responseTopic)
// TODO: Work out what we mean in a multi window environment...
// For now we will just pick the first one
var result []string
for _, client := range d.clients {
result = client.frontend.OpenDialog(title, filter)
result = client.frontend.OpenDialog(dialogOptions)
}
// Send dummy response

View File

@@ -1,15 +1,18 @@
package goruntime
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"github.com/wailsapp/wails/v2/internal/crypto"
"github.com/wailsapp/wails/v2/internal/servicebus"
"github.com/wailsapp/wails/v2/pkg/options"
)
// Dialog defines all Dialog related operations
type Dialog interface {
OpenDialog(params ...string) []string
Open(dialogOptions *options.OpenDialog) []string
}
// dialog exposes the Dialog interface
@@ -41,11 +44,8 @@ func (r *dialog) processTitleAndFilter(params ...string) (string, string) {
return title, filter
}
// OpenDialog prompts the user to select a file
func (r *dialog) OpenDialog(params ...string) []string {
// Extract title + filter
title, filter := r.processTitleAndFilter(params...)
// Open prompts the user to select a file
func (r *dialog) Open(dialogOptions *options.OpenDialog) []string {
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
@@ -57,12 +57,8 @@ func (r *dialog) OpenDialog(params ...string) []string {
fmt.Printf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
// Publish dialog request
message := "dialog:select:open:" + title
if filter != "" {
message += ":" + filter
}
r.bus.Publish(message, responseTopic)
message := "dialog:select:open:" + uniqueCallback
r.bus.Publish(message, dialogOptions)
// Wait for result
var result *servicebus.Message = <-dialogResponseChannel
@@ -72,3 +68,13 @@ func (r *dialog) OpenDialog(params ...string) []string {
return result.Data().([]string)
}
func optionsToBase64(dialogOptions *options.OpenDialog) (string, error) {
encoded, err := json.Marshal(dialogOptions)
if err != nil {
return "", err
}
return b64.StdEncoding.EncodeToString(encoded), nil
}

9
v2/pkg/options/dialog.go Normal file
View File

@@ -0,0 +1,9 @@
package options
// OpenDialog contains the options for the OpenDialog runtime method
type OpenDialog struct {
Title string
Filter string
AllowFiles bool
AllowDirectories bool
}

View File

@@ -4,6 +4,7 @@ import (
"time"
wails "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
)
// RuntimeTest to test the runtimes
@@ -68,7 +69,11 @@ func (r *RuntimeTest) SetColour(colour int) {
// OpenDialog will call the Runtime.Dialog.OpenDirectory method
func (r *RuntimeTest) OpenDialog(title string, filter string) []string {
return r.runtime.Dialog.OpenDialog(title, filter)
dialogOptions := &options.OpenDialog{
Title: title,
Filter: filter,
}
return r.runtime.Dialog.Open(dialogOptions)
}
// HideWindow will call the Runtime.Window.Hide method and then call