首页 > 其他分享 >字玩FontPlayer开发笔记10 Tauri2多窗口通信

字玩FontPlayer开发笔记10 Tauri2多窗口通信

时间:2025-01-16 18:59:50浏览次数:3  
标签:__ 10 多窗口 core Tauri2 window set allow webview

字玩FontPlayer开发笔记10 Tauri2多窗口通信

字玩FontPlayer是笔者开源的一款字体设计工具,使用Vue3 + ElementUI开发,源代码:github | gitee

笔记

最近在使用Tauri进行打包应用,这两天在测试可编程脚本模块时,发现原有代码使用了window.open方法,在Tauri应用中一直触发不了新窗口弹出,查了一下说Tauri并不推荐使用window.open操作,比较好的做法是使用Tauri自带的Webview窗口功能,于是笔者就用Tauri自带的api将脚本模块重置。

设置权限

窗口功能默认是禁用的,需要开启权限才能支持。每个细分功能都用对应的权限字段,在permissions里进行设置。笔者为方便测试暂时把所有关于窗口的权限都启用了:
src-tauri/capabilities/defualt.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "default",
  "description": "enables the default permissions",
  "windows": [
    "main",
    "glyph-script"
  ],
  "remote": {
    "urls": ["https://*.tauri.app"]
  },
  "permissions": [
    {
      "identifier": "fs:scope",
      "allow": [
        {
          "path": "$APPDATA"
        },
        {
          "path": "$APPDATA/**"
        }
      ]
    },
    "core:default",
    "fs:read-files",
    "fs:write-files",
    "fs:allow-appdata-read-recursive",
    "fs:allow-appdata-write-recursive",
    "fs:default",
    "dialog:default",
    "core:event:allow-emit",
    "core:event:allow-listen",
    "core:webview:allow-clear-all-browsing-data",
    "core:webview:allow-create-webview",
    "core:webview:allow-create-webview-window",
    "core:webview:allow-get-all-webviews",
    "core:webview:allow-internal-toggle-devtools",
    "core:webview:allow-print",
    "core:webview:allow-reparent",
    "core:webview:allow-set-webview-focus",
    "core:webview:allow-set-webview-position",
    "core:webview:allow-set-webview-size",
    "core:webview:allow-set-webview-zoom",
    "core:webview:allow-webview-close",
    "core:webview:allow-webview-hide",
    "core:webview:allow-webview-position",
    "core:webview:allow-webview-show",
    "core:webview:allow-webview-size",
    "core:window:allow-available-monitors",
    "core:window:allow-center",
    "core:window:allow-close",
    "core:window:allow-create",
    "core:window:allow-current-monitor",
    "core:window:allow-cursor-position",
    "core:window:allow-destroy",
    "core:window:allow-get-all-windows",
    "core:window:allow-hide",
    "core:window:allow-inner-position",
    "core:window:allow-inner-size",
    "core:window:allow-internal-toggle-maximize",
    "core:window:allow-is-closable",
    "core:window:allow-is-decorated",
    "core:window:allow-is-enabled",
    "core:window:allow-is-focused",
    "core:window:allow-is-fullscreen",
    "core:window:allow-is-maximizable",
    "core:window:allow-is-maximized",
    "core:window:allow-is-minimizable",
    "core:window:allow-is-minimized",
    "core:window:allow-is-resizable",
    "core:window:allow-is-visible",
    "core:window:allow-maximize",
    "core:window:allow-minimize",
    "core:window:allow-monitor-from-point",
    "core:window:allow-outer-position",
    "core:window:allow-outer-size",
    "core:window:allow-primary-monitor",
    "core:window:allow-request-user-attention",
    "core:window:allow-scale-factor",
    "core:window:allow-set-always-on-bottom",
    "core:window:allow-set-always-on-top",
    "core:window:allow-set-closable",
    "core:window:allow-set-content-protected",
    "core:window:allow-set-cursor-grab",
    "core:window:allow-set-cursor-icon",
    "core:window:allow-set-cursor-position",
    "core:window:allow-set-decorations",
    "core:window:allow-set-effects",
    "core:window:allow-set-enabled",
    "core:window:allow-set-focus",
    "core:window:allow-set-fullscreen",
    "core:window:allow-set-icon",
    "core:window:allow-set-ignore-cursor-events",
    "core:window:allow-set-max-size",
    "core:window:allow-set-maximizable",
    "core:window:allow-set-min-size",
    "core:window:allow-set-minimizable",
    "core:window:allow-set-position",
    "core:window:allow-set-progress-bar",
    "core:window:allow-set-resizable",
    "core:window:allow-set-shadow",
    "core:window:allow-set-size",
    "core:window:allow-set-size-constraints",
    "core:window:allow-set-skip-taskbar",
    "core:window:allow-set-theme",
    "core:window:allow-set-title",
    "core:window:allow-set-title-bar-style",
    "core:window:allow-set-visible-on-all-workspaces",
    "core:window:allow-show",
    "core:window:allow-start-dragging",
    "core:window:allow-start-resize-dragging",
    "core:window:allow-theme",
    "core:window:allow-title",
    "core:window:allow-toggle-maximize",
    "core:window:allow-unmaximize",
    "core:window:allow-unminimize"
  ]
}
打开窗口
import { WebviewWindow } from "@tauri-apps/api/webviewWindow"

