From 33a27b06a68c6a5d69a2c809a6a3782b2e8318ef Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 31 Dec 2020 14:52:45 +1100 Subject: [PATCH] Add Message dialog to Kitchen Sink --- v2/internal/ffenestri/ffenestri_client.go | 8 +- v2/internal/runtime/js/core/dialog.js | 3 +- v2/internal/runtime/js/runtime/dialog.js | 2 +- v2/test/kitchensink/dialog.go | 14 -- .../pages/{browser => Browser}/Browser.svelte | 4 +- .../Browser/Browser.svelte | 2 +- .../{browser => Browser}/Browser/code.go | 0 .../{browser => Browser}/Browser/code.jsx | 0 .../frontend/src/pages/Dialog/Dialog.svelte | 23 ++- .../src/pages/Dialog/Message/Message.svelte | 159 ++++++++++++++++++ .../frontend/src/pages/Dialog/Message/code.go | 42 +++++ .../src/pages/Dialog/Message/code.jsx | 23 +++ .../pages/{logging => Logging}/Log/Log.svelte | 2 +- .../pages/{logging => Logging}/Log/code.go | 0 .../pages/{logging => Logging}/Log/code.jsx | 0 .../pages/{logging => Logging}/Logging.svelte | 6 +- .../SetLogLevel/SetLogLevel.svelte | 5 +- .../{logging => Logging}/SetLogLevel/code.go | 0 .../{logging => Logging}/SetLogLevel/code.jsx | 0 19 files changed, 255 insertions(+), 38 deletions(-) rename v2/test/kitchensink/frontend/src/pages/{browser => Browser}/Browser.svelte (95%) rename v2/test/kitchensink/frontend/src/pages/{browser => Browser}/Browser/Browser.svelte (97%) rename v2/test/kitchensink/frontend/src/pages/{browser => Browser}/Browser/code.go (100%) rename v2/test/kitchensink/frontend/src/pages/{browser => Browser}/Browser/code.jsx (100%) create mode 100644 v2/test/kitchensink/frontend/src/pages/Dialog/Message/Message.svelte create mode 100644 v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.go create mode 100644 v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.jsx rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/Log/Log.svelte (98%) rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/Log/code.go (100%) rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/Log/code.jsx (100%) rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/Logging.svelte (96%) rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/SetLogLevel/SetLogLevel.svelte (98%) rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/SetLogLevel/code.go (100%) rename v2/test/kitchensink/frontend/src/pages/{logging => Logging}/SetLogLevel/code.jsx (100%) diff --git a/v2/internal/ffenestri/ffenestri_client.go b/v2/internal/ffenestri/ffenestri_client.go index 431bac55..3a9def77 100644 --- a/v2/internal/ffenestri/ffenestri_client.go +++ b/v2/internal/ffenestri/ffenestri_client.go @@ -162,14 +162,10 @@ func (c *Client) MessageDialog(dialogOptions *options.MessageDialog, callbackID return } - // Reverse - // Process buttons buttons := []string{"", "", "", ""} - count := 0 - for i := len(dialogOptions.Buttons) - 1; i >= 0; i-- { - buttons[count] = dialogOptions.Buttons[i] - count++ + for i, button := range dialogOptions.Buttons { + buttons[i] = button } C.MessageDialog(c.app.app, diff --git a/v2/internal/runtime/js/core/dialog.js b/v2/internal/runtime/js/core/dialog.js index 6b8aa546..10a9b8dd 100644 --- a/v2/internal/runtime/js/core/dialog.js +++ b/v2/internal/runtime/js/core/dialog.js @@ -72,7 +72,7 @@ export function Save(options) { * @param {DialogType} [Type=InfoDialog] - The type of the dialog * @param {string} [Title=""] - The dialog title * @param {string} [Message=""] - The dialog message - * @param {string[]} [Buttons=[]] - The button titles in the order they should appear + * @param {string[]} [Buttons=[]] - The button titles * @param {string} [DefaultButton=""] - The button that should be used as the default button * @param {string} [CancelButton=""] - The button that should be used as the cancel button * @param {string} [Icon=""] - The name of the icon to use in the dialog @@ -83,6 +83,7 @@ export function Save(options) { * or prompt the user to select an option * * @export + * @name Message * @param {MessageDialogOptions} options * @returns {Promise} - The button text that was selected */ diff --git a/v2/internal/runtime/js/runtime/dialog.js b/v2/internal/runtime/js/runtime/dialog.js index 20cb4c0c..10a7fdf0 100644 --- a/v2/internal/runtime/js/runtime/dialog.js +++ b/v2/internal/runtime/js/runtime/dialog.js @@ -67,7 +67,7 @@ export function Save(options) { * @param {DialogType} [Type=InfoDialog] - The type of the dialog * @param {string} [Title=""] - The dialog title * @param {string} [Message=""] - The dialog message - * @param {string[]} [Buttons=[]] - The button titles in the order they should appear + * @param {string[]} [Buttons=[]] - The button titles * @param {string} [DefaultButton=""] - The button that should be used as the default button * @param {string} [CancelButton=""] - The button that should be used as the cancel button * @param {string} [Icon=""] - The name of the icon to use in the dialog diff --git a/v2/test/kitchensink/dialog.go b/v2/test/kitchensink/dialog.go index 58ca5588..f1b14129 100644 --- a/v2/test/kitchensink/dialog.go +++ b/v2/test/kitchensink/dialog.go @@ -31,17 +31,3 @@ func (l *Dialog) Save(options *options.SaveDialog) string { func (l *Dialog) Message(options *options.MessageDialog) string { return l.runtime.Dialog.Message(options) } - -// Message Dialog -func (l *Dialog) Test() string { - return l.runtime.Dialog.Message(&options.MessageDialog{ - Type: options.InfoDialog, - Title: " ", - Message: "I am a longer message but these days, can't be too long!", - // Buttons are declared in the order they should be appear in - Buttons: []string{"test", "Cancel", "OK"}, - DefaultButton: "OK", - CancelButton: "Cancel", - //Icon: "wails", - }) -} diff --git a/v2/test/kitchensink/frontend/src/pages/browser/Browser.svelte b/v2/test/kitchensink/frontend/src/pages/Browser/Browser.svelte similarity index 95% rename from v2/test/kitchensink/frontend/src/pages/browser/Browser.svelte rename to v2/test/kitchensink/frontend/src/pages/Browser/Browser.svelte index d0fea6a8..033c6190 100644 --- a/v2/test/kitchensink/frontend/src/pages/browser/Browser.svelte +++ b/v2/test/kitchensink/frontend/src/pages/Browser/Browser.svelte @@ -11,7 +11,7 @@ Here is an example.

