2021-09-27 19:35:30 +10:00
---
sidebar_position: 3
---
# 参数选项
## 应用程序参数选项
2022-09-18 20:01:50 +08:00
该 `Options.App` 结构包含应用程序配置。 它被传递给 `wails.Run()` 方法:
2021-09-27 19:35:30 +10:00
```go title="Example"
2022-11-09 03:25:18 +08:00
import (
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/linux"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/options/windows"
)
2021-09-27 19:35:30 +10:00
func main() {
err := wails.Run(&options.App{
2022-09-12 21:56:43 +10:00
Title: "Menus Demo",
Width: 800,
Height: 600,
DisableResize: false,
Fullscreen: false,
2022-11-21 19:13:43 +08:00
WindowStartState: options.Maximised,
2022-09-12 21:56:43 +10:00
Frameless: true,
MinWidth: 400,
MinHeight: 400,
MaxWidth: 1280,
MaxHeight: 1024,
StartHidden: false,
HideWindowOnClose: false,
BackgroundColour: &options.RGBA{R: 0, G: 0, B: 0, A: 255},
AlwaysOnTop: false,
2022-11-09 03:25:18 +08:00
AssetServer: &assetserver.Options{
Assets: assets,
Handler: assetsHandler,
Middleware: assetsMidldeware,
},
2022-09-12 21:56:43 +10:00
Menu: app.applicationMenu(),
Logger: nil,
LogLevel: logger.DEBUG,
LogLevelProduction: logger.ERROR,
OnStartup: app.startup,
OnDomReady: app.domready,
OnShutdown: app.shutdown,
OnBeforeClose: app.beforeClose,
CSSDragProperty: "--wails-draggable",
CSSDragValue: "drag",
2023-09-08 23:25:21 +10:00
EnableDefaultContextMenu: false,
2023-03-11 17:57:56 +11:00
EnableFraudulentWebsiteDetection: false,
2022-10-26 05:02:36 +08:00
ZoomFactor: 1.0,
IsZoomControlEnabled: false,
2021-09-27 19:35:30 +10:00
Bind: []interface{}{
app,
},
2023-09-08 23:25:21 +10:00
ErrorFormatter: func(err error) any { return err.Error() },
2021-09-27 19:35:30 +10:00
Windows: &windows.Options{
2022-02-16 10:28:46 +01:00
WebviewIsTransparent: false,
WindowIsTranslucent: false,
2022-09-18 11:00:39 +10:00
BackdropType: windows.Mica,
2022-02-16 10:28:46 +01:00
DisableWindowIcon: false,
DisableFramelessWindowDecorations: false,
WebviewUserDataPath: "",
2022-09-12 21:56:43 +10:00
WebviewBrowserPath: "",
2022-04-04 18:05:54 +08:00
Theme: windows.SystemDefault,
CustomTheme: &windows.ThemeSettings{
DarkModeTitleBar: windows.RGB(20, 20, 20),
DarkModeTitleText: windows.RGB(200, 200, 200),
DarkModeBorder: windows.RGB(20, 0, 20),
LightModeTitleBar: windows.RGB(200, 200, 200),
LightModeTitleText: windows.RGB(20, 20, 20),
LightModeBorder: windows.RGB(200, 200, 200),
},
2023-04-17 23:48:27 +10:00
// User messages that can be customised
2022-04-04 18:05:54 +08:00
Messages *windows.Messages
2023-04-17 23:48:27 +10:00
// OnSuspend is called when Windows enters low power mode
2022-09-18 11:00:39 +10:00
OnSuspend func()
2023-04-17 23:48:27 +10:00
// OnResume is called when Windows resumes from low power mode
2023-03-11 17:57:56 +11:00
OnResume func(),
WebviewGpuDisabled: false,
2021-09-27 19:35:30 +10:00
},
2021-11-20 20:50:32 +08:00
Mac: &mac.Options{
TitleBar: &mac.TitleBar{
TitlebarAppearsTransparent: true,
HideTitle: false,
HideTitleBar: false,
FullSizeContent: false,
UseToolbar: false,
HideToolbarSeparator: true,
},
2022-08-09 20:48:29 +10:00
Appearance: mac.NSAppearanceNameDarkAqua,
2021-11-20 20:50:32 +08:00
WebviewIsTransparent: true,
WindowIsTranslucent: false,
About: &mac.AboutInfo{
Title: "My Application",
Message: "© 2021 Me",
Icon: icon,
},
},
2022-04-04 18:05:54 +08:00
Linux: &linux.Options{
Icon: icon,
2022-10-04 16:22:35 +08:00
WindowIsTranslucent: false,
2023-03-11 17:57:56 +11:00
WebviewGpuPolicy: linux.WebviewGpuPolicyAlways,
2023-09-08 23:25:21 +10:00
ProgramName: "wails"
2022-04-04 18:05:54 +08:00
},
2022-11-18 19:11:25 +08:00
Debug: options.Debug{
OpenInspectorOnStartup: false,
},
2021-09-27 19:35:30 +10:00
})
2022-11-21 19:13:43 +08:00
2021-09-27 19:35:30 +10:00
if err != nil {
log.Fatal(err)
}
}
```
### 标题
窗口标题栏中显示的文本。
2022-09-18 20:01:50 +08:00
名称:Title<br/> 类型:`string`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 宽度
2022-09-12 21:56:43 +10:00
窗口的初始宽度。
2022-09-18 20:01:50 +08:00
名称:Width<br/> 类型:`int`<br/> 默认值:1024.
2021-09-27 19:35:30 +10:00
### 高度
2022-09-12 21:56:43 +10:00
窗口的初始高度。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:Height<br/> 类型:`int`<br/> 默认值:768
2021-09-27 19:35:30 +10:00
### 禁用调整窗口尺寸
2022-08-09 20:48:29 +10:00
默认情况下,主窗口可调整大小。 将此设置为 `true` 将使其保持固定大小。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:DisableResize<br/> 类型:`bool`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
### 全屏
2021-09-27 19:35:30 +10:00
2022-11-21 19:13:43 +08:00
已弃用:请使用 [窗口启动状态](#窗口启动状态).
2021-09-27 19:35:30 +10:00
2022-11-21 19:13:43 +08:00
### 窗口启动状态
定义窗口在启动时应如何呈现。
| 值 | Win | Mac | Lin |
| -------------- | --- | --- | --- |
| Fullscreen(全屏) | ✅ | ✅ | ✅ |
| Maximised(最大化) | ✅ | ✅ | ✅ |
| Minimised(最小化) | ✅ | ❌ | ✅ |
名称:WindowStartState<br/> 类型:`options.WindowStartState`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 无边框
2022-09-18 20:01:50 +08:00
设置为`true`时,窗口将没有边框或标题栏。 另请参阅 [无边框窗口](../guides/frameless)。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:Frameless<br/> 类型:`bool`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 最小宽度
2022-09-18 20:01:50 +08:00
这将设置窗口的最小宽度。 如果给出的值 `Width` 小于这个值,窗口将被设置为 `MinWidth` 默认值。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:MinWidth<br/> 类型:`int`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 最小高度
2022-09-18 20:01:50 +08:00
这将设置窗口的最小高度。 如果给出的值 `Height` 小于这个值,窗口将被设置为 `MinHeight` 默认值。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:MinHeight<br/> 类型:`int`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 最大宽度
2022-09-18 20:01:50 +08:00
这将设置窗口的最大宽度。 如果给出的值 `Width` 大于这个值,窗口将被设置为 `MaxWidth` 默认值。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:MaxWidth<br/> 类型:`int`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 最大高度
2022-09-18 20:01:50 +08:00
这将设置窗口的最大高度。 如果给出的值 `Height` 大于这个值,窗口将被设置为 `MaxHeight` 默认值。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:MaxHeight<br/> 类型:`int`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 启动时隐藏窗口
2022-09-18 20:01:50 +08:00
设置为 `true` 时,应用程序将被隐藏,直到调用 [显示窗口](../reference/runtime/window#显示窗口)。
名称:StartHidden<br/> 类型:`bool`
2021-09-27 19:35:30 +10:00
### 关闭时隐藏窗口
2022-09-18 20:01:50 +08:00
默认情况下,关闭窗口将关闭应用程序。 将此设置为 `true` 意味着关闭窗口将隐藏窗口。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
隐藏窗口。
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
名称:HideWindowOnClose<br/> 类型:`bool`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
### 背景颜色
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
此值是窗口的默认背景颜色。 示例:options.NewRGBA(255,0,0,128) - 红色,透明度为 50%
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:BackgroundColour<br/> 类型:`*options.RGBA`<br/> 默认值:white
2021-09-27 19:35:30 +10:00
2021-10-29 22:15:09 +08:00
### 窗口固定在最顶层
窗口在失去焦点时应保持在其他窗口之上。
2022-09-18 20:01:50 +08:00
名称:AlwaysOnTop<br/> 类型:`bool`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
### 资产
2021-09-27 19:35:30 +10:00
2022-11-09 03:25:18 +08:00
已弃用:请在 [AssetServer 特定选项](#资产服务器) 上使用资产。
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
### 资产处理程序
2021-09-27 19:35:30 +10:00
2022-11-09 03:25:18 +08:00
已弃用:请在 [AssetServer 特定选项](#资产服务器) 上使用资产处理程序。
2021-09-27 19:35:30 +10:00
2022-11-09 03:25:18 +08:00
### 资产服务器
2021-11-20 20:50:32 +08:00
2022-11-09 03:25:18 +08:00
这定义了资产服务器特定的选项。 它允许使用静态资产自定义资产服务器,使用 `http.Handler` 动态地提供资产或使用 `assetsserver.Middleware` 钩到请求链。
并非当前支持 `http.Request` 的所有功能,请参阅以下功能矩阵:
2022-12-01 16:18:59 +08:00
| 功能 | Win | Mac | Lin |
| ----------------------- | --- | --- | ------ |
| GET | ✅ | ✅ | ✅ |
| POST | ✅ | ✅ | ✅ [^1] |
| PUT | ✅ | ✅ | ✅ [^1] |
| PATCH | ✅ | ✅ | ✅ [^1] |
| DELETE | ✅ | ✅ | ✅ [^1] |
| Request Headers | ✅ | ✅ | ✅ [^1] |
2023-04-17 23:48:27 +10:00
| Request Body | ✅ | ✅ | ✅ [^2] |
| Request Body Streaming | ✅ | ✅ | ✅ [^2] |
2022-12-01 16:18:59 +08:00
| Response StatusCodes | ✅ | ✅ | ✅ [^1] |
| Response Headers | ✅ | ✅ | ✅ [^1] |
| Response Body | ✅ | ✅ | ✅ |
2023-03-11 17:57:56 +11:00
| Response Body Streaming | ❌ | ✅ | ✅ |
2022-12-01 16:18:59 +08:00
| WebSockets | ❌ | ❌ | ❌ |
| HTTP Redirects 30x | ✅ | ❌ | ❌ |
2022-08-09 20:48:29 +10:00
2022-11-09 03:25:18 +08:00
名称: AssetServer<br/> 类型: `*assetserver.Options`
#### 资产
应用程序要使用的静态前端资产。
首先尝试从 `fs.FS` 提供 GET 请求。 如果 `fs.FS` 为该文件返回 `os.ErrNotExist`,则请求处理将回退到 [处理程序](#处理程序) 并尝试服务来自它的 GET 请求。
如果设置为 nil,则所有 GET 请求都将转发给 [处理程序](#处理程序)。
名称: Assets<br/> 类型: `fs.FS`
#### 处理程序
资产处理程序是一个通用的 `http.Handler`,用于对无法找到的资产进行后备处理。
2022-11-21 19:13:43 +08:00
由于 `os.ErrNotExist`,对于每个无法从 [资产](#资产) 提供服务的 GET 请求,都会调用该处理程序。 此外,所有非 GET 请求将始终从此处理程序提供服务。 如果未定义,则调用处理程序的结果如下:
2022-11-09 03:25:18 +08:00
2022-11-21 19:13:43 +08:00
- GET 请求: `http.StatusNotFound`
2022-11-09 03:25:18 +08:00
- 其他请求: `http.StatusMethodNotAllowed`
2022-09-18 20:01:50 +08:00
注意:当与前端 DevServer 结合使用时,可能会有一些限制,例如。 Vite 在不包含文件扩展名的每个路径上提供 index.html。
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
名称:AssetsHandler<br/> 类型:`http.Handler`
2022-11-09 03:25:18 +08:00
#### 中间件
中间件是一个 HTTP 中间件,它允许挂钩到资产服务器请求链。 它允许动态跳过默认请求处理程序,例如实现专门的路由等。 调用中间件来构建资产服务器使用的新 `http.Handler`,它还接收资产服务器使用的默认处理程序作为参数。
如果未定义,则执行默认的资产服务器请求链。
名称: Middleware<br/> 类型: `assetserver.Middleware`
2022-09-18 20:01:50 +08:00
### 菜单
应用程序要使用的菜单。 [菜单参考](../reference/runtime/menu) 中有关菜单的更多详细信息。
:::note
在 Mac 上,如果未指定菜单,将创建一个默认菜单。
:::
名称:Menu<br/> 类型:`*menu.Menu`
2022-09-12 21:56:43 +10:00
2021-09-27 19:35:30 +10:00
### 日志
2022-09-18 20:01:50 +08:00
应用程序要使用的记录器。 有关日志记录的更多详细信息,请参阅 [日志参考](../reference/runtime/log)。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:Logger<br/> 类型:`logger.Logger`<br/> 默认值:Logs to Stdout
2021-11-20 20:50:32 +08:00
2022-08-09 20:48:29 +10:00
### 日志级别
2022-09-18 20:01:50 +08:00
默认日志级别。 有关日志记录的更多详细信息,请参阅 [日志参考](../reference/runtime/log)。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:LogLevel<br/> 类型:`logger.LogLevel`<br/> 默认值:开发模式为 `Info`, 生产模式为 `Error`
### 生产日志级别
生产构建的默认日志级别。 有关日志记录的更多详细信息,请参阅 [日志参考](../reference/runtime/log)。
名称:LogLevelProduction<br/> 类型:`logger.LogLevel`<br/> 默认值:`Error`
2022-09-12 21:56:43 +10:00
2022-08-09 20:48:29 +10:00
### 应用启动回调
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
此回调在前端创建之后调用,但在 `index.html` 加载之前调用。 它提供了应用程序上下文。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:OnStartup<br/> 类型:`func(ctx context.Context)`
2022-09-12 21:56:43 +10:00
2022-08-09 20:48:29 +10:00
### 前端 Dom 加载完成回调
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
在前端加载完毕 `index.html` 及其资源后调用此回调。 它提供了应用程序上下文。
2021-12-04 21:04:59 +08:00
2022-09-18 20:01:50 +08:00
名称:OnDomReady<br/> 类型:`func(ctx context.Context)`
2022-09-12 21:56:43 +10:00
2022-08-09 20:48:29 +10:00
### 应用退出回调
2021-12-04 21:04:59 +08:00
2022-08-09 20:48:29 +10:00
在前端被销毁之后,应用程序终止之前,调用此回调。 它提供了应用程序上下文。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:OnShutdown<br/> 类型:`func(ctx context.Context)`
2022-09-12 21:56:43 +10:00
2022-09-18 11:00:39 +10:00
### 应用关闭前回调
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
如果设置了此回调,它将在通过单击窗口关闭按钮或调用`runtime.Quit`即将退出应用程序时被调用. 返回 `true` 将导致应用程序继续,`false` 将继续正常关闭。 返回 true 将导致应用程序继续,false 将继续正常关闭。 这有助于与用户确认他们希望退出程序。
2021-12-04 21:04:59 +08:00
2022-09-18 20:01:50 +08:00
示例:
2022-09-12 21:56:43 +10:00
2022-08-09 20:48:29 +10:00
```go title=windowsapp.go
func (b *App) beforeClose(ctx context.Context) (prevent bool) {
dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
2022-09-18 20:01:50 +08:00
Type: runtime.QuestionDialog,
2022-08-09 20:48:29 +10:00
Title: "Quit?",
Message: "Are you sure you want to quit?",
})
if err != nil {
return false
}
return dialog != "Yes"
2022-09-18 20:01:50 +08:00
}
2022-08-09 20:48:29 +10:00
```
2022-09-18 20:01:50 +08:00
名称:OnBeforeClose<br/> 类型:`func(ctx context.Context) bool`
2022-09-12 21:56:43 +10:00
2022-11-21 19:13:43 +08:00
### CSS 拖动属性
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
指示用于标识哪些元素可用于拖动窗口的 CSS 属性。 默认值:`--wails-draggable`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
名称:CSSDragProperty<br/> 类型:`string`
2022-09-12 21:56:43 +10:00
2022-11-21 19:13:43 +08:00
### CSS 拖动值
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
指示 `CSSDragProperty` 样式应该具有什么值才能拖动窗口。 默认值:`drag`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
名称:CSSDragValue<br/> 类型:`string`
2022-09-12 21:56:43 +10:00
2023-09-08 23:25:21 +10:00
### EnableDefaultContextMenu
EnableDefaultContextMenu enables the browser's default context-menu in production.
2023-10-23 20:38:03 +11:00
By default, the browser's default context-menu is only available in development and in a `-debug` [build](../reference/cli.mdx#build) along with the devtools inspector, Using this option you can enable the default context-menu in `production` while the devtools inspector won't be available unless the `-devtools` build flag is used.
2023-09-08 23:25:21 +10:00
When this option is enabled, by default the context-menu will only be shown for text contexts (where Cut/Copy/Paste is needed), to override this behavior, you can use the CSS property `--default-contextmenu` on any HTML element (including the `body`) with the following values :
| CSS Style | Behavior |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--default-contextmenu: auto;` | (**default**) will show the default context menu only if :<br/> contentEditable is true OR text has been selected OR element is input or textarea |
| `--default-contextmenu: show;` | will always show the default context menu |
| `--default-contextmenu: hide;` | will always hide the default context menu |
This rule is inherited like any normal CSS rule, so nesting works as expected.
:::note
This filtering functionality is only enabled in production, so in development and in debug build, the full context-menu is always available everywhere.
:::
:::warning
This filtering functionality is NOT a security measure, the developer should expect that the full context-menu could be leaked anytime which could contain commands like (Download image, Reload, Save webpage), if this is a concern, the developer SHOULD NOT enable the default context-menu.
:::
Name: EnableDefaultContextMenu<br/> Type: `bool`
2023-03-11 17:57:56 +11:00
### 启用欺诈网站检测
EnableFraudulentWebsiteDetection 启用针对欺诈内容(例如恶意软件或网络钓鱼尝试)的扫描服务。 这些服务可能会从你的应用中发送信息,比如导航到苹果和微软的云服务的url和其他内容。
名称:EnableFraudulentWebsiteDetection<br/> 类型:`bool`
2022-11-21 19:13:43 +08:00
### 缩放比例
名称:ZoomFactor<br/> 类型:`float64`
这定义了 WebView2 的缩放比例。 这是匹配 Edge 用户激活放大或缩小的选项
### 启用缩放比例
名称:IsZoomControlEnabled<br/> 类型:`bool`
这将允许用户更改缩放比例。 请注意,可以在选项中设置缩放比例,但不允许在运行时更改它。 适用于屏幕固定的或类似的应用程序。
2022-09-18 20:01:50 +08:00
### 绑定
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
定义需要绑定到前端的方法的结构实例切片。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:Bind<br/> 类型:`[]interface{}`
2022-09-12 21:56:43 +10:00
2023-09-08 23:25:21 +10:00
### ErrorFormatter
A function that determines how errors are formatted when returned by a JS-to-Go method call. The returned value will be marshalled as JSON.
Name: ErrorFormatter<br/> Type: `func (error) any`
2022-08-09 20:48:29 +10:00
### Windows
2022-09-18 11:00:39 +10:00
这定义了 [Windows 特定的选项](#windows)。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:Windows<br/> 类型:`*windows.Options`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
#### Webview 透明
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
当使用 `alpha` 值 `0` 时,将此设置为 true 将使 webview 背景透明。 这意味着如果您在 CSS 中使用 `rgba(0,0,0,0)` 作为 `background-color`,则主机窗口将显示出来。 通常与 [窗口半透明](#窗口半透明) 结合使用以制作看起来冷冰冰的应用程序。
2021-09-27 19:35:30 +10:00
2022-09-18 20:01:50 +08:00
名称:WebviewIsTransparent<br/> 类型:`bool`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
#### 窗口半透明
2022-08-09 20:48:29 +10:00
2022-09-18 11:00:39 +10:00
将此设置为 `true` 将使窗口半透明。 通常与 [Webview 透明](#webview-透明) 结合使用。
对于 build 22621 之前的 Windows 11 版本,将使用 [BlurBehind](https://learn.microsoft.com/en-us/windows/win32/dwm/blur-ovw) 方法来实现半透明,这可能会很慢。 对于构建 build 22621 之后的 Windows 11 版本,这将启用速度更快的新半透明类型。 默认情况下,使用的半透明类型将由 Windows 确定。 要对此进行配置,请使用 [背景类型](#背景类型) 选项。
2021-12-04 21:04:59 +08:00
2022-09-18 20:01:50 +08:00
名称:WindowIsTranslucent<br/> 类型:`bool`
2022-09-12 21:56:43 +10:00
2022-09-18 11:00:39 +10:00
#### 背景类型
:::note
需要 Windows 11 build 22621 或更高版本。
:::
设置窗口的半透明类型。 这仅在 [窗口半透明](#窗口半透明) 设置为 `true` 时适用。
名称:BackdropType<br/> 类型:`windows.BackdropType`
值可以是以下之一:
| 值 | 描述 |
| ------- | -------------------------------------------------------------------------------- |
| Auto | 让 Windows 决定使用哪个背景 |
| None | 不要使用半透明 |
| Acrylic | 使用 [亚克力](https://learn.microsoft.com/en-us/windows/apps/design/style/acrylic) 效果 |
| Mica | 使用 [Mica](https://learn.microsoft.com/en-us/windows/apps/design/style/mica) 效果 |
| Tabbed | 使用 Tabbed。 这是一个类似于 Mica 的背景。 |
2022-09-18 20:01:50 +08:00
#### 禁用窗口图标
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
将此设置为 `true` 将删除标题栏左上角的图标。
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
名称:DisableWindowIcon<br/> 类型:`bool`
2022-02-01 16:43:03 +08:00
2022-09-18 20:01:50 +08:00
#### 禁用无边框窗口装饰
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
将此设置为 `true` 将移除 [无边框](#无边框) 模式下的窗口装饰。 这意味着将不会有`Aero 阴影` 和 `圆角`显示在窗口上。 请注意,'圆角' 只在 Windows 11 上支持。
2022-02-01 16:43:03 +08:00
2022-09-18 20:01:50 +08:00
名称:DisableFramelessWindowDecorations<br/> 类型:`bool`
2022-04-04 18:05:54 +08:00
2022-09-18 20:01:50 +08:00
#### Webview 用户数据路径
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
这定义了 WebView2 存储用户数据的路径。 如果为空将使用 `%APPDATA%\[BinaryName.exe]`。
名称:WebviewUserDataPath<br/> 类型:`string`
#### Webview 浏览器路径
这定义了带有 WebView2 可执行文件和库的目录的路径 如果为空,则使用系统中安装的 webview2
有关固定版本运行时分发的重要信息:
- [如何获取和提取运行时](https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution#details-about-the-fixed-version-runtime-distribution-mode)
- [固定版本的已知问题](https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution#known-issues-for-fixed-version)
- [WebView2 Runtime 固定版本的路径不应包含 \Edge\Application](https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl?view=webview2-1.0.1245.22#createcorewebview2environmentwithoptions)
名称:WebviewBrowserPath<br/> 类型:`string`
#### 主题
最低 Windows 版本:Windows 10 2004/20H1
2022-04-04 18:05:54 +08:00
这定义了应用程序应该使用的主题:
2022-09-18 20:01:50 +08:00
| 值 | 描述 |
| ------------- | -------------------------------------------- |
| SystemDefault | _默认_。 主题将基于系统默认值。 如果用户更改了他们的主题,应用程序将更新以使用新设置 |
| Dark | 该应用程序将只使用深色主题 |
| Light | 该应用程序将专门使用浅色主题 |
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
名称:Theme<br/> 类型:`windows.Theme`
2022-04-04 18:05:54 +08:00
2022-09-18 20:01:50 +08:00
#### 自定义主题
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
:::note
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
最低 Windows 版本:Windows 10/11 2009/21H2 Build 22000
2022-04-04 18:05:54 +08:00
2022-09-18 20:01:50 +08:00
:::
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
允许您为浅色和深色模式以及窗口处于活动或非活动状态的 TitleBar、TitleText 和 Border 指定自定义颜色。
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
名称:CustomTheme<br/> 类型:`windows.CustomTheme`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
##### 自定义主题类型
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
CustomTheme 结构体使用 `int32` 指定颜色值。 它们采用标准(!)Windows 格式:`0x00BBGGAA`。 These are in the standard(!) Windows format of: `0x00BBGGAA`. 提供了一个辅助函数来将 RGB 转换为这种格式:`windows.RGB(r,g,b uint8)`。
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
注意:任何未提供的值都将默认为黑色。
2022-08-09 20:48:29 +10:00
```go
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
}
```
2022-09-18 20:01:50 +08:00
示例:
2022-09-12 21:56:43 +10:00
2022-04-04 18:05:54 +08:00
```go
CustomTheme: &windows.ThemeSettings{
// Theme to use when window is active
DarkModeTitleBar: windows.RGB(255, 0, 0), // Red
DarkModeTitleText: windows.RGB(0, 255, 0), // Green
DarkModeBorder: windows.RGB(0, 0, 255), // Blue
LightModeTitleBar: windows.RGB(200, 200, 200),
LightModeTitleText: windows.RGB(20, 20, 20),
LightModeBorder: windows.RGB(200, 200, 200),
// Theme to use when window is inactive
DarkModeTitleBarInactive: windows.RGB(128, 0, 0),
DarkModeTitleTextInactive: windows.RGB(0, 128, 0),
DarkModeBorderInactive: windows.RGB(0, 0, 128),
LightModeTitleBarInactive: windows.RGB(100, 100, 100),
LightModeTitleTextInactive: windows.RGB(10, 10, 10),
LightModeBorderInactive: windows.RGB(100, 100, 100),
},
```
2022-09-18 20:01:50 +08:00
#### 消息
2021-11-20 20:50:32 +08:00
2022-09-12 21:56:43 +10:00
一个如果找不到有效的 webview2 运行时,webview2 安装程序所使用的字符串结构。
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
名称:Messages<br/> 类型:`*windows.Messages`
2021-11-20 20:50:32 +08:00
2022-09-12 21:56:43 +10:00
您可以选择支持的任意语言定制此选项。
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
#### 重置尺寸防抖间隔
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
ResizeDebounceMS 是调整窗口大小时去抖动 webview2 重绘的时间量。 默认值 (0) 将尽可能快地执行重绘。
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
名称:ResizeDebounceMS<br/> 类型:`uint16`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
#### 待机回调
2022-08-09 20:48:29 +10:00
2022-09-18 11:00:39 +10:00
如果设置,当 Windows 启动切换到低功耗模式(挂起/休眠)时将调用此函数
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
名称:OnSuspend<br/> 类型:`func()`
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
#### 恢复回调
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
如果设置,当 Windows 从低功耗模式(挂起/休眠)恢复时将调用此函数
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
名称:OnResume<br/> 类型:`func()`
2022-08-09 20:48:29 +10:00
2023-03-11 17:57:56 +11:00
#### 禁用 Webview GPU 硬件加速
设置为 `true` 将禁用 webview 的 GPU 硬件加速。
名称:WebviewGpuIsDisabled<br/> 类型:`bool`
2023-10-23 20:38:03 +11:00
#### EnableSwipeGestures
Setting this to `true` will enable swipe gestures for the webview.
Name: EnableSwipeGestures<br/> Type: `bool`
2022-09-18 20:01:50 +08:00
### Mac
2022-08-09 20:48:29 +10:00
2022-09-18 11:00:39 +10:00
这定义了 [Mac 特定的选项](#mac)。
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
名称:Mac<br/> 类型:`*mac.Options`
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
#### 标题栏
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
TitleBar 结构提供了配置标题栏外观的能力。
2022-09-12 21:56:43 +10:00
2022-09-18 11:00:39 +10:00
名称:TitleBar<br/> 类型:[`*mac.TitleBar`](#标题栏结构体)
2022-08-09 20:48:29 +10:00
2022-09-18 11:00:39 +10:00
##### 标题栏结构体
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
可以使用 TitleBar 选项自定义应用程序的标题栏:
2021-11-20 20:50:32 +08:00
2022-08-09 20:48:29 +10:00
```go
type TitleBar struct {
TitlebarAppearsTransparent bool
HideTitle bool
HideTitleBar bool
FullSizeContent bool
UseToolbar bool
HideToolbarSeparator bool
}
```
2022-09-18 11:00:39 +10:00
| 设置 | 描述 |
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| TitlebarAppearsTransparent | 使标题栏透明。 这具有隐藏标题栏和内容填充窗口的效果。 [苹果文档](https://developer.apple.com/documentation/appkit/nswindow/1419167-titlebarappearstransparent?language=objc) |
| HideTitle | 隐藏窗口的标题。 [苹果文档](https://developer.apple.com/documentation/appkit/nswindowtitlevisibility?language=objc) |
| HideTitleBar | 从 style mask 中删除 [NSWindowStyleMaskTitled](https://developer.apple.com/documentation/appkit/nswindowstylemask/nswindowstylemasktitled/) |
| FullSizeContent | 使 webview 填满整个窗口。 [苹果文档](https://developer.apple.com/documentation/appkit/nswindowstylemask/nswindowstylemaskfullsizecontentview) |
| UseToolbar | 向窗口添加默认工具栏。 [苹果文档](https://developer.apple.com/documentation/appkit/nstoolbar?language=objc) |
| HideToolbarSeparator | 删除工具栏下方的线条。 [苹果文档](https://developer.apple.com/documentation/appkit/nstoolbar/1516954-showsbaselineseparator?language=objc) |
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
预配置的标题栏设置可用:
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
| 设置 | 示例 |
2022-09-12 21:56:43 +10:00
| --------------------------- | ---------------------------------------------- |
2022-08-29 20:04:52 +10:00
| `mac.TitleBarDefault()` |  |
| `mac.TitleBarHidden()` |  |
| `mac.TitleBarHiddenInset()` |  |
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
示例:
2022-09-12 21:56:43 +10:00
2021-11-20 20:50:32 +08:00
```go
Mac: &mac.Options{
2022-08-09 20:48:29 +10:00
TitleBar: mac.TitleBarHiddenInset(),
2021-11-20 20:50:32 +08:00
}
```
2022-09-18 20:01:50 +08:00
单击 [此处](https://github.com/lukakerr/NSWindowStyles) 获取有关自定义标题栏的一些灵感。
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
#### 外观
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
Appearance 用于根据 Apple 的 [NSAppearance](https://developer.apple.com/documentation/appkit/nsappearancename?language=objc) 名称设置您的应用程序的样式。
2022-09-18 11:00:39 +10:00
名称:Appearance<br/> 类型:[`mac.AppearanceType`](#外观类型)
2022-09-18 20:01:50 +08:00
##### 外观类型
您可以指定应用程序的 [外观](https://developer.apple.com/documentation/appkit/nsappearance?language=objc)。
2021-11-20 20:50:32 +08:00
2022-08-09 20:48:29 +10:00
| 值 | 描述 |
| ----------------------------------------------------- | --------------- |
| DefaultAppearance | 使用默认系统值 |
| NSAppearanceNameAqua | 标准日间系统外观 |
| NSAppearanceNameDarkAqua | 标准黑夜系统外观 |
| NSAppearanceNameVibrantLight | 轻盈灵动的外观 |
2021-11-20 20:50:32 +08:00
| NSAppearanceNameAccessibilityHighContrastAqua | 标准白天系统外观的高对比度版本 |
| NSAppearanceNameAccessibilityHighContrastDarkAqua | 标准黑夜系统外观的高对比度版本 |
2022-08-09 20:48:29 +10:00
| NSAppearanceNameAccessibilityHighContrastVibrantLight | 轻盈灵动外观的高对比度版本 |
| NSAppearanceNameAccessibilityHighContrastVibrantDark | 深色活力外观的高对比度版本 |
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
示例:
2022-09-12 21:56:43 +10:00
2021-11-20 20:50:32 +08:00
```go
Mac: &mac.Options{
Appearance: mac.NSAppearanceNameDarkAqua,
}
```
2022-09-18 20:01:50 +08:00
#### Webview 透明
2022-09-18 11:00:39 +10:00
当使用 `alpha` 值 `0` 时,将此设置为 true 将使 webview 背景透明。 这意味着如果您在 CSS 中使用 `rgba(0,0,0,0)` 作为 `background-color`,则主机窗口将显示出来。 通常与 [窗口半透明](#窗口半透明-1) 结合使用以制作看起来冷冰冰的应用程序。
2022-09-18 20:01:50 +08:00
名称:WebviewIsTransparent<br/> 类型:`bool`
#### 窗口半透明
将此设置为 `true` 将使窗口半透明。 通常与[Webview 透明](#webview-透明) 结合使用以制作冰霜效果的应用程序。
名称:WindowIsTranslucent<br/> 类型:`bool`
2023-10-23 20:38:03 +11:00
#### Preferences
The Preferences struct provides the ability to configure the Webview preferences.
Name: Preferences<br/> Type: [`*mac.Preferences`](#preferences-struct)
##### Preferences struct
You can specify the webview preferences.
```go
type Preferences struct {
TabFocusesLinks u.Bool
TextInteractionEnabled u.Bool
FullscreenEnabled u.Bool
}
```
| 设置 | 描述 |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| TabFocusesLinks | A Boolean value that indicates whether pressing the tab key changes the focus to links and form controls. [Apple Docs](https://developer.apple.com/documentation/webkit/wkpreferences/2818595-tabfocuseslinks?language=objc) |
| TextInteractionEnabled | A Boolean value that indicates whether to allow people to select or otherwise interact with text. [Apple Docs](https://developer.apple.com/documentation/webkit/wkpreferences/3727362-textinteractionenabled?language=objc) |
| FullscreenEnabled | A Boolean value that indicates whether a web view can display content full screen. [Apple Docs](https://developer.apple.com/documentation/webkit/wkpreferences/3917769-elementfullscreenenabled?language=objc) |
示例:
```go
Mac: &mac.Options{
Preferences: &mac.Preferences{
TabFocusesLinks: mac.Enabled,
TextInteractionEnabled: mac.Disabled,
FullscreenEnabled: mac.Enabled,
}
}
```
2022-09-18 20:01:50 +08:00
#### 关于
此配置允许您在“AppMenu”角色创建的应用程序菜单中设置“关于”菜单项的标题、消息和图标。
2022-09-18 11:00:39 +10:00
名称:About<br/> 类型:[`*mac.AboutInfo`](#关于结构体)
2022-09-18 20:01:50 +08:00
2022-09-18 11:00:39 +10:00
##### 关于结构体
2021-11-20 20:50:32 +08:00
```go
2022-09-18 20:01:50 +08:00
2021-11-20 20:50:32 +08:00
type AboutInfo struct {
2022-08-09 20:48:29 +10:00
Title string
Message string
Icon []byte
2021-11-20 20:50:32 +08:00
}
```
2022-09-12 21:56:43 +10:00
2022-08-09 20:48:29 +10:00
如果提供了这些设置,“关于”菜单项将出现在应用程序菜单中(使用`AppMenu` role 时)。 建议这样配置:
2022-09-12 21:56:43 +10:00
2021-11-20 20:50:32 +08:00
```go
//go:embed build/appicon.png
var icon []byte
func main() {
2022-08-09 20:48:29 +10:00
err := wails.Run(&options.App{
...
Mac: &mac.Options{
About: &mac.AboutInfo{
Title: "My Application",
Message: "© 2021 Me",
Icon: icon,
},
},
})
2021-11-20 20:50:32 +08:00
Mac: &mac.Options{
About: &mac.AboutInfo{
Title: "My Application",
Message: "© 2021 Me",
Icon: icon,
},
},
})
2022-08-10 18:41:22 +10:00
Mac: &mac.Options{
About: &mac.AboutInfo{
Title: "My Application",
Message: "© 2021 Me",
Icon: icon,
},
},
})
2021-11-20 20:50:32 +08:00
```
2022-09-12 21:56:43 +10:00
2022-09-18 20:01:50 +08:00
“关于”菜单项将出现在应用程序菜单中:
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
```mdx-code-block
2021-11-20 20:50:32 +08:00
<div class="text--center">
2022-09-12 21:56:43 +10:00
<img
src={require("@site/static/img/reference/about-menu.webp").default}
class="screenshot"
/>
2021-11-20 20:50:32 +08:00
</div>
2022-09-12 21:56:43 +10:00
<br />
2022-09-18 20:01:50 +08:00
```
2022-08-09 20:48:29 +10:00
2022-09-18 20:01:50 +08:00
单击后,将打开一个关于消息框:
2021-11-20 20:50:32 +08:00
2022-09-18 20:01:50 +08:00
```mdx-code-block
2021-11-20 20:50:32 +08:00
<div class="text--center">
2022-09-12 21:56:43 +10:00
<img
src={require("@site/static/img/reference/about-dialog.webp").default}
width="40%"
class="screenshot"
/>
2021-11-20 20:50:32 +08:00
</div>
2022-09-12 21:56:43 +10:00
<br />
2022-09-18 20:01:50 +08:00
```
2022-04-04 18:05:54 +08:00
2022-09-18 20:01:50 +08:00
### Linux
2022-04-04 18:05:54 +08:00
2022-09-18 11:00:39 +10:00
这定义了 [Linux 特定的选项](#linux)。
2022-09-18 20:01:50 +08:00
名称:Linux<br/> 类型:`*linux.Options`
#### 图标
2022-04-04 18:05:54 +08:00
2022-09-12 21:56:43 +10:00
设置代表窗口的图标。 当窗口最小化(也称为图标化)时使用此图标。
2022-04-04 18:05:54 +08:00
2022-09-18 20:01:50 +08:00
名称:Icon<br/> 类型:`[]byte`
2022-04-04 18:05:54 +08:00
2022-09-12 21:56:43 +10:00
一些窗口管理器或桌面环境也可能将其放置在窗口框架中,或在其他上下文中显示。 在其他情况下,根本不使用该图标,因此您的预计情况可能会有所不同。
2022-04-04 18:05:54 +08:00
2022-08-09 20:48:29 +10:00
注意:Wayland 上的 Gnome 至少不显示此图标。 要在那里有一个应用程序图标,必须使用一个`.desktop`文件。 在 KDE 上它应该可以工作。
2022-04-04 18:05:54 +08:00
2022-08-09 20:48:29 +10:00
图标应该以自然绘制的任何尺寸提供;也就是说,在传递图像之前不要缩放图像。 缩放将延迟到当所需的最终尺寸已知的最后一刻,以获得最佳质量。
2022-10-04 16:22:35 +08:00
#### 窗口半透明
将此设置为 `true` 将使窗口半透明。 某些窗口管理员可能忽略它,或导致黑窗口。
2022-11-18 19:11:25 +08:00
名称:WindowIsTranslucent<br/> 类型:`bool`
2023-03-11 17:57:56 +11:00
#### Webview GPU 策略
该选项用于决定 webview 的硬件加速策略。
名称:WebviewGpuPolicy<br/> 类型:[`options.WebviewGpuPolicy`](#webviewgpupolicy-type)<br/> 默认:`WebviewGpuPolicyAlways`
##### Webview GPU 策略类型
| 值 | 描述 |
| ------------------------ | --------------------- |
| WebviewGpuPolicyAlways | 始终启用硬件加速 |
| WebviewGpuPolicyOnDemand | 根据 Web 内容的请求启用/禁用硬件加速 |
| WebviewGpuPolicyNever | 硬件加速始终处于禁用状态 |
2023-09-08 23:25:21 +10:00
#### ProgramName
This option is used to set the program's name for the window manager via GTK's g_set_prgname(). This name should not be localized, [see the docs](https://docs.gtk.org/glib/func.set_prgname.html).
When a .desktop file is created this value helps with window grouping and desktop icons when the .desktop file's `Name` property differs form the executable's filename.
Name: ProgramName<br/> Type: string<br/>
2022-11-18 19:11:25 +08:00
### 调试
这定义了用于调试构建的 [调试特定选项](#调试)。
名称: Debug<br/> 类型: `options.Debug`
#### 启动时打开检查器
设置为 `true` 将在应用程序启动时打开 Web 检查器。
名称: OpenInspectorOnStartup<br/> 类型: `bool`
2022-12-01 16:18:59 +08:00
[^1]: 这需要 WebKit2GTK 2.36+ 支持,并且您的应用程序需要使用构建标签 `webkit2_36` 来构建以激活对此功能的支持。 这也会将您应用程序的 WebKit2GTK 最低要求提高到 2.36。
2023-04-17 23:48:27 +10:00
[^2]: 这需要 WebKit2GTK 2.40+ 支持,并且您的应用程序需要使用构建标签 `webkit2_40` 来构建以激活对此功能的支持。 这也将您的应用程序的 WebKit2GTK 最低要求提高到 2.40。