From b24bcb93820cd0fb80a79422db71559a3c5d1561 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 30 Sep 2023 02:46:11 +0900 Subject: [PATCH] internal/ui: bug fix: SetWindowIcon(nil) didn't reset the window icon Closes #2796 --- examples/windowsize/main.go | 4 ++++ internal/goglfw/win32window_windows.go | 3 +++ internal/ui/ui_glfw.go | 24 +++++++++++++++--------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index ab0f9f4e7..c2073e0cf 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -307,6 +307,9 @@ func (g *game) Update() error { if inpututil.IsKeyJustPressed(ebiten.KeyI) { ebiten.SetWindowIcon([]image.Image{createRandomIconImage()}) } + if inpututil.IsKeyJustPressed(ebiten.KeyJ) { + ebiten.SetWindowIcon(nil) + } ebiten.SetWindowMousePassthrough(mousePassthrough) @@ -358,6 +361,7 @@ func (g *game) Draw(screen *ebiten.Image) { [U] Switch the runnable-on-unfocused state [C] Switch the cursor mode (visible, hidden, or captured) [I] Change the window icon (only for desktops) +[J] Reset the window icon (only for desktops) [V] Switch the vsync [T] Switch TPS (ticks per second) [D] Switch the window decoration (only for desktops) diff --git a/internal/goglfw/win32window_windows.go b/internal/goglfw/win32window_windows.go index 3b2924ef0..81435dba2 100644 --- a/internal/goglfw/win32window_windows.go +++ b/internal/goglfw/win32window_windows.go @@ -1559,6 +1559,9 @@ func (w *Window) platformSetWindowIcon(images []*Image) error { if len(images) > 0 { w.platform.bigIcon = bigIcon w.platform.smallIcon = smallIcon + } else { + w.platform.bigIcon = 0 + w.platform.smallIcon = 0 } return nil } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 7edacfe51..820782a11 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -425,17 +425,19 @@ func (u *userInterfaceImpl) setRunnableOnUnfocused(runnableOnUnfocused bool) { u.m.Unlock() } -func (u *userInterfaceImpl) getIconImages() []image.Image { +func (u *userInterfaceImpl) getAndResetIconImages() []image.Image { u.m.RLock() + defer u.m.RUnlock() i := u.iconImages - u.m.RUnlock() + u.iconImages = nil return i } func (u *userInterfaceImpl) setIconImages(iconImages []image.Image) { u.m.Lock() - u.iconImages = iconImages - u.m.Unlock() + defer u.m.Unlock() + u.iconImages = make([]image.Image, len(iconImages)) + copy(u.iconImages, iconImages) } func (u *userInterfaceImpl) getInitWindowPositionInDIP() (int, int) { @@ -1262,14 +1264,18 @@ func (u *userInterfaceImpl) updateIconIfNeeded() error { return nil } - imgs := u.getIconImages() - if len(imgs) == 0 { + imgs := u.getAndResetIconImages() + // A 0-size slice and nil are distinguished here. + // A 0-size slice means a user indicates to reset the icon. + // On the other hand, nil means a user didn't update the icon state. + if imgs == nil { return nil } - u.setIconImages(nil) - - newImgs := make([]image.Image, len(imgs)) + var newImgs []image.Image + if len(imgs) > 0 { + newImgs = make([]image.Image, len(imgs)) + } for i, img := range imgs { // TODO: If img is not *ebiten.Image, this converting is not necessary. // However, this package cannot refer *ebiten.Image due to the package