internal/ui: refactoring: add userInterfaceImplNative for each OS

This commit is contained in:
Hajime Hoshi 2022-08-10 11:43:58 +09:00
parent d0fa68691c
commit a57560105f
4 changed files with 44 additions and 35 deletions

View File

@ -65,8 +65,6 @@ type userInterfaceImpl struct {
maxWindowHeightInDIP int maxWindowHeightInDIP int
running uint32 running uint32
origWindowPosX int
origWindowPosY int
runnableOnUnfocused bool runnableOnUnfocused bool
fpsMode FPSModeType fpsMode FPSModeType
iconImages []image.Image iconImages []image.Image
@ -117,6 +115,8 @@ type userInterfaceImpl struct {
// t is the main thread == the rendering thread. // t is the main thread == the rendering thread.
t thread.Thread t thread.Thread
m sync.RWMutex m sync.RWMutex
native userInterfaceImplNative
} }
const ( const (
@ -132,8 +132,6 @@ func init() {
minWindowHeightInDIP: glfw.DontCare, minWindowHeightInDIP: glfw.DontCare,
maxWindowWidthInDIP: glfw.DontCare, maxWindowWidthInDIP: glfw.DontCare,
maxWindowHeightInDIP: glfw.DontCare, maxWindowHeightInDIP: glfw.DontCare,
origWindowPosX: invalidPos,
origWindowPosY: invalidPos,
initCursorMode: CursorModeVisible, initCursorMode: CursorModeVisible,
initWindowDecorated: true, initWindowDecorated: true,
initWindowPositionXInDIP: invalidPos, initWindowPositionXInDIP: invalidPos,
@ -143,6 +141,7 @@ func init() {
initFocused: true, initFocused: true,
fpsMode: FPSModeVsyncOn, fpsMode: FPSModeVsyncOn,
} }
theUI.native.initialize()
theUI.input.ui = &theUI.userInterfaceImpl theUI.input.ui = &theUI.userInterfaceImpl
theUI.iwindow.ui = &theUI.userInterfaceImpl theUI.iwindow.ui = &theUI.userInterfaceImpl
} }
@ -1662,23 +1661,6 @@ func (u *userInterfaceImpl) setWindowTitle(title string) {
u.window.SetTitle(title) u.window.SetTitle(title)
} }
func (u *userInterfaceImpl) origWindowPos() (int, int) {
if x, y, ok := u.origWindowPosByOS(); ok {
return x, y
}
return u.origWindowPosX, u.origWindowPosY
}
func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
// TODO: The original position should be updated at a 'PosCallback'.
if u.setOrigWindowPosByOS(x, y) {
return
}
u.origWindowPosX = x
u.origWindowPosY = y
}
// forceToRefreshIfNeeded forces to refresh the framebuffer by resizing the window quickly. // forceToRefreshIfNeeded forces to refresh the framebuffer by resizing the window quickly.
// This is a very dirty but necessary hack for DirectX (#2050). // This is a very dirty but necessary hack for DirectX (#2050).
// With DirectX, the framebuffer is not rendered correctly when the window is resized by dragging // With DirectX, the framebuffer is not rendered correctly when the window is resized by dragging

View File

@ -287,6 +287,9 @@ func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
// clearVideoModeScaleCache must be called from the main thread. // clearVideoModeScaleCache must be called from the main thread.
func clearVideoModeScaleCache() {} func clearVideoModeScaleCache() {}
type userInterfaceImplNative struct {
}
// dipFromGLFWMonitorPixel must be called from the main thread. // dipFromGLFWMonitorPixel must be called from the main thread.
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 { func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 {
return x return x
@ -391,23 +394,25 @@ func initializeWindowAfterCreation(w *glfw.Window) {
C.initializeWindow(C.uintptr_t(w.GetCocoaWindow())) C.initializeWindow(C.uintptr_t(w.GetCocoaWindow()))
} }
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) { func (u *userInterfaceImpl) origWindowPos() (int, int) {
if !u.isNativeFullscreen() { if !u.isNativeFullscreen() {
return invalidPos, invalidPos, true return invalidPos, invalidPos
} }
var cx, cy C.int var cx, cy C.int
C.windowOriginalPosition(C.uintptr_t(u.window.GetCocoaWindow()), &cx, &cy) C.windowOriginalPosition(C.uintptr_t(u.window.GetCocoaWindow()), &cx, &cy)
x := int(cx) x := int(cx)
y := flipY(int(cy)) - u.windowHeightInDIP y := flipY(int(cy)) - u.windowHeightInDIP
return x, y, true return x, y
} }
func (u *userInterfaceImpl) setOrigWindowPosByOS(x, y int) bool { func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
if !u.isNativeFullscreen() { if !u.isNativeFullscreen() {
return true return
} }
cx := C.int(x) cx := C.int(x)
cy := C.int(flipY(y + u.windowHeightInDIP)) cy := C.int(flipY(y + u.windowHeightInDIP))
C.setWindowOriginalPosition(C.uintptr_t(u.window.GetCocoaWindow()), cx, cy) C.setWindowOriginalPosition(C.uintptr_t(u.window.GetCocoaWindow()), cx, cy)
return true }
func (u *userInterfaceImplNative) initialize() {
} }

View File

@ -142,6 +142,11 @@ func videoModeScaleUncached(m *glfw.Monitor) float64 {
return 1 return 1
} }
type userInterfaceImplNative struct {
origWindowPosX int
origWindowPosY int
}
// dipFromGLFWMonitorPixel must be called from the main thread. // dipFromGLFWMonitorPixel must be called from the main thread.
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 { func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 {
return x / (videoModeScale(monitor) * u.deviceScaleFactor(monitor)) return x / (videoModeScale(monitor) * u.deviceScaleFactor(monitor))
@ -231,10 +236,16 @@ func initializeWindowAfterCreation(w *glfw.Window) {
// For more details, see the discussion in #1829. // For more details, see the discussion in #1829.
} }
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) { func (u *userInterfaceImpl) origWindowPos() (int, int) {
return 0, 0, false return u.native.origWindowPosX, u.native.origWindowPosY
} }
func (u *userInterfaceImpl) setOrigWindowPosByOS(x, y int) bool { func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
return false u.native.origWindowPosX = x
u.native.origWindowPosY = y
}
func (u *userInterfaceImplNative) initialize() {
u.native.origWindowPosX = invalidPos
u.native.origWindowPosY = invalidPos
} }

View File

@ -137,6 +137,11 @@ func _GetCursorPos() (int32, int32, error) {
return pt.x, pt.y, nil return pt.x, pt.y, nil
} }
type userInterfaceImplNative struct {
origWindowPosX int
origWindowPosY int
}
// clearVideoModeScaleCache must be called from the main thread. // clearVideoModeScaleCache must be called from the main thread.
func clearVideoModeScaleCache() {} func clearVideoModeScaleCache() {}
@ -262,10 +267,16 @@ func (u *userInterfaceImpl) setWindowResizingModeForOS(mode WindowResizingMode)
func initializeWindowAfterCreation(w *glfw.Window) { func initializeWindowAfterCreation(w *glfw.Window) {
} }
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) { func (u *userInterfaceImpl) origWindowPos() (int, int) {
return 0, 0, false return u.native.origWindowPosX, u.native.origWindowPosY
} }
func (u *userInterfaceImpl) setOrigWindowPosByOS(x, y int) bool { func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
return false u.native.origWindowPosX = x
u.native.origWindowPosY = y
}
func (u *userInterfaceImplNative) initialize() {
u.origWindowPosX = invalidPos
u.origWindowPosY = invalidPos
} }