Skip to content

Commit 2b1ceb4

Browse files
authored
refactor(api)!: renamed getCurrent functions to avoid ambiguity (#10229)
* refactor(api)!: renamed `getCurrent` functions to avoid ambiguity closes #10193 * Update .changes/get-current-ambguity.md * rename `getAll` and update docs and examples
1 parent 249cdde commit 2b1ceb4

10 files changed

Lines changed: 207 additions & 191 deletions

File tree

.changes/get-current-ambguity.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"@tauri-apps/api": "patch:breaking"
3+
"tauri": "patch:breaking"
4+
---
5+
6+
Renamed the JS `getCurrent` and `getAll` functions to a clearer name to avoid ambiguity:
7+
- `getCurrent` in `window` module has been renamed to `getCurrentWindow`
8+
- `getCurrent` in `webview` module has been renamed to `getCurrentWebview`
9+
- `getCurrent` in `webviewWindow` module has been renamed to `getCurrentWebviewWindow`
10+
- `getAll` in `window` module has been renamed to `getAllWindows`
11+
- `getAll` in `webview` module has been renamed to `getAllWebviews`
12+
- `getAll` in `webviewWindow` module has been renamed to `getAllWebviewWindows`
13+

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src/views/Communication.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script>
2-
import { getCurrent } from '@tauri-apps/api/webviewWindow'
2+
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
33
import { invoke } from '@tauri-apps/api/core'
44
import { onMount, onDestroy } from 'svelte'
55
66
export let onMessage
77
let unlisten
88
9-
const webviewWindow = getCurrent()
9+
const webviewWindow = getCurrentWebviewWindow()
1010
1111
onMount(async () => {
1212
unlisten = await webviewWindow.listen('rust-event', onMessage)

examples/multiwindow/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<script>
1717
const WebviewWindow = window.__TAURI__.webviewWindow.WebviewWindow
18-
const appWindow = window.__TAURI__.window.getCurrent()
18+
const appWindow = window.__TAURI__.window.getCurrentWindow()
1919
const windowLabel = appWindow.label
2020
const windowLabelContainer = document.getElementById('window-label')
2121
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'
@@ -75,7 +75,7 @@
7575
})
7676
container.appendChild(globalMessageButton)
7777

78-
const allWindows = window.__TAURI__.window.getAll()
78+
const allWindows = window.__TAURI__.window.getAllWindows()
7979
for (const index in allWindows) {
8080
const label = allWindows[index].label
8181
if (label === windowLabel) {

examples/parent-window/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<script>
1717
const { WebviewWindow } = window.__TAURI__.webviewWindow
18-
const thisTauriWindow = window.__TAURI__.window.getCurrent()
18+
const thisTauriWindow = window.__TAURI__.window.getCurrentWindow()
1919
const windowLabel = thisTauriWindow.label
2020
const windowLabelContainer = document.getElementById('window-label')
2121
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'

tooling/api/src/dpi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class PhysicalSize {
3737
* Converts the physical size to a logical one.
3838
* @example
3939
* ```typescript
40-
* import { getCurrent } from '@tauri-apps/api/window';
41-
* const appWindow = getCurrent();
40+
* import { getCurrentWindow } from '@tauri-apps/api/window';
41+
* const appWindow = getCurrentWindow();
4242
* const factor = await appWindow.scaleFactor();
4343
* const size = await appWindow.innerSize();
4444
* const logical = size.toLogical(factor);
@@ -84,8 +84,8 @@ class PhysicalPosition {
8484
* Converts the physical position to a logical one.
8585
* @example
8686
* ```typescript
87-
* import { getCurrent } from '@tauri-apps/api/window';
88-
* const appWindow = getCurrent();
87+
* import { getCurrentWindow } from '@tauri-apps/api/window';
88+
* const appWindow = getCurrentWindow();
8989
* const factor = await appWindow.scaleFactor();
9090
* const position = await appWindow.innerPosition();
9191
* const logical = position.toLogical(factor);

tooling/api/src/mocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ export function mockIPC(
111111
*
112112
* ```js
113113
* import { mockWindows } from "@tauri-apps/api/mocks";
114-
* import { getCurrent } from "@tauri-apps/api/window";
114+
* import { getCurrentWindow } from "@tauri-apps/api/window";
115115
*
116116
* mockWindows("main", "second", "third");
117117
*
118-
* const win = getCurrent();
118+
* const win = getCurrentWindow();
119119
*
120120
* win.label // "main"
121121
* ```

tooling/api/src/webview.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*
1010
* Events can be listened to using {@link Webview.listen}:
1111
* ```typescript
12-
* import { getCurrent } from "@tauri-apps/api/webview";
13-
* getCurrent().listen("my-webview-event", ({ event, payload }) => { });
12+
* import { getCurrentWebview } from "@tauri-apps/api/webview";
13+
* getCurrentWebview().listen("my-webview-event", ({ event, payload }) => { });
1414
* ```
1515
*
1616
* @module
@@ -29,7 +29,7 @@ import {
2929
once
3030
} from './event'
3131
import { invoke } from './core'
32-
import { Window, getCurrent as getCurrentWindow } from './window'
32+
import { Window, getCurrentWindow } from './window'
3333
import { WebviewWindow } from './webviewWindow'
3434

3535
interface DragDropPayload {
@@ -53,7 +53,7 @@ type DragDropEvent =
5353
*
5454
* @since 2.0.0
5555
*/
56-
function getCurrent(): Webview {
56+
function getCurrentWebview(): Webview {
5757
return new Webview(
5858
getCurrentWindow(),
5959
window.__TAURI_INTERNALS__.metadata.currentWebview.label,
@@ -69,7 +69,7 @@ function getCurrent(): Webview {
6969
*
7070
* @since 2.0.0
7171
*/
72-
function getAll(): Webview[] {
72+
function getAllWebviews(): Webview[] {
7373
return window.__TAURI_INTERNALS__.metadata.webviews.map(
7474
(w) =>
7575
new Webview(Window.getByLabel(w.windowLabel)!, w.label, {
@@ -184,30 +184,30 @@ class Webview {
184184
* @returns The Webview instance to communicate with the webview or null if the webview doesn't exist.
185185
*/
186186
static getByLabel(label: string): Webview | null {
187-
return getAll().find((w) => w.label === label) ?? null
187+
return getAllWebviews().find((w) => w.label === label) ?? null
188188
}
189189

190190
/**
191191
* Get an instance of `Webview` for the current webview.
192192
*/
193193
static getCurrent(): Webview {
194-
return getCurrent()
194+
return getCurrentWebview()
195195
}
196196

197197
/**
198198
* Gets a list of instances of `Webview` for all available webviews.
199199
*/
200200
static getAll(): Webview[] {
201-
return getAll()
201+
return getAllWebviews()
202202
}
203203

204204
/**
205205
* Listen to an emitted event on this webview.
206206
*
207207
* @example
208208
* ```typescript
209-
* import { getCurrent } from '@tauri-apps/api/webview';
210-
* const unlisten = await getCurrent().listen<string>('state-changed', (event) => {
209+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
210+
* const unlisten = await getCurrentWebview().listen<string>('state-changed', (event) => {
211211
* console.log(`Got error: ${payload}`);
212212
* });
213213
*
@@ -241,7 +241,7 @@ class Webview {
241241
*
242242
* @example
243243
* ```typescript
244-
* import { getCurrent } from '@tauri-apps/api/webview';
244+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
245245
* const unlisten = await getCurrent().once<null>('initialized', (event) => {
246246
* console.log(`Webview initialized!`);
247247
* });
@@ -276,8 +276,8 @@ class Webview {
276276
*
277277
* @example
278278
* ```typescript
279-
* import { getCurrent } from '@tauri-apps/api/webview';
280-
* await getCurrent().emit('webview-loaded', { loggedIn: true, token: 'authToken' });
279+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
280+
* await getCurrentWebview().emit('webview-loaded', { loggedIn: true, token: 'authToken' });
281281
* ```
282282
*
283283
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
@@ -303,8 +303,8 @@ class Webview {
303303
*
304304
* @example
305305
* ```typescript
306-
* import { getCurrent } from '@tauri-apps/api/webview';
307-
* await getCurrent().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });
306+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
307+
* await getCurrentWebview().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });
308308
* ```
309309
*
310310
* @param target Label of the target Window/Webview/WebviewWindow or raw {@link EventTarget} object.
@@ -350,8 +350,8 @@ class Webview {
350350
* The position of the top-left hand corner of the webview's client area relative to the top-left hand corner of the desktop.
351351
* @example
352352
* ```typescript
353-
* import { getCurrent } from '@tauri-apps/api/webview';
354-
* const position = await getCurrent().position();
353+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
354+
* const position = await getCurrentWebview().position();
355355
* ```
356356
*
357357
* @returns The webview's position.
@@ -367,8 +367,8 @@ class Webview {
367367
* The client area is the content of the webview, excluding the title bar and borders.
368368
* @example
369369
* ```typescript
370-
* import { getCurrent } from '@tauri-apps/api/webview';
371-
* const size = await getCurrent().size();
370+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
371+
* const size = await getCurrentWebview().size();
372372
* ```
373373
*
374374
* @returns The webview's size.
@@ -388,8 +388,8 @@ class Webview {
388388
* Closes the webview.
389389
* @example
390390
* ```typescript
391-
* import { getCurrent } from '@tauri-apps/api/webview';
392-
* await getCurrent().close();
391+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
392+
* await getCurrentWebview().close();
393393
* ```
394394
*
395395
* @returns A promise indicating the success or failure of the operation.
@@ -405,7 +405,7 @@ class Webview {
405405
* @example
406406
* ```typescript
407407
* import { getCurrent, LogicalSize } from '@tauri-apps/api/webview';
408-
* await getCurrent().setSize(new LogicalSize(600, 500));
408+
* await getCurrentWebview().setSize(new LogicalSize(600, 500));
409409
* ```
410410
*
411411
* @param size The logical or physical size.
@@ -435,7 +435,7 @@ class Webview {
435435
* @example
436436
* ```typescript
437437
* import { getCurrent, LogicalPosition } from '@tauri-apps/api/webview';
438-
* await getCurrent().setPosition(new LogicalPosition(600, 500));
438+
* await getCurrentWebview().setPosition(new LogicalPosition(600, 500));
439439
* ```
440440
*
441441
* @param position The new position, in logical or physical pixels.
@@ -469,8 +469,8 @@ class Webview {
469469
* Bring the webview to front and focus.
470470
* @example
471471
* ```typescript
472-
* import { getCurrent } from '@tauri-apps/api/webview';
473-
* await getCurrent().setFocus();
472+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
473+
* await getCurrentWebview().setFocus();
474474
* ```
475475
*
476476
* @returns A promise indicating the success or failure of the operation.
@@ -485,8 +485,8 @@ class Webview {
485485
* Set webview zoom level.
486486
* @example
487487
* ```typescript
488-
* import { getCurrent } from '@tauri-apps/api/webview';
489-
* await getCurrent().setZoom(1.5);
488+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
489+
* await getCurrentWebview().setZoom(1.5);
490490
* ```
491491
*
492492
* @returns A promise indicating the success or failure of the operation.
@@ -502,8 +502,8 @@ class Webview {
502502
* Moves this webview to the given label.
503503
* @example
504504
* ```typescript
505-
* import { getCurrent } from '@tauri-apps/api/webview';
506-
* await getCurrent().reparent('other-window');
505+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
506+
* await getCurrentWebview().reparent('other-window');
507507
* ```
508508
*
509509
* @returns A promise indicating the success or failure of the operation.
@@ -525,7 +525,7 @@ class Webview {
525525
* @example
526526
* ```typescript
527527
* import { getCurrent } from "@tauri-apps/api/webview";
528-
* const unlisten = await getCurrent().onDragDropEvent((event) => {
528+
* const unlisten = await getCurrentWebview().onDragDropEvent((event) => {
529529
* if (event.payload.type === 'hover') {
530530
* console.log('User hovering', event.payload.paths);
531531
* } else if (event.payload.type === 'drop') {
@@ -680,6 +680,6 @@ interface WebviewOptions {
680680
zoomHotkeysEnabled?: boolean
681681
}
682682

683-
export { Webview, getCurrent, getAll }
683+
export { Webview, getCurrentWebview, getAllWebviews }
684684

685685
export type { DragDropEvent, DragDropPayload, WebviewOptions }

tooling/api/src/webviewWindow.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
import {
6-
getCurrent as getCurrentWebview,
6+
getCurrentWebview,
77
Webview,
88
WebviewLabel,
99
WebviewOptions
@@ -20,7 +20,7 @@ import type { DragDropEvent, DragDropPayload } from './webview'
2020
*
2121
* @since 2.0.0
2222
*/
23-
function getCurrent(): WebviewWindow {
23+
function getCurrentWebviewWindow(): WebviewWindow {
2424
const webview = getCurrentWebview()
2525
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
2626
return new WebviewWindow(webview.label, { skip: true })
@@ -31,7 +31,7 @@ function getCurrent(): WebviewWindow {
3131
*
3232
* @since 2.0.0
3333
*/
34-
function getAll(): WebviewWindow[] {
34+
function getAllWebviewWindows(): WebviewWindow[] {
3535
return window.__TAURI_INTERNALS__.metadata.webviews.map(
3636
(w) =>
3737
new WebviewWindow(w.label, {
@@ -108,7 +108,8 @@ class WebviewWindow {
108108
* @returns The Webview instance to communicate with the webview or null if the webview doesn't exist.
109109
*/
110110
static getByLabel(label: string): WebviewWindow | null {
111-
const webview = getAll().find((w) => w.label === label) ?? null
111+
const webview =
112+
getAllWebviewWindows().find((w) => w.label === label) ?? null
112113
if (webview) {
113114
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
114115
return new WebviewWindow(webview.label, { skip: true })
@@ -120,15 +121,17 @@ class WebviewWindow {
120121
* Get an instance of `Webview` for the current webview.
121122
*/
122123
static getCurrent(): WebviewWindow {
123-
return getCurrent()
124+
return getCurrentWebviewWindow()
124125
}
125126

126127
/**
127128
* Gets a list of instances of `Webview` for all available webviews.
128129
*/
129130
static getAll(): WebviewWindow[] {
130-
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
131-
return getAll().map((w) => new WebviewWindow(w.label, { skip: true }))
131+
return getAllWebviewWindows().map(
132+
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
133+
(w) => new WebviewWindow(w.label, { skip: true })
134+
)
132135
}
133136

134137
/**
@@ -232,5 +235,5 @@ function applyMixins(
232235
})
233236
}
234237

235-
export { WebviewWindow, getCurrent, getAll }
238+
export { WebviewWindow, getCurrentWebviewWindow, getAllWebviewWindows }
236239
export type { DragDropEvent, DragDropPayload }

0 commit comments

Comments
 (0)