当前位置:   article > 正文

tauri自定义窗口window并实现拖拽和阴影效果_tauri 1.4 怎么定义拖动窗口区域

tauri 1.4 怎么定义拖动窗口区域

需求说明

由于官方提供的窗口标题并不能实现我的需求,不能很好的实现主题切换的功能,所以根据官方文档实现了一个自定义的窗口,官方文档地址:Window Customization | Tauri Apps

但是实现之后, 没有了窗体拖拽移动的功能,,,,,,而且没有边框和阴影,所以就显得特别奇怪,所以今天在这里,我都给你安排上了:

因为录制gif图的工具问题,导致录屏效果不是很好,实现的效果图:

动图效果: 

具体实现步骤

1.先实现自定义窗口效果

实现自定义窗口效果非常简单,只需要按照官方文档提示的,在tauri.config.json里面添加一个配置:"decorations": false,然后再打开你的程序,你就会发现,没有了窗口的头部,哈哈哈

  1. "windows": [
  2. {
  3. "label": "customization",
  4. "fullscreen": false,
  5. "resizable": true,
  6. "title": "FileHub",
  7. "fileDropEnabled": false,
  8. "width": 1031,
  9. "height": 580,
  10. "center": true,
  11. "decorations": false
  12. }
  13. ]

这个时候还要在tauri.config.json里面给程序加上窗口放大缩小和关闭的权限:不然后面点击按钮没效果,会报错说没有权限

  1. "allowlist": {
  2. "all": true,
  3. "http": {
  4. "scope": ["http://**", "https://**"]
  5. },
  6. "shell": {
  7. "all": false,
  8. "open": true
  9. },
  10. "fs": {
  11. "all": true,
  12. "scope": ["*", "$DOWNLOAD/*"]
  13. },
  14. "window": {
  15. "all": true
  16. }
  17. },

然后就是添加自定义的缩小,全屏,关闭按钮的功能:

其实按照官方文档的来添加也可以,我这里是使用的自己自定义的html内容样式:

html内容:因为我这里用的element-plus,所以用的他们的icon图标,你可以换成官方的内容

  1. <div data-tauri-drag-region class="titlebar">
  2. <div class="titlebar-button" id="titlebar-minimize">
  3. <img
  4. src="https://api.iconify.design/mdi:window-minimize.svg"
  5. alt="minimize"
  6. />
  7. </div>
  8. <div class="titlebar-button" id="titlebar-maximize">
  9. <img
  10. src="https://api.iconify.design/mdi:window-maximize.svg"
  11. alt="maximize"
  12. />
  13. </div>
  14. <div class="titlebar-button" id="titlebar-close">
  15. <img src="https://api.iconify.design/mdi:close.svg" alt="close" />
  16. </div>
  17. </div>

css样式:

  1. .titlebar {
  2. height: 30px;
  3. background: #329ea3;
  4. user-select: none;
  5. display: flex;
  6. justify-content: flex-end;
  7. position: fixed;
  8. top: 0;
  9. left: 0;
  10. right: 0;
  11. }
  12. .titlebar-button {
  13. display: inline-flex;
  14. justify-content: center;
  15. align-items: center;
  16. width: 30px;
  17. height: 30px;
  18. }
  19. .titlebar-button:hover {
  20. background: #5bbec3;
  21. }

然后js代码:

  1. import { appWindow } from '@tauri-apps/api/window'
  2. document
  3. .getElementById('titlebar-minimize')
  4. .addEventListener('click', () => appWindow.minimize())
  5. document
  6. .getElementById('titlebar-maximize')
  7. .addEventListener('click', () => appWindow.toggleMaximize())
  8. document
  9. .getElementById('titlebar-close')
  10. .addEventListener('click', () => appWindow.close())

 如果你使用的是vue3,需要在onMounted函数中添加:

  1. import { onMounted } from 'vue'
  2. onMounted(() => {
  3. document.getElementById('titlebar-minimize')!.addEventListener('click', () => appWindow.minimize())
  4. document.getElementById('titlebar-maximize')!.addEventListener('click', () => appWindow.toggleMaximize())
  5. document.getElementById('titlebar-close')!.addEventListener('click', () => appWindow.close())
  6. console.log("onMounted------", document.getElementById('titlebar-close'));
  7. })

然后效果就出来了: 

2.实现拖拽效果

拖拽效果其实更简单,只需要在你想要的元素上添加属性:data-tauri-drag-region 就可以了,然后你按住这个元素所在区域,就可以拖拽移动了:

3.实现窗口阴影

阴影效果要通过添加一个依赖实现:GitHub - tauri-apps/window-shadows: Add native shadows to your windows.

具体实现步骤:

3.1 给窗口添加label标签:主要是让程序识别到这个窗口

3.2 然后在src-tauri\Cargo.toml中添加依赖

window-shadows = "0.2.1"

3.3 在main.rs程序中引用

在src-tauri\src\下创建utils.rs文件,并添加内容:这里就用到了3.1中添加的customization这个lable窗口标识

  1. use tauri::{Manager, Runtime};
  2. use window_shadows::set_shadow;
  3. pub fn set_window_shadow<R: Runtime>(app: &tauri::App<R>) {
  4. let window = app.get_window("customization").unwrap();
  5. set_shadow(&window, true).expect("Unsupported platform!");
  6. }

然后在src-tauri\src\main.rs中引用这个utils.rs:

  1. // Prevents additional console window on Windows in release, DO NOT REMOVE!!
  2. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
  3. use crate::{
  4. utils::{set_window_shadow}
  5. };
  6. mod utils;
  7. fn main() {
  8. tauri::Builder::default()
  9. .setup(|app| {
  10. set_window_shadow(app);
  11. Ok(())
  12. })
  13. .invoke_handler(tauri::generate_handler![])
  14. .run(tauri::generate_context!())
  15. .expect("error while running tauri application");
  16. }

最后重启应用,就可以看到有阴影效果了:

我的项目中已经使用了,可以查看我的FileHub项目: 

 GitHub - Sjj1024/s-hub: 一个使用github作为资源存储的软件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/803661
推荐阅读
相关标签
  

闽ICP备14008679号