From c0d9954b3e855264c3d4a66e41025dcc72934305 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Mar 2024 23:31:14 +0900 Subject: [PATCH] exp/textinput: use native pixels for a candidate window position --- exp/textinput/textinput.go | 2 +- internal/ui/context.go | 8 ++++++-- internal/ui/ui_android.go | 4 ++++ internal/ui/ui_glfw.go | 4 ++++ internal/ui/ui_ios.go | 4 ++++ internal/ui/ui_js.go | 4 ++++ internal/ui/ui_nintendosdk.go | 4 ++++ internal/ui/ui_playstation5.go | 4 ++++ 8 files changed, 31 insertions(+), 3 deletions(-) diff --git a/exp/textinput/textinput.go b/exp/textinput/textinput.go index 70b88ac31..fde6a1a4c 100644 --- a/exp/textinput/textinput.go +++ b/exp/textinput/textinput.go @@ -47,7 +47,7 @@ type State struct { // // Start returns nil and nil if the current environment doesn't support this package. func Start(x, y int) (states chan State, close func()) { - cx, cy := ui.Get().LogicalPositionToClientPosition(float64(x), float64(y)) + cx, cy := ui.Get().LogicalPositionToClientPositionInNativePixels(float64(x), float64(y)) return theTextInput.Start(int(cx), int(cy)) } diff --git a/internal/ui/context.go b/internal/ui/context.go index d045e3b5e..ec7f3774f 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -285,6 +285,10 @@ func (c *context) screenScaleAndOffsets() (scale, offsetX, offsetY float64) { return } -func (u *UserInterface) LogicalPositionToClientPosition(x, y float64) (float64, float64) { - return u.context.logicalPositionToClientPosition(x, y, u.Monitor().DeviceScaleFactor()) +func (u *UserInterface) LogicalPositionToClientPositionInNativePixels(x, y float64) (float64, float64) { + s := u.Monitor().DeviceScaleFactor() + x, y = u.context.logicalPositionToClientPosition(x, y, s) + x = dipToNativePixels(x, s) + y = dipToNativePixels(y, s) + return x, y } diff --git a/internal/ui/ui_android.go b/internal/ui/ui_android.go index ae4c0ab63..3e9326f48 100644 --- a/internal/ui/ui_android.go +++ b/internal/ui/ui_android.go @@ -129,3 +129,7 @@ func deviceScaleFactorImpl() float64 { } return s } + +func dipToNativePixels(x float64, scale float64) float64 { + return x * scale +} diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 1221b4c16..e4e0d96da 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -2131,3 +2131,7 @@ func IsScreenTransparentAvailable() bool { func (u *UserInterface) RunOnMainThread(f func()) { u.mainThread.Call(f) } + +func dipToNativePixels(x float64, scale float64) float64 { + return dipToGLFWPixel(x, scale) +} diff --git a/internal/ui/ui_ios.go b/internal/ui/ui_ios.go index be8a149e4..9da63fecb 100644 --- a/internal/ui/ui_ios.go +++ b/internal/ui/ui_ios.go @@ -92,3 +92,7 @@ func deviceScaleFactorImpl() float64 { // TODO: Can this be called from non-main threads? return float64(C.devicePixelRatio()) } + +func dipToNativePixels(x float64, scale float64) float64 { + return x +} diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index fc16e05db..505cb1ec4 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -811,3 +811,7 @@ func (u *UserInterface) updateIconIfNeeded() error { func IsScreenTransparentAvailable() bool { return true } + +func dipToNativePixels(x float64, scale float64) float64 { + return x +} diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index 914703dd9..52cef975d 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -181,3 +181,7 @@ func (u *UserInterface) Monitor() *Monitor { func IsScreenTransparentAvailable() bool { return false } + +func dipToNativePixels(x float64, scale float64) float64 { + return x +} diff --git a/internal/ui/ui_playstation5.go b/internal/ui/ui_playstation5.go index faadc296e..a367cc91c 100644 --- a/internal/ui/ui_playstation5.go +++ b/internal/ui/ui_playstation5.go @@ -174,3 +174,7 @@ func (u *UserInterface) Monitor() *Monitor { func IsScreenTransparentAvailable() bool { return false } + +func dipToNativePixels(x float64, scale float64) float64 { + return x +}