swift-cross-ui

swift-cross-ui

简化跨平台Swift应用开发的UI框架

SwiftCrossUI是一个用于创建跨平台Swift应用的开源框架,其API设计类似SwiftUI。该框架支持Gtk 4、AppKit和Qt5等多个后端,便于开发者构建具有原生外观的应用。SwiftCrossUI提供简洁的API,实现状态管理、窗口控制和丰富的UI控件,适用于各种复杂度的应用开发。尽管仍在持续完善中,SwiftCrossUI已展现出作为灵活高效的跨平台UI解决方案的潜力。

SwiftCrossUI跨平台开发Swift框架GUI开发开源项目Github
<p align="center"> <img width="100%" src="banner.png"> </p> <p align="center"> <img src="https://github.com/stackotter/swift-cross-ui/workflows/Build%20macOS/badge.svg?branch=main"> <img src="https://github.com/stackotter/swift-cross-ui/workflows/Build%20Linux/badge.svg?branch=main"> <img src="https://github.com/stackotter/swift-cross-ui/workflows/Build%20Windows/badge.svg?branch=main"> <img alt="GitHub" src="https://img.shields.io/github/license/stackotter/swift-cross-ui"> </p>

A SwiftUI-like framework for creating cross-platform apps in Swift.

This package is still quite a work-in-progress so don't expect it to be very useful or stable yet.

NOTE: SwiftCrossUI does not attempt to replicate SwiftUI's API perfectly because SwiftCrossUI is intended to be simpler than SwiftUI. However, many concepts from SwiftUI should still be transferrable.

Community

Discussion about SwiftCrossUI now happens in the SwiftCrossUI Discord server (it used to happen in a channel of the SwiftGtk Discord server). Feel free to join if you want to get involved, discuss the library, or just be kept up-to-date on progress!

Supporting SwiftCrossUI

If you find SwiftCrossUI useful, please consider supporting me by becoming a sponsor. I spend most of my spare time working on open-source projects, and each sponsorship helps me focus more time on making high quality libraries and tools for the community.

Backends

Work has been started to support multiple different backends. Switching backends only requires changing a single line of code! Currently there's the Gtk 4 backend, as well as an experimental AppKit backend (AppKitBackend, macOS-only). All examples use GtkBackend for maximum compatibility, but you can update them manually to try out the various available backends. Work is being done to allow the backend used by the examples to be changed from the command line.

  • GtkBackend: Requires gtk 4 to be installed, has maximum compatibility, and supports all SwiftCrossUI features.
  • AppKitBackend: Experimental, only supports macOS, and currently supports a very limited subset of SwiftCrossUI features.
  • QtBackend: Experimental, requires qt5 to be installed, and currently supports a very limited subset of SwiftCrossUI features.

Example

Here's a simple example app demonstrate how easy it is to get started with SwiftCrossUI:

import SwiftCrossUI // Import whichever backend you need import GtkBackend class CounterState: Observable { @Observed var count = 0 } @main struct CounterApp: App { // Optionally, you can explicitly select which imported backend to use (this is done for // you if only one backend is imported). // typealias Backend = GtkBackend let identifier = "dev.stackotter.CounterApp" let state = CounterState() var body: some Scene { WindowGroup("CounterApp") { HStack { Button("-") { state.count -= 1 } Text("Count: \(state.count)") Button("+") { state.count += 1 } } .padding(10) } } }

To run this example, run these commands:

git clone https://github.com/stackotter/swift-cross-ui cd swift-cross-ui/Examples swift run CounterExample

Other examples

A few examples are included with SwiftCrossUI to demonstrate some of its basic features;

  • CounterExample, a simple app with buttons to increase and decrease a count.
  • RandomNumberGeneratorExample, a simple app to generate random numbers between a minimum and maximum.
  • WindowingExample, a simple app showcasing how WindowGroup is used to make multi-window apps and control the properties of each window.
  • GreetingGeneratorExample, a simple app demonstrating dynamic state and the ForEach view.
  • FileViewerExample, an app showcasing integration with the system's file chooser.
  • NavigationExample, an app showcasing NavigationStack and related concepts.
  • SplitExample, an app showcasing sidebar-based navigation with multiple levels.
  • StressTestExample, an app used to test view update performance.
  • SpreadsheetExample, an app showcasing tables.
  • ControlsExample, an app showcasing the various types of controls available.

Running examples on other backends

All examples use GtkBackend by default but you can override this using the SCUI_BACKEND environment variable like so;

SCUI_BACKEND=QtBackend swift run CounterExample

Documentation

Here's the documentation site. Keep in mind that the project is still very much a work-in-progress, proper documentation and tutorials will be created once the project has matured a bit, because otherwise I have to spend too much time keeping the documentation up-to-date.

