internal/ui: bug fix: SetWindowIcon(nil) didn't reset the window icon

Closes #2796
This commit is contained in:
Hajime Hoshi 2023-09-30 02:46:11 +09:00
parent ff64920b73
commit b24bcb9382
3 changed files with 22 additions and 9 deletions

View File

@ -307,6 +307,9 @@ func (g *game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyI) { if inpututil.IsKeyJustPressed(ebiten.KeyI) {
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()}) ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
} }
if inpututil.IsKeyJustPressed(ebiten.KeyJ) {
ebiten.SetWindowIcon(nil)
}
ebiten.SetWindowMousePassthrough(mousePassthrough) ebiten.SetWindowMousePassthrough(mousePassthrough)
@ -358,6 +361,7 @@ func (g *game) Draw(screen *ebiten.Image) {
[U] Switch the runnable-on-unfocused state [U] Switch the runnable-on-unfocused state
[C] Switch the cursor mode (visible, hidden, or captured) [C] Switch the cursor mode (visible, hidden, or captured)
[I] Change the window icon (only for desktops) [I] Change the window icon (only for desktops)
[J] Reset the window icon (only for desktops)
[V] Switch the vsync [V] Switch the vsync
[T] Switch TPS (ticks per second) [T] Switch TPS (ticks per second)
[D] Switch the window decoration (only for desktops) [D] Switch the window decoration (only for desktops)

View File

@ -1559,6 +1559,9 @@ func (w *Window) platformSetWindowIcon(images []*Image) error {
if len(images) > 0 { if len(images) > 0 {
w.platform.bigIcon = bigIcon w.platform.bigIcon = bigIcon
w.platform.smallIcon = smallIcon w.platform.smallIcon = smallIcon
} else {
w.platform.bigIcon = 0
w.platform.smallIcon = 0
} }
return nil return nil
} }

View File

@ -425,17 +425,19 @@ func (u *userInterfaceImpl) setRunnableOnUnfocused(runnableOnUnfocused bool) {
u.m.Unlock() u.m.Unlock()
} }
func (u *userInterfaceImpl) getIconImages() []image.Image { func (u *userInterfaceImpl) getAndResetIconImages() []image.Image {
u.m.RLock() u.m.RLock()
defer u.m.RUnlock()
i := u.iconImages i := u.iconImages
u.m.RUnlock() u.iconImages = nil
return i return i
} }
func (u *userInterfaceImpl) setIconImages(iconImages []image.Image) { func (u *userInterfaceImpl) setIconImages(iconImages []image.Image) {
u.m.Lock() u.m.Lock()
u.iconImages = iconImages defer u.m.Unlock()
u.m.Unlock() u.iconImages = make([]image.Image, len(iconImages))
copy(u.iconImages, iconImages)
} }
func (u *userInterfaceImpl) getInitWindowPositionInDIP() (int, int) { func (u *userInterfaceImpl) getInitWindowPositionInDIP() (int, int) {
@ -1262,14 +1264,18 @@ func (u *userInterfaceImpl) updateIconIfNeeded() error {
return nil return nil
} }
imgs := u.getIconImages() imgs := u.getAndResetIconImages()
if len(imgs) == 0 { // 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 return nil
} }
u.setIconImages(nil) var newImgs []image.Image
if len(imgs) > 0 {
newImgs := make([]image.Image, len(imgs)) newImgs = make([]image.Image, len(imgs))
}
for i, img := range imgs { for i, img := range imgs {
// TODO: If img is not *ebiten.Image, this converting is not necessary. // TODO: If img is not *ebiten.Image, this converting is not necessary.
// However, this package cannot refer *ebiten.Image due to the package // However, this package cannot refer *ebiten.Image due to the package