窗口自定义
Tauri 提供了许多自定义应用窗口外观和感觉的选项。您可以创建自定义标题栏、拥有透明窗口、强制大小约束等等。
配置
有三种方法可以更改窗口配置
用法
创建自定义标题栏
这些窗口功能的一个常见用途是创建自定义标题栏。这个简短的教程将指导您完成该过程。
tauri.conf.json
在您的tauri.conf.json
中将decorations
设置为false
"tauri": { "windows": [ { "decorations": false } ]}
权限
在 capability 文件中添加窗口权限。
默认情况下,所有插件命令都被阻止且无法访问。您必须在您的 capabilities
配置中定义权限列表。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": ["core:window:default", "core:window:allow-start-dragging"]}
权限 | 描述 |
---|---|
core:window:default | 插件的默认权限。除了 window:allow-start-dragging 。 |
core:window:allow-close | 启用 close 命令,无需任何预配置的作用域。 |
core:window:allow-minimize | 启用 minimize 命令,无需任何预配置的作用域。 |
core:window:allow-start-dragging | 启用 start_dragging 命令,无需任何预配置的作用域。 |
core:window:allow-toggle-maximize | 启用 toggle_maximize 命令,无需任何预配置的作用域。 |
core:window:allow-internal-toggle-maximize | 启用 internal_toggle_maximize 命令,无需任何预配置的作用域。 |
CSS
添加此 CSS 示例以使其保持在屏幕顶部并设置按钮样式
.titlebar { height: 30px; background: #329ea3; user-select: none; display: flex; justify-content: flex-end; position: fixed; top: 0; left: 0; right: 0;}.titlebar-button { display: inline-flex; justify-content: center; align-items: center; width: 30px; height: 30px; user-select: none; -webkit-user-select: none;}.titlebar-button:hover { background: #5bbec3;}
HTML
将其放在 <body>
标记的顶部
<div data-tauri-drag-region class="titlebar"> <div class="titlebar-button" id="titlebar-minimize"> <img src="https://api.iconify.design/mdi:window-minimize.svg" alt="minimize" /> </div> <div class="titlebar-button" id="titlebar-maximize"> <img src="https://api.iconify.design/mdi:window-maximize.svg" alt="maximize" /> </div> <div class="titlebar-button" id="titlebar-close"> <img src="https://api.iconify.design/mdi:close.svg" alt="close" /> </div></div>
请注意,您可能需要向下移动其余内容,以使标题栏不会覆盖它。
JavaScript
使用此代码片段使按钮工作
import { getCurrentWindow } from '@tauri-apps/api/window';
// when using `"withGlobalTauri": true`, you may use// const { getCurrentWindow } = window.__TAURI__.window;
const appWindow = getCurrentWindow();
document .getElementById('titlebar-minimize') ?.addEventListener('click', () => appWindow.minimize());document .getElementById('titlebar-maximize') ?.addEventListener('click', () => appWindow.toggleMaximize());document .getElementById('titlebar-close') ?.addEventListener('click', () => appWindow.close());
请注意,如果您使用的是基于 Rust 的前端,您可以将上面的代码复制到 index.html
文件中的 <script>
元素中。
data-tauri-drag-region
的手动实现
对于您自定义拖动行为的用例,您可以手动添加带有 window.startDragging
的事件侦听器,而不是使用 data-tauri-drag-region
。
HTML
从上一节的代码中,我们删除 data-tauri-drag-region
并添加一个 id
<div data-tauri-drag-region class="titlebar"> <div id="titlebar" class="titlebar"> <!-- ... --> </div></div>
Javascript
向标题栏元素添加事件监听器
// ...document.getElementById('titlebar')?.addEventListener('mousedown', (e) => { if (e.buttons === 1) { // Primary (left) button e.detail === 2 ? appWindow.toggleMaximize() // Maximize on double click : appWindow.startDragging(); // Else start dragging }});
(macOS) 具有自定义窗口背景颜色的透明标题栏
我们将从 Rust 端创建主窗口并更改其背景颜色。
从 tauri.conf.json
文件中删除主窗口
"tauri": { "windows": [ { "title": "Transparent Titlebar Window", "width": 800, "height": 600 } ],}
将 cocoa
crate 添加到依赖项,以便我们可以使用它来调用 macOS 原生 API
[target."cfg(target_os = \"macos\")".dependencies]cocoa = "0.26"
创建主窗口并更改其背景颜色
use tauri::{TitleBarStyle, WebviewUrl, WebviewWindowBuilder};
pub fn run() { tauri::Builder::default() .setup(|app| { let win_builder = WebviewWindowBuilder::new(app, "main", WebviewUrl::default()) .title("Transparent Titlebar Window") .inner_size(800.0, 600.0);
// set transparent title bar only when building for macOS #[cfg(target_os = "macos")] let win_builder = win_builder.title_bar_style(TitleBarStyle::Transparent);
let window = win_builder.build().unwrap();
// set background color only when building for macOS #[cfg(target_os = "macos")] { use cocoa::appkit::{NSColor, NSWindow}; use cocoa::base::{id, nil};
let ns_window = window.ns_window().unwrap() as id; unsafe { let bg_color = NSColor::colorWithRed_green_blue_alpha_( nil, 50.0 / 255.0, 158.0 / 255.0, 163.5 / 255.0, 1.0, ); ns_window.setBackgroundColor_(bg_color); } }
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
© 2025 Tauri 贡献者。CC-BY / MIT