Dependencies

  1. Swift 5.5 or higher
  2. Gtk 4 (only required for GtkBackend)
  3. clang (only required on Linux for GtkBackend)
  4. Qt5 (only required for QtBackend)

macOS: Installing Gtk 4

Install Gtk 4 using HomeBrew or the package manager of your choice.

brew install pkg-config gtk4

If you run into errors related to libffi or FFI when trying to build a swift-cross-ui project (likely caused by having Xcode CLTs installed), try running the following command to patch libffi:

sed -i '' 's/-I..includedir.//g' $(brew --prefix)/Library/Homebrew/os/mac/pkgconfig/*/libffi.pc

Linux: Installing Gtk 4 and Clang

Install Gtk 4 and Clang using apt or the package manager of your choice. On most GNOME-based systems, Gtk should already be installed (although you should verify that it's Gtk 4).

sudo apt install libgtk-4-dev clang

If you run into errors related to not finding gtk/gtk.h when trying to build a swift-cross-ui project, try restarting your computer. This has worked in some cases (although there may be a more elegant solution).

Windows (experimental): Installing Gtk 4 through vcpkg

Installing Gtk 4 using vcpkg is the supported method for setting up SwiftCrossUI on Windows.

Install vcpkg

git clone https://github.com/microsoft/vcpkg C:\vcpkg C:\vcpkg\bootstrap-vcpkg.bat

NOTE: It's important to install vcpkg to the root of C: or any other drive due to limitations of the Gtk build system.

Install Gtk 4 globally (recommended)

Run the following command to install Gtk 4 globally. This can take 45 minutes or longer depending on your machine. Running this command in the root of your drive will ensure that vcpkg doesn't run in manifest mode.

C:\vcpkg\vcpkg.exe install gtk --triplet x64-windows

After installation, you must make the following changes to your environment variables:

  1. Set the PKG_CONFIG_PATH environment variable to C:\vcpkg\installed\x64-windows\lib\pkgconfig. This is only required for building.
  2. Add C:\vcpkg\installed\x64-windows\bin to your Path environment variable. This is only required for running.

If installing globally fails, try deleting C:\vcpkg and starting over, otherwise file an issue to the vcpkg repository and let me know at stackotter@stackotter.dev.

Install Gtk 4 with project manifest (more unreliable)

NOTE: If the absolute path to your project contains spaces, it is possible that vcpkg will break, and installing globally will be a more reliable strategy.

You can install Gtk 4 inside your package workspace, to have a package-specific dependency store. First, create a vcpkg.json at your package root. You can download the vcpkg.json file from this repo, or create it yourself;

{ "name": "swift-cross-ui", "version-string": "main", "dependencies": ["gtk"] }

Change directory to your package root, then run the following command to build and install dependencies.

C:\vcpkg\vcpkg.exe install --triplet x64-windows

After installation, you must make the following changes to your environment variables:

  1. Set the PKG_CONFIG_PATH environment variable to PACKAGE_ROOT\vcpkg_installed\x64-windows\lib\pkgconfig to allow SwiftPM to consume the installed packages.
  2. Add C:\path\to\your\project\vcpkg_installed\x64-windows\bin to your PATH environment variable.

If you run into issues (potentially related to libsass), try installing globally instead (see above).

Distribute SwiftCrossUI Apps

vcpkg_installed\<triplet>\bin contains all required DLLs for running a SwiftCrossUI application on Windows, but not all of them are necessary.

To identify which of them are required, you can use the Dependencies tool to inspect the compiled executable, and copy all vcpkg-installed DLLs along with the executable for distribution.

Installing Qt5

# On macOS brew install qt@5 brew link qt@5 # Linux with apt sudo apt install qtcreator qtbase5-dev qt5-qmake cmake

Usage

Just add SwiftCrossUI as a dependency in your Package.swift. See below for an example package manifest:

import PackageDescription let package = Package( name: "Example", dependencies: [ .package(url: "https://github.com/stackotter/swift-cross-ui", branch: "main") ], targets: [ .executableTarget( name: "Example", dependencies: [ .product(name: "SwiftCrossUI", package: "swift-cross-ui") ] ) ] )

编辑推荐精选

音述AI

音述AI

全球首个AI音乐社区

音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。

QoderWork

QoderWork

阿里Qoder团队推出的桌面端AI智能体

QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。

lynote.ai

lynote.ai

一站式搞定所有学习需求

不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。

AniShort

AniShort

为AI短剧协作而生

专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。

seedancetwo2.0

seedancetwo2.0

能听懂你表达的视频模型

Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。

nano-banana纳米香蕉中文站

nano-banana纳米香蕉中文站

国内直接访问,限时3折

输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

下拉加载更多