const windowOptions = {
  url: `${location.origin}${location.pathname}#/glyph-programming-editor`,
  width: 1280,
  height: 800,
  x: (screen.width - 1280) / 2,
  y: (screen.height - 800) / 2,
  devtools: true,
}
const webview = new WebviewWindow('glyph-script', windowOptions)

注意windowOptions里devtools是指开发者工具,如果希望启用调试的话,可以设置成true方便在窗口中调用webview调试窗口。

监听窗口状态
webview.once('tauri://created', async () => {
  console.log('webview created')
})

webview.once('tauri://error', function (e) {
  console.log('error creating webview', e)
})
窗口间通信

窗口间通信统一用全局Event即可。

  1. 发送事件
import { emit, listen } from '@tauri-apps/api/event'

await emit('init-data', {
  __constants: constants.value,
  __script: editGlyph.value.script,
  __isWeb: ENV.value === 'web'
})
  1. 监听事件
import { emit, listen } from '@tauri-apps/api/event'

unlistenInitData = await listen('init-data', (event) => {
  const { __constants, __parameters, __script, __isWeb } = event.payload as any

  window.__constants = __constants
  window.__parameters = __parameters
  window.__script = __script
  window.__is_web = __isWeb

  constants.value = __constants
  parameters.value = __parameters
  script.value = __script
  isWeb = __isWeb

  constants.value?.map((constant) => {
    editMap.value[constant.uuid] = false
  })

  parameters.value?.map((parameter) => {
    editMap.value[parameter.uuid] = false
  })

  codeEditor = new EditorView({
    doc: script.value,
    extensions: [basicSetup, javascript(), oneDark,
      EditorView.updateListener.of((v) => {
        script.value = v.state.doc.toString()
        console.log(v.state.doc.toString()) //监测得到的最新代码 
      }
    )],
    parent: document.getElementById('codes-container'),
  })
})
  1. 注销事件
    如果希望注销事件,直接调用事件监听赋值(unlistenInitData = await listen('init-data', (event) => {...)时的返回值(此处是unlistenInitData)即可:
unlistenInitData()

标签:__,10,多窗口,core,Tauri2,window,set,allow,webview
From: https://blog.csdn.net/toysmaker/article/details/145078738

相关文章

  • 专为高性能计算和低功耗应用设计的MPF100T-1FCVG484E MPF100TL-FCSG325E MPF100TL-FCV
    ‌PolarFireFPGA是Microchip推出的一款高性能、低功耗的FPGA产品系列,特别适用于各种需要高性能计算和低功耗的应用场景‌。这些器件包括MPF100T-1FCVG484EMPF100TL-FCG484EMPF100TL-FCG484IMPF100TL-FCSG325EMPF100TL-FCVG484EMPF100TL-FCVG484IFPGA-现场可编程门阵列......
  • docker containner挂掉,无法exec进入bash,如何修改文件的终极解决方法.210730
    场景:Nginx在bash里面配置的时候挂掉了,然后dockerstart不起来,execbash进不去,造成无法再改里面的文件了解决方法:1,dockerps–a可以查到所有docker,包括没有运行的,找到containnerID[root@hecs-29489~]#dockerps-aCONTAINERIDIMAGECOMMANDC......
  • HP惠普笔记本重装系统无法引导无法进操作系统的终极解决方法.210804
    F9进入BIOS-先进(Advanced)-安全引导配置-启用传统支持和禁用安全引导......
  • wordpress 从服务器收到预料之外的响应。此文件可能已被成功上传。请检查媒体库或刷新
    两种报错方式:1.此响应不是合法的JSON响应。2.从服务器收到预料之外的响应。此文件可能已被成功上传。请检查媒体库或刷新本页。情况:媒体服务器上传小文件没问题,大一点的文件报这个错误。原因:这是因为nginx限制了请求体大小方案:需要在nginx的虚拟机配置文件中添加:client_max_b......
  • 域用户完美执行应用程序.210907
    企业环境中,为了安全起见一般都没有赋予域用户或者企业的PC客户端用户管理员权限。但偶尔会有个别的程序一定需要管理员身份才能执行,如财务某些程序或专业的应用程序。那么如何不赋予用户管理员权限及密码但又可以让用户有权限执行指定的程序呢?下面就介绍几种主流的办法:1,runas命......
  • Docker安装wikijs wiki系统.210818
    1.拉取mysql8的镜像并运行dockerpullmysqldockerrun-d-v/data/mysql/data:/var/lib/mysql-v/data/mysql/conf:/etc/mysql/conf.d--namemysql-eTZ=Asia/Shanghai-eMYSQL\_ROOT\_PASSWORD=1234-p3306:3306mysql:latest2.进入mysql,创建并修改权限docker......
  • vscode使用github.211014
    1,vscode打开terminal,生成RSA密钥,并查看蜜月PSD:\\code\\SQL>gitinitReinitializedexistingGitrepositoryinD:/code/SQL/.git/PSD:\\code\\SQL\>gitconfig--globaluser.nameamadeusPSD:\\code\\SQL\>gitconfig--globaluser.emailvegas......
  • (14-3-02)基于Latent Diffusion Transformer的文生视频系统:数据集处理(2)加载并处理Taic
    6.4.3 加载并处理Taichi数据集文件taichi_datasets.py实现了一个Taichi数据集类,用于加载和处理分帧存储的视频数据,特别是太极表演相关的帧序列。它包括从数据目录中读取视频帧、按时间进行帧采样、将帧数据转换为张量并应用数据增强等功能。代码通过torch.utils.data.Da......
  • LeetCode:100.相同的树
    LeetCode:100.相同的树两个树:根节点的值相同,左子树相同,右子树相同。符合“分、解、合”特性。考虑选择分而治之。分:获取两个树的左子树和右子树。解:递归地判断两个树的左子树是否相同,右子树是否相同。合:将上述结果合并,如果根节点的值也相同,树就相同。/***Definitionforab......
  • STM32F103使用flash_algo解析FLM相关
    1、全局区(.bss段和.data段)根据实际情况修改2、栈顶地址根据实际情况修改/*FlashOSRoutines(AutomagicallyGenerated)*Copyright(c)2009-2015ARMLimited*/#include"flash_blob.h"//代码区flash_code[]使用JLINK/STLINK等放到RAM,一般是0x20000000staticconst......