Cais#
Mae'r API cais yn cynorthwyo i greu cais gan ddefnyddio fframwaith Wails.
Newydd#
API: New(appOptions Options) *App
New(appOptions Options) yn creu cais newydd gan ddefnyddio'r opsiynau cais a ddarperir. Mae'n cymhwyso gwerthoedd rhagosodedig ar gyfer opsiynau heb eu pennu, yn eu cyfuno â'r rhai a ddarparwyd, yn eu cychwyn a'n dychwelyd enghraifft o'r cais.
Os bydd gwall yn ystod y cychwyn, caiff y cais ei atal gyda'r neges gwall a ddarperir.
Dylid nodi, os oes enghraifft gyffredinol o gais yn bodoli eisoes, y bydd yr enghraifft honno'n cael ei dychwelyd yn hytrach na chreu un newydd.
package main
import "github.com/wailsapp/wails/v3/pkg/application"
func main() {
app := application.New(application.Options{
Name: "Demo Ffenestr Gweddarlunydd",
// Opsiynau eraill
})
// Gweddill y cais
}
Cael#
Get() yn dychwelyd yr enghraifft gyffredinol o'r cais. Mae'n ddefnyddiol pan fydd angen mynediad i'r cais o wahanol rannau o'ch cod.
Galluoedd#
API: Capabilities() capabilities.Capabilities
Capabilities() yn adfer map o'r galluoedd sydd gan y cais ar hyn o bryd. Gall y galluoedd fod ynghylch y nodweddion gwahanol y system weithredu sy'n darparu, fel nodweddion gweddarlunydd.
// Cael galluoedd y cais
capabilities := app.Capabilities()
if capabilities.HasNativeDrag {
// Gwneud rhywbeth
}
GetPID#
API: GetPID() int
GetPID() yn dychwelyd ID y Broses y cais.
Rhedeg#
API: Run() error
Run() yn dechrau gweithredu'r cais a'i gydrannau.
app := application.New(application.Options{
//options
})
// Rhedeg y cais
err := application.Run()
if err != nil {
// Ymdrin â'r gwall
}
Gadael#
API: Quit()
Quit() yn gadael y cais trwy ddinistrio ffenestri a rhai cydrannau eraill o bosibl.
AydunDdyryslyd#
API: IsDarkMode() bool
IsDarkMode() yn gwirio a yw'r cais yn rhedeg mewn modd tywyll. Mae'n dychwelyd gwerth boolean yn nodi a yw'r modd tywyll wedi'i alluogi.
Cuddio#
API: Hide()
Hide() yn cuddio ffenestr y cais.
Dangos#
API: Show()
Show() yn dangos ffenestr y cais.
Dyma'r testun wedi'i gyfieithu i'r Gymraeg:
NewWebviewWindow#
API: NewWebviewWindow() *WebviewWindow
Mae NewWebviewWindow() yn creu ffenestr Webview newydd gyda'r opsiynau rhagosodedig, ac yn ei dychwelyd.
NewWebviewWindowWithOptions#
API:
NewWebviewWindowWithOptions(windowOptions WebviewWindowOptions) *WebviewWindow
Mae NewWebviewWindowWithOptions() yn creu ffenestr webview newydd gydag opsiynau custom. Caiff y ffenestr newydd ei ychwanegu at fap o ffenestri a reolir gan y cymhwysiad.
// Creu ffenestr webview newydd gydag opsiynau custom
window := app.NewWebviewWindowWithOptions(WebviewWindowOptions{
Name: "Main",
Title: "Fy Ffenestr",
Width: 800,
Height: 600,
})
OnWindowCreation#
API: OnWindowCreation(callback func(window *WebviewWindow))
Mae OnWindowCreation() yn cofrestru ffwythiant alw-nôl i'w alw pan grëir ffenestr.
// Cofrestru ffwythiant alw-nôl i'w alw pan grëir ffenestr
app.OnWindowCreation(func(window *WebviewWindow) {
// Gwneud rhywbeth
})
GetWindowByName#
API: GetWindowByName(name string) *WebviewWindow
Mae GetWindowByName() yn nôl ac yn dychwelyd ffenestr gyda enw penodol.
CurrentWindow#
API: CurrentWindow() *WebviewWindow
Mae CurrentWindow() yn nôl ac yn dychwelyd cyfeiriad at y ffenestr weithredol yn y cymhwysiad. Os nad oes ffenestr, mae'n dychwelyd nil.
RegisterContextMenu#
API: RegisterContextMenu(name string, menu *Menu)
Mae RegisterContextMenu() yn cofrestru dewislen cyd-destun gyda enw penodol. Gellir defnyddio'r dewislen hon yn ddiweddarach yn yr ap.
// Creu dewislen newydd
ctxmenu := app.NewMenu()
// Cofrestru'r dewislen fel dewislen cyd-destun
app.RegisterContextMenu("MyContextMenu", ctxmenu)
SetMenu#
API: SetMenu(menu *Menu)
Mae SetMenu() yn gosod y ddewislen ar gyfer yr ap. Ar Mac, bydd hyn yn fod y ddewislen fyd-eang. Ar gyfer Windows a Linux, bydd hyn yn fod y ddewislen ddiofyn ar gyfer unrhyw ffenestr newydd a grëir.
Dangos Deialog Ynghylch#
API: ShowAboutDialog()
Mae ShowAboutDialog() yn dangos blwch deialog "Ynghylch". Gall ddangos enw'r
cymhwysiad, disgrifiad ac eicon.
Gwybodaeth#
API: InfoDialog()
Mae InfoDialog() yn creu ac yn dychwelyd esiampl newydd o MessageDialog gyda
InfoDialogType. Defnyddir y deialog hon fel arfer i ddangos negeseuon
gwybodaeth i'r defnyddiwr.
Cwestiwn#
API: QuestionDialog()
Mae QuestionDialog() yn creu ac yn dychwelyd esiampl newydd o MessageDialog
gyda QuestionDialogType. Defnyddir y deialog hon yn aml i ofyn cwestiwn i'r
defnyddiwr a disgwyl ymateb.
Rhybudd#
API: WarningDialog()
Mae WarningDialog() yn creu ac yn dychwelyd esiampl newydd o MessageDialog
gyda WarningDialogType. Fel y mae'r enw yn awgrymu, defnyddir y deialog hon yn
bennaf i ddangos negeseuon rhybudd i'r defnyddiwr.
Gwall#
API: ErrorDialog()
Mae ErrorDialog() yn creu ac yn dychwelyd esiampl newydd o MessageDialog gyda
ErrorDialogType. Cynlluniwyd y deialog hon i'w defnyddio pan fydd angen
dangos neges gwall i'r defnyddiwr.
Agor Ffeil#
API: OpenFileDialog()
Mae OpenFileDialog() yn creu ac yn dychwelyd esiampl newydd o
OpenFileDialogStruct. Mae'r deialog hon yn annog y defnyddiwr i ddewis un neu
ragor o ffeiliau o'u system ffeiliau.
Cadw Ffeil#
API: SaveFileDialog()
Mae SaveFileDialog() yn creu ac yn dychwelyd esiampl newydd o
SaveFileDialogStruct. Mae'r deialog hon yn annog y defnyddiwr i ddewis lleoliad
yn eu system ffeiliau lle y dylid cadw ffeil.
Agor Cyfeiriadur#
API: OpenDirectoryDialog()
Mae OpenDirectoryDialog() yn creu ac yn dychwelyd esiampl newydd o
MessageDialog gyda OpenDirectoryDialogType. Mae'r deialog hon yn galluogi'r
defnyddiwr i ddewis cyfeiriadur o'u system ffeiliau.
Ar#
API:
Ar(eventType digwyddiadau.DdigwyddiadweithgangeningApplicationEventType, atebydd func(digwyddiad *Digwyddiad)) func()
Mae Ar() yn cofrestru gwrandäwr digwyddiad ar gyfer digwyddiadau cymhwysiad penodol. Bydd y swyddogaeth atebydd a ddarperir yn cael ei sbarduno pan fydd y digwyddiad cysylltiedig yn digwydd. Mae'r swyddogaeth yn dychwelyd swyddogaeth y gellir ei galw i dynnu'r gwrandäwr.
CofrestruArgraffwyr#
API:
CofrestruArgraffwyr(eventType digwyddiadau.DdigwyddiadweithgangeningApplicationEventType, atebydd func(digwyddiad *Digwyddiad)) func()
Mae CofrestruArgraffwyr() yn cofrestru atebydd i'w redeg fel crocen yn ystod digwyddiadau penodol. Caiff y crocenau hyn eu rhedeg cyn gwrandawyr sy'n gysylltiedig ag Ar(). Mae'r swyddogaeth yn dychwelyd swyddogaeth y gellir ei galw i dynnu'r bâs.
GetPrimaryScreen#
API: GetPrimaryScreen() (*Sgrin, error)
Mae GetPrimaryScreen() yn dychwelyd y sgrin brif y system.
GetScreens#
API: GetScreens() ([]*Sgrin, error)
Mae GetScreens() yn dychwelyd gwybodaeth am bob sgrin sydd wedi'i chysylltu â'r system.
Dyma grynodeb byr o'r dulliau allforio yn y App strwythur a ddarparwyd. Cofiwch, ar gyfer mwy o swyddogaethau neu ystyriaethau manwl, cyfeiriwch at y cod Go gwirioneddol neu ddogfennaeth fewnol bellach.
Opsiynau#
package application
import (
"io/fs"
"log/slog"
"net/http"
"github.com/wailsapp/wails/v3/internal/assetserver"
)
// Options contains the options for the application
type Options struct {
// Name is the name of the application (used in the default about box)
Name string
// Description is the description of the application (used in the default about box)
Description string
// Icon is the icon of the application (used in the default about box)
Icon []byte
// Mac is the Mac specific configuration for Mac builds
Mac MacOptions
// Windows is the Windows specific configuration for Windows builds
Windows WindowsOptions
// Linux is the Linux specific configuration for Linux builds
Linux LinuxOptions
// Bind allows you to bind Go methods to the frontend.
Bind []any
// BindAliases allows you to specify alias IDs for your bound methods.
// Example: `BindAliases: map[uint32]uint32{1: 1411160069}` states that alias ID 1 maps to the Go method with ID 1411160069.
BindAliases map[uint32]uint32
// Logger i a slog.Logger instance used for logging Wails system messages (not application messages).
// If not defined, a default logger is used.
Logger *slog.Logger
// LogLevel defines the log level of the Wails system logger.
LogLevel slog.Level
// Assets are the application assets to be used.
Assets AssetOptions
// Plugins is a map of plugins used by the application
Plugins map[string]Plugin
// Flags are key value pairs that are available to the frontend.
// This is also used by Wails to provide information to the frontend.
Flags map[string]any
// PanicHandler is called when a panic occurs
PanicHandler func(any)
// DisableDefaultSignalHandler disables the default signal handler
DisableDefaultSignalHandler bool
// KeyBindings is a map of key bindings to functions
KeyBindings map[string]func(window *WebviewWindow)
// OnShutdown is called when the application is about to terminate.
// This is useful for cleanup tasks.
// The shutdown process blocks until this function returns
OnShutdown func()
// ShouldQuit is a function that is called when the user tries to quit the application.
// If the function returns true, the application will quit.
// If the function returns false, the application will not quit.
ShouldQuit func() bool
}
// AssetOptions defines the configuration of the AssetServer.
type AssetOptions struct {
// Handler which serves all the content to the WebView.
Handler http.Handler
// Middleware is a HTTP Middleware which allows to hook into the AssetServer request chain. It allows to skip the default
// request handler dynamically, e.g. implement specialized Routing etc.
// The Middleware is called to build a new `http.Handler` used by the AssetSever and it also receives the default
// handler used by the AssetServer as an argument.
//
// This middleware injects itself before any of Wails internal middlewares.
//
// If not defined, the default AssetServer request chain is executed.
//
// Multiple Middlewares can be chained together with:
// ChainMiddleware(middleware ...Middleware) Middleware
Middleware Middleware
// DisableLogging disables logging of the AssetServer. By default, the AssetServer logs every request.
DisableLogging bool
}
// Middleware defines HTTP middleware that can be applied to the AssetServer.
// The handler passed as next is the next handler in the chain. One can decide to call the next handler
// or implement a specialized handling.
type Middleware func(next http.Handler) http.Handler
// ChainMiddleware allows chaining multiple middlewares to one middleware.
func ChainMiddleware(middleware ...Middleware) Middleware {
return func(h http.Handler) http.Handler {
for i := len(middleware) - 1; i >= 0; i-- {
h = middleware[i](h)
}
return h
}
}
// AssetFileServerFS returns a http handler which serves the assets from the fs.FS.
// If an external devserver has been provided 'FRONTEND_DEVSERVER_URL' the files are being served
// from the external server, ignoring the `assets`.
func AssetFileServerFS(assets fs.FS) http.Handler {
return assetserver.NewAssetFileServer(assets)
}
// BundledAssetFileServer returns a http handler which serves the assets from the fs.FS.
// If an external devserver has been provided 'FRONTEND_DEVSERVER_URL' the files are being served
// from the external server, ignoring the `assets`.
// It also serves the compiled runtime.js file at `/wails/runtime.js`.
// It will provide the production runtime.js file from the embedded assets if the `production` tag is used.
func BundledAssetFileServer(assets fs.FS) http.Handler {
return assetserver.NewBundledAssetFileServer(assets)
}
/******** Mac Options ********/
// ActivationPolicy is the activation policy for the application.
type ActivationPolicy int
const (
// ActivationPolicyRegular is used for applications that have a user interface,
ActivationPolicyRegular ActivationPolicy = iota
// ActivationPolicyAccessory is used for applications that do not have a main window,
// such as system tray applications or background applications.
ActivationPolicyAccessory
ActivationPolicyProhibited
)
// MacOptions contains options for macOS applications.
type MacOptions struct {
// ActivationPolicy is the activation policy for the application. Defaults to
// applicationActivationPolicyRegular.
ActivationPolicy ActivationPolicy
// If set to true, the application will terminate when the last window is closed.
ApplicationShouldTerminateAfterLastWindowClosed bool
}
/****** Windows Options *******/
// WindowsOptions contains options for Windows applications.
type WindowsOptions struct {
// WndProcInterceptor is a function that will be called for every message sent in the application.
// Use this to hook into the main message loop. This is useful for handling custom window messages.
// If `shouldReturn` is `true` then `returnCode` will be returned by the main message loop.
// If `shouldReturn` is `false` then returnCode will be ignored and the message will be processed by the main message loop.
WndProcInterceptor func(hwnd uintptr, msg uint32, wParam, lParam uintptr) (returnCode uintptr, shouldReturn bool)
// DisableQuitOnLastWindowClosed disables the auto quit of the application if the last window has been closed.
DisableQuitOnLastWindowClosed bool
// Path where the WebView2 stores the user data. If empty %APPDATA%\[BinaryName.exe] will be used.
// If the path is not valid, a messagebox will be displayed with the error and the app will exit with error code.
WebviewUserDataPath string
// Path to the directory with WebView2 executables. If empty WebView2 installed in the system will be used.
WebviewBrowserPath string
}
/********* Linux Options *********/
// LinuxOptions contains options for Linux applications.
type LinuxOptions struct {
// DisableQuitOnLastWindowClosed disables the auto quit of the application if the last window has been closed.
DisableQuitOnLastWindowClosed bool
// ProgramName 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]
//
//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.
//
//[see the docs]: https://docs.gtk.org/glib/func.set_prgname.html
ProgramName string
}