5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 05:11:29 +08:00

[v3] Add ability to get window by name

This commit is contained in:
Lea Anthony 2023-04-07 19:47:01 +10:00
parent 7f1093dc51
commit 424e4fc2e8
16 changed files with 140 additions and 110 deletions

View File

@ -33,8 +33,11 @@ func main() {
windowCounter := 1
newWindow := func() {
app.NewWebviewWindowWithOptions(&application.WebviewWindowOptions{}).
SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)).
windowName := "WebviewWindow " + strconv.Itoa(windowCounter)
app.NewWebviewWindowWithOptions(&application.WebviewWindowOptions{
Name: windowName,
}).
SetTitle(windowName).
SetPosition(rand.Intn(1000), rand.Intn(800)).
Show()
windowCounter++
@ -50,6 +53,7 @@ func main() {
})
newWindow()
newWindow()
app.SetMenu(menu)
err := app.Run()

View File

@ -12,6 +12,7 @@ require (
github.com/leaanthony/winicon v1.0.0
github.com/matryer/is v1.4.0
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.51
github.com/samber/lo v1.37.0
github.com/tc-hib/winres v0.1.6

View File

@ -96,6 +96,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 h1:acNfDZXmm28D2Yg/c3ALnZStzNaZMSagpbr96vY6Zjc=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@ -22,7 +22,7 @@ import {enableContextMenus} from "./contextmenu";
import {reloadWML} from "./wml";
window.wails = {
...newRuntime(-1),
...newRuntime(null),
};
// Internal wails endpoints
@ -34,13 +34,16 @@ window._wails = {
callErrorCallback,
};
export function newRuntime(id) {
export function newRuntime(windowName) {
return {
Clipboard: {
...Clipboard
},
Application: {
...Application
...Application,
GetWindowByName(windowName) {
return newRuntime(windowName);
}
},
Log,
Screens,
@ -65,7 +68,7 @@ export function newRuntime(id) {
Off,
OffAll,
},
Window: newWindow(id),
Window: newWindow(windowName),
};
}

View File

@ -12,14 +12,20 @@ The electron alternative for Go
const runtimeURL = window.location.origin + "/wails/runtime";
function runtimeCall(method, args) {
function runtimeCall(method, windowName, args) {
let url = new URL(runtimeURL);
url.searchParams.append("method", method);
if(args) {
if (args) {
url.searchParams.append("args", JSON.stringify(args));
}
let fetchOptions = {
headers: {},
};
if (windowName) {
fetchOptions.headers["x-wails-window-name"] = windowName;
}
return new Promise((resolve, reject) => {
fetch(url)
fetch(url, fetchOptions)
.then(response => {
if (response.ok) {
// check content type
@ -36,15 +42,8 @@ function runtimeCall(method, args) {
});
}
export function newRuntimeCaller(object, id) {
if (!id || id === -1) {
return function (method, args) {
return runtimeCall(object + "." + method, args);
};
}
return function (method, args) {
args = args || {};
args.windowID = id;
return runtimeCall(object + "." + method, args);
export function newRuntimeCaller(object, windowName) {
return function (method, args=null) {
return runtimeCall(object + "." + method, windowName, args);
};
}

View File

@ -18,8 +18,8 @@ The electron alternative for Go
import {newRuntimeCaller} from "./runtime";
export function newWindow(id) {
let call = newRuntimeCaller("window", id);
export function newWindow(windowName) {
let call = newRuntimeCaller("window", windowName);
return {
// Reload: () => call('WR'),
// ReloadApp: () => call('WR'),

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -129,10 +129,12 @@ var windowDragAndDropBuffer = make(chan *dragAndDropMessage)
var _ webview.Request = &webViewAssetRequest{}
const webViewRequestHeaderWindowId = "x-wails-window-id"
const webViewRequestHeaderWindowName = "x-wails-window-name"
type webViewAssetRequest struct {
webview.Request
windowId uint
windowId uint
windowName string
}
func (r *webViewAssetRequest) Header() (http.Header, error) {
@ -215,7 +217,7 @@ func (a *App) On(eventType events.ApplicationEventType, callback func()) {
}
}
func (a *App) NewWebviewWindow() *WebviewWindow {
return a.NewWebviewWindowWithOptions(nil)
return a.NewWebviewWindowWithOptions(&WebviewWindowOptions{})
}
func (a *App) GetPID() int {
@ -261,7 +263,6 @@ func (a *App) NewWebviewWindowWithOptions(windowOptions *WebviewWindowOptions) *
if windowOptions == nil {
windowOptions = WebviewWindowDefaults
}
newWindow := NewWindow(windowOptions)
id := newWindow.id
if a.windows == nil {
@ -410,7 +411,7 @@ func (a *App) handleWindowMessage(event *windowMessage) {
func (a *App) handleWebViewRequest(request *webViewAssetRequest) {
// Get window from window map
url, _ := request.URL()
a.info("Window: %d, Request: %s", request.windowId, url)
a.info("Window: '%s', Request: %s", request.windowName, url)
a.assets.ServeWebViewRequest(request)
}
@ -585,3 +586,14 @@ func (a *App) getContextMenu(name string) (*Menu, bool) {
func (a *App) OnWindowCreation(callback func(window *WebviewWindow)) {
a.windowCreatedCallbacks = append(a.windowCreatedCallbacks, callback)
}
func (a *App) GetWindowByName(name string) *WebviewWindow {
a.windowsLock.Lock()
defer a.windowsLock.Unlock()
for _, window := range a.windows {
if window.Name() == name {
return window
}
}
return nil
}

View File

@ -225,8 +225,9 @@ func processMessage(windowID C.uint, message *C.char) {
//export processURLRequest
func processURLRequest(windowID C.uint, wkUrlSchemeTask unsafe.Pointer) {
webviewRequests <- &webViewAssetRequest{
Request: webview.NewRequest(wkUrlSchemeTask),
windowId: uint(windowID),
Request: webview.NewRequest(wkUrlSchemeTask),
windowId: uint(windowID),
windowName: globalApplication.getWindowForID(uint(windowID)).Name(),
}
}

View File

@ -26,6 +26,28 @@ func (m *MessageProcessor) httpError(rw http.ResponseWriter, message string, arg
rw.Write([]byte(fmt.Sprintf(message, args...)))
}
func (m *MessageProcessor) getTargetWindow(r *http.Request) *WebviewWindow {
windowName := r.Header.Get(webViewRequestHeaderWindowName)
if windowName != "" {
return globalApplication.GetWindowByName(windowName)
}
windowID := r.Header.Get(webViewRequestHeaderWindowId)
if windowID == "" {
return nil
}
wID, err := strconv.ParseUint(windowID, 10, 64)
if err != nil {
m.Error("Window ID '%s' not parsable: %s", windowID, err)
return nil
}
targetWindow := globalApplication.getWindowForID(uint(wID))
if targetWindow == nil {
m.Error("Window ID %d not found", wID)
return nil
}
return targetWindow
}
func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Request) {
// Read "method" from query string
method := r.URL.Query().Get("method")
@ -45,26 +67,9 @@ func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Req
params := QueryParams(r.URL.Query())
var windowID uint
if hWindowID := r.Header.Get(webViewRequestHeaderWindowId); hWindowID != "" {
// Get windowID out of the request header
wID, err := strconv.ParseUint(hWindowID, 10, 64)
if err != nil {
m.Error("Window ID '%s' not parsable: %s", hWindowID, err)
return
}
windowID = uint(wID)
}
if qWindowID := params.UInt("windowID"); qWindowID != nil {
// Get windowID out of the query parameters if provided
windowID = *qWindowID
}
targetWindow := globalApplication.getWindowForID(windowID)
targetWindow := m.getTargetWindow(r)
if targetWindow == nil {
m.Error("Window ID %s not found", windowID)
m.Error("No valid window found")
return
}

View File

@ -117,10 +117,7 @@ func (w *WebviewWindow) SetTitle(title string) *WebviewWindow {
}
func (w *WebviewWindow) Name() string {
if w.options.Name != "" {
return w.options.Name
}
return fmt.Sprintf("Window %d", w.id)
return w.options.Name
}
func (w *WebviewWindow) SetSize(width, height int) *WebviewWindow {