[v2] [broken - WIP] Major refactor of runtime in progress

This commit is contained in:
Lea Anthony
2021-07-18 20:00:01 +10:00
parent 5d2cc81123
commit b80a64b0ee
157 changed files with 493 additions and 13909 deletions
@@ -1,14 +1,13 @@
package main
import (
"context"
"fmt"
"github.com/wailsapp/wails/v2"
)
// App struct
type App struct {
runtime *wails.Runtime
runtime context.Context
}
// NewApp creates a new App application struct
@@ -17,10 +16,12 @@ func NewApp() *App {
}
// startup is called at application startup
func (b *App) startup(runtime *wails.Runtime) {
func (b *App) startup(ctx context.Context) {
// Perform your setup here
b.runtime = runtime
runtime.Window.SetTitle("{{.ProjectName}}")
//TODO: move to new runtime layout
//b.runtime = runtime
//runtime.Window.SetTitle("{{.ProjectName}}")
}
// shutdown is called at application termination
+1 -1
View File
@@ -51,7 +51,7 @@ type App struct {
appconfigStore *runtime.Store
// Startup/Shutdown
startupCallback func(*runtime.Runtime)
startupCallback func(ctx context.Context)
shutdownCallback func()
}
+6 -6
View File
@@ -1,7 +1,7 @@
package bridge
import (
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
)
type BridgeClient struct {
@@ -34,23 +34,23 @@ func (b BridgeClient) CallResult(message string) {
b.session.sendMessage("c" + message)
}
func (b BridgeClient) OpenFileDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (b BridgeClient) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
// Handled by dialog_client
}
func (b BridgeClient) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (b BridgeClient) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
// Handled by dialog_client
}
func (b BridgeClient) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (b BridgeClient) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
// Handled by dialog_client
}
func (b BridgeClient) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) {
func (b BridgeClient) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) {
// Handled by dialog_client
}
func (b BridgeClient) MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string) {
func (b BridgeClient) MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) {
// Handled by dialog_client
}
+12 -12
View File
@@ -11,7 +11,7 @@ import (
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/leaanthony/slicer"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
)
type DialogClient struct {
@@ -37,17 +37,17 @@ func (d *DialogClient) NotifyEvent(message string) {
func (d *DialogClient) CallResult(message string) {
}
func (d *DialogClient) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (d *DialogClient) OpenDirectoryDialog(options dialog.OpenDialogOptions, callbackID string) {
}
func (d *DialogClient) OpenFileDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (d *DialogClient) OpenFileDialog(options dialog.OpenDialogOptions, callbackID string) {
}
func (d *DialogClient) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (d *DialogClient) OpenMultipleFilesDialog(options dialog.OpenDialogOptions, callbackID string) {
}
func (d *DialogClient) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) {
func (d *DialogClient) SaveDialog(options dialog.SaveDialogOptions, callbackID string) {
}
func (d *DialogClient) MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string) {
func (d *DialogClient) MessageDialog(options dialog.MessageDialogOptions, callbackID string) {
osa, err := exec.LookPath("osascript")
if err != nil {
@@ -58,23 +58,23 @@ func (d *DialogClient) MessageDialog(dialogOptions *dialog.MessageDialog, callba
var btns slicer.StringSlicer
defaultButton := ""
cancelButton := ""
for index, btn := range dialogOptions.Buttons {
for index, btn := range options.Buttons {
btns.Add(strconv.Quote(btn))
if btn == dialogOptions.DefaultButton {
if btn == options.DefaultButton {
defaultButton = fmt.Sprintf("default button %d", index+1)
}
if btn == dialogOptions.CancelButton {
if btn == options.CancelButton {
cancelButton = fmt.Sprintf("cancel button %d", index+1)
}
}
buttons := "{" + btns.Join(",") + "}"
script := fmt.Sprintf("display dialog \"%s\" buttons %s %s %s with title \"%s\"", dialogOptions.Message, buttons, defaultButton, cancelButton, dialogOptions.Title)
script := fmt.Sprintf("display dialog \"%s\" buttons %s %s %s with title \"%s\"", options.Message, buttons, defaultButton, cancelButton, options.Title)
go func() {
out, err := exec.Command(osa, "-e", script).Output()
if err != nil {
// Assume user has pressed cancel button
if dialogOptions.CancelButton != "" {
d.dispatcher.DispatchMessage("DM" + callbackID + "|" + dialogOptions.CancelButton)
if options.CancelButton != "" {
d.dispatcher.DispatchMessage("DM" + callbackID + "|" + options.CancelButton)
return
}
d.log.Error("Dialog had bad exit code. If this was a Cancel button, add 'CancelButton' to the dialog.MessageDialog struct. Error: %s", err.Error())
+6 -7
View File
@@ -12,7 +12,7 @@ import (
"strconv"
"strings"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
"github.com/wailsapp/wails/v2/internal/logger"
)
@@ -127,7 +127,7 @@ func (c *Client) WindowSetColour(colour int) {
}
// OpenFileDialog will open a dialog with the given title and filter
func (c *Client) OpenFileDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (c *Client) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
filters := []string{}
if runtime.GOOS == "darwin" {
for _, filter := range dialogOptions.Filters {
@@ -150,9 +150,8 @@ func (c *Client) OpenFileDialog(dialogOptions *dialog.OpenDialog, callbackID str
)
}
// OpenDirectoryDialog will open a dialog with the given title and filter
func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (c *Client) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
filters := []string{}
if runtime.GOOS == "darwin" {
for _, filter := range dialogOptions.Filters {
@@ -166,7 +165,7 @@ func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackI
c.app.string2CString(dialogOptions.DefaultFilename),
c.app.string2CString(dialogOptions.DefaultDirectory),
c.app.bool2Cint(false), // Files
c.app.bool2Cint(true), // Directories
c.app.bool2Cint(true), // Directories
c.app.bool2Cint(false), // Multiple
c.app.bool2Cint(dialogOptions.ShowHiddenFiles),
c.app.bool2Cint(dialogOptions.CanCreateDirectories),
@@ -176,7 +175,7 @@ func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackI
}
// OpenMultipleFilesDialog will open a dialog with the given title and filter
func (c *Client) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (c *Client) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
filters := []string{}
if runtime.GOOS == "darwin" {
for _, filter := range dialogOptions.Filters {
@@ -220,7 +219,7 @@ func (c *Client) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string)
}
// MessageDialog will open a message dialog with the given options
func (c *Client) MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string) {
func (c *Client) MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) {
// Sanity check button length
if len(dialogOptions.Buttons) > 4 {
@@ -15,7 +15,7 @@ import (
"strconv"
"syscall"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
"github.com/wailsapp/wails/v2/internal/logger"
)
@@ -140,7 +140,7 @@ func convertFilters(filters []dialog.FileFilter) []cfd.FileFilter {
}
// OpenFileDialog will open a dialog with the given title and filter
func (c *Client) OpenFileDialog(options *dialog.OpenDialog, callbackID string) {
func (c *Client) OpenFileDialog(options dialog.OpenDialogOptions, callbackID string) {
config := cfd.DialogConfig{
Folder: options.DefaultDirectory,
FileFilters: convertFilters(options.Filters),
@@ -166,7 +166,7 @@ func (c *Client) OpenFileDialog(options *dialog.OpenDialog, callbackID string) {
}
// OpenDirectoryDialog will open a dialog with the given title and filter
func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (c *Client) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
config := cfd.DialogConfig{
Title: dialogOptions.Title,
Role: "PickFolder",
@@ -191,7 +191,7 @@ func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackI
}
// OpenMultipleFilesDialog will open a dialog with the given title and filter
func (c *Client) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (c *Client) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
config := cfd.DialogConfig{
Title: dialogOptions.Title,
Role: "OpenMultipleFiles",
@@ -222,7 +222,7 @@ func (c *Client) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callb
}
// SaveDialog will open a dialog with the given title and filter
func (c *Client) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) {
func (c *Client) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) {
saveDialog, err := cfd.NewSaveFileDialog(cfd.DialogConfig{
Title: dialogOptions.Title,
Role: "SaveFile",
@@ -246,7 +246,7 @@ func (c *Client) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string)
}
// MessageDialog will open a message dialog with the given options
func (c *Client) MessageDialog(options *dialog.MessageDialog, callbackID string) {
func (c *Client) MessageDialog(options dialog.MessageDialogOptions, callbackID string) {
title, err := syscall.UTF16PtrFromString(options.Title)
if err != nil {
@@ -6,7 +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/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
)
// Client defines what a frontend client can do
@@ -14,11 +14,11 @@ type Client interface {
Quit()
NotifyEvent(message string)
CallResult(message string)
OpenFileDialog(dialogOptions *dialog.OpenDialog, callbackID string)
OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callbackID string)
OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackID string)
SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string)
MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string)
OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string)
OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string)
OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string)
SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string)
MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string)
WindowSetTitle(title string)
WindowShow()
WindowHide()
@@ -7,7 +7,7 @@ import (
"strings"
"sync"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
"github.com/wailsapp/wails/v2/internal/crypto"
"github.com/wailsapp/wails/v2/internal/logger"
@@ -411,7 +411,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
dialogType := splitTopic[2]
switch dialogType {
case "open":
dialogOptions, ok := result.Data().(*dialog.OpenDialog)
dialogOptions, ok := result.Data().(dialog.OpenDialogOptions)
if !ok {
d.logger.Error("Invalid data for 'dialog:select:open' : %#v", result.Data())
return
@@ -425,7 +425,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
client.frontend.OpenFileDialog(dialogOptions, callbackID)
}
case "openmultiple":
dialogOptions, ok := result.Data().(*dialog.OpenDialog)
dialogOptions, ok := result.Data().(dialog.OpenDialogOptions)
if !ok {
d.logger.Error("Invalid data for 'dialog:select:openmultiple' : %#v", result.Data())
return
@@ -439,7 +439,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
client.frontend.OpenMultipleFilesDialog(dialogOptions, callbackID)
}
case "directory":
dialogOptions, ok := result.Data().(*dialog.OpenDialog)
dialogOptions, ok := result.Data().(dialog.OpenDialogOptions)
if !ok {
d.logger.Error("Invalid data for 'dialog:select:directory' : %#v", result.Data())
return
@@ -453,7 +453,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
client.frontend.OpenDirectoryDialog(dialogOptions, callbackID)
}
case "save":
dialogOptions, ok := result.Data().(*dialog.SaveDialog)
dialogOptions, ok := result.Data().(dialog.SaveDialogOptions)
if !ok {
d.logger.Error("Invalid data for 'dialog:select:save' : %#v", result.Data())
return
@@ -467,7 +467,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) {
client.frontend.SaveDialog(dialogOptions, callbackID)
}
case "message":
dialogOptions, ok := result.Data().(*dialog.MessageDialog)
dialogOptions, ok := result.Data().(dialog.MessageDialogOptions)
if !ok {
d.logger.Error("Invalid data for 'dialog:select:message' : %#v", result.Data())
return
+170 -170
View File
@@ -1,172 +1,172 @@
package runtime
import (
"fmt"
"github.com/wailsapp/wails/v2/internal/crypto"
"github.com/wailsapp/wails/v2/internal/servicebus"
dialogoptions "github.com/wailsapp/wails/v2/pkg/options/dialog"
)
// Dialog defines all Dialog related operations
type Dialog interface {
OpenFile(dialogOptions *dialogoptions.OpenDialog) (string, error)
OpenMultipleFiles(dialogOptions *dialogoptions.OpenDialog) ([]string, error)
OpenDirectory(dialogOptions *dialogoptions.OpenDialog) (string, error)
SaveFile(dialogOptions *dialogoptions.SaveDialog) (string, error)
Message(dialogOptions *dialogoptions.MessageDialog) (string, error)
}
// dialog exposes the Dialog interface
type dialog struct {
bus *servicebus.ServiceBus
}
// newDialogs creates a new Dialogs struct
func newDialog(bus *servicebus.ServiceBus) Dialog {
return &dialog{
bus: bus,
}
}
// 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) {
var title, filter string
if len(params) > 0 {
title = params[0]
}
if len(params) > 1 {
filter = params[1]
}
return title, filter
}
// OpenDirectory prompts the user to select a directory
func (r *dialog) OpenDirectory(dialogOptions *dialogoptions.OpenDialog) (string, error) {
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:opendirectoryselected:" + uniqueCallback
dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:directory:" + uniqueCallback
r.bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
r.bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
// OpenFile prompts the user to select a file
func (r *dialog) OpenFile(dialogOptions *dialogoptions.OpenDialog) (string, error) {
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:openselected:" + uniqueCallback
dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:open:" + uniqueCallback
r.bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
r.bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
// OpenMultipleFiles prompts the user to select a file
func (r *dialog) OpenMultipleFiles(dialogOptions *dialogoptions.OpenDialog) ([]string, error) {
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:openmultipleselected:" + uniqueCallback
dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
if err != nil {
return nil, fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:openmultiple:" + uniqueCallback
r.bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
r.bus.UnSubscribe(responseTopic)
return result.Data().([]string), nil
}
// SaveFile prompts the user to select a file
func (r *dialog) SaveFile(dialogOptions *dialogoptions.SaveDialog) (string, error) {
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:saveselected:" + uniqueCallback
dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:save:" + uniqueCallback
r.bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
r.bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
// Message show a message to the user
func (r *dialog) Message(dialogOptions *dialogoptions.MessageDialog) (string, error) {
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:messageselected:" + uniqueCallback
dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:message:" + uniqueCallback
r.bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
r.bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
//
//import (
// "fmt"
// "github.com/wailsapp/wails/v2/internal/crypto"
// "github.com/wailsapp/wails/v2/internal/servicebus"
// d "github.com/wailsapp/wails/v2/pkg/runtime/dialog"
//)
//
//// Dialog defines all Dialog related operations
//type Dialog interface {
// OpenFile(options d.OpenDialogOptions) (string, error)
// OpenMultipleFiles(options d.OpenDialogOptions) ([]string, error)
// OpenDirectory(options d.OpenDialogOptions) (string, error)
// SaveFile(options d.SaveDialogOptions) (string, error)
// Message(options d.MessageDialogOptions) (string, error)
//}
//
//// dialog exposes the Dialog interface
//type dialog struct {
// bus *servicebus.ServiceBus
//}
//
//// newDialogs creates a new Dialogs struct
//func newDialog(bus *servicebus.ServiceBus) *dialog {
// return &dialog{
// bus: bus,
// }
//}
//
//// 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) {
//
// var title, filter string
//
// if len(params) > 0 {
// title = params[0]
// }
//
// if len(params) > 1 {
// filter = params[1]
// }
//
// return title, filter
//}
//
//// OpenDirectory prompts the user to select a directory
//func (r *dialog) OpenDirectory(dialogOptions d.OpenDialogOptions) (string, error) {
//
// // Create unique dialog callback
// uniqueCallback := crypto.RandomID()
//
// // Subscribe to the respose channel
// responseTopic := "dialog:opendirectoryselected:" + uniqueCallback
// dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
// if err != nil {
// return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
// }
//
// message := "dialog:select:directory:" + uniqueCallback
// r.bus.Publish(message, dialogOptions)
//
// // Wait for result
// var result = <-dialogResponseChannel
//
// // Delete subscription to response topic
// r.bus.UnSubscribe(responseTopic)
//
// return result.Data().(string), nil
//}
//
//// OpenFile prompts the user to select a file
//func (r *dialog) OpenFile(dialogOptions d.OpenDialogOptions) (string, error) {
//
// // Create unique dialog callback
// uniqueCallback := crypto.RandomID()
//
// // Subscribe to the respose channel
// responseTopic := "dialog:openselected:" + uniqueCallback
// dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
// if err != nil {
// return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
// }
//
// message := "dialog:select:open:" + uniqueCallback
// r.bus.Publish(message, dialogOptions)
//
// // Wait for result
// var result = <-dialogResponseChannel
//
// // Delete subscription to response topic
// r.bus.UnSubscribe(responseTopic)
//
// return result.Data().(string), nil
//}
//
//// OpenMultipleFiles prompts the user to select a file
//func (r *dialog) OpenMultipleFiles(dialogOptions d.OpenDialogOptions) ([]string, error) {
//
// // Create unique dialog callback
// uniqueCallback := crypto.RandomID()
//
// // Subscribe to the respose channel
// responseTopic := "dialog:openmultipleselected:" + uniqueCallback
// dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
// if err != nil {
// return nil, fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
// }
//
// message := "dialog:select:openmultiple:" + uniqueCallback
// r.bus.Publish(message, dialogOptions)
//
// // Wait for result
// var result = <-dialogResponseChannel
//
// // Delete subscription to response topic
// r.bus.UnSubscribe(responseTopic)
//
// return result.Data().([]string), nil
//}
//
//// SaveFile prompts the user to select a file
//func (r *dialog) SaveFile(dialogOptions d.SaveDialogOptions) (string, error) {
//
// // Create unique dialog callback
// uniqueCallback := crypto.RandomID()
//
// // Subscribe to the respose channel
// responseTopic := "dialog:saveselected:" + uniqueCallback
// dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
// if err != nil {
// return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
// }
//
// message := "dialog:select:save:" + uniqueCallback
// r.bus.Publish(message, dialogOptions)
//
// // Wait for result
// var result = <-dialogResponseChannel
//
// // Delete subscription to response topic
// r.bus.UnSubscribe(responseTopic)
//
// return result.Data().(string), nil
//}
//
//// Message show a message to the user
//func (r *dialog) Message(dialogOptions d.MessageDialogOptions) (string, error) {
//
// // Create unique dialog callback
// uniqueCallback := crypto.RandomID()
//
// // Subscribe to the respose channel
// responseTopic := "dialog:messageselected:" + uniqueCallback
// dialogResponseChannel, err := r.bus.Subscribe(responseTopic)
// if err != nil {
// return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
// }
//
// message := "dialog:select:message:" + uniqueCallback
// r.bus.Publish(message, dialogOptions)
//
// // Wait for result
// var result = <-dialogResponseChannel
//
// // Delete subscription to response topic
// r.bus.UnSubscribe(responseTopic)
//
// return result.Data().(string), nil
//}
-2
View File
@@ -9,7 +9,6 @@ type Runtime struct {
Browser Browser
Events Events
Window Window
Dialog Dialog
System System
Menu Menu
Store *StoreProvider
@@ -23,7 +22,6 @@ func New(serviceBus *servicebus.ServiceBus) *Runtime {
Browser: newBrowser(),
Events: newEvents(serviceBus),
Window: newWindow(serviceBus),
Dialog: newDialog(serviceBus),
System: newSystem(serviceBus),
Menu: newMenu(serviceBus),
Log: newLog(serviceBus),
+7 -8
View File
@@ -4,11 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
"strings"
"sync"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher/message"
@@ -135,34 +134,34 @@ func (c *Call) processSystemCall(payload *message.CallMessage, clientID string)
darkModeEnabled := c.runtime.System.IsDarkMode()
c.sendResult(darkModeEnabled, payload, clientID)
case "Dialog.Open":
dialogOptions := new(dialog.OpenDialog)
var dialogOptions dialog.OpenDialogOptions
err := json.Unmarshal(payload.Args[0], dialogOptions)
if err != nil {
c.logger.Error("Error decoding: %s", err)
}
result, err := c.runtime.Dialog.OpenFile(dialogOptions)
result, err := dialog.OpenFile(c.ctx, dialogOptions)
if err != nil {
c.logger.Error("Error: %s", err)
}
c.sendResult(result, payload, clientID)
case "Dialog.Save":
dialogOptions := new(dialog.SaveDialog)
var dialogOptions dialog.SaveDialogOptions
err := json.Unmarshal(payload.Args[0], dialogOptions)
if err != nil {
c.logger.Error("Error decoding: %s", err)
}
result, err := c.runtime.Dialog.SaveFile(dialogOptions)
result, err := dialog.SaveFile(c.ctx, dialogOptions)
if err != nil {
c.logger.Error("Error: %s", err)
}
c.sendResult(result, payload, clientID)
case "Dialog.Message":
dialogOptions := new(dialog.MessageDialog)
var dialogOptions dialog.MessageDialogOptions
err := json.Unmarshal(payload.Args[0], dialogOptions)
if err != nil {
c.logger.Error("Error decoding: %s", err)
}
result, err := c.runtime.Dialog.Message(dialogOptions)
result, err := dialog.Message(c.ctx, dialogOptions)
if err != nil {
c.logger.Error("Error: %s", err)
}
+4 -5
View File
@@ -17,7 +17,7 @@ type Runtime struct {
// The hooks channel allows us to hook into frontend startup
hooksChannel <-chan *servicebus.Message
startupCallback func(*runtime.Runtime)
startupCallback func(ctx context.Context)
shutdownCallback func()
// quit flag
@@ -39,7 +39,7 @@ type Runtime struct {
}
// NewRuntime creates a new runtime subsystem
func NewRuntime(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.Logger, startupCallback func(*runtime.Runtime)) (*Runtime, error) {
func NewRuntime(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.Logger, startupCallback func(context.Context)) (*Runtime, error) {
// Subscribe to log messages
runtimeChannel, err := bus.Subscribe("runtime:")
@@ -59,9 +59,9 @@ func NewRuntime(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.
logger: logger.CustomLogger("Runtime Subsystem"),
runtime: runtime.New(bus),
startupCallback: startupCallback,
ctx: ctx,
bus: bus,
}
result.ctx = context.WithValue(ctx, "bus", bus)
return result, nil
}
@@ -83,8 +83,7 @@ func (r *Runtime) Start() error {
if r.startupCallback != nil {
r.startupOnce.Do(func() {
go func() {
r.startupCallback(r.runtime)
r.startupCallback(r.ctx)
// If we got a url, publish it now startup completed
url, ok := hooksMessage.Data().(string)
if ok && len(url) > 0 {
+24 -4
View File
@@ -3,7 +3,7 @@ package webserver
import (
"context"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
"net/http"
"strings"
@@ -20,6 +20,18 @@ type WebClient struct {
running bool
}
func (wc *WebClient) WindowSetMinSize(width int, height int) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) WindowSetMaxSize(width int, height int) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) DeleteTrayMenuByID(id string) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) SetTrayMenu(trayMenuJSON string) {
wc.logger.Info("Not implemented in server build")
}
@@ -28,7 +40,7 @@ func (wc *WebClient) UpdateTrayMenuLabel(trayMenuJSON string) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string) {
func (wc *WebClient) MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) {
wc.logger.Info("Not implemented in server build")
}
@@ -44,11 +56,19 @@ func (wc *WebClient) UpdateContextMenu(contextMenuJSON string) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) OpenDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
func (wc *WebClient) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) {
func (wc *WebClient) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) {
wc.logger.Info("Not implemented in server build")
}
func (wc *WebClient) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) {
wc.logger.Info("Not implemented in server build")
}
-52
View File
@@ -1,52 +0,0 @@
package dialog
// FileFilter defines a filter for dialog boxes
type FileFilter struct {
DisplayName string // Filter information EG: "Image Files (*.jpg, *.png)"
Pattern string // semi-colon separated list of extensions, EG: "*.jpg;*.png"
}
// OpenDialog contains the options for the OpenDialog runtime method
type OpenDialog struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
AllowFiles bool
AllowDirectories bool
ShowHiddenFiles bool
CanCreateDirectories bool
ResolvesAliases bool
TreatPackagesAsDirectories bool
}
// SaveDialog contains the options for the SaveDialog runtime method
type SaveDialog struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
ShowHiddenFiles bool
CanCreateDirectories bool
TreatPackagesAsDirectories bool
}
type DialogType string
const (
InfoDialog DialogType = "info"
WarningDialog DialogType = "warning"
ErrorDialog DialogType = "error"
QuestionDialog DialogType = "question"
)
// MessageDialog contains the options for the Message dialogs, EG Info, Warning, etc runtime methods
type MessageDialog struct {
Type DialogType
Title string
Message string
Buttons []string
DefaultButton string
CancelButton string
Icon string
}
+3 -3
View File
@@ -1,12 +1,12 @@
package options
import (
"context"
"log"
"runtime"
"github.com/wailsapp/wails/v2/pkg/options/windows"
wailsruntime "github.com/wailsapp/wails/v2/internal/runtime"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/imdario/mergo"
@@ -37,8 +37,8 @@ type App struct {
Mac *mac.Options
Logger logger.Logger `json:"-"`
LogLevel logger.LogLevel
Startup func(*wailsruntime.Runtime) `json:"-"`
Shutdown func() `json:"-"`
Startup func(ctx context.Context) `json:"-"`
Shutdown func() `json:"-"`
Bind []interface{}
}
+235
View File
@@ -0,0 +1,235 @@
package dialog
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/wailsapp/wails/v2/internal/crypto"
"github.com/wailsapp/wails/v2/internal/servicebus"
)
// FileFilter defines a filter for dialog boxes
type FileFilter struct {
DisplayName string // Filter information EG: "Image Files (*.jpg, *.png)"
Pattern string // semi-colon separated list of extensions, EG: "*.jpg;*.png"
}
// OpenDialogOptions contains the options for the OpenDialogOptions runtime method
type OpenDialogOptions struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
AllowFiles bool
AllowDirectories bool
ShowHiddenFiles bool
CanCreateDirectories bool
ResolvesAliases bool
TreatPackagesAsDirectories bool
}
// SaveDialogOptions contains the options for the SaveDialog runtime method
type SaveDialogOptions struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
ShowHiddenFiles bool
CanCreateDirectories bool
TreatPackagesAsDirectories bool
}
type DialogType string
const (
InfoDialog DialogType = "info"
WarningDialog DialogType = "warning"
ErrorDialog DialogType = "error"
QuestionDialog DialogType = "question"
)
// MessageDialogOptions contains the options for the Message dialogs, EG Info, Warning, etc runtime methods
type MessageDialogOptions struct {
Type DialogType
Title string
Message string
Buttons []string
DefaultButton string
CancelButton string
Icon string
}
func extractBus(ctx context.Context) (*servicebus.ServiceBus, error) {
bus := ctx.Value("bus")
if bus == nil {
return nil, errors.New("wails runtime has not been initialised correctly")
}
return bus.(*servicebus.ServiceBus), nil
}
// processTitleAndFilter return the title and filter from the given params.
// title is the first string, filter is the second
func processTitleAndFilter(params ...string) (string, string) {
var title, filter string
if len(params) > 0 {
title = params[0]
}
if len(params) > 1 {
filter = params[1]
}
return title, filter
}
// OpenDirectory prompts the user to select a directory
func OpenDirectory(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
bus, err := extractBus(ctx)
if err != nil {
return "", errors.Wrap(err, "OpenDirectory")
}
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:opendirectoryselected:" + uniqueCallback
dialogResponseChannel, err := bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:directory:" + uniqueCallback
bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
// OpenFile prompts the user to select a file
func OpenFile(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
bus, err := extractBus(ctx)
if err != nil {
return "", errors.Wrap(err, "OpenFile")
}
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:openselected:" + uniqueCallback
dialogResponseChannel, err := bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:open:" + uniqueCallback
bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
// OpenMultipleFiles prompts the user to select a file
func OpenMultipleFiles(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error) {
bus, err := extractBus(ctx)
if err != nil {
return nil, errors.Wrap(err, "OpenMultipleFiles")
}
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:openmultipleselected:" + uniqueCallback
dialogResponseChannel, err := bus.Subscribe(responseTopic)
if err != nil {
return nil, fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:openmultiple:" + uniqueCallback
bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
bus.UnSubscribe(responseTopic)
return result.Data().([]string), nil
}
// SaveFile prompts the user to select a file
func SaveFile(ctx context.Context, dialogOptions SaveDialogOptions) (string, error) {
bus, err := extractBus(ctx)
if err != nil {
return "", errors.Wrap(err, "SaveFile")
}
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:saveselected:" + uniqueCallback
dialogResponseChannel, err := bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:save:" + uniqueCallback
bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
// Message show a message to the user
func Message(ctx context.Context, dialogOptions MessageDialogOptions) (string, error) {
bus, err := extractBus(ctx)
if err != nil {
return "", errors.Wrap(err, "Message")
}
// Create unique dialog callback
uniqueCallback := crypto.RandomID()
// Subscribe to the respose channel
responseTopic := "dialog:messageselected:" + uniqueCallback
dialogResponseChannel, err := bus.Subscribe(responseTopic)
if err != nil {
return "", fmt.Errorf("ERROR: Cannot subscribe to bus topic: %+v\n", err.Error())
}
message := "dialog:select:message:" + uniqueCallback
bus.Publish(message, dialogOptions)
// Wait for result
var result = <-dialogResponseChannel
// Delete subscription to response topic
bus.UnSubscribe(responseTopic)
return result.Data().(string), nil
}
-35
View File
@@ -1,35 +0,0 @@
package main
import (
"fmt"
"github.com/wailsapp/wails/v2"
)
// Basic application struct
type Basic struct {
runtime *wails.Runtime
}
// newBasic creates a new Basic application struct
func newBasic() *Basic {
return &Basic{}
}
// WailsInit is called at application startup
func (b *Basic) WailsInit(runtime *wails.Runtime) error {
// Perform your setup here
b.runtime = runtime
runtime.Window.SetTitle("minmax")
return nil
}
// WailsShutdown is called at application termination
func (b *Basic) WailsShutdown() {
// Perform your teardown here
}
// Greet returns a greeting for the given name
func (b *Basic) Greet(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
@@ -1,18 +0,0 @@
<html>
<head>
<link rel="stylesheet" href="/main.css">
</head>
<body>
<div id="logo"></div>
<div id="input">
<input id="name" type="text"></input>
<button onclick="greet()">Greet</button>
</div>
<div id="result"></div>
<script src="/main.js"></script>
</body>
</html>
File diff suppressed because one or more lines are too long
-16
View File
@@ -1,16 +0,0 @@
// Get input + focus
var nameElement = document.getElementById("name");
nameElement.focus();
// Stup the greet function
window.greet = function () {
// Get name
var name = nameElement.value;
// Call Basic.Greet(name)
window.backend.main.Basic.Greet(name).then((result) => {
// Update result with data back from Basic.Greet()
document.getElementById("result").innerText = result;
});
}

Some files were not shown because too many files have changed in this diff Show More