How I build simple Mac apps using Go

WBOY
Release: 2024-07-17 21:12:01
Original
845 people have browsed it

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
        })
    })
}
Copy after login

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!

The above is the detailed content of How I build simple Mac apps using Go. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!