wails的项目配置及应用配置
qingheluo2024-08-27清河洛596
在创建了一个wails项目后,如果需要对项目进行更精细的控制,有两个配置可以为我们提供项目的精细化控制项目配置,用于配置项目的静态属性,包括版本、名称、编译等,配置文件为项目根目录中的wails.json文件应用配置,用于配置程序运行时的属性,包括窗口标题、尺寸、颜色、菜单、回调等,使用结构体options.App来创建项目配置
{
"version": "",
"name": "",
"info": { // 程序元数据
"companyName": "", // 公司名称,默认项目...
在创建了一个wails项目后,如果需要对项目进行更精细的控制,有两个配置可以为我们提供项目的精细化控制
项目配置,用于配置项目的静态属性,包括版本、名称、编译等,配置文件为项目根目录中的wails.json文件
应用配置,用于配置程序运行时的属性,包括窗口标题、尺寸、颜色、菜单、回调等,使用结构体options.App来创建
项目配置
{ "version": "", "name": "", "info": { // 程序元数据 "companyName": "", // 公司名称,默认项目名 "productName": "", // 产品名称,默认项目名 "productVersion": "1.0.0", // 产品版本 "copyright": "Copyright.........", // 产品版权 "comments": "" // 程序描述,默认Built using Wails (https://wails.app) }, "build:dir": "build", // 项目编译文件所在目录 "wailsjsdir": "", // 编译生成的Wails JS模块目录 "outputfilename": "", // 编译生成二进制文件的名称 "obfuscated": false, // 编译时是否进行代码混淆 "garbleargs": "" // 使用obfuscated标志时传递给乱码命令的参数 "frontend:dir": "frontend", // 前端目录路径 "assetdir": "assets", // 前端静态资源目录 "frontend:install": "npm install", // 前端安装Node依赖命令 "frontend:build": "npm run build", // 前端编译命令 "frontend:dev:build": "", // 开启"实时开发"模式时运行的前端编译命令 "frontend:dev": "", // 优先使用frontend:dev:build // 没有设置使用frontend:dev // 没有设置使用frontend:build "frontend:dev:watcher": "", // 开启"实时开发"模式时运行的监控进程命令 "devServer": "localhost:34115", // 绑定的开发服务器地址 "appargs": "", // 实时开发模式下以shell样式传递给应用程序的参数 "reloaddirs": "", // 触发重新加载的附加目录(逗号分隔) "debounceMS": 100, // 检测到静态资源改变后重新加载的延时,单位毫秒 }
应用配置
要使用应用配置,需要导入相关库
import ( "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/windows" ) func main() { err := wails.Run(&options.App{ // 应用配置项 }) if err != nil { panic(err) } }
常用的应用配置项有
Title string("") Width int(1024) Height int(768) MinWidth int MinHeight int MaxWidth int MaxHeight int DisableResize: bool(false) // 是否禁用窗口尺寸调整 WindowStartState: options.WindowStartState(1) // 启动时窗口大小 // type WindowStartState int // Normal WindowStartState = 0 Maximised WindowStartState = 1 Minimised WindowStartState = 2 Fullscreen WindowStartState = 3 Frameless bool(true) // 启用无边框模式 StartHidden bool(false) // 启动时隐藏,直到调用显示窗口函数 HideWindowOnClose bool(false) // 关闭时隐藏窗口而不是退出程序 AlwaysOnTop bool(false) // 窗口在失去焦点时保持在其他窗口之上 BackgroundColour:*options.RGBA // 设置窗口背景颜色 默认&options.RGBA{R:255,G:255,B:255,A255} type RGBA struct { R uint8 `json:"r"` G uint8 `json:"g"` B uint8 `json:"b"` A uint8 `json:"a"` } options.NewRGBA(r, g, b, a uint8) *RGBA options.NewRGB(r, g, b, uint8) *RGBA // a为255 Bind []interface{} // 定义需要绑定到前端的方法的结构实例切片 EnumBind []interface{} DragAndDrop *DragAndDrop type DragAndDrop struct { EnableFileDrop bool // 启用wails拖放功能 DisableWebViewDrop bool // 禁用webview的拖放功能,防止在Web视图中意外打开拖入文件 CSSDropProperty string // 指示用于标识哪些元素可用于拖动窗口的 CSS 属性 CSSDropValue string // 指示CSSDragProperty样式应该具有什么值才能拖动窗口 } CSSDropProperty默认为"--wails-draggable",CSSDragValue默认为"drag" 只有设置了该属性的元素才能接收拖放事件 Logger logger.Logger logger.NewDefaultLogger() // 应用程序要使用的记录器 LogLevel logger.LogLevel logger.INFO // 默认日志级别 LogLevelProduction logger.LogLevel logger.ERROR // 生产构建的默认日志级别 import "github.com/wailsapp/wails/v2/pkg/logger" type LogLevel uint8 const ( TRACE LogLevel = 1 DEBUG LogLevel = 2 INFO LogLevel = 3 WARNING LogLevel = 4 ERROR LogLevel = 5 ) ErrorFormatter ErrorFormatter // 重写后端方法返回的错误的格式 // type ErrorFormatter func(error) any EnableFraudulentWebsiteDetection bool(false) // 启用扫描服务检测欺诈内容 Debug Debug // 在应用程序启动时打开调试器调,该配置项在生产版本中这些选项将被忽略 type DeBug struct { OpenInspectorOnStartup bool } 唯一的字段用于设置是否在应用程序启动时打开调试器调 AssetServer: *assetserver.Options // 网络资源请求 type Options struct { Assets: fs.FS, // 打包的静态资源变量 Handler: http.Handler, // 无法由Assets提供静态资源的请求处理程序 // 无果未定义,GET请求返回http.StatusNotFound,其他请求返回http.StatusMethodNotAllowed Middleware: assetserver.Middleware, // 自定义请求路由 } type Middleware func(next http.Handler) http.Handler func ChainMiddleware(middleware ...Middleware) Middleware{...} Menu:*menu.Menu EnableDefaultContextMenu bool(false) // 在生产环境中启用浏览器的默认上下文菜单 // 但devtools仍不可用,除非使用-devtools构建标志 // 要覆盖此行为,可在HTML元素上使用CSS属性--default-contextmenu:auto/show/hide OnStartup func(ctx context.Context) app.startup // 在前端创建之后且index.html加载之前调用 OnDomReady func(ctx context.Context) app.domready // 在前端加载完毕index.html及其资源后调用 OnShutdown func(ctx context.Context) app.shutdown // 在前端被销毁之后,应用程序终止之前调用 OnBeforeClose func(ctx context.Context) (prevent bool) app.beforeClose // 在通过单击窗口关闭按钮或调用runtime.Quit即将退出应用程序时调用 // 返回true将导致应用程序继续,false将继续正常关闭 Windows *windows.Options // windows相关配置项 Mac *mac.Options // MacOS相关配置项 Linux *linux.Options // Linux相关配置项
windows.Options
表示windows中的特有配置项
type Options struct { WebviewIsTransparent bool // 窗口透明,默认全透明 WindowIsTranslucent bool // 窗口半透明,需要开启窗口透明 BackdropType BackdropType // 设置窗口的半透明类型。 这仅在 窗口半透明 设置为 true 时适用 type BackdropType int32 const ( Auto BackdropType = 0 None BackdropType = 1 // 不使用半透明 Mica BackdropType = 2 // 使用 Mica 效果 Acrylic BackdropType = 3 // 使用 亚克力 效果 Tabbed BackdropType = 4 // 使用Tabbed,类似于Mica ) DisableWindowIcon bool // 禁用窗口左上角图标 IsZoomControlEnabled bool // 启用缩放比例,注意仅可在选项中设置缩放比例但不允许在运行时更改 ZoomFactor float64 // 设置缩放比例 DisablePinchZoom bool // 禁止减小缩放比例 DisableFramelessWindowDecorations bool // 移除无边框模式下的窗口装饰。将不会有Aero阴影和圆角显示在窗口上 WebviewUserDataPath string // 指定WebView2存储用户数据的路径。 为空将使用 %APPDATA%\[BinaryName.exe] WebviewBrowserPath string // 指定WebView2可执行文件和库的目录,为空使用系统中安装的webview2 Theme Theme // 主题 type Theme int const ( SystemDefault Theme = 0 Dark Theme = 1 Light Theme = 2 ) CustomTheme *ThemeSettings // 自定义主题 type ThemeSettings struct { DarkModeTitleBar int32 DarkModeTitleBarInactive int32 DarkModeTitleText int32 DarkModeTitleTextInactive int32 DarkModeBorder int32 DarkModeBorderInactive int32 LightModeTitleBar int32 LightModeTitleBarInactive int32 LightModeTitleText int32 LightModeTitleTextInactive int32 LightModeBorder int32 LightModeBorderInactive int32 } 可以使用16进制数字:0x00BBGGRR 也可以使用函数:RGB(r, g, b uint8) int32 任何未提供的值都将默认为黑色 Messages *Messages // 如果找不到有效的webview2运行时定制的用户消息 type windows struct{ InstallationRequired string // 提示下载安装弹窗内容 UpdateRequired string // 需要更新弹窗内容 MissingRequirements string // 提示下载安装弹窗标题 Webview2NotInstalled string Error string FailedToInstall string // 安装失败弹窗内容 PressOKToInstall string ContactAdmin string WebView2ProcessCrash string // WebView2进程崩溃 } ResizeDebounceMS uint16 // 调整窗口大小时去抖动webview2重绘的时间量。默认0,将尽可能快地执行重绘 OnSuspend func() // 当Windows切换到低功耗模式(挂起/休眠)时调用此函数 OnResume func() // 当Windows从低功耗模式(挂起/休眠)恢复时调用此函数 WebviewGpuIsDisabled bool // 禁用webview的GPU硬件加速 WebviewDisableRendererCodeIntegrity bool // 是否禁用WebView2的渲染器代码完整性检查 // 某些安全软件使用未签名或错误签名的dll注入WebView2 // 当启动程序是检测到渲染器代码被修改会停止WebView2进程 // 设置为true时也意味着允许恶意软件注入WebView2 EnableSwipeGestures bool // 启用滑动手势 }