- - + + diff --git a/v2/test/kitchensink/frontend/src/pages/browser/Browser/Browser.svelte b/v2/test/kitchensink/frontend/src/pages/Browser/Browser/Browser.svelte similarity index 97% rename from v2/test/kitchensink/frontend/src/pages/browser/Browser/Browser.svelte rename to v2/test/kitchensink/frontend/src/pages/Browser/Browser/Browser.svelte index f01a09ff..6ea8d8ab 100644 --- a/v2/test/kitchensink/frontend/src/pages/browser/Browser/Browser.svelte +++ b/v2/test/kitchensink/frontend/src/pages/Browser/Browser/Browser.svelte @@ -37,7 +37,7 @@ - + diff --git a/v2/test/kitchensink/frontend/src/pages/browser/Browser/code.go b/v2/test/kitchensink/frontend/src/pages/Browser/Browser/code.go similarity index 100% rename from v2/test/kitchensink/frontend/src/pages/browser/Browser/code.go rename to v2/test/kitchensink/frontend/src/pages/Browser/Browser/code.go diff --git a/v2/test/kitchensink/frontend/src/pages/browser/Browser/code.jsx b/v2/test/kitchensink/frontend/src/pages/Browser/Browser/code.jsx similarity index 100% rename from v2/test/kitchensink/frontend/src/pages/browser/Browser/code.jsx rename to v2/test/kitchensink/frontend/src/pages/Browser/Browser/code.jsx diff --git a/v2/test/kitchensink/frontend/src/pages/Dialog/Dialog.svelte b/v2/test/kitchensink/frontend/src/pages/Dialog/Dialog.svelte index 7041c3f6..b36de4a5 100644 --- a/v2/test/kitchensink/frontend/src/pages/Dialog/Dialog.svelte +++ b/v2/test/kitchensink/frontend/src/pages/Dialog/Dialog.svelte @@ -1,14 +1,23 @@
- The ability to open a dialog and prompt the user to select files or directories is provided as part of the runtime.Dialog component. There are 2 types of dialogs available: Save and Open. - + Sometimes there is a need to prompt the user for input. Dialogs are a good approach to this and Wails provides access to native dialogs. The dialog types supported are: + +
    +
  • Open - Prompts the user to select one or more files or folders
  • +
  • Save - Prompts the user to select a file or input a filename
  • +
  • Message - Prompts the user with information and optionally provides action buttons
  • +
+

