internal/ui: replace deprecated APIs

[UIScreen mainScreen] is now deprecated.
This commit is contained in:
Hajime Hoshi 2024-09-16 18:20:01 +09:00
parent a113687d56
commit 4a10702f6c
3 changed files with 33 additions and 7 deletions

View File

@ -119,7 +119,7 @@ func (*graphicsDriverCreatorImpl) newPlayStation5() (graphicsdriver.Graphics, er
return nil, errors.New("ui: PlayStation 5 is not supported in this environment")
}
func deviceScaleFactorImpl() float64 {
func (u *UserInterface) deviceScaleFactor() float64 {
var s float64
if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
// TODO: This might be crash when this is called from init(). How can we detect this?

View File

@ -19,8 +19,31 @@ package ui
//
// #import <UIKit/UIKit.h>
//
// static double devicePixelRatio() {
// return [[UIScreen mainScreen] nativeScale];
// static double devicePixelRatioOnMainThread(UIView* view) {
// UIWindow* window = view.window;
// if (!window) {
// return 1;
// }
// UIWindowScene* scene = window.windowScene;
// if (!scene) {
// return 1;
// }
// return scene.screen.nativeScale;
// }
//
// static double devicePixelRatio(uintptr_t viewPtr) {
// if (!viewPtr) {
// return 1;
// }
// UIView* view = (__bridge UIView*)(void*)viewPtr;
// if ([NSThread isMainThread]) {
// return devicePixelRatioOnMainThread(view);
// }
// __block double scale;
// dispatch_sync(dispatch_get_main_queue(), ^{
// scale = devicePixelRatioOnMainThread(view);
// });
// return scale;
// }
import "C"
@ -66,6 +89,7 @@ func (*graphicsDriverCreatorImpl) newPlayStation5() (graphicsdriver.Graphics, er
}
func (u *UserInterface) SetUIView(uiview uintptr) error {
u.uiView.Store(uiview)
select {
case err := <-u.errCh:
return err
@ -89,9 +113,8 @@ func (u *UserInterface) IsGL() (bool, error) {
return u.GraphicsLibrary() == GraphicsLibraryOpenGL, nil
}
func deviceScaleFactorImpl() float64 {
// TODO: Can this be called from non-main threads?
return float64(C.devicePixelRatio())
func (u *UserInterface) deviceScaleFactor() float64 {
return float64(C.devicePixelRatio(C.uintptr_t(u.uiView.Load())))
}
func dipToNativePixels(x float64, scale float64) float64 {

View File

@ -104,6 +104,9 @@ type userInterfaceImpl struct {
strictContextRestoration atomic.Bool
strictContextRestorationOnce sync.Once
// uiView is used only on iOS.
uiView atomic.Uintptr
m sync.RWMutex
}
@ -285,7 +288,7 @@ func (m *Monitor) DeviceScaleFactor() float64 {
// Initialize this lazily.
m.deviceScaleFactorOnce.Do(func() {
// Assume that the device scale factor never changes on mobiles.
m.deviceScaleFactor = deviceScaleFactorImpl()
m.deviceScaleFactor = theUI.deviceScaleFactor()
})
return m.deviceScaleFactor
}