2022-08-17 18:39:34 +02:00
|
|
|
// Copyright 2022 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package cocoa
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/ebitengine/purego/objc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
class_NSAutoreleasePool = objc.GetClass("NSAutoreleasePool")
|
|
|
|
class_NSString = objc.GetClass("NSString")
|
2022-09-16 04:53:46 +02:00
|
|
|
class_NSProcessInfo = objc.GetClass("NSProcessInfo")
|
|
|
|
class_NSColor = objc.GetClass("NSColor")
|
|
|
|
class_NSScreen = objc.GetClass("NSScreen")
|
2022-08-17 18:39:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-10-19 06:30:47 +02:00
|
|
|
sel_alloc = objc.RegisterName("alloc")
|
|
|
|
sel_new = objc.RegisterName("new")
|
|
|
|
sel_release = objc.RegisterName("release")
|
|
|
|
sel_initWithUTF8String = objc.RegisterName("initWithUTF8String:")
|
|
|
|
sel_UTF8String = objc.RegisterName("UTF8String")
|
|
|
|
sel_length = objc.RegisterName("length")
|
|
|
|
sel_processInfo = objc.RegisterName("processInfo")
|
|
|
|
sel_frame = objc.RegisterName("frame")
|
|
|
|
sel_contentView = objc.RegisterName("contentView")
|
|
|
|
sel_setBackgroundColor = objc.RegisterName("setBackgroundColor:")
|
|
|
|
sel_colorWithSRGBRedGreenBlueAlpha = objc.RegisterName("colorWithSRGBRed:green:blue:alpha:")
|
|
|
|
sel_setFrameSize = objc.RegisterName("setFrameSize:")
|
|
|
|
sel_object = objc.RegisterName("object")
|
|
|
|
sel_styleMask = objc.RegisterName("styleMask")
|
|
|
|
sel_setStyleMask = objc.RegisterName("setStyleMask:")
|
|
|
|
sel_mainScreen = objc.RegisterName("mainScreen")
|
|
|
|
sel_screen = objc.RegisterName("screen")
|
|
|
|
sel_isVisible = objc.RegisterName("isVisible")
|
|
|
|
sel_deviceDescription = objc.RegisterName("deviceDescription")
|
|
|
|
sel_objectForKey = objc.RegisterName("objectForKey:")
|
|
|
|
sel_unsignedIntValue = objc.RegisterName("unsignedIntValue")
|
|
|
|
sel_setLayer = objc.RegisterName("setLayer:")
|
|
|
|
sel_setWantsLayer = objc.RegisterName("setWantsLayer:")
|
2022-08-17 18:39:34 +02:00
|
|
|
)
|
|
|
|
|
2022-11-05 06:06:59 +01:00
|
|
|
const (
|
|
|
|
NSWindowCollectionBehaviorManaged = 1 << 2
|
|
|
|
NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
|
|
|
|
NSWindowCollectionBehaviorFullScreenNone = 1 << 9
|
|
|
|
)
|
2022-09-16 04:53:46 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
NSWindowStyleMaskResizable = 1 << 3
|
|
|
|
NSWindowStyleMaskFullScreen = 1 << 14
|
|
|
|
)
|
|
|
|
|
|
|
|
type CGFloat = float64
|
2022-08-17 18:39:34 +02:00
|
|
|
|
|
|
|
type CGSize struct {
|
|
|
|
Width, Height CGFloat
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:53:46 +02:00
|
|
|
type CGPoint struct {
|
|
|
|
X, Y float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type CGRect struct {
|
|
|
|
Origin CGPoint
|
|
|
|
Size CGSize
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSUInteger = uint
|
|
|
|
type NSInteger = int
|
|
|
|
|
|
|
|
type NSPoint = CGPoint
|
|
|
|
type NSRect = CGRect
|
|
|
|
type NSSize = CGSize
|
|
|
|
|
2022-08-17 18:39:34 +02:00
|
|
|
type NSError struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:53:46 +02:00
|
|
|
type NSColor struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func NSColor_colorWithSRGBRedGreenBlueAlpha(red, green, blue, alpha CGFloat) (color NSColor) {
|
2022-11-20 15:48:22 +01:00
|
|
|
return NSColor{objc.ID(class_NSColor).Send(sel_colorWithSRGBRedGreenBlueAlpha, red, green, blue, alpha)}
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type NSOperatingSystemVersion struct {
|
|
|
|
Major, Minor, Patch NSInteger
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSProcessInfo struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func NSProcessInfo_processInfo() NSProcessInfo {
|
|
|
|
return NSProcessInfo{objc.ID(class_NSProcessInfo).Send(sel_processInfo)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSWindow struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w NSWindow) StyleMask() NSUInteger {
|
|
|
|
return NSUInteger(w.Send(sel_styleMask))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w NSWindow) SetStyleMask(styleMask NSUInteger) {
|
|
|
|
w.Send(sel_setStyleMask, styleMask)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w NSWindow) SetBackgroundColor(color NSColor) {
|
|
|
|
w.Send(sel_setBackgroundColor, color.ID)
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
func (w NSWindow) IsVisible() bool {
|
2022-09-16 04:53:46 +02:00
|
|
|
return w.Send(sel_isVisible) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w NSWindow) Screen() NSScreen {
|
|
|
|
return NSScreen{w.Send(sel_screen)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w NSWindow) Frame() NSRect {
|
2024-10-18 04:44:37 +02:00
|
|
|
return objc.Send[NSRect](w.ID, sel_frame)
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w NSWindow) ContentView() NSView {
|
|
|
|
return NSView{w.Send(sel_contentView)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSView struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v NSView) SetFrameSize(size CGSize) {
|
2024-10-18 04:44:37 +02:00
|
|
|
v.ID.Send(sel_setFrameSize, size)
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v NSView) Frame() NSRect {
|
2024-10-18 04:44:37 +02:00
|
|
|
return objc.Send[NSRect](v.ID, sel_frame)
|
2022-09-16 04:53:46 +02:00
|
|
|
}
|
|
|
|
|
2024-01-02 14:22:19 +01:00
|
|
|
func (v NSView) SetLayer(layer uintptr) {
|
2024-10-18 04:44:37 +02:00
|
|
|
v.Send(sel_setLayer, layer)
|
2024-01-02 14:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v NSView) SetWantsLayer(wantsLayer bool) {
|
2024-10-18 04:44:37 +02:00
|
|
|
v.Send(sel_setWantsLayer, wantsLayer)
|
2024-01-02 14:22:19 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:39:34 +02:00
|
|
|
type NSAutoreleasePool struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func NSAutoreleasePool_new() NSAutoreleasePool {
|
|
|
|
return NSAutoreleasePool{objc.ID(class_NSAutoreleasePool).Send(sel_new)}
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:53:46 +02:00
|
|
|
func (p NSAutoreleasePool) Release() {
|
|
|
|
p.Send(sel_release)
|
2022-08-17 18:39:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type NSString struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func NSString_alloc() NSString {
|
|
|
|
return NSString{objc.ID(class_NSString).Send(sel_alloc)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s NSString) InitWithUTF8String(utf8 string) NSString {
|
|
|
|
return NSString{s.Send(sel_initWithUTF8String, utf8)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s NSString) String() string {
|
2022-11-10 16:28:45 +01:00
|
|
|
return string(unsafe.Slice((*byte)(unsafe.Pointer(s.Send(sel_UTF8String))), s.Send(sel_length)))
|
2022-08-17 18:39:34 +02:00
|
|
|
}
|
2022-09-16 04:53:46 +02:00
|
|
|
|
|
|
|
type NSNotification struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NSNotification) Object() objc.ID {
|
|
|
|
return n.Send(sel_object)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSScreen struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func NSScreen_mainScreen() NSScreen {
|
|
|
|
return NSScreen{objc.ID(class_NSScreen).Send(sel_mainScreen)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s NSScreen) DeviceDescription() NSDictionary {
|
|
|
|
return NSDictionary{s.Send(sel_deviceDescription)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSDictionary struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d NSDictionary) ObjectForKey(object objc.ID) objc.ID {
|
|
|
|
return d.Send(sel_objectForKey, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NSNumber struct {
|
|
|
|
objc.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NSNumber) UnsignedIntValue() uint {
|
|
|
|
return uint(n.Send(sel_unsignedIntValue))
|
|
|
|
}
|