A cross-platform UI for Swift

I'm working on SwiftCrossUI a bit again and I'd like to try running it on Windows for myself. Swift 5.7.1 is now released and the Vcpkg port seems to have made it into a release (added in release 2022.10.19) so as far as I understand this should be possible without unreleased patches now?

I'm happy to write the guide for SwiftCrossUI if you don't have the time, as long as you can give some basic pointers on how to get Gtk compiling and installed (as a non-Windows user).

Great work that you are doing, having a cross-platform GUI library is certainly a good idea. But what I would be more interested in is getting native GUIs on Mac, Linux, and Windows. GTK+ might be a good choice for that when being on Linux. For Windows, The Browser Company is working on ā€œdeclarative bindings for WinUI3ā€, and they said they would like to open-source it, and that they would like it to be as far as possible ā€œcompatibleā€ with SwiftUI, if I understood it correctly. So maybe it might be possible to some degree to use the same application code to get a native GUI on all three platforms? Just using compiler directives where the code differs / using special features of a platform? Maybe these projects should collaborate at some points?

2 Likes

That would definitely be possible, thanks for alerting me to that project! It is a future goal of mine to allow making native UIs with a single code base, but that is a very broad and difficult problem to solve (differing layout systems etc.). I've been sticking to using a single backend so that SwiftCrossUI code hopefully produces a predictable UI on all systems until it is more stable.

I would love to move away from Gtk because imo it's clunky, UBD (ugly-by-default) and it also doesn't support iOS. I've been looking into making an experimental custom renderer backend with WebGPU (using a work-in-progress wrapper of wgpu-native for Swift called Kinzoku). Not because it's necessarily the best solution, mostly just because I'm interested in writing a renderer with WebGPU :sweat_smile: Anyway that would be experimental and quite a while off. It probably wouldn't ever become a stable backend.

My overarching plan for getting a native look on all platforms is to

  1. create more abstraction between the view graph and the Gtk backend (there is already pretty good separation, but it will need to be improved further to allow for the view graph to be backend agnostic)
  2. implement an AppKit/UIKit backend (either or both) as a proof of concept that SwiftCrossUI can work with multiple backends
  3. create a Windows-specific backend

Community-made backends will be welcomed too of course :slight_smile:

I plan on allowing developers to choose between all available backends on a single platform. That is, they can choose to use Gtk on all desktop platforms if they want maximum consistency, or they could use Gtk on Windows and Linux but AppKit on macOS, etc.

3 Likes

Sorry for the late response.

Yes, vcpkg has accepted gtk3 and weā€™re able to build it on Windows now. However, static linking is not available because of a pango bug (patch submitted but not merged, Iā€™m going to recheck its state).

For a quick reference on how to use it from SwiftPM:

  1. Clone vcpkg at the root of a drive, eg. C:;
  2. Run C:\vcpkg\bootstrap-vcpkg.bat to set up Vcpkg;
  3. Run C:\vcpkg\vcpkg.exe install gtk3:x64-windows to build and install the package;
  4. Set the environment variable though set PKG_CONFIG_PATH=C:\vcpkg\installed\x64-windows\lib\pkgconfig.
  5. Run swift build.
1 Like

Awesome, thanks! I look forward to testing that out on the weekend

I just saw that Saleem Abdulrasool @compnerd joined The Browser Company.

Wowwww !!! This means that Swift on Windows will take it to the next level.

We can now seriously foresee a not so distant day of developing in Swift on Windows as we love on macOS using SwiftUI.

3 Likes

Wow, thatā€™s cool that they hired him.

If you donā€™t wanna wait around, you can check out SwiftCrossUI :slight_smile: Itā€™s still quite experimental but weā€™re making pretty quick progress on adding more views and a better layout system and all that.

1 Like

You might then have a look at the video in this tweet (it is missing a little bit the reference that the Visual Studio Code extension is originally made by others and that Swift on Windows is an older thing in the making, but I attribute this to the enthusiastic style of Hursh Agrawal).

You may also have a look at the tweets of Steve Kirbach.

The videos by The Browser Company are generally fun to watch, but they also seem really serious about what they are doing. A big win for the Swift community! And also for Saleem Abdulrasool, who can devote much more time for something he seems to be enthusiastic about. Congratulations! :tada:

Update: How we're building the Arc Windows app with *Swift* - YouTube

3 Likes

Now that C++ interoperability is here, has anyone tried to use existing cross platform UIs frameworks such as Qt or wxWidgets?

Wouldn't that be a more sensible way to proceed, instead of writing something new or trying to port SwiftUi to other platforms?

I havenā€™t tried Qt with c++ interop, but I have tried it a bit before 5.9 (SwiftCrossUI is built on top of frameworks including Gtk, Qt, and WinUI).

The main reason that Iā€™m porting SwiftUI to other platforms is that I believe itā€™s a much more expressive way of making apps, and itā€™d make porting macOS/iOS apps to non-Apple platforms a lot easier.

If you like Qt, thereā€™s nothing to stop you using it!

1 Like