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

Reduce flicker.

This commit is contained in:
Lea Anthony 2024-12-27 09:09:00 +11:00
parent a90764891f
commit 27480bc6cb
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
5 changed files with 55 additions and 50 deletions

View File

@ -2,15 +2,20 @@ package main
import ( import (
_ "embed" _ "embed"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/icons"
"log" "log"
"runtime" "runtime"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/icons"
) )
func createWindow(app *application.App) *application.WebviewWindow { var windowShowing bool
func createWindow(app *application.App) {
if windowShowing {
return
}
// Log the time taken to create the window
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Width: 500, Width: 500,
Height: 500, Height: 500,
@ -23,12 +28,13 @@ func createWindow(app *application.App) *application.WebviewWindow {
HiddenOnTaskbar: true, HiddenOnTaskbar: true,
}, },
}) })
windowShowing = true
window.OnWindowEvent(events.Common.WindowClosing, func(e *application.WindowEvent) { window.OnWindowEvent(events.Common.WindowClosing, func(e *application.WindowEvent) {
println("Window Closing") windowShowing = false
}) })
return window window.Show()
} }
func main() { func main() {
@ -45,7 +51,6 @@ func main() {
}) })
systemTray := app.NewSystemTray() systemTray := app.NewSystemTray()
window := createWindow(app)
menu := app.NewMenu() menu := app.NewMenu()
menu.Add("Quit").OnClick(func(data *application.Context) { menu.Add("Quit").OnClick(func(data *application.Context) {
app.Quit() app.Quit()
@ -57,12 +62,9 @@ func main() {
} }
systemTray.OnClick(func() { systemTray.OnClick(func() {
println("Creating New Window!") createWindow(app)
createWindow(app).Show()
}) })
systemTray.AttachWindow(window).WindowOffset(5)
err := app.Run() err := app.Run()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ import (
func DefaultLogger(level slog.Level) *slog.Logger { func DefaultLogger(level slog.Level) *slog.Logger {
return slog.New(tint.NewHandler(colorable.NewColorable(os.Stderr), &tint.Options{ return slog.New(tint.NewHandler(colorable.NewColorable(os.Stderr), &tint.Options{
TimeFormat: time.Kitchen, TimeFormat: time.StampMilli,
NoColor: !isatty.IsTerminal(os.Stderr.Fd()), NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
Level: level, Level: level,
})) }))

View File

@ -254,7 +254,7 @@ func NewWindow(options WebviewWindowOptions) *WebviewWindow {
// Listen for window closing events and de // Listen for window closing events and de
result.OnWindowEvent(events.Common.WindowClosing, func(event *WindowEvent) { result.OnWindowEvent(events.Common.WindowClosing, func(event *WindowEvent) {
result.unconditionallyClose = true result.unconditionallyClose = true
result.markAsDestroyed() InvokeSync(result.markAsDestroyed)
InvokeSync(result.impl.close) InvokeSync(result.impl.close)
globalApplication.deleteWindowByID(result.id) globalApplication.deleteWindowByID(result.id)
}) })
@ -417,6 +417,7 @@ func (w *WebviewWindow) Show() Window {
InvokeSync(w.Run) InvokeSync(w.Run)
return w return w
} }
w.options.Hidden = false
InvokeSync(w.impl.show) InvokeSync(w.impl.show)
return w return w
} }
@ -434,7 +435,7 @@ func (w *WebviewWindow) SetURL(s string) Window {
url, _ := assetserver.GetStartURL(s) url, _ := assetserver.GetStartURL(s)
w.options.URL = url w.options.URL = url
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setURL(url) w.impl.setURL(url)
}) })
} }
@ -452,7 +453,7 @@ func (w *WebviewWindow) GetBorderSizes() *LRTB {
func (w *WebviewWindow) SetZoom(magnification float64) Window { func (w *WebviewWindow) SetZoom(magnification float64) Window {
w.options.Zoom = magnification w.options.Zoom = magnification
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setZoom(magnification) w.impl.setZoom(magnification)
}) })
} }
@ -471,7 +472,7 @@ func (w *WebviewWindow) GetZoom() float64 {
func (w *WebviewWindow) SetResizable(b bool) Window { func (w *WebviewWindow) SetResizable(b bool) Window {
w.options.DisableResize = !b w.options.DisableResize = !b
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setResizable(b) w.impl.setResizable(b)
}) })
} }
@ -504,11 +505,11 @@ func (w *WebviewWindow) SetMinSize(minWidth, minHeight int) Window {
} }
if w.impl != nil { if w.impl != nil {
if newSize { if newSize {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setSize(newWidth, newHeight) w.impl.setSize(newWidth, newHeight)
}) })
} }
InvokeAsync(func() { InvokeSync(func() {
w.impl.setMinSize(minWidth, minHeight) w.impl.setMinSize(minWidth, minHeight)
}) })
} }
@ -536,11 +537,11 @@ func (w *WebviewWindow) SetMaxSize(maxWidth, maxHeight int) Window {
} }
if w.impl != nil { if w.impl != nil {
if newSize { if newSize {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setSize(newWidth, newHeight) w.impl.setSize(newWidth, newHeight)
}) })
} }
InvokeAsync(func() { InvokeSync(func() {
w.impl.setMaxSize(maxWidth, maxHeight) w.impl.setMaxSize(maxWidth, maxHeight)
}) })
} }
@ -553,7 +554,7 @@ func (w *WebviewWindow) ExecJS(js string) {
return return
} }
if w.runtimeLoaded { if w.runtimeLoaded {
InvokeAsync(func() { InvokeSync(func() {
w.impl.execJS(js) w.impl.execJS(js)
}) })
} else { } else {
@ -569,7 +570,7 @@ func (w *WebviewWindow) Fullscreen() Window {
} }
if !w.IsFullscreen() { if !w.IsFullscreen() {
w.DisableSizeConstraints() w.DisableSizeConstraints()
InvokeAsync(w.impl.fullscreen) InvokeSync(w.impl.fullscreen)
} }
return w return w
} }
@ -577,7 +578,7 @@ func (w *WebviewWindow) Fullscreen() Window {
func (w *WebviewWindow) SetMinimiseButtonState(state ButtonState) Window { func (w *WebviewWindow) SetMinimiseButtonState(state ButtonState) Window {
w.options.MinimiseButtonState = state w.options.MinimiseButtonState = state
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setMinimiseButtonState(state) w.impl.setMinimiseButtonState(state)
}) })
} }
@ -587,7 +588,7 @@ func (w *WebviewWindow) SetMinimiseButtonState(state ButtonState) Window {
func (w *WebviewWindow) SetMaximiseButtonState(state ButtonState) Window { func (w *WebviewWindow) SetMaximiseButtonState(state ButtonState) Window {
w.options.MaximiseButtonState = state w.options.MaximiseButtonState = state
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setMaximiseButtonState(state) w.impl.setMaximiseButtonState(state)
}) })
} }
@ -597,7 +598,7 @@ func (w *WebviewWindow) SetMaximiseButtonState(state ButtonState) Window {
func (w *WebviewWindow) SetCloseButtonState(state ButtonState) Window { func (w *WebviewWindow) SetCloseButtonState(state ButtonState) Window {
w.options.CloseButtonState = state w.options.CloseButtonState = state
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setCloseButtonState(state) w.impl.setCloseButtonState(state)
}) })
} }
@ -610,7 +611,7 @@ func (w *WebviewWindow) Flash(enabled bool) {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
w.impl.flash(enabled) w.impl.flash(enabled)
}) })
} }
@ -671,7 +672,7 @@ func (w *WebviewWindow) IsFullscreen() bool {
func (w *WebviewWindow) SetBackgroundColour(colour RGBA) Window { func (w *WebviewWindow) SetBackgroundColour(colour RGBA) Window {
w.options.BackgroundColour = colour w.options.BackgroundColour = colour
if w.impl != nil { if w.impl != nil {
InvokeAsync(func() { InvokeSync(func() {
w.impl.setBackgroundColour(colour) w.impl.setBackgroundColour(colour)
}) })
} }
@ -683,7 +684,7 @@ func (w *WebviewWindow) HandleMessage(message string) {
switch true { switch true {
case message == "wails:drag": case message == "wails:drag":
if !w.IsFullscreen() { if !w.IsFullscreen() {
InvokeAsync(func() { InvokeSync(func() {
err := w.startDrag() err := w.startDrag()
if err != nil { if err != nil {
w.Error("Failed to start drag: %s", err) w.Error("Failed to start drag: %s", err)
@ -729,7 +730,7 @@ func (w *WebviewWindow) Center() {
w.options.InitialPosition = WindowCentered w.options.InitialPosition = WindowCentered
return return
} }
InvokeAsync(w.impl.center) InvokeSync(w.impl.center)
} }
// OnWindowEvent registers a callback for the given window event // OnWindowEvent registers a callback for the given window event
@ -849,7 +850,7 @@ func (w *WebviewWindow) SetBounds(bounds Rect) {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
w.impl.setBounds(bounds) w.impl.setBounds(bounds)
}) })
} }
@ -860,7 +861,7 @@ func (w *WebviewWindow) Position() (int, int) {
return 0, 0 return 0, 0
} }
var x, y int var x, y int
InvokeAsync(func() { InvokeSync(func() {
x, y = w.impl.position() x, y = w.impl.position()
}) })
return x, y return x, y
@ -871,7 +872,7 @@ func (w *WebviewWindow) SetPosition(x int, y int) {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
w.impl.setPosition(x, y) w.impl.setPosition(x, y)
}) })
} }
@ -934,7 +935,7 @@ func (w *WebviewWindow) ToggleFullscreen() {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
if w.IsFullscreen() { if w.IsFullscreen() {
w.UnFullscreen() w.UnFullscreen()
} else { } else {
@ -948,7 +949,7 @@ func (w *WebviewWindow) ToggleMaximise() {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
if w.IsMaximised() { if w.IsMaximised() {
w.UnMaximise() w.UnMaximise()
} else { } else {
@ -993,7 +994,7 @@ func (w *WebviewWindow) Close() {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
w.emit(events.Common.WindowClosing) w.emit(events.Common.WindowClosing)
}) })
} }
@ -1182,7 +1183,7 @@ func (w *WebviewWindow) OpenContextMenu(data *ContextMenuData) {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
return return
} }
InvokeAsync(func() { InvokeSync(func() {
w.impl.openContextMenu(menu, data) w.impl.openContextMenu(menu, data)
}) })
} }

