chore(docs): simplify event system documentation

This commit is contained in:
Lucas Nogueira 2021-09-27 13:07:05 -03:00
parent 4866404f04
commit f68603aee4
No known key found for this signature in database
GPG Key ID: 2714B66BCFB01F7F
7 changed files with 20 additions and 13 deletions

View File

@ -0,0 +1,5 @@
---
"api": patch
---
Change `WindowLabel` type to `string`.

File diff suppressed because one or more lines are too long

View File

@ -36,11 +36,10 @@ emit('click', {
Window-specific events are exposed on the `window` module. Window-specific events are exposed on the `window` module.
```ts ```ts
import { getCurrent, WebviewWindow } from '@tauri-apps/api/window' import { appWindow, WebviewWindow } from '@tauri-apps/api/window'
// emit an event that are only visible to the current window // emit an event that are only visible to the current window
const current = getCurrent() appWindow.emit('event', { message: 'Tauri is awesome!' })
current.emit('event', { message: 'Tauri is awesome!' })
// create a new webview window and emit an event only to that window // create a new webview window and emit an event only to that window
const webview = new WebviewWindow('window') const webview = new WebviewWindow('window')

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,10 +1,10 @@
<script> <script>
import { appWindow, WebviewWindow, LogicalSize, LogicalPosition, UserAttentionType, getCurrent, PhysicalSize, PhysicalPosition } from "@tauri-apps/api/window"; import { appWindow, WebviewWindow, LogicalSize, LogicalPosition, UserAttentionType, PhysicalSize, PhysicalPosition } from "@tauri-apps/api/window";
import { open as openDialog } from "@tauri-apps/api/dialog"; import { open as openDialog } from "@tauri-apps/api/dialog";
import { open } from "@tauri-apps/api/shell"; import { open } from "@tauri-apps/api/shell";
window.UserAttentionType = UserAttentionType; window.UserAttentionType = UserAttentionType;
let selectedWindow = getCurrent().label; let selectedWindow = appWindow.label;
const windowMap = { const windowMap = {
[selectedWindow]: appWindow [selectedWindow]: appWindow
} }

View File

@ -220,7 +220,7 @@ function getAll(): WebviewWindow[] {
// events that are emitted right here instead of by the created webview // events that are emitted right here instead of by the created webview
const localTauriEvents = ['tauri://created', 'tauri://error'] const localTauriEvents = ['tauri://created', 'tauri://error']
/** @ignore */ /** @ignore */
export type WindowLabel = string | null | undefined export type WindowLabel = string
/** /**
* A webview window handle allows emitting and listening to events from the backend that are tied to the window. * A webview window handle allows emitting and listening to events from the backend that are tied to the window.
*/ */
@ -230,8 +230,8 @@ class WebviewWindowHandle {
/** Local event listeners. */ /** Local event listeners. */
listeners: { [key: string]: Array<EventCallback<any>> } listeners: { [key: string]: Array<EventCallback<any>> }
constructor(label: WindowLabel) { constructor(label: WindowLabel | null | undefined) {
this.label = label this.label = label ?? window.__TAURI__.__currentWindow.label
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.listeners = Object.create(null) this.listeners = Object.create(null)
} }
@ -1092,7 +1092,10 @@ class WindowManager extends WebviewWindowHandle {
* ``` * ```
*/ */
class WebviewWindow extends WindowManager { class WebviewWindow extends WindowManager {
constructor(label: WindowLabel, options: WindowOptions = {}) { constructor(
label: WindowLabel | null | undefined,
options: WindowOptions = {}
) {
super(label) super(label)
// @ts-expect-error // @ts-expect-error
if (!options?.skip) { if (!options?.skip) {