コンテンツにスキップ
Tauri

Positioner(ウィンドウ配置)

《訳注》

Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。

ウィンドウをよく知られた場所に配置します。

このプラグインは、electron-positioner を Tauri に移植したものです。

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

はじめに、「positioner」プラグインをインストールしてください。

自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:

npm run tauri add positioner

「トレイの相対配置」を機能させるには追加の設定が必要です。

  1. Cargo.toml ファイルに tray-icon 機能を追加します:

    src-tauri/Cargo.toml
    [dependencies]
    tauri-plugin-positioner = { version = "2.0.0", features = ["tray-icon"] }
  2. 「positioner」プラグインに on_tray_event を設定します:

    src-tauri/src/lib.rs
    pub fn run() {
    tauri::Builder::default()
    // これは「トレイ相対配置」を機能させるために必要です
    .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");
    }

このプラグインの API は、「JavaScript Guest」バインディングを通じて利用できます:

import { moveWindow, Position } from '@tauri-apps/plugin-positioner';
// `"withGlobalTauri": true` を使用する場合は、
// const { moveWindow, Position } = window.__TAURI__.positioner; を使用できます
moveWindow(Position.TopRight);

Rust を通して Window 特性拡張(trait extention)を直接インポートして使用することができます:

use tauri_plugin_positioner::{WindowExt, Position};
let mut win = app.get_webview_window("main").unwrap();
let _ = win.as_ref().window().move_window(Position::TopRight);

デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。

詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。

src-tauri/capabilities/default.json
{
"permissions": [
...,
"positioner:default",
]
}

Default Permission

Allows the moveWindow and handleIconState APIs

This default permission set includes the following:

  • allow-move-window
  • allow-move-window-constrained
  • allow-set-tray-icon-state

Permission Table

Identifier Description

positioner:allow-move-window

Enables the move_window command without any pre-configured scope.

positioner:deny-move-window

Denies the move_window command without any pre-configured scope.

positioner:allow-move-window-constrained

Enables the move_window_constrained command without any pre-configured scope.

positioner:deny-move-window-constrained

Denies the move_window_constrained command without any pre-configured scope.

positioner:allow-set-tray-icon-state

Enables the set_tray_icon_state command without any pre-configured scope.

positioner:deny-set-tray-icon-state

Denies the set_tray_icon_state command without any pre-configured scope.

【※ この日本語版は、「Feb 22, 2025 英語版」に基づいています】


© 2025 Tauri Contributors. CC-BY / MIT