github.com/ebitengine/purego@v0.8.0-alpha.2.0.20240512170805-6cd12240d332/examples/window/main_darwin.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2024 The Ebitengine Authors
     3  
     4  package main
     5  
     6  import (
     7  	"runtime"
     8  
     9  	"github.com/ebitengine/purego"
    10  	"github.com/ebitengine/purego/objc"
    11  )
    12  
    13  const (
    14  	NSApplicationActivationPolicyRegular = 0
    15  	NSWindowStyleMaskTitled              = 1 << 0
    16  	NSBackingStoreBuffered               = 2
    17  )
    18  
    19  type NSPoint struct {
    20  	X, Y float64
    21  }
    22  
    23  type NSSize struct {
    24  	Width, Height float64
    25  }
    26  
    27  type NSRect struct {
    28  	Origin NSPoint
    29  	Size   NSSize
    30  }
    31  
    32  func init() {
    33  	runtime.LockOSThread()
    34  }
    35  
    36  func main() {
    37  	if _, err := purego.Dlopen("/System/Library/Frameworks/Cocoa.framework/Cocoa", purego.RTLD_GLOBAL|purego.RTLD_LAZY); err != nil {
    38  		panic(err)
    39  	}
    40  	nsApp := objc.ID(objc.GetClass("NSApplication")).Send(objc.RegisterName("sharedApplication"))
    41  	nsApp.Send(objc.RegisterName("setActivationPolicy:"), NSApplicationActivationPolicyRegular)
    42  	wnd := objc.ID(objc.GetClass("NSWindow")).Send(objc.RegisterName("alloc"))
    43  	wnd = wnd.Send(objc.RegisterName("initWithContentRect:styleMask:backing:defer:"),
    44  		NSMakeRect(0, 0, 320, 240),
    45  		NSWindowStyleMaskTitled,
    46  		NSBackingStoreBuffered,
    47  		false,
    48  	)
    49  
    50  	title := objc.ID(objc.GetClass("NSString")).Send(objc.RegisterName("stringWithUTF8String:"), "My Title")
    51  	wnd.Send(objc.RegisterName("setTitle:"), title)
    52  	wnd.Send(objc.RegisterName("makeKeyAndOrderFront:"), objc.ID(0))
    53  	wnd.Send(objc.RegisterName("center"))
    54  	nsApp.Send(objc.RegisterName("activateIgnoringOtherApps:"), true)
    55  	nsApp.Send(objc.RegisterName("run"))
    56  }
    57  
    58  func NSMakeRect(x, y, width, height float64) NSRect {
    59  	return NSRect{Origin: NSPoint{x, y}, Size: NSSize{width, height}}
    60  }