5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-21 11:29: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 (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"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{
Width: 500,
Height: 500,
@ -23,12 +28,13 @@ func createWindow(app *application.App) *application.WebviewWindow {
HiddenOnTaskbar: true,
},
})
windowShowing = true
window.OnWindowEvent(events.Common.WindowClosing, func(e *application.WindowEvent) {
println("Window Closing")
windowShowing = false
})
return window
window.Show()
}
func main() {
@ -45,7 +51,6 @@ func main() {
})
systemTray := app.NewSystemTray()
window := createWindow(app)
menu := app.NewMenu()
menu.Add("Quit").OnClick(func(data *application.Context) {
app.Quit()
@ -57,12 +62,9 @@ func main() {
}
systemTray.OnClick(func() {
println("Creating New Window!")
createWindow(app).Show()
createWindow(app)
})
systemTray.AttachWindow(window).WindowOffset(5)
err := app.Run()
if err != nil {
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 {
return slog.New(tint.NewHandler(colorable.NewColorable(os.Stderr), &tint.Options{
TimeFormat: time.Kitchen,
TimeFormat: time.StampMilli,
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
Level: level,
}))

View File

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

View File

@ -58,8 +58,8 @@ type windowsWebviewWindow struct {
previousWindowPlacement w32.WINDOWPLACEMENT
// Webview
chromium *edge.Chromium
hasStarted bool
chromium *edge.Chromium
webviewNavigationCompleted bool
// resizeBorder* is the width/height of the resize border in pixels.
resizeBorderWidth int32
@ -153,6 +153,7 @@ func (w *windowsWebviewWindow) setAlwaysOnTop(alwaysOnTop bool) {
func (w *windowsWebviewWindow) setURL(url string) {
// Navigate to the given URL in the webview
w.webviewNavigationCompleted = false
w.chromium.Navigate(url)
}
@ -383,10 +384,6 @@ func (w *windowsWebviewWindow) run() {
w.chromium.Resize()
}
if !options.Hidden {
w.parent.Show()
w.update()
}
}
func (w *windowsWebviewWindow) center() {
@ -871,7 +868,10 @@ func (w *windowsWebviewWindow) printStyle() {
}
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() {
@ -1667,6 +1667,7 @@ func (w *windowsWebviewWindow) setupChromium() {
if err != nil {
globalApplication.fatal(err.Error())
}
w.webviewNavigationCompleted = false
chromium.Navigate(startURL)
}
@ -1709,12 +1710,12 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a
// EmitEvent DomReady ApplicationEvent
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
// a flickering of the window with every reload. So we only do this once for the first NavigationCompleted.
return
}
w.hasStarted = true
w.webviewNavigationCompleted = true
wasFocused := w.isFocused()
// 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 {
w.focus()
}
//f.mainWindow.hasBeenShown = true
if !w.parent.options.Hidden {
w.parent.Show()
w.update()
}
}
func (w *windowsWebviewWindow) processKeyBinding(vkey uint) bool {