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

Merge branch 'v3/api-package'

This commit is contained in:
Lea Anthony 2023-03-31 20:39:41 +11:00
commit eae4fe62cd
20 changed files with 1427 additions and 159 deletions

View File

@ -0,0 +1,363 @@
# Wails API
This package provides a typed Javascript API for Wails applications.
It provides methods for the following components:
- [Dialog](#dialog)
- [Events](#events)
- [Window](#window)
- [Plugin](#plugin)
- [Screens](#screens)
- [Application](#application)
## Installation
In your Wails application, run the following command in the frontend project directory:
```bash
npm install -D @wailsapp/api
```
## Usage
Import the API into your application:
```javascript
import * as Wails from "@wailsapp/api";
```
Then use the API components:
```javascript
function showDialog() {
Wails.Dialog.Info({
Title: "Hello",
}).then((result) => {
console.log("Result: " + result);
});
}
```
Individual components of the API can also be imported directly.
## API
### Dialog
The Dialog API provides access to the native system dialogs.
```javascript
import { Dialog } from "@wailsapp/api";
function example() {
Dialog.Info({
Title: "Hello",
}).then((result) => {
console.log("Result: " + result);
});
}
```
#### Message Dialogs
Message dialogs are used to display a message to the user.
They can be used to display information, errors, warnings and questions.
Each method returns the button that was pressed by the user.
- `Info(options: MessageDialogOptions): Promise<string>`
- `Error(options: MessageDialogOptions): Promise<string>`
- `Warning(options: MessageDialogOptions): Promise<string>`
- `Question(options: MessageDialogOptions): Promise<string>`
#### Open Dialog
The Open Dialog is used to open a file or directory. It returns the path of the selected file or directory.
If the `AllowsMultipleFiles` option is set, multiple files or directories can be selected and are returned
as an array of file paths.
- `Open(options: OpenDialogOptions): Promise<string[]|string>`
#### Save Dialog
The Save Dialog is used to save a file. It returns the path of the selected file.
- `Save(options: SaveDialogOptions): Promise<string>`
### Events
The Events API provides access to the Wails event system. This is a global event system
that can be used to send events between the Go and Javascript.
Events are available to every window in a multi-window application.
These API methods are specific to the window in which they are called in.
```javascript
import { Events } from "@wailsapp/api";
function example() {
// Emit an event
Events.Emit("myevent", { message: "Hello" });
// Subscribe to an event
let unsub = Events.On("otherEvent", (data) => {
console.log("Received event: " + data);
});
// Unsubscribe from the event
unsub();
}
```
#### Emit
Emit an event with optional data.
- `Emit(eventName: string, data?: any): void`
#### Subscribe
Three methods are provided to subscribe to an event:
- `On(eventName: string, callback: (data: any) => void): () => void` - Subscribe to all events of the given name
- `Once(eventName: string, callback: (data: any) => void): () => void` - Subscribe to one event of the given name
- `OnMultiple(eventName: string, callback: (data: any) => void, count: number): () => void` - Subscribe to multiple events of the given name
The callback will be called when the event is emitted.
The returned function can be called to unsubscribe from the event.
#### Unsubscribe
As well as unsubscribing from a single event, you can unsubscribe from events of a given name or all events.
- `Off(eventName: string, additionalEventNames: ...string): void` - Unsubscribe from all events of the given name(s)
- `OffAll(): void` - Unsubscribe all events
### Window
The Window API provides a number of methods that interact with the window in which the API is called.
- `Center: (void) => void` - Center the window
- `SetTitle: (title) => void` - Set the window title
- `Fullscreen: () => void` - Set the window to fullscreen
- `UnFullscreen: () => void` - Restore a fullscreen window
- `SetSize: (width: number, height: number) => void` - Set the window size
- `Size: () => Size` - Get the window size
- `SetMaxSize: (width, height) => void` - Set the window maximum size
- `SetMinSize: (width, height) => void` - Set the window minimum size
- `SetAlwaysOnTop: (onTop) => void` - Set window to be always on top
- `SetPosition: (x, y) => void` - Set the window position
- `Position: () => Position` - Get the window position
- `SetResizable: (resizable) => void` - Set whether the window is resizable
- `Screen: () => Screen` - Get information about the screen the window is on
- `Hide: () => void` - Hide the window
- `Show: () => void` - Show the window
- `Maximise: () => void` - Maximise the window
- `Close: () => void` - Close the window
- `ToggleMaximise: () => void` - Toggle the window maximise state
- `UnMaximise: () => void` - UnMaximise the window
- `Minimise: () => void` - Minimise the window
- `UnMinimise: () => void` - UnMinimise the window
- `SetBackgroundColour: (r, g, b, a) => void` - Set the background colour of the window
### Plugin
The Plugin API provides access to the Wails plugin system.
This method provides the ability to call a plugin method from the frontend.
```javascript
import { Plugin } from "@wailsapp/api";
function example() {
// Call a plugin method
Plugin.Call("myplugin", "MyMethod", { message: "Hello" }).then((result) => {
console.log("Result: " + result);
});
}
```
### Screens
The Screens API provides access to the Wails screen system.
```javascript
import { Screens } from "@wailsapp/api";
function example() {
// Get all attatched screens
Screens.GetAll().then((screens) => {
console.log("Screens: " + screens);
});
// Get the primary screen
Screens.GetPrimary().then((screen) => {
console.log("Primary screen: " + screen);
});
// Get the screen the window is on
Screens.GetCurrent().then((screen) => {
console.log("Window screen: " + screen);
});
}
```
- `GetAll: () => Promise<Screen[]>` - Get all screens
- `GetPrimary: () => Promise<Screen>` - Get the primary screen
- `GetCurrent: () => Promise<Screen>` - Get the screen the window is on
### Application
The Application API provides access to the Wails application system.
```javascript
import { Application } from "@wailsapp/api";
function example() {
// Hide the application
Application.Hide();
// Shopw the application
Application.Show();
// Quit the application
Application.Quit();
}
```
- `Hide: () => void` - Hide the application
- `Show: () => void` - Show the application
- `Quit: () => void` - Quit the application
## Types
This is a comprehensive list of types used by the Wails API.
```typescript
export interface Button {
// The label of the button
Label?: string;
// True if this button is the cancel button (selected when pressing escape)
IsCancel?: boolean;
// True if this button is the default button (selected when pressing enter)
IsDefault?: boolean;
}
interface MessageDialogOptions {
// The title for the dialog
Title?: string;
// The message to display
Message?: string;
// The buttons to use on the dialog
Buttons?: Button[];
}
export interface OpenFileDialogOptions {
// Allows the user to be able to select directories
CanChooseDirectories?: boolean;
// Allows the user to be able to select files
CanChooseFiles?: boolean;
// Provide an option to create directories in the dialog
CanCreateDirectories?: boolean;
// Makes the dialog show hidden files
ShowHiddenFiles?: boolean;
// Whether the dialog should follow filesystem aliases
ResolvesAliases?: boolean;
// Allow the user to select multiple files or directories
AllowsMultipleSelection?: boolean;
// Hide the extension when showing the filename
HideExtension?: boolean;
// Allow the user to select files where the system hides their extensions
CanSelectHiddenExtension?: boolean;
// Treats file packages as directories, e.g. .app on macOS
TreatsFilePackagesAsDirectories?: boolean;
// Allows selection of filetypes not specified in the filters
AllowsOtherFiletypes?: boolean;
// The file filters to use in the dialog
Filters?: FileFilter[];
// The title of the dialog
Title?: string;
// The message to display
Message?: string;
// The label for the select button
ButtonText?: string;
// The default directory to open the dialog in
Directory?: string;
}
export interface FileFilter {
// The display name for the filter, e.g. "Text Files"
DisplayName?: string;
// The pattern to use for the filter, e.g. "*.txt;*.md"
Pattern?: string;
}
export interface SaveFileDialogOptions {
// Provide an option to create directories in the dialog
CanCreateDirectories?: boolean;
// Makes the dialog show hidden files
ShowHiddenFiles?: boolean;
// Allow the user to select files where the system hides their extensions
CanSelectHiddenExtension?: boolean;
// Allows selection of filetypes not specified in the filters
AllowOtherFiletypes?: boolean;
// Hide the extension when showing the filename
HideExtension?: boolean;
// Treats file packages as directories, e.g. .app on macOS
TreatsFilePackagesAsDirectories?: boolean;
// The message to show in the dialog
Message?: string;
// The default directory to open the dialog in
Directory?: string;
// The default filename to use in the dialog
Filename?: string;
// The label for the select button
ButtonText?: string;
}
export interface Screen {
// The screen ID
Id: string;
// The screen name
Name: string;
// The screen scale. 1 = standard resolution, 2: 2x retina, etc.
Scale: number;
// The X position of the screen
X: number;
// The Y position of the screen
Y: number;
// The width and height of the screen
Size: Size;
// The bounds of the screen
Bounds: Rect;
// The work area of the screen
WorkArea: Rect;
// True if this is the primary screen
IsPrimary: boolean;
// The rotation of the screen
Rotation: number;
}
export interface Rect {
X: number;
Y: number;
Width: number;
Height: number;
}
export interface CustomEvent {
// The name of the event
Name: string;
// The data associated with the event
Data?: any;
}
export interface Size {
Width: number;
Height: number;
}
export interface Position {
X: number;
Y: number;
}
```

View File

@ -0,0 +1,349 @@
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/
/* jshint esversion: 9 */
/**
* @typedef {import("./types").MessageDialogOptions} MessageDialogOptions
* @typedef {import("./types").OpenDialogOptions} OpenDialogOptions
* @typedef {import("./types").SaveDialogOptions} SaveDialogOptions
* @typedef {import("./types").Screen} Screen
* @typedef {import("./types").Size} Size
* @typedef {import("./types").Position} Position
*
*/
/**
* The Clipboard API provides methods to interact with the system clipboard.
*/
export const Clipboard = {
/**
* Gets the text from the clipboard
* @returns {Promise<string>}
*/
Text: () => {
return wails.Clipboard.Text();
},
/**
* Sets the text on the clipboard
* @param {string} text - text to set in the clipboard
*/
SetText: (text) => {
return wails.Clipboard.SetText(text);
},
};
/**
* The Application API provides methods to interact with the application.
*/
export const Application = {
/**
* Hides the application
*/
Hide: () => {
return wails.Application.Hide();
},
/**
* Shows the application
*/
Show: () => {
return wails.Application.Show();
},
/**
* Quits the application
*/
Quit: () => {
return wails.Application.Quit();
},
};
/**
* The Screens API provides methods to interact with the system screens/monitors.
*/
export const Screens = {
/**
* Get the primary screen
* @returns {Promise<Screen>}
*/
GetPrimary: () => {
return wails.Screens.GetPrimary();
},
/**
* Get all screens
* @returns {Promise<Screen[]>}
*/
GetAll: () => {
return wails.Screens.GetAll();
},
/**
* Get the current screen
* @returns {Promise<Screen>}
*/
GetCurrent: () => {
return wails.Screens.GetCurrent();
},
};
/**
* Call a plugin method
* @param {string} pluginName - name of the plugin
* @param {string} methodName - name of the method
* @param {...any} args - arguments to pass to the method
* @returns {Promise<any>} - promise that resolves with the result
*/
export const Plugin = (pluginName, methodName, ...args) => {
return wails.Plugin(pluginName, methodName, ...args);
};
/**
* The Dialog API provides methods to interact with system dialogs.
*/
export const Dialog = {
/**
* Shows an info dialog
* @param {MessageDialogOptions} options - options for the dialog
* @returns {Promise<string>}
*/
Info: (options) => {
return wails.Dialog.Info(options);
},
/**
* Shows a warning dialog
* @param {MessageDialogOptions} options - options for the dialog
* @returns {Promise<string>}
*/
Warning: (options) => {
return wails.Dialog.Warning(options);
},
/**
* Shows an error dialog
* @param {MessageDialogOptions} options - options for the dialog
* @returns {Promise<string>}
*/
Error: (options) => {
return wails.Dialog.Error(options);
},
/**
* Shows a question dialog
* @param {MessageDialogOptions} options - options for the dialog
* @returns {Promise<string>}
*/
Question: (options) => {
return wails.Dialog.Question(options);
},
/**
* Shows a file open dialog and returns the files selected by the user.
* A blank string indicates that the dialog was cancelled.
* @param {OpenDialogOptions} options - options for the dialog
* @returns {Promise<string[]>|Promise<string>}
*/
OpenFile: (options) => {
return wails.Dialog.OpenFile(options);
},
/**
* Shows a file save dialog and returns the filename given by the user.
* A blank string indicates that the dialog was cancelled.
* @param {SaveDialogOptions} options - options for the dialog
* @returns {Promise<string>}
*/
SaveFile: (options) => {
return wails.Dialog.SaveFile(options);
},
};
/**
* The Events API provides methods to interact with the event system.
*/
export const Events = {
/**
* Emit an event
* @param {string} name
* @param {any=} data
*/
Emit: (name, data) => {
return wails.Events.Emit(name, data);
},
/**
* Subscribe to an event
* @param {string} name - name of the event
* @param {(any) => void} callback - callback to call when the event is emitted
@returns {function()} unsubscribeMethod - method to unsubscribe from the event
*/
On: (name, callback) => {
return wails.Events.On(name, callback);
},
/**
* Subscribe to an event once
* @param {string} name - name of the event
* @param {(any) => void} callback - callback to call when the event is emitted
* @returns {function()} unsubscribeMethod - method to unsubscribe from the event
*/
Once: (name, callback) => {
return wails.Events.Once(name, callback);
},
/**
* Subscribe to an event multiple times
* @param {string} name - name of the event
* @param {(any) => void} callback - callback to call when the event is emitted
* @param {number} count - number of times to call the callback
* @returns {Promise<void>} unsubscribeMethod - method to unsubscribe from the event
*/
OnMultiple: (name, callback, count) => {
return wails.Events.OnMultiple(name, callback, count);
},
/**
* Unsubscribe from an event
* @param {string} name - name of the event to unsubscribe from
* @param {...string} additionalNames - additional names of events to unsubscribe from
*/
Off: (name, ...additionalNames) => {
wails.Events.Off(name, additionalNames);
},
/**
* Unsubscribe all listeners from all events
*/
OffAll: () => {
wails.Events.OffAll();
},
};
/**
* The Window API provides methods to interact with the window.
*/
export const Window = {
/**
* Center the window.
*/
Center: () => void wails.Window.Center(),
/**
* Set the window title.
* @param title
*/
SetTitle: (title) => void wails.Window.SetTitle(title),
/**
* Makes the window fullscreen.
*/
Fullscreen: () => void wails.Window.Fullscreen(),
/**
* Unfullscreen the window.
*/
UnFullscreen: () => void wails.Window.UnFullscreen(),
/**
* Set the window size.
* @param {number} width The window width
* @param {number} height The window height
*/
SetSize: (width, height) => void wails.Window.SetSize(width, height),
/**
* Get the window size.
* @returns {Promise<Size>} The window size
*/
Size: () => {
return wails.Window.Size();
},
/**
* Set the window maximum size.
* @param {number} width
* @param {number} height
*/
SetMaxSize: (width, height) => void wails.Window.SetMaxSize(width, height),
/**
* Set the window minimum size.
* @param {number} width
* @param {number} height
*/
SetMinSize: (width, height) => void wails.Window.SetMinSize(width, height),
/**
* Set window to be always on top.
* @param {boolean} onTop Whether the window should be always on top
*/
SetAlwaysOnTop: (onTop) => void wails.Window.SetAlwaysOnTop(onTop),
/**
* Set the window position.
* @param {number} x
* @param {number} y
*/
SetPosition: (x, y) => void wails.Window.SetPosition(x, y),
/**
* Get the window position.
* @returns {Promise<Position>} The window position
*/
Position: () => {
return wails.Window.Position();
},
/**
* Get the screen the window is on.
* @returns {Promise<Screen>}
*/
Screen: () => {
return wails.Window.Screen();
},
/**
* Hide the window
*/
Hide: () => void wails.Window.Hide(),
/**
* Maximise the window
*/
Maximise: () => void wails.Window.Maximise(),
/**
* Show the window
*/
Show: () => void wails.Window.Show(),
/**
* Close the window
*/
Close: () => void wails.Window.Close(),
/**
* Toggle the window maximise state
*/
ToggleMaximise: () => void wails.Window.ToggleMaximise(),
/**
* Unmaximise the window
*/
UnMaximise: () => void wails.Window.UnMaximise(),
/**
* Minimise the window
*/
Minimise: () => void wails.Window.Minimise(),
/**
* Unminimise the window
*/
UnMinimise: () => void wails.Window.UnMinimise(),
/**
* Set the background colour of the window.
* @param {number} r - The red value between 0 and 255
* @param {number} g - The green value between 0 and 255
* @param {number} b - The blue value between 0 and 255
* @param {number} a - The alpha value between 0 and 255
*/
SetBackgroundColour: (r, g, b, a) => void wails.Window.SetBackgroundColour(r, g, b, a),
};

View File

@ -0,0 +1,16 @@
{
"name": "@wailsapp/api",
"version": "3.0.0-alpha.3",
"description": "Wails Runtime API",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/wailsapp/wails.git"
},
"author": "The Wails Team",
"license": "MIT",
"bugs": {
"url": "https://github.com/wailsapp/wails/issues"
},
"homepage": "https://wails.io"
}

View File

@ -0,0 +1,124 @@
export interface Button {
// The label of the button
Label?: string;
// True if this button is the cancel button (selected when pressing escape)
IsCancel?: boolean;
// True if this button is the default button (selected when pressing enter)
IsDefault?: boolean;
}
interface MessageDialogOptions {
// The title for the dialog
Title?: string;
// The message to display
Message?: string;
// The buttons to use on the dialog
Buttons?: Button[];
}
export interface OpenFileDialogOptions {
// Allows the user to be able to select directories
CanChooseDirectories?: boolean;
// Allows the user to be able to select files
CanChooseFiles?: boolean;
// Provide an option to create directories in the dialog
CanCreateDirectories?: boolean;
// Makes the dialog show hidden files
ShowHiddenFiles?: boolean;
// Whether the dialog should follow filesystem aliases
ResolvesAliases?: boolean;
// Allow the user to select multiple files or directories
AllowsMultipleSelection?: boolean;
// Hide the extension when showing the filename
HideExtension?: boolean;
// Allow the user to select files where the system hides their extensions
CanSelectHiddenExtension?: boolean;
// Treats file packages as directories, e.g. .app on macOS
TreatsFilePackagesAsDirectories?: boolean;
// Allows selection of filetypes not specified in the filters
AllowsOtherFiletypes?: boolean;
// The file filters to use in the dialog
Filters?: FileFilter[];
// The title of the dialog
Title?: string;
// The message to display
Message?: string;
// The label for the select button
ButtonText?: string;
// The default directory to open the dialog in
Directory?: string;
}
export interface FileFilter {
// The display name for the filter, e.g. "Text Files"
DisplayName?: string;
// The pattern to use for the filter, e.g. "*.txt;*.md"
Pattern?: string;
}
export interface SaveFileDialogOptions {
// Provide an option to create directories in the dialog
CanCreateDirectories?: boolean;
// Makes the dialog show hidden files
ShowHiddenFiles?: boolean;
// Allow the user to select files where the system hides their extensions
CanSelectHiddenExtension?: boolean;
// Allows selection of filetypes not specified in the filters
AllowOtherFiletypes?: boolean;
// Hide the extension when showing the filename
HideExtension?: boolean;
// Treats file packages as directories, e.g. .app on macOS
TreatsFilePackagesAsDirectories?: boolean;
// The message to show in the dialog
Message?: string;
// The default directory to open the dialog in
Directory?: string;
// The default filename to use in the dialog
Filename?: string;
// The label for the select button
ButtonText?: string;
}
export interface Screen {
// The screen ID
Id: string;
// The screen name
Name: string;
// The screen scale. 1 = standard resolution, 2: 2x retina, etc.
Scale: number;
// The X position of the screen
X: number;
// The Y position of the screen
Y: number;
// The width and height of the screen
Size: Size;
// The bounds of the screen
Bounds: Rect;
// The work area of the screen
WorkArea: Rect;
// True if this is the primary screen
IsPrimary: boolean;
// The rotation of the screen
Rotation: number;
}
export interface Rect {
X: number;
Y: number;
Width: number;
Height: number;
}
export interface CustomEvent {
// The name of the event
Name: string;
// The data associated with the event
Data?: any;
}
export interface Size {
Width: number;
Height: number;
}
export interface Position {
X: number;
Y: number;
}

View File

@ -14,14 +14,24 @@ import {newRuntimeCaller} from "./runtime";
let call = newRuntimeCaller("application");
/**
* Hide the application
*/
export function Hide() {
return call("Hide");
void call("Hide");
}
/**
* Show the application
*/
export function Show() {
return call("Show");
void call("Show");
}
/**
* Quit the application
*/
export function Quit() {
return call("Quit");
void call("Quit");
}

View File

@ -65,15 +65,16 @@ export function Call(options) {
/**
* Call a plugin method
* @param pluginName - name of the plugin
* @param methodName - name of the method
* @param {string} pluginName - name of the plugin
* @param {string} methodName - name of the method
* @param {...any} args - arguments to pass to the method
* @returns {Promise<any>} - promise that resolves with the result
*/
export function Plugin(pluginName, methodName) {
export function Plugin(pluginName, methodName, ...args) {
return callBinding("Call", {
packageName: "wails-plugins",
structName: pluginName,
methodName: methodName,
args: Array.prototype.slice.call(arguments, 2),
args: args,
});
}

View File

@ -14,10 +14,17 @@ import {newRuntimeCaller} from "./runtime";
let call = newRuntimeCaller("clipboard");
/**
* Set the Clipboard text
*/
export function SetText(text) {
return call("SetText", {text});
void call("SetText", {text});
}
/**
* Get the Clipboard text
* @returns {Promise<string>}
*/
export function Text() {
return call("Text");
}

View File

@ -10,6 +10,12 @@ The electron alternative for Go
/* jshint esversion: 9 */
/**
* @typedef {import("./api/types").MessageDialogOptions} MessageDialogOptions
* @typedef {import("./api/types").OpenDialogOptions} OpenDialogOptions
* @typedef {import("./api/types").SaveDialogOptions} SaveDialogOptions
*/
import {newRuntimeCaller} from "./runtime";
import { nanoid } from 'nanoid/non-secure';
@ -59,26 +65,56 @@ function dialog(type, options) {
}
/**
* Shows an Info dialog with the given options.
* @param {MessageDialogOptions} options
* @returns {Promise<string>} The label of the button pressed
*/
export function Info(options) {
return dialog("Info", options);
}
/**
* Shows an Warning dialog with the given options.
* @param {MessageDialogOptions} options
* @returns {Promise<string>} The label of the button pressed
*/
export function Warning(options) {
return dialog("Warning", options);
}
/**
* Shows an Error dialog with the given options.
* @param {MessageDialogOptions} options
* @returns {Promise<string>} The label of the button pressed
*/
export function Error(options) {
return dialog("Error", options);
}
/**
* Shows a Question dialog with the given options.
* @param {MessageDialogOptions} options
* @returns {Promise<string>} The label of the button pressed
*/
export function Question(options) {
return dialog("Question", options);
}
/**
* Shows an Open dialog with the given options.
* @param {OpenDialogOptions} options
* @returns {Promise<string[]|string>} Returns the selected file or an array of selected files if AllowsMultipleSelection is true. A blank string is returned if no file was selected.
*/
export function OpenFile(options) {
return dialog("OpenFile", options);
}
/**
* Shows a Save dialog with the given options.
* @param {OpenDialogOptions} options
* @returns {Promise<string>} Returns the selected file. A blank string is returned if no file was selected.
*/
export function SaveFile(options) {
return dialog("SaveFile", options);
}

View File

@ -10,6 +10,10 @@ The electron alternative for Go
/* jshint esversion: 9 */
/**
* @typedef {import("./api/types").CustomEvent} CustomEvent
*/
import {newRuntimeCaller} from "./runtime";
let call = newRuntimeCaller("events");
@ -102,7 +106,7 @@ export function On(eventName, callback) {
* @export
* @param {string} eventName
* @param {function(CustomEvent): void} callback
* @returns {function} A function to cancel the listener
@returns {function} A function to cancel the listener
*/
export function Once(eventName, callback) {
return OnMultiple(eventName, callback, 1);
@ -137,7 +141,7 @@ export function dispatchCustomEvent(event) {
// iterate listeners and call callback. If callback returns true, remove listener
let toRemove = [];
listeners.forEach(listener => {
let remove = listener.Callback(event)
let remove = listener.Callback(event);
if (remove) {
toRemove.push(listener);
}
@ -179,9 +183,10 @@ export function OffAll() {
eventListeners.clear();
}
/*
Emit emits an event to all listeners
/**
* Emit an event
* @param {CustomEvent} event The event to emit
*/
export function Emit(event) {
return call("Emit", event);
void call("Emit", event);
}

View File

@ -44,7 +44,7 @@ export function newRuntimeCaller(object, id) {
}
return function (method, args) {
args = args || {};
args["windowID"] = id;
args.windowID = id;
return runtimeCall(object + "." + method, args);
}
};
}

View File

@ -10,18 +10,35 @@ The electron alternative for Go
/* jshint esversion: 9 */
/**
* @typedef {import("./api/types").Screen} Screen
*/
import {newRuntimeCaller} from "./runtime";
let call = newRuntimeCaller("screens");
/**
* Gets all screens.
* @returns {Promise<Screen[]>}
*/
export function GetAll() {
return call("GetAll");
}
/**
* Gets the primary screen.
* @returns {Promise<Screen>}
*/
export function GetPrimary() {
return call("GetPrimary");
}
/**
* Gets the current active screen.
* @returns {Promise<Screen>}
* @constructor
*/
export function GetCurrent() {
return call("GetCurrent");
}

View File

@ -10,6 +10,12 @@ The electron alternative for Go
/* jshint esversion: 9 */
/**
* @typedef {import("../api/types").Size} Size
* @typedef {import("../api/types").Position} Position
* @typedef {import("../api/types").Screen} Screen
*/
import {newRuntimeCaller} from "./runtime";
export function newWindow(id) {
@ -24,26 +30,128 @@ export function newWindow(id) {
// IsMaximized: () => call('WIM'),
// IsMinimized: () => call('WIMN'),
// IsWindowed: () => call('WIF'),
Center: () => call('Center'),
SetTitle: (title) => call('SetTitle', {title}),
Fullscreen: () => call('Fullscreen'),
UnFullscreen: () => call('UnFullscreen'),
/**
* Centers the window.
*/
Center: () => void call('Center'),
/**
* Set the window title.
* @param title
*/
SetTitle: (title) => void call('SetTitle', {title}),
/**
* Makes the window fullscreen.
*/
Fullscreen: () => void call('Fullscreen'),
/**
* Unfullscreen the window.
*/
UnFullscreen: () => void call('UnFullscreen'),
/**
* Set the window size.
* @param {number} width The window width
* @param {number} height The window height
*/
SetSize: (width, height) => call('SetSize', {width,height}),
Size: () => { return call('Size') },
SetMaxSize: (width, height) => call('SetMaxSize', {width,height}),
SetMinSize: (width, height) => call('SetMinSize', {width,height}),
SetAlwaysOnTop: (b) => call('SetAlwaysOnTop', {alwaysOnTop:b}),
/**
* Get the window size.
* @returns {Promise<Size>} The window size
*/
Size: () => { return call('Size'); },
/**
* Set the window maximum size.
* @param {number} width
* @param {number} height
*/
SetMaxSize: (width, height) => void call('SetMaxSize', {width,height}),
/**
* Set the window minimum size.
* @param {number} width
* @param {number} height
*/
SetMinSize: (width, height) => void call('SetMinSize', {width,height}),
/**
* Set window to be always on top.
* @param {boolean} onTop Whether the window should be always on top
*/
SetAlwaysOnTop: (onTop) => void call('SetAlwaysOnTop', {alwaysOnTop:onTop}),
/**
* Set the window position.
* @param {number} x
* @param {number} y
*/
SetPosition: (x, y) => call('SetPosition', {x,y}),
Position: () => { return call('Position') },
Screen: () => { return call('Screen') },
Hide: () => call('Hide'),
Maximise: () => call('Maximise'),
Show: () => call('Show'),
Close: () => call('Close'),
ToggleMaximise: () => call('ToggleMaximise'),
UnMaximise: () => call('UnMaximise'),
Minimise: () => call('Minimise'),
UnMinimise: () => call('UnMinimise'),
SetBackgroundColour: (r, g, b, a) => call('SetBackgroundColour', {r, g, b, a}),
}
/**
* Get the window position.
* @returns {Promise<Position>} The window position
*/
Position: () => { return call('Position'); },
/**
* Get the screen the window is on.
* @returns {Promise<Screen>}
*/
Screen: () => { return call('Screen'); },
/**
* Hide the window
*/
Hide: () => void call('Hide'),
/**
* Maximise the window
*/
Maximise: () => void call('Maximise'),
/**
* Show the window
*/
Show: () => void call('Show'),
/**
* Close the window
*/
Close: () => void call('Close'),
/**
* Toggle the window maximise state
*/
ToggleMaximise: () => void call('ToggleMaximise'),
/**
* Unmaximise the window
*/
UnMaximise: () => void call('UnMaximise'),
/**
* Minimise the window
*/
Minimise: () => void call('Minimise'),
/**
* Unminimise the window
*/
UnMinimise: () => void call('UnMinimise'),
/**
* Set the background colour of the window.
* @param {number} r - A value between 0 and 255
* @param {number} g - A value between 0 and 255
* @param {number} b - A value between 0 and 255
* @param {number} a - A value between 0 and 255
*/
SetBackgroundColour: (r, g, b, a) => void call('SetBackgroundColour', {r, g, b, a}),
};
}

View File

@ -3,13 +3,12 @@ import {Emit} from "./events";
import {Question} from "./dialogs";
function sendEvent(event) {
let _ = Emit({name: event} );
Emit(event);
}
function addWMLEventListeners() {
const elements = document.querySelectorAll('[data-wml-event]');
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
elements.forEach(function (element) {
const eventType = element.getAttribute('data-wml-event');
const confirm = element.getAttribute('data-wml-confirm');
const trigger = element.getAttribute('data-wml-trigger') || "click";
@ -24,14 +23,14 @@ function addWMLEventListeners() {
return;
}
sendEvent(eventType);
}
};
// Remove existing listeners
element.removeEventListener(trigger, callback);
// Add new listener
element.addEventListener(trigger, callback);
}
});
}
function callWindowMethod(method) {
@ -43,8 +42,7 @@ function callWindowMethod(method) {
function addWMLWindowListeners() {
const elements = document.querySelectorAll('[data-wml-window]');
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
elements.forEach(function (element) {
const windowMethod = element.getAttribute('data-wml-window');
const confirm = element.getAttribute('data-wml-confirm');
const trigger = element.getAttribute('data-wml-trigger') || "click";
@ -59,14 +57,14 @@ function addWMLWindowListeners() {
return;
}
callWindowMethod(windowMethod);
}
};
// Remove existing listeners
element.removeEventListener(trigger, callback);
// Add new listener
element.addEventListener(trigger, callback);
}
});
}
export function reloadWML() {

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

View File

@ -1,26 +1,26 @@
package application
type Screen struct {
ID string `json:"id,omitempty"` // A unique identifier for the display
Name string `json:"name,omitempty"` // The name of the display
Scale float32 `json:"scale,omitempty"` // The scale factor of the display
X int `json:"x,omitempty"` // The x-coordinate of the top-left corner of the rectangle
Y int `json:"y,omitempty"` // The y-coordinate of the top-left corner of the rectangle
Size Size `json:"size"` // The size of the display
Bounds Rect `json:"bounds"` // The bounds of the display
WorkArea Rect `json:"work_area"` // The work area of the display
IsPrimary bool `json:"is_primary,omitempty"` // Whether this is the primary display
Rotation float32 `json:"rotation,omitempty"` // The rotation of the display
ID string // A unique identifier for the display
Name string // The name of the display
Scale float32 // The scale factor of the display
X int // The x-coordinate of the top-left corner of the rectangle
Y int // The y-coordinate of the top-left corner of the rectangle
Size Size // The size of the display
Bounds Rect // The bounds of the display
WorkArea Rect // The work area of the display
IsPrimary bool // Whether this is the primary display
Rotation float32 // The rotation of the display
}
type Rect struct {
X int `json:"x,omitempty"`
Y int `json:"y,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
X int
Y int
Width int
Height int
}
type Size struct {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Width int
Height int
}