- - + +
- + +
+
diff --git a/v2/test/kitchensink/frontend/src/pages/Dialog/Message/Message.svelte b/v2/test/kitchensink/frontend/src/pages/Dialog/Message/Message.svelte new file mode 100644 index 00000000..be3dab56 --- /dev/null +++ b/v2/test/kitchensink/frontend/src/pages/Dialog/Message/Message.svelte @@ -0,0 +1,159 @@ + + + +
+
+
+
+
Dialog Type
+ {#each dialogTypes as option} +
+ + +
+ {/each} +
+
+
+
+ + +
The title for the dialog
+
+
+ + +
The dialog message
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
The button that is the default option
+
+
+ + +
The button that is the cancel option
+
+
+
+
+ + +
The icon to use in the dialog
+
+
+ + + + + + +
+
+ diff --git a/v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.go b/v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.go new file mode 100644 index 00000000..1655ca8e --- /dev/null +++ b/v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.go @@ -0,0 +1,42 @@ +package main + +import ( + "io/ioutil" + + "github.com/wailsapp/wails/v2" + "github.com/wailsapp/wails/v2/pkg/options" +) + +type Notepad struct { + runtime *wails.Runtime +} + +func (n *Notepad) WailsInit(runtime *wails.Runtime) error { + n.runtime = runtime + return nil +} + +// SaveNotes attempts to save the given notes to disk. +// Returns false if the user cancelled the save, true on +// successful save. +func (n *Notepad) SaveNotes(notes string) (bool, error) { + + selectedFile := n.runtime.Dialog.Save(&options.SaveDialog{ + DefaultFilename: "notes.md", + Filters: "*.md", + }) + + // Check if the user pressed cancel + if selectedFile == "" { + // Cancelled + return false, nil + } + + // Save notes + err := ioutil.WriteFile(selectedFile, []byte(notes), 0700) + if err != nil { + return false, err + } + + return true, nil +} diff --git a/v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.jsx b/v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.jsx new file mode 100644 index 00000000..265954fc --- /dev/null +++ b/v2/test/kitchensink/frontend/src/pages/Dialog/Message/code.jsx @@ -0,0 +1,23 @@ +import { Dialog } from '@wails/runtime'; + +let notes = ""; + +function saveNotes() { + // Prompt the user to select a single file + let filename = Dialog.Save({ + "DefaultFilename": "notes.md", + "Filters": "*.md", + }); + + // Do something with the file + backend.main.SaveNotes(filename, notes).then( (result) => { + if ( !result ) { + // Cancelled + return + } + showMessage('Notes saved!'); + }).catch( (err) => { + // Show an alert + showAlert(err); + }) +} diff --git a/v2/test/kitchensink/frontend/src/pages/logging/Log/Log.svelte b/v2/test/kitchensink/frontend/src/pages/Logging/Log/Log.svelte similarity index 98% rename from v2/test/kitchensink/frontend/src/pages/logging/Log/Log.svelte rename to v2/test/kitchensink/frontend/src/pages/Logging/Log/Log.svelte index 3dc86c71..0c0f50f8 100644 --- a/v2/test/kitchensink/frontend/src/pages/logging/Log/Log.svelte +++ b/v2/test/kitchensink/frontend/src/pages/Logging/Log/Log.svelte @@ -58,7 +58,7 @@ - + diff --git a/v2/test/kitchensink/frontend/src/pages/logging/Log/code.go b/v2/test/kitchensink/frontend/src/pages/Logging/Log/code.go similarity index 100% rename from v2/test/kitchensink/frontend/src/pages/logging/Log/code.go rename to v2/test/kitchensink/frontend/src/pages/Logging/Log/code.go diff --git a/v2/test/kitchensink/frontend/src/pages/logging/Log/code.jsx b/v2/test/kitchensink/frontend/src/pages/Logging/Log/code.jsx similarity index 100% rename from v2/test/kitchensink/frontend/src/pages/logging/Log/code.jsx rename to v2/test/kitchensink/frontend/src/pages/Logging/Log/code.jsx diff --git a/v2/test/kitchensink/frontend/src/pages/logging/Logging.svelte b/v2/test/kitchensink/frontend/src/pages/Logging/Logging.svelte similarity index 96% rename from v2/test/kitchensink/frontend/src/pages/logging/Logging.svelte rename to v2/test/kitchensink/frontend/src/pages/Logging/Logging.svelte index 5b081741..0a17a674 100644 --- a/v2/test/kitchensink/frontend/src/pages/logging/Logging.svelte +++ b/v2/test/kitchensink/frontend/src/pages/Logging/Logging.svelte @@ -38,9 +38,9 @@ I am a Print message Custom loggers may be given to your Wails application. More details here.

- - + +
- + diff --git a/v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/SetLogLevel.svelte b/v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/SetLogLevel.svelte similarity index 98% rename from v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/SetLogLevel.svelte rename to v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/SetLogLevel.svelte index 0a8a95bf..a9caeb76 100644 --- a/v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/SetLogLevel.svelte +++ b/v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/SetLogLevel.svelte @@ -19,7 +19,8 @@ let logLevelUpper = loglevelText.toUpperCase(); let logLevelMethod = Log.Level[logLevelUpper]; setLogLevelMethod(logLevelMethod); - }; + } + $: lang = isJs ? 'Javascript' : 'Go'; let description = `You can set the log level using Log.SetLogLevel(). It accepts a log level (number) but there are consts available which may be used. See example code for more details.`; @@ -41,7 +42,7 @@ {/each} - + \ No newline at end of file diff --git a/v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/code.go b/v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/code.go similarity index 100% rename from v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/code.go rename to v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/code.go diff --git a/v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/code.jsx b/v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/code.jsx similarity index 100% rename from v2/test/kitchensink/frontend/src/pages/logging/SetLogLevel/code.jsx rename to v2/test/kitchensink/frontend/src/pages/Logging/SetLogLevel/code.jsx