From 1a128905562fe17934ce6561eb27910bcbb7d854 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 4 Jun 2023 19:01:53 +1000 Subject: [PATCH] [v3 windows] Add Webview2NavigationCompleted event. Support CSS + JS injection --- v3/go.mod | 2 +- v3/go.sum | 2 ++ v3/pkg/application/webview_window_windows.go | 22 ++++++++++++--- v3/pkg/events/events.go | 28 +++++++++++--------- v3/pkg/events/events.txt | 1 + 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/v3/go.mod b/v3/go.mod index 4109f9653..ab7209257 100644 --- a/v3/go.mod +++ b/v3/go.mod @@ -19,7 +19,7 @@ require ( github.com/pterm/pterm v0.12.51 github.com/samber/lo v1.37.0 github.com/tc-hib/winres v0.1.6 - github.com/wailsapp/go-webview2 v1.0.1 + github.com/wailsapp/go-webview2 v1.0.2-0.20230604075323-d593c659ca7b github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 golang.org/x/sys v0.8.0 modernc.org/sqlite v1.21.0 diff --git a/v3/go.sum b/v3/go.sum index 8d2aa703b..741dabbe6 100644 --- a/v3/go.sum +++ b/v3/go.sum @@ -146,6 +146,8 @@ github.com/tc-hib/winres v0.1.6 h1:qgsYHze+BxQPEYilxIz/KCQGaClvI2+yLBAZs+3+0B8= github.com/tc-hib/winres v0.1.6/go.mod h1:pe6dOR40VOrGz8PkzreVKNvEKnlE8t4yR8A8naL+t7A= github.com/wailsapp/go-webview2 v1.0.1 h1:dEJIeEApW/MhO2tTMISZBFZPuW7kwrFA1NtgFB1z1II= github.com/wailsapp/go-webview2 v1.0.1/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo= +github.com/wailsapp/go-webview2 v1.0.2-0.20230604075323-d593c659ca7b h1:cztK9x+ikg6nFscy5c8NgtfIXv/d0ESdENy9+JkE8i4= +github.com/wailsapp/go-webview2 v1.0.2-0.20230604075323-d593c659ca7b/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo= github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs= github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index bb86b0da6..358a8e54e 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -374,8 +374,8 @@ func (w *windowsWebviewWindow) setPosition(x int, y int) { // on is used to indicate that a particular event should be listened for func (w *windowsWebviewWindow) on(eventID uint) { - //TODO implement me - panic("implement me") + // We don't need to worry about this in Windows as we do not need + // to optimise cgo calls } func (w *windowsWebviewWindow) minimise() { @@ -1192,13 +1192,27 @@ func (w *windowsWebviewWindow) setupChromium() { chromium.SetGlobalPermission(edge.CoreWebView2PermissionStateAllow) chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL) - chromium.Navigate("http://wails.localhost") + + if w.parent.options.HTML != "" { + var script string + if w.parent.options.JS != "" { + script = w.parent.options.JS + } + if w.parent.options.CSS != "" { + script += fmt.Sprintf("; addEventListener(\"DOMContentLoaded\", (event) => { document.head.appendChild(document.createElement('style')).innerHTML=\"%s\"; });", strings.ReplaceAll(w.parent.options.CSS, `"`, `\"`)) + } + chromium.Init(script) + chromium.NavigateToString(w.parent.options.HTML) + } else { + chromium.Navigate("http://wails.localhost") + } } func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) { - // TODO: DomReady Event + // Emit DomReady Event + windowEvents <- &WindowEvent{EventID: uint(events.Windows.WebViewNavigationCompleted), WindowID: w.parent.id} // Todo: Resize hacks /* diff --git a/v3/pkg/events/events.go b/v3/pkg/events/events.go index a3a36a8b9..fe1b64702 100644 --- a/v3/pkg/events/events.go +++ b/v3/pkg/events/events.go @@ -15,7 +15,7 @@ type commonEvents struct { func newCommonEvents() commonEvents { return commonEvents{ - ApplicationStarted: 1152, + ApplicationStarted: 1153, } } @@ -276,21 +276,23 @@ func newMacEvents() macEvents { var Windows = newWindowsEvents() type windowsEvents struct { - SystemThemeChanged ApplicationEventType - APMPowerStatusChange ApplicationEventType - APMSuspend ApplicationEventType - APMResumeAutomatic ApplicationEventType - APMResumeSuspend ApplicationEventType - APMPowerSettingChange ApplicationEventType + SystemThemeChanged ApplicationEventType + APMPowerStatusChange ApplicationEventType + APMSuspend ApplicationEventType + APMResumeAutomatic ApplicationEventType + APMResumeSuspend ApplicationEventType + APMPowerSettingChange ApplicationEventType + WebViewNavigationCompleted WindowEventType } func newWindowsEvents() windowsEvents { return windowsEvents{ - SystemThemeChanged: 1146, - APMPowerStatusChange: 1147, - APMSuspend: 1148, - APMResumeAutomatic: 1149, - APMResumeSuspend: 1150, - APMPowerSettingChange: 1151, + SystemThemeChanged: 1146, + APMPowerStatusChange: 1147, + APMSuspend: 1148, + APMResumeAutomatic: 1149, + APMResumeSuspend: 1150, + APMPowerSettingChange: 1151, + WebViewNavigationCompleted: 1152, } } diff --git a/v3/pkg/events/events.txt b/v3/pkg/events/events.txt index 2857cfd88..95159c17d 100644 --- a/v3/pkg/events/events.txt +++ b/v3/pkg/events/events.txt @@ -126,4 +126,5 @@ windows:APMSuspend windows:APMResumeAutomatic windows:APMResumeSuspend windows:APMPowerSettingChange +windows:WebViewNavigationCompleted common:ApplicationStarted