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
running uint32
origWindowPosX int
origWindowPosY int
runnableOnUnfocused bool
fpsMode FPSModeType
iconImages []image.Image
@ -117,6 +115,8 @@ type userInterfaceImpl struct {
// t is the main thread == the rendering thread.
t thread.Thread
m sync.RWMutex
native userInterfaceImplNative
}
const (
@ -132,8 +132,6 @@ func init() {
minWindowHeightInDIP: glfw.DontCare,
maxWindowWidthInDIP: glfw.DontCare,
maxWindowHeightInDIP: glfw.DontCare,
origWindowPosX: invalidPos,
origWindowPosY: invalidPos,
initCursorMode: CursorModeVisible,
initWindowDecorated: true,
initWindowPositionXInDIP: invalidPos,
@ -143,6 +141,7 @@ func init() {
initFocused: true,
fpsMode: FPSModeVsyncOn,
}
theUI.native.initialize()
theUI.input.ui = &theUI.userInterfaceImpl
theUI.iwindow.ui = &theUI.userInterfaceImpl
}
@ -1662,23 +1661,6 @@ func (u *userInterfaceImpl) setWindowTitle(title string) {
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.
// 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

View File

@ -287,6 +287,9 @@ func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
// clearVideoModeScaleCache must be called from the main thread.
func clearVideoModeScaleCache() {}
type userInterfaceImplNative struct {
}
// dipFromGLFWMonitorPixel must be called from the main thread.
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 {
return x
@ -391,23 +394,25 @@ func initializeWindowAfterCreation(w *glfw.Window) {
C.initializeWindow(C.uintptr_t(w.GetCocoaWindow()))
}
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) {
func (u *userInterfaceImpl) origWindowPos() (int, int) {
if !u.isNativeFullscreen() {
return invalidPos, invalidPos, true
return invalidPos, invalidPos
}
var cx, cy C.int
C.windowOriginalPosition(C.uintptr_t(u.window.GetCocoaWindow()), &cx, &cy)
x := int(cx)
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() {
return true
return
}
cx := C.int(x)
cy := C.int(flipY(y + u.windowHeightInDIP))
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
}
type userInterfaceImplNative struct {
origWindowPosX int
origWindowPosY int
}
// dipFromGLFWMonitorPixel must be called from the main thread.
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 {
return x / (videoModeScale(monitor) * u.deviceScaleFactor(monitor))
@ -231,10 +236,16 @@ func initializeWindowAfterCreation(w *glfw.Window) {
// For more details, see the discussion in #1829.
}
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) {
return 0, 0, false
func (u *userInterfaceImpl) origWindowPos() (int, int) {
return u.native.origWindowPosX, u.native.origWindowPosY
}
func (u *userInterfaceImpl) setOrigWindowPosByOS(x, y int) bool {
return false
func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
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
}
type userInterfaceImplNative struct {
origWindowPosX int
origWindowPosY int
}
// clearVideoModeScaleCache must be called from the main thread.
func clearVideoModeScaleCache() {}
@ -262,10 +267,16 @@ func (u *userInterfaceImpl) setWindowResizingModeForOS(mode WindowResizingMode)
func initializeWindowAfterCreation(w *glfw.Window) {
}
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) {
return 0, 0, false
func (u *userInterfaceImpl) origWindowPos() (int, int) {
return u.native.origWindowPosX, u.native.origWindowPosY
}
func (u *userInterfaceImpl) setOrigWindowPosByOS(x, y int) bool {
return false
func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
u.native.origWindowPosX = x
u.native.origWindowPosY = y
}
func (u *userInterfaceImplNative) initialize() {
u.origWindowPosX = invalidPos
u.origWindowPosY = invalidPos
}