mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 22:13:36 +08:00
Add WindowExecJS method (#1927)
* Add WindowExecJS * [devserver] Embed the base frontend into the DevServer struct * Update docs Co-authored-by: stffabi <stffabi@users.noreply.github.com>
This commit is contained in:
parent
bd378b2e0f
commit
eee6797907
@ -181,6 +181,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
|
||||
eventHandler.AddFrontend(appFrontend)
|
||||
eventHandler.AddFrontend(desktopFrontend)
|
||||
|
||||
ctx = context.WithValue(ctx, "frontend", appFrontend)
|
||||
result := &App{
|
||||
ctx: ctx,
|
||||
frontend: appFrontend,
|
||||
|
@ -84,6 +84,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
|
||||
appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
|
||||
eventHandler.AddFrontend(appFrontend)
|
||||
|
||||
ctx = context.WithValue(ctx, "frontend", appFrontend)
|
||||
result := &App{
|
||||
ctx: ctx,
|
||||
frontend: appFrontend,
|
||||
|
@ -145,9 +145,7 @@ func (f *Frontend) WindowSetDarkTheme() {
|
||||
}
|
||||
|
||||
func (f *Frontend) Run(ctx context.Context) error {
|
||||
|
||||
f.ctx = context.WithValue(ctx, "frontend", f)
|
||||
|
||||
f.ctx = ctx
|
||||
var _debug = ctx.Value("debug")
|
||||
if _debug != nil {
|
||||
f.debug = _debug.(bool)
|
||||
|
@ -142,8 +142,7 @@ func (f *Frontend) WindowSetDarkTheme() {
|
||||
}
|
||||
|
||||
func (f *Frontend) Run(ctx context.Context) error {
|
||||
|
||||
f.ctx = context.WithValue(ctx, "frontend", f)
|
||||
f.ctx = ctx
|
||||
|
||||
go func() {
|
||||
if f.frontendOptions.OnStartup != nil {
|
||||
|
@ -128,8 +128,7 @@ func (f *Frontend) WindowSetDarkTheme() {
|
||||
}
|
||||
|
||||
func (f *Frontend) Run(ctx context.Context) error {
|
||||
|
||||
f.ctx = context.WithValue(ctx, "frontend", f)
|
||||
f.ctx = ctx
|
||||
|
||||
mainWindow := NewWindow(nil, f.frontendOptions, f.versionInfo)
|
||||
f.mainWindow = mainWindow
|
||||
|
@ -23,7 +23,6 @@ import (
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
||||
"github.com/wailsapp/wails/v2/internal/logger"
|
||||
"github.com/wailsapp/wails/v2/internal/menumanager"
|
||||
"github.com/wailsapp/wails/v2/pkg/menu"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
@ -43,31 +42,11 @@ type DevWebServer struct {
|
||||
starttime string
|
||||
|
||||
// Desktop frontend
|
||||
desktopFrontend frontend.Frontend
|
||||
frontend.Frontend
|
||||
|
||||
devServerAddr string
|
||||
}
|
||||
|
||||
func (d *DevWebServer) Hide() {
|
||||
d.desktopFrontend.Hide()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) Show() {
|
||||
d.desktopFrontend.Show()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetSystemDefaultTheme() {
|
||||
d.desktopFrontend.WindowSetSystemDefaultTheme()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetLightTheme() {
|
||||
d.desktopFrontend.WindowSetLightTheme()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetDarkTheme() {
|
||||
d.desktopFrontend.WindowSetDarkTheme()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) Run(ctx context.Context) error {
|
||||
d.ctx = ctx
|
||||
|
||||
@ -135,159 +114,19 @@ func (d *DevWebServer) Run(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Launch desktop app
|
||||
err = d.desktopFrontend.Run(ctx)
|
||||
err = d.Frontend.Run(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DevWebServer) RunMainLoop() {
|
||||
d.desktopFrontend.RunMainLoop()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) Quit() {
|
||||
d.desktopFrontend.Quit()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) OpenFileDialog(dialogOptions frontend.OpenDialogOptions) (string, error) {
|
||||
return d.desktopFrontend.OpenFileDialog(dialogOptions)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) OpenMultipleFilesDialog(dialogOptions frontend.OpenDialogOptions) ([]string, error) {
|
||||
return d.OpenMultipleFilesDialog(dialogOptions)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) OpenDirectoryDialog(dialogOptions frontend.OpenDialogOptions) (string, error) {
|
||||
return d.OpenDirectoryDialog(dialogOptions)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) SaveFileDialog(dialogOptions frontend.SaveDialogOptions) (string, error) {
|
||||
return d.desktopFrontend.SaveFileDialog(dialogOptions)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) MessageDialog(dialogOptions frontend.MessageDialogOptions) (string, error) {
|
||||
return d.desktopFrontend.MessageDialog(dialogOptions)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowReload() {
|
||||
d.broadcast("reload")
|
||||
d.desktopFrontend.WindowReload()
|
||||
d.Frontend.WindowReload()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowReloadApp() {
|
||||
d.broadcast("reloadapp")
|
||||
d.desktopFrontend.WindowReloadApp()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetTitle(title string) {
|
||||
d.desktopFrontend.WindowSetTitle(title)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowClose() {
|
||||
d.desktopFrontend.WindowClose()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowShow() {
|
||||
d.desktopFrontend.WindowShow()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowHide() {
|
||||
d.desktopFrontend.WindowHide()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowCenter() {
|
||||
d.desktopFrontend.WindowCenter()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowMaximise() {
|
||||
d.desktopFrontend.WindowMaximise()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowToggleMaximise() {
|
||||
d.desktopFrontend.WindowToggleMaximise()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowUnmaximise() {
|
||||
d.desktopFrontend.WindowUnmaximise()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowIsMaximised() bool {
|
||||
return d.desktopFrontend.WindowIsMaximised()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowMinimise() {
|
||||
d.desktopFrontend.WindowMinimise()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowUnminimise() {
|
||||
d.desktopFrontend.WindowUnminimise()
|
||||
}
|
||||
func (d *DevWebServer) WindowSetAlwaysOnTop(b bool) {
|
||||
d.desktopFrontend.WindowSetAlwaysOnTop(b)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowIsMinimised() bool {
|
||||
return d.desktopFrontend.WindowIsMinimised()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetPosition(x int, y int) {
|
||||
d.desktopFrontend.WindowSetPosition(x, y)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowGetPosition() (int, int) {
|
||||
return d.desktopFrontend.WindowGetPosition()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetSize(width int, height int) {
|
||||
d.desktopFrontend.WindowSetSize(width, height)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowGetSize() (int, int) {
|
||||
return d.desktopFrontend.WindowGetSize()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetMinSize(width int, height int) {
|
||||
d.desktopFrontend.WindowSetMinSize(width, height)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetMaxSize(width int, height int) {
|
||||
d.desktopFrontend.WindowSetMaxSize(width, height)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowFullscreen() {
|
||||
d.desktopFrontend.WindowFullscreen()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowUnfullscreen() {
|
||||
d.desktopFrontend.WindowUnfullscreen()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowSetBackgroundColour(col *options.RGBA) {
|
||||
d.desktopFrontend.WindowSetBackgroundColour(col)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) ScreenGetAll() ([]Screen, error) {
|
||||
return d.desktopFrontend.ScreenGetAll()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowIsFullscreen() bool {
|
||||
return d.desktopFrontend.WindowIsFullscreen()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) WindowIsNormal() bool {
|
||||
return d.desktopFrontend.WindowIsNormal()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) MenuSetApplicationMenu(menu *menu.Menu) {
|
||||
d.desktopFrontend.MenuSetApplicationMenu(menu)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) MenuUpdateApplicationMenu() {
|
||||
d.desktopFrontend.MenuUpdateApplicationMenu()
|
||||
}
|
||||
|
||||
// BrowserOpenURL uses the system default browser to open the url
|
||||
func (d *DevWebServer) BrowserOpenURL(url string) {
|
||||
d.desktopFrontend.BrowserOpenURL(url)
|
||||
d.Frontend.WindowReloadApp()
|
||||
}
|
||||
|
||||
func (d *DevWebServer) Notify(name string, data ...interface{}) {
|
||||
@ -298,6 +137,7 @@ func (d *DevWebServer) handleReload(c echo.Context) error {
|
||||
d.WindowReload()
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (d *DevWebServer) handleReloadApp(c echo.Context) error {
|
||||
d.WindowReloadApp()
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
@ -426,13 +266,13 @@ func (d *DevWebServer) notifyExcludingSender(eventMessage []byte, sender *websoc
|
||||
d.logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
d.desktopFrontend.Notify(notifyMessage.Name, notifyMessage.Data...)
|
||||
d.Frontend.Notify(notifyMessage.Name, notifyMessage.Data...)
|
||||
}
|
||||
|
||||
func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher, menuManager *menumanager.Manager, desktopFrontend frontend.Frontend) *DevWebServer {
|
||||
result := &DevWebServer{
|
||||
ctx: ctx,
|
||||
desktopFrontend: desktopFrontend,
|
||||
Frontend: desktopFrontend,
|
||||
appoptions: appoptions,
|
||||
logger: myLogger,
|
||||
appBindings: appBindings,
|
||||
|
@ -66,6 +66,7 @@ type MessageDialogOptions struct {
|
||||
type Frontend interface {
|
||||
Run(context.Context) error
|
||||
RunMainLoop()
|
||||
ExecJS(js string)
|
||||
Hide()
|
||||
Show()
|
||||
Quit()
|
||||
|
@ -163,6 +163,12 @@ func WindowIsNormal(ctx context.Context) bool {
|
||||
return appFrontend.WindowIsNormal()
|
||||
}
|
||||
|
||||
// WindowExecJS executes the given Js in the window
|
||||
func WindowExecJS(ctx context.Context, js string) {
|
||||
appFrontend := getFrontend(ctx)
|
||||
appFrontend.ExecJS(js)
|
||||
}
|
||||
|
||||
func WindowSetBackgroundColour(ctx context.Context, R, G, B, A uint8) {
|
||||
appFrontend := getFrontend(ctx)
|
||||
col := &options.RGBA{
|
||||
|
@ -41,6 +41,15 @@ Centers the window on the monitor the window is currently on.
|
||||
Go: `WindowCenter(ctx context.Context)`<br/>
|
||||
JS: `WindowCenter()`
|
||||
|
||||
### WindowExecJS
|
||||
|
||||
Executes arbitrary JS code in the window.
|
||||
|
||||
This method runs the code in the browser asynchronously and returns immediately.
|
||||
If the script causes any errors, they will only be available in the browser console.
|
||||
|
||||
Go: `WindowExecJS(ctx context.Context, js string)`
|
||||
|
||||
### WindowReload
|
||||
|
||||
Performs a "reload" (Reloads current page).
|
||||
|
Loading…
Reference in New Issue
Block a user