5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 03:01:45 +08:00

Added Wails ML experiment

added Window.Close to js runtime
This commit is contained in:
Lea Anthony 2023-02-15 20:58:57 +11:00
parent a5e10557c5
commit c415c1b321
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
15 changed files with 394 additions and 7 deletions

View File

@ -69,4 +69,27 @@ Context menus are contextual menus that are shown when the user right-clicks on
To indicate that an element has a context menu, add the `data-contextmenu` attribute to the element. The value of this attribute should be the name of a context menu previously registered with the window.
It is possible to register a context menu at the application level, making it available to all windows. This can be done using `app.RegisterContextMenu(name, menu)`. If a context menu cannot be found at the window level, the application context menus will be checked. A demo of this can be found in `v3/examples/contextmenus`.
It is possible to register a context menu at the application level, making it available to all windows. This can be done using `app.RegisterContextMenu(name, menu)`. If a context menu cannot be found at the window level, the application context menus will be checked. A demo of this can be found in `v3/examples/contextmenus`.
## Wails Markup Language (WML)
The Wails Markup Language is a simple markup language that allows you to add functionality to standard HTML elements without the use of Javascript.
The following tags are currently supported:
### `data-wml-event`
This specifies that a Wails event will be emitted when the element is clicked. The value of the attribute should be the name of the event to emit.
Example:
```html
<button data-wml-event="myevent">Click Me</button>
```
Sometimes you need the user to confirm an action. This can be done by adding the `data-wml-confirm` attribute to the element. The value of this attribute will be the message to display to the user.
Example:
```html
<button data-wml-event="delete-all-items" data-wml-confirm="Are you sure?">Delete All Items</button>
```
### `data-wml-window`

View File

@ -0,0 +1,22 @@
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/leaanthony/slicer v1.5.0 h1:aHYTN8xbCCLxJmkNKiLB6tgcMARl4eWmH9/F+S/0HtY=
github.com/leaanthony/slicer v1.5.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 h1:Wn+nhnS+VytzE0PegUzSh4T3hXJCtggKGD/4U5H9+wQ=
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6/go.mod h1:zlNLI0E2c2qA6miiuAHtp0Bac8FaGH0tlhA19OssR/8=
github.com/wailsapp/wails/v3 v3.0.0-alpha.0 h1:T5gqG98Xr8LBf69oxlPkhpsFD59w2SnqUZk6XHj8Zoc=
github.com/wailsapp/wails/v3 v3.0.0-alpha.0/go.mod h1:OAfO5bP0TSUvCIHZYc6Dqfow/9RqxzHvYtmhWPpo1c0=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -0,0 +1,8 @@
// Code generated by tygo. DO NOT EDIT.
//////////
// source: person.go
export interface Person {
Name: string;
}

View File

@ -0,0 +1,9 @@
// Code generated by tygo. DO NOT EDIT.
//////////
// source: GreetService.go
export interface GreetService {
SomeVariable: number /* int */;
Parent?: any /* models.Person */;
}

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wails ML Demo</title>
</head>
<body style="margin-top:50px">
<h2>Wails ML Demo</h2>
<p>This application contains no Javascript!</p>
<button data-wml-event="button-pressed">Press me!</button>
<button data-wml-event="delete-things" data-wml-confirm="Are you sure?">Delete all the things!</button>
<button data-wml-window="Close" data-wml-confirm="Are you sure?">Close the Window?</button>
<button data-wml-window="Center">Center</button>
<button data-wml-window="Minimise">Minimise</button>
<button data-wml-window="Maximise">Maximise</button>
<button data-wml-window="Fullscreen">Fullscreen</button>
</body>
</html>

View File

@ -0,0 +1,47 @@
package main
import (
"embed"
_ "embed"
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed assets/*
var assets embed.FS
func main() {
app := application.New(application.Options{
Name: "Wails ML Demo",
Description: "A demo of the Wails ML API",
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
app.NewWebviewWindowWithOptions(&application.WebviewWindowOptions{
Title: "Wails ML Demo",
Width: 800,
Height: 600,
Assets: application.AssetOptions{
FS: assets,
},
Mac: application.MacWindow{
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInsetUnified,
InvisibleTitleBarHeight: 50,
},
})
app.Events.On("button-pressed", func(_ *application.CustomEvent) {
println("Button Pressed!")
})
err := app.Run()
if err != nil {
log.Fatal(err.Error())
}
}

View File

@ -19,6 +19,7 @@ import {newWindow} from "./window";
import {dispatchCustomEvent, Emit, Off, OffAll, On, Once, OnMultiple} from "./events";
import {dialogCallback, dialogErrorCallback, Error, Info, OpenFile, Question, SaveFile, Warning,} from "./dialogs";
import {enableContextMenus} from "./contextmenu";
import {addEventListeners} from "./wml";
window.wails = {
...newRuntime(-1),
@ -67,3 +68,7 @@ if (DEBUG) {
}
enableContextMenus(true);
document.addEventListener("DOMContentLoaded", function(event) {
addEventListeners();
});

View File

@ -39,6 +39,7 @@ export function newWindow(id) {
Hide: () => call('Hide'),
Maximise: () => call('Maximise'),
Show: () => call('Show'),
Close: () => call('Close'),
ToggleMaximise: () => call('ToggleMaximise'),
UnMaximise: () => call('UnMaximise'),
Minimise: () => call('Minimise'),

View File

@ -0,0 +1,71 @@
import {Emit} from "./events";
import {Question} from "./dialogs";
function sendEvent(event) {
let _ = Emit({name: event} );
}
function addWMLEventListeners() {
const elements = document.querySelectorAll('[data-wml-event]');
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const eventType = element.getAttribute('data-wml-event');
const confirm = element.getAttribute('data-wml-confirm');
let callback = function () {
if (confirm) {
Question({Title: "Confirm", Message:confirm, Buttons:[{Label:"Yes"},{Label:"No", IsDefault:true}]}).then(function (result) {
if (result !== "No") {
sendEvent(eventType);
}
});
return;
}
sendEvent(eventType);
}
// Remove existing listeners
element.removeEventListener("click", callback);
// Add new listener
element.addEventListener("click", callback);
}
}
function callWindowMethod(method) {
if (wails.Window[method] === undefined) {
console.log("Window method " + method + " not found");
}
wails.Window[method]();
}
function addWMLWindowListeners() {
const elements = document.querySelectorAll('[data-wml-window]');
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const windowMethod = element.getAttribute('data-wml-window');
const confirm = element.getAttribute('data-wml-confirm');
let callback = function () {
if (confirm) {
Question({Title: "Confirm", Message:confirm, Buttons:[{Label:"Yes"},{Label:"No", IsDefault:true}]}).then(function (result) {
if (result !== "No") {
callWindowMethod(windowMethod);
}
});
return;
}
callWindowMethod(windowMethod);
}
// Remove existing listeners
element.removeEventListener("click", callback);
// Add new listener
element.addEventListener("click", callback);
}
}
export function addEventListeners() {
addWMLEventListeners();
addWMLWindowListeners();
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long