mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 07:21:32 +08:00
[v3] Add ability to get window by name
This commit is contained in:
parent
7f1093dc51
commit
424e4fc2e8
@ -33,8 +33,11 @@ func main() {
|
|||||||
windowCounter := 1
|
windowCounter := 1
|
||||||
|
|
||||||
newWindow := func() {
|
newWindow := func() {
|
||||||
app.NewWebviewWindowWithOptions(&application.WebviewWindowOptions{}).
|
windowName := "WebviewWindow " + strconv.Itoa(windowCounter)
|
||||||
SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)).
|
app.NewWebviewWindowWithOptions(&application.WebviewWindowOptions{
|
||||||
|
Name: windowName,
|
||||||
|
}).
|
||||||
|
SetTitle(windowName).
|
||||||
SetPosition(rand.Intn(1000), rand.Intn(800)).
|
SetPosition(rand.Intn(1000), rand.Intn(800)).
|
||||||
Show()
|
Show()
|
||||||
windowCounter++
|
windowCounter++
|
||||||
@ -50,6 +53,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
newWindow()
|
newWindow()
|
||||||
|
newWindow()
|
||||||
|
|
||||||
app.SetMenu(menu)
|
app.SetMenu(menu)
|
||||||
err := app.Run()
|
err := app.Run()
|
||||||
|
@ -12,6 +12,7 @@ require (
|
|||||||
github.com/leaanthony/winicon v1.0.0
|
github.com/leaanthony/winicon v1.0.0
|
||||||
github.com/matryer/is v1.4.0
|
github.com/matryer/is v1.4.0
|
||||||
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2
|
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/pterm/pterm v0.12.51
|
||||||
github.com/samber/lo v1.37.0
|
github.com/samber/lo v1.37.0
|
||||||
github.com/tc-hib/winres v0.1.6
|
github.com/tc-hib/winres v0.1.6
|
||||||
|
@ -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 h1:acNfDZXmm28D2Yg/c3ALnZStzNaZMSagpbr96vY6Zjc=
|
||||||
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
|
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.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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
@ -22,7 +22,7 @@ import {enableContextMenus} from "./contextmenu";
|
|||||||
import {reloadWML} from "./wml";
|
import {reloadWML} from "./wml";
|
||||||
|
|
||||||
window.wails = {
|
window.wails = {
|
||||||
...newRuntime(-1),
|
...newRuntime(null),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Internal wails endpoints
|
// Internal wails endpoints
|
||||||
@ -34,13 +34,16 @@ window._wails = {
|
|||||||
callErrorCallback,
|
callErrorCallback,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function newRuntime(id) {
|
export function newRuntime(windowName) {
|
||||||
return {
|
return {
|
||||||
Clipboard: {
|
Clipboard: {
|
||||||
...Clipboard
|
...Clipboard
|
||||||
},
|
},
|
||||||
Application: {
|
Application: {
|
||||||
...Application
|
...Application,
|
||||||
|
GetWindowByName(windowName) {
|
||||||
|
return newRuntime(windowName);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Log,
|
Log,
|
||||||
Screens,
|
Screens,
|
||||||
@ -65,7 +68,7 @@ export function newRuntime(id) {
|
|||||||
Off,
|
Off,
|
||||||
OffAll,
|
OffAll,
|
||||||
},
|
},
|
||||||
Window: newWindow(id),
|
Window: newWindow(windowName),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,14 +12,20 @@ The electron alternative for Go
|
|||||||
|
|
||||||
const runtimeURL = window.location.origin + "/wails/runtime";
|
const runtimeURL = window.location.origin + "/wails/runtime";
|
||||||
|
|
||||||
function runtimeCall(method, args) {
|
function runtimeCall(method, windowName, args) {
|
||||||
let url = new URL(runtimeURL);
|
let url = new URL(runtimeURL);
|
||||||
url.searchParams.append("method", method);
|
url.searchParams.append("method", method);
|
||||||
if(args) {
|
if (args) {
|
||||||
url.searchParams.append("args", JSON.stringify(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) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch(url)
|
fetch(url, fetchOptions)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
// check content type
|
// check content type
|
||||||
@ -36,15 +42,8 @@ function runtimeCall(method, args) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function newRuntimeCaller(object, id) {
|
export function newRuntimeCaller(object, windowName) {
|
||||||
if (!id || id === -1) {
|
return function (method, args=null) {
|
||||||
return function (method, args) {
|
return runtimeCall(object + "." + method, windowName, args);
|
||||||
return runtimeCall(object + "." + method, args);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return function (method, args) {
|
|
||||||
args = args || {};
|
|
||||||
args.windowID = id;
|
|
||||||
return runtimeCall(object + "." + method, args);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -18,8 +18,8 @@ The electron alternative for Go
|
|||||||
|
|
||||||
import {newRuntimeCaller} from "./runtime";
|
import {newRuntimeCaller} from "./runtime";
|
||||||
|
|
||||||
export function newWindow(id) {
|
export function newWindow(windowName) {
|
||||||
let call = newRuntimeCaller("window", id);
|
let call = newRuntimeCaller("window", windowName);
|
||||||
return {
|
return {
|
||||||
// Reload: () => call('WR'),
|
// Reload: () => call('WR'),
|
||||||
// ReloadApp: () => 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
@ -129,10 +129,12 @@ var windowDragAndDropBuffer = make(chan *dragAndDropMessage)
|
|||||||
var _ webview.Request = &webViewAssetRequest{}
|
var _ webview.Request = &webViewAssetRequest{}
|
||||||
|
|
||||||
const webViewRequestHeaderWindowId = "x-wails-window-id"
|
const webViewRequestHeaderWindowId = "x-wails-window-id"
|
||||||
|
const webViewRequestHeaderWindowName = "x-wails-window-name"
|
||||||
|
|
||||||
type webViewAssetRequest struct {
|
type webViewAssetRequest struct {
|
||||||
webview.Request
|
webview.Request
|
||||||
windowId uint
|
windowId uint
|
||||||
|
windowName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *webViewAssetRequest) Header() (http.Header, error) {
|
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 {
|
func (a *App) NewWebviewWindow() *WebviewWindow {
|
||||||
return a.NewWebviewWindowWithOptions(nil)
|
return a.NewWebviewWindowWithOptions(&WebviewWindowOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) GetPID() int {
|
func (a *App) GetPID() int {
|
||||||
@ -261,7 +263,6 @@ func (a *App) NewWebviewWindowWithOptions(windowOptions *WebviewWindowOptions) *
|
|||||||
if windowOptions == nil {
|
if windowOptions == nil {
|
||||||
windowOptions = WebviewWindowDefaults
|
windowOptions = WebviewWindowDefaults
|
||||||
}
|
}
|
||||||
|
|
||||||
newWindow := NewWindow(windowOptions)
|
newWindow := NewWindow(windowOptions)
|
||||||
id := newWindow.id
|
id := newWindow.id
|
||||||
if a.windows == nil {
|
if a.windows == nil {
|
||||||
@ -410,7 +411,7 @@ func (a *App) handleWindowMessage(event *windowMessage) {
|
|||||||
func (a *App) handleWebViewRequest(request *webViewAssetRequest) {
|
func (a *App) handleWebViewRequest(request *webViewAssetRequest) {
|
||||||
// Get window from window map
|
// Get window from window map
|
||||||
url, _ := request.URL()
|
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)
|
a.assets.ServeWebViewRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -585,3 +586,14 @@ func (a *App) getContextMenu(name string) (*Menu, bool) {
|
|||||||
func (a *App) OnWindowCreation(callback func(window *WebviewWindow)) {
|
func (a *App) OnWindowCreation(callback func(window *WebviewWindow)) {
|
||||||
a.windowCreatedCallbacks = append(a.windowCreatedCallbacks, callback)
|
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
|
||||||
|
}
|
||||||
|
@ -225,8 +225,9 @@ func processMessage(windowID C.uint, message *C.char) {
|
|||||||
//export processURLRequest
|
//export processURLRequest
|
||||||
func processURLRequest(windowID C.uint, wkUrlSchemeTask unsafe.Pointer) {
|
func processURLRequest(windowID C.uint, wkUrlSchemeTask unsafe.Pointer) {
|
||||||
webviewRequests <- &webViewAssetRequest{
|
webviewRequests <- &webViewAssetRequest{
|
||||||
Request: webview.NewRequest(wkUrlSchemeTask),
|
Request: webview.NewRequest(wkUrlSchemeTask),
|
||||||
windowId: uint(windowID),
|
windowId: uint(windowID),
|
||||||
|
windowName: globalApplication.getWindowForID(uint(windowID)).Name(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +26,28 @@ func (m *MessageProcessor) httpError(rw http.ResponseWriter, message string, arg
|
|||||||
rw.Write([]byte(fmt.Sprintf(message, args...)))
|
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) {
|
func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Request) {
|
||||||
// Read "method" from query string
|
// Read "method" from query string
|
||||||
method := r.URL.Query().Get("method")
|
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())
|
params := QueryParams(r.URL.Query())
|
||||||
|
|
||||||
var windowID uint
|
targetWindow := m.getTargetWindow(r)
|
||||||
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)
|
|
||||||
if targetWindow == nil {
|
if targetWindow == nil {
|
||||||
m.Error("Window ID %s not found", windowID)
|
m.Error("No valid window found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,10 +117,7 @@ func (w *WebviewWindow) SetTitle(title string) *WebviewWindow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebviewWindow) Name() string {
|
func (w *WebviewWindow) Name() string {
|
||||||
if w.options.Name != "" {
|
return w.options.Name
|
||||||
return w.options.Name
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("Window %d", w.id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebviewWindow) SetSize(width, height int) *WebviewWindow {
|
func (w *WebviewWindow) SetSize(width, height int) *WebviewWindow {
|
||||||
|
Loading…
Reference in New Issue
Block a user