5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 19:31:20 +08:00

Support data-wml-trigger attribute

This commit is contained in:
Lea Anthony 2023-02-16 20:14:55 +11:00
parent c9492ec258
commit 0881914244
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
11 changed files with 69 additions and 97 deletions

View File

@ -93,3 +93,18 @@ Example:
``` ```
### `data-wml-window` ### `data-wml-window`
Any `wails.window` method can be called by adding the `data-wml-window` attribute to an element. The value of the attribute should be the name of the method to call. The method name should be in the same case as the method.
```html
<button data-wml-window="Close">Close Window</button>
```
### `data-wml-trigger`
This attribute specifies which javascript event should trigger the action. The default is `click`.
```html
<button data-wml-event="hover-box" data-wml-trigger="mouseover">Hover over me!</button>
```

View File

@ -1,18 +0,0 @@
<!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

@ -1,47 +0,0 @@
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,7 +19,7 @@ import {newWindow} from "./window";
import {dispatchCustomEvent, Emit, Off, OffAll, On, Once, OnMultiple} from "./events"; import {dispatchCustomEvent, Emit, Off, OffAll, On, Once, OnMultiple} from "./events";
import {dialogCallback, dialogErrorCallback, Error, Info, OpenFile, Question, SaveFile, Warning,} from "./dialogs"; import {dialogCallback, dialogErrorCallback, Error, Info, OpenFile, Question, SaveFile, Warning,} from "./dialogs";
import {enableContextMenus} from "./contextmenu"; import {enableContextMenus} from "./contextmenu";
import {addEventListeners} from "./wml"; import {refresh} from "./wml";
window.wails = { window.wails = {
...newRuntime(-1), ...newRuntime(-1),
@ -43,6 +43,9 @@ export function newRuntime(id) {
}, },
Log, Log,
Screens, Screens,
WML: {
Refresh: refresh,
},
Dialog: { Dialog: {
Info, Info,
Warning, Warning,
@ -70,5 +73,5 @@ if (DEBUG) {
enableContextMenus(true); enableContextMenus(true);
document.addEventListener("DOMContentLoaded", function(event) { document.addEventListener("DOMContentLoaded", function(event) {
addEventListeners(); refresh();
}); });

View File

@ -12,6 +12,7 @@ function addWMLEventListeners() {
const element = elements[i]; const element = elements[i];
const eventType = element.getAttribute('data-wml-event'); const eventType = element.getAttribute('data-wml-event');
const confirm = element.getAttribute('data-wml-confirm'); const confirm = element.getAttribute('data-wml-confirm');
const trigger = element.getAttribute('data-wml-trigger') || "click";
let callback = function () { let callback = function () {
if (confirm) { if (confirm) {
@ -25,10 +26,11 @@ function addWMLEventListeners() {
sendEvent(eventType); sendEvent(eventType);
} }
// Remove existing listeners // Remove existing listeners
element.removeEventListener("click", callback);
element.removeEventListener(trigger, callback);
// Add new listener // Add new listener
element.addEventListener("click", callback); element.addEventListener(trigger, callback);
} }
} }
@ -45,6 +47,7 @@ function addWMLWindowListeners() {
const element = elements[i]; const element = elements[i];
const windowMethod = element.getAttribute('data-wml-window'); const windowMethod = element.getAttribute('data-wml-window');
const confirm = element.getAttribute('data-wml-confirm'); const confirm = element.getAttribute('data-wml-confirm');
const trigger = element.getAttribute('data-wml-trigger') || "click";
let callback = function () { let callback = function () {
if (confirm) { if (confirm) {
@ -57,15 +60,16 @@ function addWMLWindowListeners() {
} }
callWindowMethod(windowMethod); callWindowMethod(windowMethod);
} }
// Remove existing listeners // Remove existing listeners
element.removeEventListener("click", callback); element.removeEventListener(trigger, callback);
// Add new listener // Add new listener
element.addEventListener("click", callback); element.addEventListener(trigger, callback);
} }
} }
export function addEventListeners() { export function refresh() {
addWMLEventListeners(); addWMLEventListeners();
addWMLWindowListeners(); 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