How I build simple Mac apps using Go

WBOY
发布: 2024-07-17 21:12:01
原创
630 人浏览过

I started DarwinKit a few years ago because there were no bindings to native Mac APIs for Go. We've slowly turned the project into bindings and generation tooling to someday reach full coverage of all Apple APIs. The release of v0.5.0 last week is the largest the project has seen:

  • Bindings for 33 frameworks with near complete coverage:
    • 2,353 classes
    • 23,822 methods and properties
    • 9,519 constants/enums
    • 543 structs
  • Automatic conversion and use of native Go builtin types in APIs
  • Support for block arguments as Go functions with properly typed arguments
  • Pre-made delegate implementations you can simply set Go functions on
  • 1-to-1 mapping to Objective-C symbols while still idiomatic to Go
  • Documentation for all symbols including a link to official Apple docs on that symbol
  • Growing collection of high-quality example starter apps for sponsors

Demo Screenshots

Here is a quick example using DarwinKit to build a native webview window application in a few lines of Go:

package main

import (
    "github.com/progrium/darwinkit/objc"
    "github.com/progrium/darwinkit/macos"
    "github.com/progrium/darwinkit/macos/appkit"
    "github.com/progrium/darwinkit/macos/foundation"
    "github.com/progrium/darwinkit/macos/webkit"
)

func main() {
    // runs macOS application event loop with a callback on success
    macos.RunApp(func(app appkit.Application, delegate *appkit.ApplicationDelegate) {
        app.SetActivationPolicy(appkit.ApplicationActivationPolicyRegular)
        app.ActivateIgnoringOtherApps(true)

        url := foundation.URL_URLWithString("https://github.com/sponsors/darwinkitdev")
        req := foundation.NewURLRequestWithURL(url)
        frame := foundation.Rect{Size: foundation.Size{1440, 900}}

        config := webkit.NewWebViewConfiguration()
        wv := webkit.NewWebViewWithFrameConfiguration(frame, config)
        wv.LoadRequest(req)

        w := appkit.NewWindowWithContentRectStyleMaskBackingDefer(frame,
            appkit.ClosableWindowMask|appkit.TitledWindowMask,
            appkit.BackingStoreBuffered, false)
        objc.Retain(&w)
        w.SetContentView(wv)
        w.MakeKeyAndOrderFront(w)
        w.Center()

        delegate.SetApplicationShouldTerminateAfterLastWindowClosed(func(appkit.Application) bool {
            return true
        })
    })
}
登录后复制

In less than 40 lines we made a native Mac app without opening XCode or using Objective-C. I think this might now be the best bindings project in existence for Apple APIs. Possibly even the best way to make small utilities on the Mac. And soon even other Apple devices.

The Future

There is one big missing piece to DarwinKit: there are no bindings to Apple framework functions. Luckily, most frameworks are built with OOP, which we have great bindings for now. But some frameworks, especially lower-level frameworks, are mostly functions. While there is a workaround that involves using CGO (which DarwinKit is trying to help you avoid), we're working on generating native Go function bindings for every framework function.

The other big thing we're working towards is making DarwinKit not use CGO at all! Using purego, we can call into Apple frameworks without involving CGO. This will improve build time, make smaller binaries, and allow DarwinKit to be used in programs that need to avoid CGO for whatever reason.

For iOS and mobile devs out there, I really want to get this working for iOS. In fact, it already should! But we generate bindings for MacOS for now. If anybody wants to help bring this to iOS to let people make Apple mobile apps with Go, please reach out!

Until then, try building an app with what we've got so far. Let me know how it goes!

以上是How I build simple Mac apps using Go的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!