瀏覽代碼

chore(docs): simplify event system documentation

Lucas Nogueira 3 年之前
父節點
當前提交
f68603aee4

+ 5 - 0
.changes/simplify-window-label-types.md

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

File diff suppressed because it is too large
+ 0 - 0
core/tauri/scripts/bundle.js


+ 2 - 3
docs/usage/guides/events.md

@@ -36,11 +36,10 @@ emit('click', {
 Window-specific events are exposed on the `window` module.
 
 ```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
-const current = getCurrent()
-current.emit('event', { message: 'Tauri is awesome!' })
+appWindow.emit('event', { message: 'Tauri is awesome!' })
 
 // create a new webview window and emit an event only to that window
 const webview = new WebviewWindow('window')

File diff suppressed because it is too large
+ 0 - 0
examples/api/public/build/bundle.js


File diff suppressed because it is too large
+ 0 - 0
examples/api/public/build/bundle.js.map


+ 2 - 2
examples/api/src/components/Window.svelte

@@ -1,10 +1,10 @@
 <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 } from "@tauri-apps/api/shell";
 
   window.UserAttentionType = UserAttentionType;
-  let selectedWindow = getCurrent().label;
+  let selectedWindow = appWindow.label;
   const windowMap = {
     [selectedWindow]: appWindow
   }

+ 7 - 4
tooling/api/src/window.ts

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

Some files were not shown because too many files changed in this diff