Skip to content

Commit

Permalink
internal/ui: replace deprecated APIs
Browse files Browse the repository at this point in the history
[UIScreen mainScreen] is now deprecated.
  • Loading branch information
hajimehoshi committed Sep 16, 2024
1 parent a113687 commit 4a10702
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/ui/ui_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
33 changes: 28 additions & 5 deletions internal/ui/ui_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion internal/ui/ui_mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4a10702

Please sign in to comment.