View File

@ -58,8 +58,8 @@ type windowsWebviewWindow struct {
previousWindowPlacement w32.WINDOWPLACEMENT previousWindowPlacement w32.WINDOWPLACEMENT
// Webview // Webview
chromium *edge.Chromium chromium *edge.Chromium
hasStarted bool webviewNavigationCompleted bool
// resizeBorder* is the width/height of the resize border in pixels. // resizeBorder* is the width/height of the resize border in pixels.
resizeBorderWidth int32 resizeBorderWidth int32
@ -153,6 +153,7 @@ func (w *windowsWebviewWindow) setAlwaysOnTop(alwaysOnTop bool) {
func (w *windowsWebviewWindow) setURL(url string) { func (w *windowsWebviewWindow) setURL(url string) {
// Navigate to the given URL in the webview // Navigate to the given URL in the webview
w.webviewNavigationCompleted = false
w.chromium.Navigate(url) w.chromium.Navigate(url)
} }
@ -383,10 +384,6 @@ func (w *windowsWebviewWindow) run() {
w.chromium.Resize() w.chromium.Resize()
} }
if !options.Hidden {
w.parent.Show()
w.update()
}
} }
func (w *windowsWebviewWindow) center() { func (w *windowsWebviewWindow) center() {
@ -871,7 +868,10 @@ func (w *windowsWebviewWindow) printStyle() {
} }
func (w *windowsWebviewWindow) show() { func (w *windowsWebviewWindow) show() {
w32.ShowWindow(w.hwnd, w32.SW_SHOW) if w.webviewNavigationCompleted {
w.chromium.Show()
w32.ShowWindow(w.hwnd, w32.SW_SHOW)
}
} }
func (w *windowsWebviewWindow) hide() { func (w *windowsWebviewWindow) hide() {
@ -1667,6 +1667,7 @@ func (w *windowsWebviewWindow) setupChromium() {
if err != nil { if err != nil {
globalApplication.fatal(err.Error()) globalApplication.fatal(err.Error())
} }
w.webviewNavigationCompleted = false
chromium.Navigate(startURL) chromium.Navigate(startURL)
} }
@ -1709,12 +1710,12 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a
// EmitEvent DomReady ApplicationEvent // EmitEvent DomReady ApplicationEvent
windowEvents <- &windowEvent{EventID: uint(events.Windows.WebViewNavigationCompleted), WindowID: w.parent.id} windowEvents <- &windowEvent{EventID: uint(events.Windows.WebViewNavigationCompleted), WindowID: w.parent.id}
if w.hasStarted { if w.webviewNavigationCompleted {
// NavigationCompleted is triggered for every Load. If an application uses reloads the Hide/Show will trigger // NavigationCompleted is triggered for every Load. If an application uses reloads the Hide/Show will trigger
// a flickering of the window with every reload. So we only do this once for the first NavigationCompleted. // a flickering of the window with every reload. So we only do this once for the first NavigationCompleted.
return return
} }
w.hasStarted = true w.webviewNavigationCompleted = true
wasFocused := w.isFocused() wasFocused := w.isFocused()
// Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026 // Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026
@ -1729,9 +1730,10 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a
if wasFocused { if wasFocused {
w.focus() w.focus()
} }
if !w.parent.options.Hidden {
//f.mainWindow.hasBeenShown = true w.parent.Show()
w.update()
}
} }
func (w *windowsWebviewWindow) processKeyBinding(vkey uint) bool { func (w *windowsWebviewWindow) processKeyBinding(vkey uint) bool {