定位器
将你的窗口放置在众所周知的位置。
¥Position your windows at well-known locations.
此插件是 Tauri 的 electron-positioner 端口。
¥This plugin is a port of electron-positioner for Tauri.
支持的平台
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
设置
¥Setup
安装定位器插件即可开始使用。
¥Install the positioner plugin to get started.
:::note 注意
如果你只打算从 Rust 代码中移动窗口,则只需要 src-tauri/Cargo.toml
中的依赖,并且如果选择自动设置,则可以从 lib.rs
中删除插件注册。
¥If you only intend on moving the window from Rust code, you only need the dependency in src-tauri/Cargo.toml
, and can remove the plugin registration from lib.rs
if you choose to setup automatically.
:::
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
npm run tauri add positioner
yarn run tauri add positioner
pnpm tauri add positioner
deno task tauri add positioner
bun tauri add positioner
cargo tauri add positioner
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-positioner --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))' -
Modify
lib.rs
to initialize the plugin:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_positioner::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-positioneryarn add @tauri-apps/plugin-positionerpnpm add @tauri-apps/plugin-positionerdeno add npm:@tauri-apps/plugin-positionerbun add @tauri-apps/plugin-positioner
需要进行其他设置才能使托盘相对位置正常工作。
¥Additional setup is required to get tray-relative positions to work.
-
将
tray-icon
功能添加到你的Cargo.toml
文件:¥Add
tray-icon
feature to yourCargo.toml
file:src-tauri/Cargo.toml [dependencies]tauri-plugin-positioner = { version = "2.0.0", features = ["tray-icon"] } -
为定位器插件设置
on_tray_event
:¥Setup
on_tray_event
for positioner plugin:src-tauri/src/lib.rs pub fn run() {tauri::Builder::default()// This is required to get tray-relative positions to work.setup(|app| {#[cfg(desktop)]{app.handle().plugin(tauri_plugin_positioner::init());tauri::tray::TrayIconBuilder::new().on_tray_icon_event(|tray_handle, event| {tauri_plugin_positioner::on_tray_event(tray_handle.app_handle(), &event);}).build(app)?;}Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");}
使用
¥Usage
插件的 API 可通过 JavaScript 来宾绑定获得:
¥The plugin’s APIs are available through the JavaScript guest bindings:
import { moveWindow, Position } from '@tauri-apps/plugin-positioner';// when using `"withGlobalTauri": true`, you may use// const { moveWindow, Position } = window.__TAURI__.positioner;
moveWindow(Position.TopRight);
你可以直接通过 Rust 导入和使用 Window trait 扩展:
¥You can import and use the Window trait extension directly through Rust:
use tauri_plugin_positioner::{WindowExt, Position};
let mut win = app.get_webview_window("main").unwrap();let _ = win.as_ref().window().move_window(Position::TopRight);
权限
¥Permissions
默认情况下,所有潜在危险的插件命令和范围都会被阻止,无法访问。你必须修改 capabilities
配置中的权限才能启用这些权限。
¥By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities
configuration to enable these.
有关更详细的说明,请参阅 功能概述。
¥See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "permissions": [ ..., "positioner:default", ]}
Default Permission
Allows the moveWindow and handleIconState APIs
allow-move-window
allow-move-window-constrained
allow-set-tray-icon-state
Permission Table
Identifier | Description |
---|---|
|
Enables the move_window command without any pre-configured scope. |
|
Denies the move_window command without any pre-configured scope. |
|
Enables the move_window_constrained command without any pre-configured scope. |
|
Denies the move_window_constrained command without any pre-configured scope. |
|
Enables the set_tray_icon_state command without any pre-configured scope. |
|
Denies the set_tray_icon_state command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号