5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-16 00:49:32 +08:00

Updated Dialog Examples

This commit is contained in:
Lea Anthony 2020-11-19 08:56:57 +11:00
parent cc4967d457
commit 5ba03937c3
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
4 changed files with 112 additions and 38 deletions

View File

@ -1,11 +1,40 @@
package main
import wails "github.com/wailsapp/wails/v2"
import (
"io/ioutil"
type MyStruct struct {
wails "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
)
type Notepad struct {
runtime *wails.Runtime
}
func (l *MyStruct) ShowHelp() {
l.runtime.Browser.Open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
func (n *Notepad) WailsInit(runtime *wails.Runtime) error {
n.runtime = runtime
return nil
}
func (n *Notepad) LoadNotes() (string, error) {
selectedFiles := n.runtime.Dialog.Open(&options.OpenDialog{
DefaultFilename: "notes.md",
Filters: "*.md",
AllowFiles: true,
})
// selectedFiles is a string slice. Get the first selection
if len(selectedFiles) == 0 {
// Cancelled
return "", nil
}
// Load notes
noteData, err := ioutil.ReadFile(selectedFiles[0])
if err != nil {
return "", err
}
return string(noteData), nil
}

View File

@ -1,17 +1,25 @@
import { Log } from '@wails/runtime';
import { Dialog } from '@wails/runtime';
function doSomeOperation() {
// Do things
let value = doSomething();
Log.Print("A raw message");
Log.Trace("I got: " + value);
Log.Debug("A debug message");
Log.Info("An Info message");
Log.Warning("A Warning message");
Log.Error("An Error message");
let notes = "";
function loadNotes() {
// Prompt the user to select a single file
let filename = Dialog.Open({
"DefaultFilename": "notes.md",
"Filters": "*.md",
"AllowFiles": true,
});
// Do something with the file
backend.main.LoadNotes(filename).then( (result) => {
if (result.length == 0) {
// Cancelled
return
}
// We only prompted for a single file
notes = result[0];
}).catch( (err) => {
// Show an alert
showAlert(err);
})
}
function abort() {
// Do some things
Log.Fatal("I accidentally the whole application!");
}

View File

@ -1,11 +1,42 @@
package main
import wails "github.com/wailsapp/wails/v2"
import (
"io/ioutil"
type MyStruct struct {
wails "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
)
type Notepad struct {
runtime *wails.Runtime
}
func (l *MyStruct) ShowHelp() {
l.runtime.Browser.Open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
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
}

View File

@ -1,17 +1,23 @@
import { Log } from '@wails/runtime';
import { Dialog } from '@wails/runtime';
function doSomeOperation() {
// Do things
let value = doSomething();
Log.Print("A raw message");
Log.Trace("I got: " + value);
Log.Debug("A debug message");
Log.Info("An Info message");
Log.Warning("A Warning message");
Log.Error("An Error message");
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);
})
}
function abort() {
// Do some things
Log.Fatal("I accidentally the whole application!");
}