Files
upstream-tauri-plugins/plugins/positioner/guest-js/index.ts
upstream-mirror 9ad64ec987
Some checks failed
integration tests / run-integration-tests (windows-latest) (push) Has been cancelled
Test Rust / test (push) Has been cancelled
test plugins e2e (mobile) / ios (push) Has been cancelled
test plugins e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test plugins e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
integration tests / run-integration-tests (macos-latest) (push) Has been cancelled
version or publish / version-or-publish (push) Failing after 0s
integration tests / run-integration-tests (ubuntu-22.04) (push) Failing after 0s
Lint JavaScript / eslint (push) Failing after 0s
Sync / sync-to-mirrors (push) Failing after 0s
test plugins e2e (mobile) / android (push) Failing after 0s
test plugins e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
Test Rust / changes (push) Failing after 0s
Lint Rust / changes (push) Failing after 0s
Lint Rust / clippy (push) Has been skipped
Audit Rust / audit-rust (push) Failing after 1s
Audit JavaScript / audit-js (push) Failing after 1s
snapshot of https://github.com/tauri-apps/plugins-workspace.git (v2, 2026-09-23; blobs > 131072B omitted)
2026-09-23 17:17:10 +05:00

141 lines
4.3 KiB
TypeScript

// Copyright 2021 Jonas Kruckenberg
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* Move the window to well-known positions, including positions relative to the tray icon.
*
* @module
*/
import { invoke } from '@tauri-apps/api/core'
import type { TrayIconEvent } from '@tauri-apps/api/tray'
/**
* Well known window positions.
*/
export enum Position {
/** Top left corner of the screen. */
TopLeft = 0,
/** Top right corner of the screen. */
TopRight,
/** Bottom left corner of the screen. */
BottomLeft,
/** Bottom right corner of the screen. */
BottomRight,
/** Top center of the screen. */
TopCenter,
/** Bottom center of the screen. */
BottomCenter,
/** Vertically centered on the left edge of the screen. */
LeftCenter,
/** Vertically centered on the right edge of the screen. */
RightCenter,
/** Center of the screen. */
Center,
/**
* Above the tray icon, aligned with its left edge. Requires the `tray-icon` feature and the
* tray icon's events to be forwarded through {@link handleIconState}.
*/
TrayLeft,
/**
* Directly below the tray icon, aligned with its left edge. Requires the `tray-icon` feature
* and the tray icon's events to be forwarded through {@link handleIconState}.
*/
TrayBottomLeft,
/**
* Above the tray icon, aligned with its right edge. Requires the `tray-icon` feature and the
* tray icon's events to be forwarded through {@link handleIconState}.
*/
TrayRight,
/**
* Directly below the tray icon, aligned with its right edge. Requires the `tray-icon` feature
* and the tray icon's events to be forwarded through {@link handleIconState}.
*/
TrayBottomRight,
/**
* Above the tray icon, horizontally centered on it. Requires the `tray-icon` feature and the
* tray icon's events to be forwarded through {@link handleIconState}.
*/
TrayCenter,
/**
* Directly below the tray icon, horizontally centered on it. Requires the `tray-icon` feature
* and the tray icon's events to be forwarded through {@link handleIconState}.
*/
TrayBottomCenter
}
/**
* Moves the `Window` to the given {@link Position} using `WindowExt.move_window()`
* All positions are relative to the **current** screen.
*
* @example
* ```typescript
* import { moveWindow, Position } from '@tauri-apps/plugin-positioner'
*
* await moveWindow(Position.TopRight)
* ```
*
* @param to The {@link Position} to move to.
* @since 2.0.0
*/
export async function moveWindow(to: Position): Promise<void> {
await invoke('plugin:positioner|move_window', {
position: to
})
}
/**
* Moves the `Window` to the given {@link Position} using `WindowExt.move_window_constrained()`
*
* This move operation constrains the window to the screen dimensions in case of
* tray-icon positions.
*
* @example
* ```typescript
* import { moveWindowConstrained, Position } from '@tauri-apps/plugin-positioner'
*
* await moveWindowConstrained(Position.TrayCenter)
* ```
*
* @param to The (tray) {@link Position} to move to.
* @since 2.1.0
*/
export async function moveWindowConstrained(to: Position): Promise<void> {
await invoke('plugin:positioner|move_window_constrained', {
position: to
})
}
/**
* Reports the tray icon's current position and size so the `Tray*` {@link Position} variants
* can be resolved.
*
* This is an internal helper meant to be called from the tray icon's event handler (the
* `action` callback passed to `TrayIcon.new()`), forwarding every {@link TrayIconEvent} it
* receives so that {@link moveWindow} and {@link moveWindowConstrained} can later position the
* window relative to the tray icon.
*
* @example
* ```typescript
* import { handleIconState } from '@tauri-apps/plugin-positioner'
* import { TrayIcon, type TrayIconEvent } from '@tauri-apps/api/tray'
*
* const action = async (event: TrayIconEvent) => {
* await handleIconState(event)
* }
*
* const tray = await TrayIcon.new({ id: 'main', action })
* ```
*
* @param event The tray icon event to read the position and size from.
* @since 2.0.0
*/
export async function handleIconState(event: TrayIconEvent): Promise<void> {
await invoke('plugin:positioner|set_tray_icon_state', {
position: event.rect.position,
size: event.rect.size
})
}