Debian Codegraph @

examples/window/main_darwin.go

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