From 4a10702f6c39279c2d4613f12622046399a3eecd Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 16 Sep 2024 18:20:01 +0900 Subject: [PATCH] internal/ui: replace deprecated APIs [UIScreen mainScreen] is now deprecated. --- internal/ui/ui_android.go | 2 +- internal/ui/ui_ios.go | 33 ++++++++++++++++++++++++++++----- internal/ui/ui_mobile.go | 5 ++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/internal/ui/ui_android.go b/internal/ui/ui_android.go index d688d85fc..ab71b61dd 100644 --- a/internal/ui/ui_android.go +++ b/internal/ui/ui_android.go @@ -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? diff --git a/internal/ui/ui_ios.go b/internal/ui/ui_ios.go index 190140df7..64760256e 100644 --- a/internal/ui/ui_ios.go +++ b/internal/ui/ui_ios.go @@ -19,8 +19,31 @@ package ui // // #import // -// 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 { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index d755798e3..fd6a86038 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -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 }