参数选项
应用程序参数选项
该 Options.App 结构包含应用程序配置。 它被传递给 wails.Run() 方法:
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"
)
func main() {
err := wails.Run(&options.App{
Title: "Menus Demo",
Width: 800,
Height: 600,
DisableResize: false,
Fullscreen: false,
WindowStartState: options.Maximised,
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,
AssetServer: &assetserver.Options{
Assets: assets,
Handler: assetsHandler,
Middleware: assetsMidldeware,
},
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",
EnableDefaultContextMenu: false,
EnableFraudulentWebsiteDetection: false,
Bind: []interface{}{
app,
},
EnumBind: []interface{}{
AllWeekdays,
},
ErrorFormatter: func(err error) any { return err.Error() },
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "c9c8fd93-6758-4144-87d1-34bdb0a8bd60",
OnSecondInstanceLaunch: app.onSecondInstanceLaunch,
},
DragAndDrop: &options.DragAndDrop{
EnableFileDrop: false,
DisableWebViewDrop: false,
CSSDropProperty: "--wails-drop-target",
CSSDropValue: "drop",
},
Windows: &windows.Options{
WebviewIsTransparent: false,
WindowIsTranslucent: false,
BackdropType: windows.Mica,
DisablePinchZoom: false,
DisableWindowIcon: false,
DisableFramelessWindowDecorations: false,
WebviewUserDataPath: "",
WebviewBrowserPath: "",
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),
},
// ZoomFactor is the zoom factor for the WebView2. 这是匹配 Edge 用户激活放大或缩小的选项
ZoomFactor: float64,
// IsZoomControlEnabled enables the zoom factor to be changed by the user.
IsZoomControlEnabled: bool,
// User messages that can be customised
Messages: *windows.Messages
// OnSuspend is called when Windows enters low power mode
OnSuspend: func()
// OnResume is called when Windows resumes from low power mode
OnResume: func(),
// Disable GPU hardware acceleration for the webview
WebviewGpuDisabled: false,
// Class name for the window. If empty, 'wailsWindow' will be used.
WindowClassName: "MyWindow",
},
Mac: &mac.Options{
TitleBar: &mac.TitleBar{
TitlebarAppearsTransparent: true,
HideTitle: false,
HideTitleBar: false,
FullSizeContent: false,
UseToolbar: false,
HideToolbarSeparator: true,
OnFileOpen: app.onFileOpen,
OnUrlOpen: app.onUrlOpen,
},
Appearance: mac.NSAppearanceNameDarkAqua,
WebviewIsTransparent: true,
WindowIsTranslucent: false,
About: &mac.AboutInfo{
Title: "My Application",
Message: "© 2021 Me",
Icon: icon,
},
},
Linux: &linux.Options{
Icon: icon,
WindowIsTranslucent: false,
WebviewGpuPolicy: linux.WebviewGpuPolicyAlways,
ProgramName: "wails"
},
Debug: options.Debug{
OpenInspectorOnStartup: false,
},
})
if err != nil {
log.Fatal(err)
}
}
标题
窗口标题栏中显示的文本。
名称:Title
类型:string
宽度
窗口的初始宽度。
名称:Width
类型:int
默认值:1024.
高度
窗口的初始高度。
名称:Height
类型:int
默认值:768
禁用调整窗口尺寸
默认情况下,主窗口可调整大小。 将此设置为 true 将使其保持固定大小。
名称:DisableResize
类型:bool
全屏
已弃用:请使用 窗口启动状态.
窗口启动状态
定义窗口在启动时应如何呈现。
| 值 | Win | Mac | Lin |
|---|---|---|---|
| Fullscreen(全屏) | ✅ | ✅ | ✅ |
| Maximised(最大化) | ✅ | ✅ | ✅ |
| Minimised(最小化) | ✅ | ❌ | ✅ |
名称:WindowStartState
类型:options.WindowStartState
无边框
设置为true时,窗口将没有边框或标题栏。 另请参阅 无边框窗口。
名称:Frameless
类型:bool
最小宽度
这将设置窗口的最小宽度。 如果给出的值 Width 小于这个值,窗口将被设置为 MinWidth 默认值。
名称:MinWidth
类型:int
最小高度
这将设置窗口的最小高度。 如果给出的值 Height 小于这个值,窗口将被设置为 MinHeight 默认值。
名称:MinHeight
类型:int
最大宽度
这将设置窗口的最大宽度。 如果给出的值 Width 大于这个值,窗口将被设置为 MaxWidth 默认值。
名称:MaxWidth
类型:int
最大高度
这将设置窗口的最大高度。 如果给出的值 Height 大于这个值,窗口将被设置为 MaxHeight 默认值。
名称:MaxHeight
类型:int
启动时隐藏窗口
设置为 true 时,应用程序将被隐藏,直到调用 显示窗口。
名称:StartHidden
类型:bool
关闭时隐藏窗口
默认情况下,关闭窗口将关闭应用程序。 将此设置为 true 意味着关闭窗口将隐藏窗口。
隐藏窗口。
名称:HideWindowOnClose
类型:bool
背景颜色
此值是 窗口的默认背景颜色。 示例:options.NewRGBA(255,0,0,128) - 红色,透明度为 50%
名称:BackgroundColour
类型:*options.RGBA
默认值:white
窗口固定在最顶层
窗口在失去焦点时应保持在其他窗口之上。
名称:AlwaysOnTop
类型:bool
资产
已弃用:请在 AssetServer 特定选项 上使用资产。
资产处理程序
已弃用:请在 AssetServer 特定选项 上使用资产处理程序。
资产服务器
这定义了资产服务器特定的选项。 它允许使用静态资产自定义资产服务器,使用 http.Handler 动态地提供资产或使用 assetsserver.Middleware 钩到请求链。
并非当前支持 http.Request 的所有功能,请参阅以下功能矩阵:
| 功能 | Win | Mac | Lin |
|---|---|---|---|
| GET | ✅ | ✅ | ✅ |
| POST | ✅ | ✅ | ✅ 1 |
| PUT | ✅ | ✅ | ✅ 1 |
| PATCH | ✅ | ✅ | ✅ 1 |
| DELETE | ✅ | ✅ | ✅ 1 |
| Request Headers | ✅ | ✅ | ✅ 1 |
| Request Body | ✅ | ✅ | ✅ 2 |
| Request Body Streaming | ✅ | ✅ | ✅ 2 |
| Response StatusCodes | ✅ | ✅ | ✅ 1 |
| Response Headers | ✅ | ✅ | ✅ 1 |
| Response Body | ✅ | ✅ | ✅ |
| Response Body Streaming | ❌ | ✅ | ✅ |
| WebSockets | ❌ | ❌ | ❌ |
| HTTP Redirects 30x | ✅ | ❌ | ❌ |
名称: AssetServer
类型: *assetserver.Options
资产
应用程序要使用的静态前端资产。
首先尝试从 fs.FS 提供 GET 请求。 如果 fs.FS 为该文件返回 os.ErrNotExist,则请求处理将回退到 处理程序 并尝试服务来自它的 GET 请求。
如果设置为 nil,则所有 GET 请求都将转发给 处理程序。
名称: Assets
类型: fs.FS
处理程序
资产处理程序是一个通用的 http.Handler,用于对无法找到的资产进行后备处理。
由于 os.ErrNotExist,对于每个无法从 资产 提供服务的 GET 请求,都会调用该处理程序。 此外,所有非 GET 请求将始终从此处理程序提供服务。 如果未定义,则调用处理程序的结果如下:
- GET 请求:
http.StatusNotFound - 其他请求:
http.StatusMethodNotAllowed
This does not work with vite v5.0.0+ and wails v2 due to changes in vite. Changes are planned in v3 to support similar functionality under vite v5.0.0+. If you need this feature, stay with vite v4.0.0+. See issue 3240 for details
注意:当与前端 DevServer 结合使用时,可能会有一些限制,例如。 Vite 在不包含文件扩展名的每个路径上提供 index.html。
名称:AssetsHandler
类型:http.Handler
中间件
中间件是一个 HTTP 中间件,它允许挂钩到资产服务器请求链。 它允许动态跳过默认请求处理程序,例如实现专门的路由等。 调用中间件来构建资产服务器使用的新 http.Handler,它还接收资产服务器使用的默认处理程序作为参数。
如果未定义,则执行默认的资产服务器请求链。
名称: Middleware
类型: assetserver.Middleware
菜单
应用程序要使用的菜单。 菜单参考 中有关菜单的更多详细信息。
在 Mac 上,如果未指定菜单,将创建一个默认菜单。
名称:Menu
类型:*menu.Menu
日志
应用程序要使用的记录器。 有关日志记录的更多详细信息,请参阅 日志参考。
名称:Logger
类型:logger.Logger
默认值:Logs to Stdout
日志级别
默认日志级别。 有关日志记录的更多详细信息,请参阅 日志参考。
名称:LogLevel
类型:logger.LogLevel
默认值:开发模式为 Info, 生产模式为 Error
生产日志级别
生产构建的默认日志级别。 有关日志记录的更多详细信息,请参阅 日志参考。
名称:LogLevelProduction
类型:logger.LogLevel
默认值:Error
应用启动回调
此回调在前端创建之后调用,但在 index.html 加载之前调用。 它提供了应用程序上下文。
名称:OnStartup
类型:func(ctx context.Context)
前端 Dom 加载完成回调
在前端加载完毕 index.html 及其资源后调用此回调。 它提供了应用程序上下文。
名称:OnDomReady
类型:func(ctx context.Context)
应用退出回调
在前端被销毁之后,应用程序终止之前,调用此回调。 它提供了应用程序上下文。
名称:OnShutdown
类型:func(ctx context.Context)
应用关闭前回调
如果设置了此回调,它将在通过单击窗口关闭按钮或调用runtime.Quit即将退出应用程序时被调用. 返回 true 将导致应用程序继续,false 将继续正常关闭。 返回 true 将导致应用程序继续,false 将继续正常关闭。 这有助于与用户确认他们希望退出程序。
示例:
func (b *App) beforeClose(ctx context.Context) (prevent bool) {
dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "Quit?",
Message: "Are you sure you want to quit?",
})
if err != nil {
return false
}
return dialog != "Yes"
}
名称:OnBeforeClose
类型:func(ctx context.Context) bool
CSS 拖动属性
指示用于标识哪些元素可用于拖动窗口的 CSS 属性。 默认值:--wails-draggable
名称:CSSDragProperty
类型:string
CSS 拖动值
指示 CSSDragProperty 样式应该具有什么值才能拖动窗口。 默认值:drag
名称:CSSDragValue
类型:string
EnableDefaultContextMenu
EnableDefaultContextMenu enables the browser's default context-menu in production.
By default, the browser's default context-menu is only available in development and in a -debug 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.
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 : 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.
This filtering functionality is only enabled in production, so in development and in debug build, the full context-menu is always available everywhere.
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
Type: bool