MacDriver

Go 的 Mac 原生 API。「Native Mac APIs for Go」

Github星跟蹤圖

Native Mac APIs for Golang!

GoDoc
Go Report Card



MacDriver is a toolkit for working with Apple/Mac APIs and frameworks in Go. It currently has 2 parts:

1. Bindings for Objective-C

The objc package wraps the Objective-C runtime to dynamically interact with Objective-C objects and classes:

cls := objc.NewClass("AppDelegate", "NSObject")
cls.AddMethod("applicationDidFinishLaunching:", func(app objc.Object) {
	fmt.Println("Launched!")
})
objc.RegisterClass(cls)

delegate := objc.Get("AppDelegate").Alloc().Init()
app := objc.Get("NSApplication").Get("sharedApplication")
app.Set("delegate:", delegate)
app.Send("run")
  • Access any class or method you can access in Objective-C
  • Common object convenience methods: Get, Set, Alloc, Init, ...
  • Create and extend classes at runtime that can be used by Objective-C code
  • Retain and Release methods for working with Objective-C memory management

2. Framework Packages

The cocoa, webkit, and core packages wrap objc with wrapper types for parts of the Apple/Mac APIs. They're being added to as needed by hand until
we can automate this process with schema data. These packages effectively let you use Apple APIs as if they were native Go libraries, letting
you write Mac applications (potentially also iOS, watchOS, etc) as Go applications:

func main() {
	app := cocoa.NSApp_WithDidLaunch(func(notification objc.Object) {
		config := webkit.WKWebViewConfiguration_New()
		wv := webkit.WKWebView_Init(core.Rect(0, 0, 1440, 900), config)
		url := core.URL("http://progrium.com")
		req := core.NSURLRequest_Init(url)
		wv.LoadRequest(req)

		w := cocoa.NSWindow_Init(core.Rect(0, 0, 1440, 900),
			cocoa.NSClosableWindowMask|
				cocoa.NSTitledWindowMask,
			cocoa.NSBackingStoreBuffered, false)
		w.SetContentView(wv)
		w.MakeKeyAndOrderFront(w)
		w.Center()
	})
	app.SetActivationPolicy(cocoa.NSApplicationActivationPolicyRegular)
	app.ActivateIgnoringOtherApps(true)
	app.Run()
}
  • 1:1 mapping of API names with Objective-C APIs
  • Cocoa types: NSApplication, NSImage, NSMenu, NSWindow, more ...
  • Webkit types: WKWebView and configuration classes
  • Core types: NSData, NSDictionary, NSNumber, NSPoint, NSRect, NSSize, NSString, NSURL, more ...
  • Core also allows dispatching Go functions in the Cocoa run loop
  • Many constants/enums are defined

Examples

examples/largetype - A Contacts/Quicksilver-style Large Type utility in under 80 lines:
largetype screenshot

examples/pomodoro - A menu bar pomodoro timer in under 80 lines:
pomodoro gif

examples/topframe - An always-on-top webview with transparent background in 120 lines [requires Go 1.16+]:
topframe screenshot

NEW: See progrium/topframe for a more fully-featured standalone version!

Generating wrappers

Eventually we can generate most of the wrapper APIs using bridgesupport and/or doc schemas. However, the number of APIs
is pretty ridiculous so there are lots of edge cases I wouldn't know how to automate yet. We can just continue to create them by hand
as needed until we have enough coverage/confidence to know how we'd generate wrappers.

Thanks

The original objc and variadic packages were written by Mikkel Krautz. The variadic package is some assembly magic to make everything possible since libobjc relies heavily on variadic function calls, which aren't possible out of the box in Cgo.

License

MIT

主要指標

概覽
名稱與所有者progrium/darwinkit
主編程語言Go
編程語言Makefile (語言數: 6)
平台
許可證MIT License
所有者活动
創建於2020-11-03 19:04:37
推送於2025-03-08 16:47:06
最后一次提交
發布數9
最新版本名稱v0.5.0 (發布於 )
第一版名稱v0.0.1 (發布於 )
用户参与
星數5.2k
關注者數47
派生數165
提交數266
已啟用問題?
問題數123
打開的問題數29
拉請求數87
打開的拉請求數6
關閉的拉請求數14
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?