2016-06-18 22:04:38 +02:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-10-01 16:24:42 +02:00
|
|
|
//go:build !ios
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
import (
|
2023-10-15 16:27:04 +02:00
|
|
|
"errors"
|
2022-06-16 19:10:29 +02:00
|
|
|
"fmt"
|
2023-09-10 19:05:42 +02:00
|
|
|
"reflect"
|
2022-09-16 04:53:46 +02:00
|
|
|
"unsafe"
|
2022-06-16 19:10:29 +02:00
|
|
|
|
2022-09-16 04:53:46 +02:00
|
|
|
"github.com/ebitengine/purego/objc"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/cocoa"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2022-03-22 15:33:21 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
2018-10-06 15:42:28 +02:00
|
|
|
)
|
|
|
|
|
2023-09-10 19:05:42 +02:00
|
|
|
var class_EbitengineWindowDelegate objc.Class
|
2022-11-07 06:31:39 +01:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) initializePlatform() error {
|
2023-09-10 19:05:42 +02:00
|
|
|
pushResizableState := func(id, win objc.ID) {
|
2022-11-07 06:31:39 +01:00
|
|
|
window := cocoa.NSWindow{ID: win}
|
2023-09-10 19:05:42 +02:00
|
|
|
id.Send(sel_setOrigResizable, window.StyleMask()&cocoa.NSWindowStyleMaskResizable != 0)
|
|
|
|
if !objc.Send[bool](id, sel_origResizable) {
|
|
|
|
window.SetStyleMask(window.StyleMask() | cocoa.NSWindowStyleMaskResizable)
|
|
|
|
}
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2023-09-10 19:05:42 +02:00
|
|
|
popResizableState := func(id, win objc.ID) {
|
|
|
|
if !objc.Send[bool](id, sel_origResizable) {
|
|
|
|
window := cocoa.NSWindow{ID: win}
|
|
|
|
window.SetStyleMask(window.StyleMask() & ^uint(cocoa.NSWindowStyleMaskResizable))
|
|
|
|
}
|
|
|
|
id.Send(sel_setOrigResizable, false)
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
d, err := objc.RegisterClass(
|
2023-09-10 19:05:42 +02:00
|
|
|
"EbitengineWindowDelegate",
|
|
|
|
objc.GetClass("NSObject"),
|
|
|
|
[]*objc.Protocol{objc.GetProtocol("NSWindowDelegate")},
|
|
|
|
[]objc.FieldDef{
|
|
|
|
{
|
|
|
|
Name: "origDelegate",
|
|
|
|
Type: reflect.TypeOf(objc.ID(0)),
|
|
|
|
Attribute: objc.ReadWrite,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "origResizable",
|
|
|
|
Type: reflect.TypeOf(true),
|
|
|
|
Attribute: objc.ReadWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]objc.MethodDef{
|
|
|
|
{
|
|
|
|
Cmd: sel_initWithOrigDelegate,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, origDelegate objc.ID) objc.ID {
|
|
|
|
self := id.SendSuper(sel_init)
|
|
|
|
if self != 0 {
|
|
|
|
id.Send(sel_setOrigDelegate, origDelegate)
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// The method set of origDelegate must sync with GLFWWindowDelegate's implementation.
|
|
|
|
// See cocoa_window.m in GLFW.
|
|
|
|
{
|
|
|
|
Cmd: sel_windowShouldClose,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) bool {
|
|
|
|
return id.Send(sel_origDelegate).Send(cmd, notification) != 0
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidResize,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
id.Send(sel_origDelegate).Send(cmd, notification)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidMove,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
id.Send(sel_origDelegate).Send(cmd, notification)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidMiniaturize,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
id.Send(sel_origDelegate).Send(cmd, notification)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidBecomeKey,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
id.Send(sel_origDelegate).Send(cmd, notification)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidResignKey,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
id.Send(sel_origDelegate).Send(cmd, notification)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidChangeOcclusionState,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
id.Send(sel_origDelegate).Send(cmd, notification)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowWillEnterFullScreen,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := u.setOrigWindowPosWithCurrentPos(); err != nil {
|
2023-10-15 08:50:39 +02:00
|
|
|
u.setError(err)
|
2023-10-03 16:07:53 +02:00
|
|
|
return
|
|
|
|
}
|
2023-09-10 19:05:42 +02:00
|
|
|
pushResizableState(id, cocoa.NSNotification{ID: notification}.Object())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidEnterFullScreen,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
popResizableState(id, cocoa.NSNotification{ID: notification}.Object())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowWillExitFullScreen,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
pushResizableState(id, cocoa.NSNotification{ID: notification}.Object())
|
2023-09-16 09:49:49 +02:00
|
|
|
// Even a window has a size limitation, a window can be fullscreen by calling SetFullscreen(true).
|
|
|
|
// In this case, the window size limitation is disabled temporarily.
|
|
|
|
// When exiting from fullscreen, reset the window size limitation.
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := u.updateWindowSizeLimits(); err != nil {
|
2023-10-15 08:50:39 +02:00
|
|
|
u.setError(err)
|
2023-10-03 16:07:53 +02:00
|
|
|
return
|
|
|
|
}
|
2023-09-10 19:05:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Cmd: sel_windowDidExitFullScreen,
|
|
|
|
Fn: func(id objc.ID, cmd objc.SEL, notification objc.ID) {
|
|
|
|
popResizableState(id, cocoa.NSNotification{ID: notification}.Object())
|
|
|
|
// Do not call setFrame here (#2295). setFrame here causes unexpected results.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-11-07 06:31:39 +01:00
|
|
|
if err != nil {
|
2023-10-14 17:06:07 +02:00
|
|
|
return err
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
class_EbitengineWindowDelegate = d
|
|
|
|
|
|
|
|
return nil
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
type graphicsDriverCreatorImpl struct {
|
2022-02-11 12:38:45 +01:00
|
|
|
transparent bool
|
|
|
|
}
|
2022-03-22 15:33:21 +01:00
|
|
|
|
2022-07-30 19:56:16 +02:00
|
|
|
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) {
|
2022-06-16 19:10:29 +02:00
|
|
|
m, err1 := g.newMetal()
|
|
|
|
if err1 == nil {
|
2022-07-30 19:56:16 +02:00
|
|
|
return m, GraphicsLibraryMetal, nil
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
2022-06-16 19:10:29 +02:00
|
|
|
o, err2 := g.newOpenGL()
|
|
|
|
if err2 == nil {
|
2022-07-30 19:56:16 +02:00
|
|
|
return o, GraphicsLibraryOpenGL, nil
|
2022-06-16 19:10:29 +02:00
|
|
|
}
|
2022-07-30 19:56:16 +02:00
|
|
|
return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2)
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
2022-11-13 07:25:22 +01:00
|
|
|
return opengl.NewGraphics()
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 04:39:43 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
2023-10-15 16:27:04 +02:00
|
|
|
return nil, errors.New("ui: DirectX is not supported in this environment")
|
2022-03-25 11:43:32 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
|
|
|
return metal.NewGraphics()
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2023-10-01 16:14:47 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newPlayStation5() (graphicsdriver.Graphics, error) {
|
|
|
|
return nil, errors.New("ui: PlayStation 5 is not supported in this environment")
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:20:59 +02:00
|
|
|
// glfwMonitorSizeInGLFWPixels must be called from the main thread.
|
2023-10-03 16:07:53 +02:00
|
|
|
func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) {
|
|
|
|
vm, err := m.GetVideoMode()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return vm.Width, vm.Height, nil
|
2020-09-18 17:21:08 +02:00
|
|
|
}
|
|
|
|
|
2024-03-03 15:27:02 +01:00
|
|
|
func dipFromGLFWPixel(x float64, scale float64) float64 {
|
2021-09-14 18:03:04 +02:00
|
|
|
// NOTE: On macOS, GLFW exposes the device independent coordinate system.
|
|
|
|
// Thus, the conversion functions are unnecessary,
|
|
|
|
// however we still need the deviceScaleFactor internally
|
|
|
|
// so we can create and maintain a HiDPI frame buffer.
|
2020-09-18 17:31:34 +02:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2024-03-03 15:27:02 +01:00
|
|
|
func dipToGLFWPixel(x float64, scale float64) float64 {
|
2020-09-18 17:21:08 +02:00
|
|
|
return x
|
2016-06-18 22:04:38 +02:00
|
|
|
}
|
2017-04-18 17:51:15 +02:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) {
|
2017-04-18 17:51:15 +02:00
|
|
|
return x, y
|
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
|
2023-01-08 17:39:32 +01:00
|
|
|
var (
|
|
|
|
class_NSCursor = objc.GetClass("NSCursor")
|
|
|
|
class_NSEvent = objc.GetClass("NSEvent")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sel_alloc = objc.RegisterName("alloc")
|
|
|
|
sel_collectionBehavior = objc.RegisterName("collectionBehavior")
|
|
|
|
sel_delegate = objc.RegisterName("delegate")
|
|
|
|
sel_init = objc.RegisterName("init")
|
|
|
|
sel_initWithOrigDelegate = objc.RegisterName("initWithOrigDelegate:")
|
|
|
|
sel_mouseLocation = objc.RegisterName("mouseLocation")
|
2023-09-10 19:05:42 +02:00
|
|
|
sel_origDelegate = objc.RegisterName("origDelegate")
|
|
|
|
sel_origResizable = objc.RegisterName("isOrigResizable")
|
2023-01-08 17:39:32 +01:00
|
|
|
sel_setCollectionBehavior = objc.RegisterName("setCollectionBehavior:")
|
|
|
|
sel_setDelegate = objc.RegisterName("setDelegate:")
|
2024-07-31 16:54:16 +02:00
|
|
|
sel_setDocumentEdited = objc.RegisterName("setDocumentEdited:")
|
2023-09-10 19:15:42 +02:00
|
|
|
sel_setOrigDelegate = objc.RegisterName("setOrigDelegate:")
|
|
|
|
sel_setOrigResizable = objc.RegisterName("setOrigResizable:")
|
2023-01-08 17:39:32 +01:00
|
|
|
sel_toggleFullScreen = objc.RegisterName("toggleFullScreen:")
|
|
|
|
sel_windowDidBecomeKey = objc.RegisterName("windowDidBecomeKey:")
|
|
|
|
sel_windowDidEnterFullScreen = objc.RegisterName("windowDidEnterFullScreen:")
|
|
|
|
sel_windowDidExitFullScreen = objc.RegisterName("windowDidExitFullScreen:")
|
|
|
|
sel_windowDidMiniaturize = objc.RegisterName("windowDidMiniaturize:")
|
|
|
|
sel_windowDidMove = objc.RegisterName("windowDidMove:")
|
|
|
|
sel_windowDidResignKey = objc.RegisterName("windowDidResignKey:")
|
|
|
|
sel_windowDidResize = objc.RegisterName("windowDidResize:")
|
|
|
|
sel_windowDidChangeOcclusionState = objc.RegisterName("windowDidChangeOcclusionState:")
|
|
|
|
sel_windowShouldClose = objc.RegisterName("windowShouldClose:")
|
|
|
|
sel_windowWillEnterFullScreen = objc.RegisterName("windowWillEnterFullScreen:")
|
|
|
|
sel_windowWillExitFullScreen = objc.RegisterName("windowWillExitFullScreen:")
|
|
|
|
)
|
2022-09-16 04:53:46 +02:00
|
|
|
|
2023-09-29 09:48:20 +02:00
|
|
|
func currentMouseLocation() (x, y int) {
|
2022-09-16 04:53:46 +02:00
|
|
|
sig := cocoa.NSMethodSignature_signatureWithObjCTypes("{NSPoint=dd}@:")
|
|
|
|
inv := cocoa.NSInvocation_invocationWithMethodSignature(sig)
|
|
|
|
inv.SetTarget(objc.ID(class_NSEvent))
|
|
|
|
inv.SetSelector(sel_mouseLocation)
|
|
|
|
inv.Invoke()
|
|
|
|
var point cocoa.NSPoint
|
|
|
|
inv.GetReturnValue(unsafe.Pointer(&point))
|
2023-09-29 05:20:07 +02:00
|
|
|
|
|
|
|
x, y = int(point.X), int(point.Y)
|
|
|
|
|
|
|
|
// On macOS, the Y axis is upward. Adjust the Y position (#807, #2794).
|
|
|
|
y = -y
|
|
|
|
m := theMonitors.primaryMonitor()
|
|
|
|
y += m.videoMode.Height
|
|
|
|
return x, y
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-23 19:05:42 +02:00
|
|
|
func initialMonitorByOS() (*Monitor, error) {
|
2023-09-29 09:48:20 +02:00
|
|
|
x, y := currentMouseLocation()
|
2022-02-07 13:56:16 +01:00
|
|
|
|
|
|
|
// Find the monitor including the cursor.
|
2023-09-29 09:48:20 +02:00
|
|
|
return theMonitors.monitorFromPosition(x, y), nil
|
2021-10-09 09:49:47 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
func monitorFromWindowByOS(w *glfw.Window) (*Monitor, error) {
|
2023-10-07 11:05:03 +02:00
|
|
|
cocoaWindow, err := w.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
window := cocoa.NSWindow{ID: objc.ID(cocoaWindow)}
|
2022-09-16 04:53:46 +02:00
|
|
|
pool := cocoa.NSAutoreleasePool_new()
|
|
|
|
screen := cocoa.NSScreen_mainScreen()
|
2023-01-28 11:06:38 +01:00
|
|
|
if window.ID != 0 && window.IsVisible() {
|
2022-09-16 04:53:46 +02:00
|
|
|
// When the window is visible, the window is already initialized.
|
|
|
|
// [NSScreen mainScreen] sometimes tells a lie when the window is put across monitors (#703).
|
|
|
|
screen = window.Screen()
|
|
|
|
}
|
|
|
|
screenDictionary := screen.DeviceDescription()
|
|
|
|
screenID := cocoa.NSNumber{ID: screenDictionary.ObjectForKey(cocoa.NSString_alloc().InitWithUTF8String("NSScreenNumber").ID)}
|
|
|
|
aID := uintptr(screenID.UnsignedIntValue()) // CGDirectDisplayID
|
|
|
|
pool.Release()
|
2023-09-15 17:08:33 +02:00
|
|
|
for _, m := range theMonitors.append(nil) {
|
2023-10-07 11:05:03 +02:00
|
|
|
cocoaMonitor, err := m.m.GetCocoaMonitor()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cocoaMonitor == aID {
|
2023-10-03 16:07:53 +02:00
|
|
|
return m, nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 16:07:53 +02:00
|
|
|
return nil, nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) nativeWindow() (uintptr, error) {
|
2023-10-07 11:05:03 +02:00
|
|
|
return u.window.GetCocoaWindow()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-04-18 10:35:46 +02:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) isNativeFullscreen() (bool, error) {
|
2023-10-07 11:05:03 +02:00
|
|
|
w, err := u.window.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return cocoa.NSWindow{ID: objc.ID(w)}.StyleMask()&cocoa.NSWindowStyleMaskFullScreen != 0, nil
|
2021-04-18 10:35:46 +02:00
|
|
|
}
|
2021-05-02 07:50:50 +02:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) isNativeFullscreenAvailable() bool {
|
2021-10-31 10:21:00 +01:00
|
|
|
// TODO: If the window is transparent, we should use GLFW's windowed fullscreen (#1822, #1857).
|
|
|
|
// However, if the user clicks the green button, should this window be in native fullscreen mode?
|
|
|
|
return true
|
2021-09-17 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) setNativeFullscreen(fullscreen bool) error {
|
2021-09-17 19:21:24 +02:00
|
|
|
// Toggling fullscreen might ignore events like keyUp. Ensure that events are fired.
|
2023-10-07 11:47:41 +02:00
|
|
|
if err := glfw.WaitEventsTimeout(0.1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-07 11:05:03 +02:00
|
|
|
w, err := u.window.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
window := cocoa.NSWindow{ID: objc.ID(w)}
|
2022-09-16 04:53:46 +02:00
|
|
|
if window.StyleMask()&cocoa.NSWindowStyleMaskFullScreen != 0 == fullscreen {
|
2023-10-07 11:05:03 +02:00
|
|
|
return nil
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
// Even though EbitengineWindowDelegate is used, this hack is still required.
|
|
|
|
// toggleFullscreen doesn't work when the window is not resizable.
|
2022-11-05 06:06:59 +01:00
|
|
|
origCollectionBehavior := window.Send(sel_collectionBehavior)
|
2022-11-05 06:20:48 +01:00
|
|
|
origFullScreen := origCollectionBehavior&cocoa.NSWindowCollectionBehaviorFullScreenPrimary != 0
|
2022-09-16 04:53:46 +02:00
|
|
|
if !origFullScreen {
|
2022-11-05 06:06:59 +01:00
|
|
|
collectionBehavior := origCollectionBehavior
|
|
|
|
collectionBehavior |= cocoa.NSWindowCollectionBehaviorFullScreenPrimary
|
|
|
|
collectionBehavior &^= cocoa.NSWindowCollectionBehaviorFullScreenNone
|
|
|
|
window.Send(sel_setCollectionBehavior, cocoa.NSUInteger(collectionBehavior))
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2023-01-08 17:39:32 +01:00
|
|
|
window.Send(sel_toggleFullScreen, 0)
|
2022-09-16 04:53:46 +02:00
|
|
|
if !origFullScreen {
|
2022-11-05 06:06:59 +01:00
|
|
|
window.Send(sel_setCollectionBehavior, cocoa.NSUInteger(cocoa.NSUInteger(origCollectionBehavior)))
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2023-10-07 11:05:03 +02:00
|
|
|
|
|
|
|
return nil
|
2021-09-17 19:21:24 +02:00
|
|
|
}
|
2021-09-18 15:11:26 +02:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) adjustViewSizeAfterFullscreen() error {
|
2023-10-15 10:21:56 +02:00
|
|
|
if u.GraphicsLibrary() == GraphicsLibraryOpenGL {
|
2023-10-07 11:05:03 +02:00
|
|
|
return nil
|
2021-09-18 16:59:37 +02:00
|
|
|
}
|
2022-09-25 15:51:09 +02:00
|
|
|
|
2023-10-07 11:05:03 +02:00
|
|
|
w, err := u.window.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
window := cocoa.NSWindow{ID: objc.ID(w)}
|
2022-09-16 04:53:46 +02:00
|
|
|
if window.StyleMask()&cocoa.NSWindowStyleMaskFullScreen == 0 {
|
2023-10-07 11:05:03 +02:00
|
|
|
return nil
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2022-09-25 15:51:09 +02:00
|
|
|
|
2022-09-16 04:53:46 +02:00
|
|
|
// Reduce the view height (#1745).
|
|
|
|
// https://stackoverflow.com/questions/27758027/sprite-kit-serious-fps-issue-in-full-screen-mode-on-os-x
|
|
|
|
windowSize := window.Frame().Size
|
|
|
|
view := window.ContentView()
|
|
|
|
viewSize := view.Frame().Size
|
|
|
|
if windowSize.Width != viewSize.Width || windowSize.Height != viewSize.Height {
|
2023-10-07 11:05:03 +02:00
|
|
|
return nil
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
viewSize.Width--
|
|
|
|
view.SetFrameSize(viewSize)
|
2022-09-25 15:51:09 +02:00
|
|
|
|
2022-09-16 04:53:46 +02:00
|
|
|
// NSColor.blackColor (0, 0, 0, 1) didn't work.
|
|
|
|
// Use the transparent color instead.
|
|
|
|
window.SetBackgroundColor(cocoa.NSColor_colorWithSRGBRedGreenBlueAlpha(0, 0, 0, 0))
|
2023-10-07 11:05:03 +02:00
|
|
|
return nil
|
2021-09-18 15:11:26 +02:00
|
|
|
}
|
2021-09-24 19:46:18 +02:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) isFullscreenAllowedFromUI(mode WindowResizingMode) bool {
|
2023-09-16 09:30:02 +02:00
|
|
|
if u.maxWindowWidthInDIP != glfw.DontCare || u.maxWindowHeightInDIP != glfw.DontCare {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if mode == WindowResizingModeOnlyFullscreenEnabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if mode == WindowResizingModeEnabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) setWindowResizingModeForOS(mode WindowResizingMode) error {
|
2022-11-05 06:06:59 +01:00
|
|
|
var collectionBehavior uint
|
2023-09-16 09:30:02 +02:00
|
|
|
if u.isFullscreenAllowedFromUI(mode) {
|
2022-11-05 06:06:59 +01:00
|
|
|
collectionBehavior |= cocoa.NSWindowCollectionBehaviorManaged
|
2022-09-16 04:53:46 +02:00
|
|
|
collectionBehavior |= cocoa.NSWindowCollectionBehaviorFullScreenPrimary
|
|
|
|
} else {
|
2022-11-05 06:06:59 +01:00
|
|
|
collectionBehavior |= cocoa.NSWindowCollectionBehaviorFullScreenNone
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
2023-10-07 11:05:03 +02:00
|
|
|
w, err := u.window.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objc.ID(w).Send(sel_setCollectionBehavior, collectionBehavior)
|
|
|
|
return nil
|
2021-12-29 14:08:59 +01:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
func initializeWindowAfterCreation(w *glfw.Window) error {
|
2021-12-29 14:08:59 +01:00
|
|
|
// TODO: Register NSWindowWillEnterFullScreenNotification and so on.
|
|
|
|
// Enable resizing temporary before making the window fullscreen.
|
2023-10-07 11:05:03 +02:00
|
|
|
cocoaWindow, err := w.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nswindow := objc.ID(cocoaWindow)
|
2023-01-08 17:39:32 +01:00
|
|
|
delegate := objc.ID(class_EbitengineWindowDelegate).Send(sel_alloc).Send(sel_initWithOrigDelegate, nswindow.Send(sel_delegate))
|
|
|
|
nswindow.Send(sel_setDelegate, delegate)
|
2023-10-03 16:07:53 +02:00
|
|
|
return nil
|
2021-09-24 19:46:18 +02:00
|
|
|
}
|
2022-12-12 17:03:19 +01:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) skipTaskbar() error {
|
2022-12-12 17:03:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-31 16:54:16 +02:00
|
|
|
|
|
|
|
// setDocumentEdited must be called from the main thread.
|
|
|
|
func (u *UserInterface) setDocumentEdited(edited bool) error {
|
|
|
|
w, err := u.window.GetCocoaWindow()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objc.ID(w).Send(sel_setDocumentEdited, edited)
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-26 16:26:58 +02:00
|
|
|
|
|
|
|
func (u *UserInterface) afterWindowCreation() error {
|
|
|
|
return nil
|
|
|
|
}
|