Tauri 实战总结 系列二(窗口定制)

tauri

窗口定制

Tauri 生成的窗口顶部会有默认的标题栏,默认的效果有时候不符合我们项目需要,接下来我们来实现下如何自定义标题栏。示例源码地址:tauri-study-examples

实现效果

image-20230102184405613

1. 添加窗口

tauri.conf.json 里添加窗口

{
	"window":[
		{
		  "label": "customization",
      "url": "/customization",
      "fullscreen": false,
      "width": 800,
      "height": 600,
      "center": true,
      "resizable": true,
      "skipTaskbar": false,
      "visible": false,
      "decorations": false
		}
	]
}

2. HTML 添加标题栏

import { Button } from 'antd';
import { WebviewWindow } from '@tauri-apps/api/window';
import { appWindow } from '@tauri-apps/api/window';

const CustomizationPage = () => {
  return (
    <div className="customization-page">
      <div data-tauri-drag-region className="titlebar">
        <div
          className="titlebar-button"
          id="titlebar-minimize"
          onClick={() => appWindow.minimize()}>
          <img
            src="https://api.iconify.design/mdi:window-minimize.svg"
            alt="minimize"
          />
        </div>
        <div
          className="titlebar-button"
          id="titlebar-maximize"
          onClick={() => appWindow.toggleMaximize()}>
          <img
            src="https://api.iconify.design/mdi:window-maximize.svg"
            alt="maximize"
          />
        </div>
        <div
          className="titlebar-button"
          id="titlebar-close"
          onClick={() => appWindow.hide()}>
          <img src="https://api.iconify.design/mdi:close.svg" alt="close" />
        </div>
      </div>
    </div>
  );
};

export default CustomizationPage;

3. CSS 配置

.titlebar {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  display: flex;
  justify-content: flex-end;
  height: 30px;
  background: #329ea3;
  user-select: none;
}

.titlebar-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
}

.titlebar-button:hover {
  background: #5bbec3;
}

4. window-shadows 配置(可选)

经过上述三步配置后已经可以实现自定义标题栏的效果,但在 win11 系统上自定义窗口没有圆角及阴影的效果,接下来我们实现该效果。

  1. src-tauri/Cargo.toml 添加依赖项
[dependencies]
...
window-shadows = "0.2.1"
  1. src-tauri/src 下添加 utils.rs 文件
use tauri::{Manager, Runtime};
use window_shadows::set_shadow;

pub fn set_window_shadow<R: Runtime>(app: &tauri::App<R>) {
  let window = app.get_window("customization").unwrap();

  set_shadow(&window, true).expect("Unsupported platform!");
}
  1. main.rs 文件中使用
use crate::{
  utils::{set_window_shadow}
};

mod utils;

fn main() {
  tauri::Builder::default()
    .setup(|app| {
      set_window_shadow(app);
      Ok(())
    })
    .invoke_handler(tauri::generate_handler![])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑ii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值