internal/ui: refactoring: move device-scale implementation to internal/ui for browsers

The returned value from internal/devicescale.At never changes for
browsers, so the detection of devicePixelRatio updates didn't work
in the first place. Also, there is not a good way to detect the
change [1].

This change moves the logic from internal/devicescale to internal/ui.
We aim to merge these packages as a device scale factor belongs to
a monitor and internal/ui manages monitors.

[1] https://crbug.com/123694
This commit is contained in:
Hajime Hoshi 2023-09-24 03:03:07 +09:00
parent 5de9c5da61
commit 506a1de259
2 changed files with 19 additions and 38 deletions

View File

@ -1,31 +0,0 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package devicescale
import (
"syscall/js"
)
func impl(x, y int) float64 {
window := js.Global().Get("window")
if !window.Truthy() {
return 1
}
ratio := window.Get("devicePixelRatio").Float()
if ratio == 0 {
ratio = 1
}
return ratio
}

View File

@ -21,7 +21,6 @@ import (
"syscall/js"
"time"
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/file"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
@ -95,7 +94,7 @@ type userInterfaceImpl struct {
onceUpdateCalled bool
lastCaptureExitTime time.Time
lastDeviceScaleFactor float64
deviceScaleFactor float64
err error
@ -280,7 +279,16 @@ func (u *userInterfaceImpl) SetCursorShape(shape CursorShape) {
}
func (u *userInterfaceImpl) DeviceScaleFactor() float64 {
return devicescale.GetAt(0, 0)
if u.deviceScaleFactor != 0 {
return u.deviceScaleFactor
}
ratio := window.Get("devicePixelRatio").Float()
if ratio == 0 {
ratio = 1
}
u.deviceScaleFactor = ratio
return u.deviceScaleFactor
}
func (u *userInterfaceImpl) outsideSize() (float64, float64) {
@ -354,11 +362,13 @@ func (u *userInterfaceImpl) updateImpl(force bool) error {
return err
}
a := u.DeviceScaleFactor()
if u.lastDeviceScaleFactor != a {
if !u.onceUpdateCalled {
u.updateScreenSize()
}
u.lastDeviceScaleFactor = a
// TODO: If DeviceScaleFactor changes, call updateScreenSize.
// Now there is not a good way to detect the change.
// See also https://crbug.com/123694.
w, h := u.outsideSize()
if force {
@ -401,7 +411,9 @@ func (u *userInterfaceImpl) loop(game Game) <-chan error {
return
}
if u.needsUpdate() {
u.onceUpdateCalled = true
defer func() {
u.onceUpdateCalled = true
}()
u.renderingScheduled = false
if err := u.update(); err != nil {
close(reqStopAudioCh)