slint icon indicating copy to clipboard operation
slint copied to clipboard

Bindings to the Swift programming language

Open PetaSoft opened this issue 2 years ago • 5 comments

A Swift compiler exists not only for the Apple ecosystem but also for Windows and Linux but SwiftUI can only be used for Apple devices. Furthermore, Qt is a cross plattform framework, but only supports C++ and Python. Maybe, Slint can fill a gap, and become a cross plattform framework for Swift. Furthermore, I am not aware of any UI framework that supports Swift on Windows.

PetaSoft avatar Mar 02 '22 17:03 PetaSoft

We aim to support multiple programming language in the future. That said, Swift is not a priority for us considering that Swift already has a natural UI framework and that Swift is not really popular on Linux and Windows. But if someone were to contribute Swift bindings, we'd happily assist them.

ogoffart avatar Mar 03 '22 08:03 ogoffart

We aim to support multiple programming language in the future. That said, Swift is not a priority for us considering that Swift already has a natural UI framework and that Swift is not really popular on Linux and Windows. But if someone were to contribute Swift bindings, we'd happily assist them.

@ogoffart Is there a guide how to develop bindings for other programming languages?

thbonk avatar Oct 30 '22 10:10 thbonk

We don't have such a guide.

There is two things to do:

  1. wrap the API to the slint runtime
  2. Create a code generator for the swift language.

For example, for C++, the wrapper is in https://github.com/slint-ui/slint/tree/master/api/cpp , and the code generator in https://github.com/slint-ui/slint/blob/master/internal/compiler/generator/cpp.rs

The best would be, I guess, to look at that and try to do something similar for swift.

If you are interested, I'd be happy to answer any questions you have, here or by chat or so.

ogoffart avatar Oct 30 '22 11:10 ogoffart

I've spent a few weeks taking a crack at it. I'm no longer interested in pursuing this, but if someone else wants to this could be a good starting point.

Here's my result: https://github.com/illucid-matthew/slint-swift-bindings

The main things I accomplished are:

  • Importing the private cbindgen API into Swift
  • Integrating the Slint event loop and main-thread requirements into Swift's concurrency model
  • Basic application framework
    • Create a type that conforms to SlintApp
    • Write a start() method to initialize your UI
    • Event loop starts when start() is finished

Example:

import SlintUI

@main
struct ExampleApp: SlintApp {
    static func start() {
        print("⏰ Setting up a timer to fire in three seconds…")
        
        let timer = SlintTimer()
        timer.willRun(after: 3000) {
            print("⏰ Timer fired!")
        }
    }
}

I also converted timer and callback, but the latter runs into a strange bug that that I'm not inclined to track down.

Beyond that, there are a few unfinished core library types, and a minor attempt to create bindings for the interpreter API.

illucid-matthew avatar Feb 18 '24 01:02 illucid-matthew

Great that you managed to get that far with Swift bindings - thanks for sharing!

tronical avatar Feb 26 '24 10:02 tronical