手记

自研Tauri 2.0+Vue3 setup+ElementPlus仿微信Exe聊天|tauri2+rust聊天室

最新重磅新作Tauri2.x+Vite5+Pinia2客户端Exe聊天模板Tauri2Vue3Chat

vue3-tauri2chat基于vite5+tauri2.0+vue3 setup+element-plus跨平台仿QQ|微信桌面端聊天应用。全新封装tauri2多窗口管理、自定义透明圆角阴影窗体、打开独立聊天窗口。

技术栈

  • 编码器:VScode
  • 技术框架:tauri2.0+vite5.4.7+vue3.5.8+vue-router^4.4.5
  • 状态管理:pinia^2.2.2
  • 本地存储:pinia-plugin-persistedstate^4.0.2
  • UI组件库:element-plus^2.8.3
  • 富文本编辑器:@vueup/vue-quill^1.2.0
  • 样式预处理:sass^1.79.3
  • 小视频滑动组件:swiper^11.1.14

项目结构模板

整体项目使用tauri2.0搭建框架,使用vite.js前端框架。采用vue3 setup语法开发。

tauri2-chat布局模板

<template>
  <div class="vu__chatbox">
    <template v-if="!route?.meta?.isNewWin">
      <div class="vu__container flexbox flex-alignc flex-justifyc">
        <div class="vu__layout flexbox flex-col">
          <div class="vu__layout-body flex1 flexbox" @contextmenu.prevent>
            <!-- 菜单栏 -->
            <slot v-if="!route?.meta?.hideMenuBar" name="menubar">
              <MenuBar />
            </slot>

            <!-- 侧边栏 -->
            <div v-if="route?.meta?.showSideBar" class="vu__layout-sidebar flexbox">
              <aside class="vu__layout-sidebar__body flexbox flex-col">
                <slot name="sidebar">
                  <SideBar />
                </slot>
              </aside>
            </div>

            <!-- 主内容区 -->
            <div class="vu__layout-main flex1 flexbox flex-col">
              <ToolBar v-if="!route?.meta?.hideToolBar" />
              <router-view v-slot="{ Component, route }">
                <keep-alive>
                  <component :is="Component" :key="route.path" />
                </keep-alive>
              </router-view>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <WinLayout />
    </template>
  </div>
</template>

tauri2.0+vue3自定义无边框透明圆角阴影

设置 decorations: false 创建无边框窗体。结合 transparent: trueshadow: false 实现自定义圆角阴影窗口。

.vu__chatbox {height: calc(100vh); padding: 5px; overflow: hidden;}
.vu__layout {
  background-color: #f5f5f5;
  overflow: hidden;
  height: 100%; width: 100%;
  position: relative; z-index: 100;
  border-radius: 8px;
  box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.15),0 1px 5px -1px rgba(0, 0, 0, 0.1),0 2px 5px rgba(0, 0, 0, 0.1);
}

tauri2.0自定义最大化/最小化/关闭按钮

<script setup>
  /**
   * tauri2.0自定义系统最大化/最小化/关闭
   * by andy  Q:282310962
   */

  import { ref } from 'vue'
  import { getCurrentWindow } from '@tauri-apps/api/window'
  import { listen } from '@tauri-apps/api/event'
  import { exit } from '@tauri-apps/plugin-process'

  import { authState } from '@/pinia/modules/auth'
  import { isTrue } from '@/utils'
  import { winSet } from '@/windows/actions'

  const authstate = authState()

  const props = defineProps({
    color: String,
    // 窗口是否可最小化
    minimizable: {type: [Boolean, String], default: true},
    // 窗口是否可最大化
    maximizable: {type: [Boolean, String], default: true},
    // 窗口是否可关闭
    closable: {type: [Boolean, String], default: true},
    // 层级
    zIndex: {type: [Number, String], default: 2024},

    // 关闭前回调,会暂停实例关闭 function(done),done用于关闭
        beforeClose: Function
  })

  const hasMaximized = ref(false)
  const isResizable = ref(true)
  const isMaximizable = ref(true)

  // 用户是否可以手动调整窗口大小
  getCurrentWindow().isResizable().then(res => {
    isResizable.value = res
  })
  // 窗口是否可以最大化
  getCurrentWindow().isMaximizable().then(res => {
    isMaximizable.value = res
  })
  // 初始监听窗口是否最大化
  getCurrentWindow().isMaximized().then(res => {
    hasMaximized.value = res
  })
  // 实时监听窗口是否最大化
  listen('tauri://resize', async() => {
    hasMaximized.value = await getCurrentWindow().isMaximized()
  })

  // 最小化
  const handleWinMin = async() => {
    // winSet('minimize')
    await getCurrentWindow().minimize()
  }
  // 最大化/还原
  const handleWinToggle = async() => {
    // winSet('max2min')
    await getCurrentWindow().toggleMaximize()
  }
  // 关闭
  const handleClose = async() => {
    const isMajor = getCurrentWindow().label.indexOf('main') > -1
    if(isMajor) {
      let el = layer({
        type: 'android',
        content: '是否最小化到托盘,不退出程序?',
        layerStyle: 'background: #f9f9f9; border-radius: 8px;',
        closable: false,
        resize: false,
        btns: [
          {
            text: '最小化托盘',
            style: 'color: #646cff',
            click: () => {
              layer.close(el)
              // winSet('hide')
              await getCurrentWindow().hide()
            }
          },
          {
            text: '退出程序',
            style: 'color: #fa5151',
            click: async() => {
              authstate.logout()
              await exit()
            }
          }
        ]
      })
    }else {
      // winSet('close')
      await getCurrentWindow().close()
    }
  }
</script>

<template>
  <div class="ev__winbtns vu__drag" :style="{'z-index': zIndex}">
    <div class="ev__winbtns-actions vu__undrag" :style="{'color': color}">
      <a v-if="isTrue(minimizable)" class="wbtn min" title="最小化" @click="handleWinMin"><i class="wicon elec-icon elec-icon-min"></i></a>
      <a v-if="isTrue(maximizable) && isResizable && isMaximizable" class="wbtn toggle" :title="hasMaximized ? '向下还原' : '最大化'" @click="handleWinToggle">
        <i class="wicon elec-icon iconfont" :class="hasMaximized ? 've-icon-shrink' : 've-icon-arrowsalt'"></i>
      </a>
      <a v-if="isTrue(closable)" class="wbtn close" title="关闭" @click="handleClose"><i class="wicon elec-icon elec-icon-quit"></i></a>
    </div>
  </div>
</template>

tauri2自定义系统托盘|右键菜单

/**
 * 自定义托盘图标
 */

use tauri::{
  tray::{MouseButton, TrayIconBuilder, TrayIconEvent}, Emitter, Manager, Runtime
};

pub fn tray_create<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
  let _ = TrayIconBuilder::with_id("tray")
    .tooltip("TAURI-WINCHAT")
    .icon(app.default_window_icon().unwrap().clone())
    .on_tray_icon_event(|tray, event| match event {
      TrayIconEvent::Click {
        id: _,
        position,
        rect: _,
        button,
        button_state: _,
      } => match button {
        MouseButton::Left {} => {
          let windows = tray.app_handle().webview_windows();
          for (key, value) in windows {
            println!("点击左键: {}", key);
            if key == "main-login" || key == "main" {
              value.show().unwrap();
              value.unminimize().unwrap();
              value.set_focus().unwrap();
            }
          }
        }
        MouseButton::Right {} => {
          println!("点击右键");
          tray.app_handle().emit("tray_contextmenu", position).unwrap();
        }
        _ => {}
      },
      TrayIconEvent::Enter {
        id: _,
        position,
        rect: _,
      } => {
        println!("鼠标滑过托盘");
        tray.app_handle().emit("tray_mouseenter", position).unwrap();
      }
      TrayIconEvent::Leave {
        id: _,
        position,
        rect: _,
      } => {
        println!("鼠标离开托盘");
        tray.app_handle().emit("tray_mouseleave", position).unwrap();
      }
      _ => {}
    })
    .build(app);
  Ok(())
}

综上就是Tauri2.0+Vue3+Pinia2实战开发桌面端聊天项目的一些知识分享。

1人推荐
随时随地看视频
慕课网APP

热门评论

自研Tauri2.0+Vite5+ElementPlus桌面聊天Exe程序

https://gf.bilibili.com/item/detail/1107133011

查看全部评论