GPUI Kit 安装指南:在 macOS、Windows 与 Linux 上搭建 Rust 桌面应用开发环境
【免费下载链接】gpui-kitRust GUI components for building fantastic cross-platform desktop application by using GPUI.项目地址: https://gitcode.com/GitHub_Trending/gp/gpui-kit
GPUI Kit 是一个基于 GPUI 的 Rust 跨平台桌面 UI 组件库,官方为gpui-component提供了一站式的环境准备与安装方案。本文将以 安装文档 为主线,结合仓库内的 安装脚本、依赖清单 与 kit 层的源码实现,完整讲解系统依赖准备、gpui-kitcrate 的引入方式,以及如何通过 Cargo profile 显著加快 Debug 构建下的应用渲染速度。读完本文,你将能够从零开始在三个主流桌面平台上跑起第一个 GPUI Kit 窗口。
系统要求
GPUI Kit 目前支持在 macOS、Windows 和 Linux 三大桌面平台进行开发,各平台的最低要求如下:
macOS
- macOS 15 或更高版本
- Xcode Command Line Tools
Xcode Command Line Tools 是编译原生代码的必备工具,可通过xcode-select --install安装。macOS 上的 GPUI 使用 Metal 作为渲染后端,因此对系统版本有明确的最低要求。
Windows
- Windows 10 或更高版本
Windows 平台的构建依赖(MSVC 工具链、CMake 等)可以通过仓库提供的脚本一键安装,见下文「Windows 依赖安装」小节。
Linux
Linux 平台需要安装与图形栈、字体、Vulkan 相关的系统库。仓库提供了./script/bootstrap脚本自动完成安装,具体清单见下文「Linux 依赖安装」小节。
Windows 依赖安装
仓库在 script/install-window.ps1 中提供了 PowerShell 脚本,用于安装 Windows 构建所需的工具链与依赖。在 PowerShell 中执行:
.\script\install-window.ps1从脚本源码可以看到,它依次完成以下工作:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression winget install Microsoft.VisualStudio.2022.Community --silent --override "--wait --quiet --add ProductLang En-us --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended" scoop bucket add extras scoop install cmake- 首先将当前用户的执行策略设置为
RemoteSigned,以便运行下载的安装脚本; - 通过 scoop 安装包管理器;
- 使用
winget静默安装 Visual Studio 2022 Community,并显式附加NativeDesktop 工作负载(包含 MSVC C++ 工具链、Windows SDK 等,是编译 GPUI 及原生依赖所必需的),同时加入--includeRecommended安装推荐组件; - 最后通过 scoop 安装CMake,用于构建部分依赖的原生 C/C++ 部分。
建议以管理员身份运行 PowerShell 后执行该脚本;脚本执行完成后,重新打开终端再运行cargo build验证环境。
Linux 依赖安装
在 Linux 上运行仓库根目录下的引导脚本即可:
./script/bootstrap该脚本会判断当前系统类型:在 Linux 上转调 script/install-linux.sh,在 macOS 上则输出提示(macOS 依赖已由 Xcode Command Line Tools 覆盖)。以 Ubuntu 24.04 为基准测试环境,Linux 脚本实际安装的依赖为:
sudo apt update sudo apt install -y \ gcc g++ clang libfontconfig-dev libwayland-dev \ libwebkit2gtk-4.1-dev libxkbcommon-x11-dev libx11-xcb-dev \ libssl-dev libzstd-dev \ vulkan-validationlayers libvulkan1这些包按用途可分为几类:
- C/C++ 编译器:
gcc、g++、clang,用于编译 Rust 依赖的原生代码; - 字体与图形栈:
libfontconfig-dev(字体配置)、libwayland-dev(Wayland 协议库); - WebKit:
libwebkit2gtk-4.1-dev,是 GPUI 中 WebView 相关能力(如 webview crate)在 Linux 上的运行时依赖; - X11 与输入:
libxkbcommon-x11-dev、libx11-xcb-dev,用于 X11 平台下的键盘映射与窗口系统互操作; - 加密与压缩:
libssl-dev、libzstd-dev,对应 Rust 生态中 TLS 与 zstd 压缩的原生后端; - Vulkan:
vulkan-validationlayers、libvulkan1,GPUI 在 Linux 上使用 Vulkan 作为渲染后端。
如果你的发行版不是 Debian/Ubuntu 系(即没有apt),需要参照上述清单安装等价包。Wayland 与 X11 依赖同时安装,可以覆盖两种常见的 Linux 桌面会话。
Rust 与 Cargo
GPUI Kit 使用 Rust 构建,因此系统需要预先安装 Rust 工具链:
- Rust 1.90 或更高版本
- Cargo(通常随 Rust 一并安装)
推荐使用 rustup),还需要安装 nightly 工具链与wasm32-unknown-unknown目标:
[toolchain] channel = "nightly" components = ["rustfmt", "clippy"] targets = ["wasm32-unknown-unknown"]在 Cargo.toml 中引入 gpui-kit
在应用(或 workspace)的Cargo.toml的[dependencies]中加入一行即可:
[dependencies] gpui-kit = "0.6"关键设计是:gpui-kit会替你引入配套的 GPUI crate 集合,应用无需再单独声明 GPUI。从 crates/kit/Cargo.toml 的依赖声明可以看到,kit 层直接依赖gpui(gpui-pre)、gpui_platform、gpui-base,并可选地携带gpui-component与gpui-kit-assets。
各层模块的命名空间
根据 crates/kit/src/lib.rs 与 crates/kit/README.md 的说明,use gpui_kit::*;就是 GPUI 本身,各层按名访问:
| Path | Crate | Feature |
|---|---|---|
gpui_kit::* | gpui | always |
gpui_kit::platform | gpui_platform | always |
gpui_kit::base | gpui-base | always |
gpui_kit::component | gpui-component | component(默认开启) |
gpui_kit::assets | gpui-kit-assets | assets(默认开启) |
gpui_kit::component:带样式的组件库,即gpui-component,并会顺带初始化gpui-base;gpui_kit::base:GPUI Base 基础层;gpui_kit::assets:默认图标资源集;gpui_kit::platform:平台能力层(窗口、事件循环等)。
最小可运行示例
gpui_kit::application()负责打开平台,gpui_kit::init()负责初始化所有已启用的层(在源码中,启用componentfeature 时委托给gpui_component::init,否则退化为gpui_base::init),且必须在任何其他使用之前调用一次。一个带按钮的最小示例:
use gpui_kit::component::button::*; use gpui_kit::component::Root; use gpui_kit::*; struct Hello; impl Render for Hello { fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { div().child(Button::new("ok").primary().label("Let's Go!")) } } fn main() { gpui_kit::application().run(|cx| { gpui_kit::init(cx); cx.spawn(async move |cx| { cx.open_window(WindowOptions::default(), |window, cx| { let view = cx.new(|_| Hello); cx.new(|cx| Root::new(view, window, cx)) }) .expect("failed to open window"); }) .detach(); }); }仓库自带的 story 应用 展示了同样的启动流程:gpui_kit::application().with_assets(Assets)挂载默认资源,随后在run回调中init(cx)并打开窗口。
可选 Feature
gpui-kit的默认 feature 为component与assets(见 crates/kit/Cargo.toml)。gpui-component提供的inspector、decimal、tree-sitter、tree-sitter-languages以及各tree-sitter-<language>特性,都可以在gpui-kit上以同名开启。用于 UI 集成测试的test-support(启用 GPUI 的测试框架、#[gpui_kit::test]宏与原生平台渲染支持)通常应放在[dev-dependencies]中,而非正式依赖。
加快开发构建:仅对依赖开启优化
Debug 构建下,GPUI、组件库和文字排版相关的 crate 默认不做优化,导致cargo run出来的应用渲染明显比 release 慢。解决方案是:只对这些依赖开启优化,你自己的代码仍然保持快速、可调试的 debug 构建。
Cargo 的 Profile 只在应用(或 workspace)根目录的Cargo.toml中生效。将以下片段加入根Cargo.toml:
[profile.dev.package] gpui-pre = { opt-level = 3 } gpui-component = { opt-level = 3 } gpui-kit = { opt-level = 3 } gpui-kit-assets = { opt-level = 3 } gpui-pre-macros = { opt-level = 3 } gpui-pre-platform = { opt-level = 3 } rustybuzz = { opt-level = 3 } taffy = { opt-level = 3 } ttf-parser = { opt-level = 3 }各条目的作用说明:
gpui-pre、gpui-pre-platform、gpui-pre-macros:GPUI 运行时、平台层与宏层,是渲染热路径的核心;gpui-component、gpui-kit、gpui-kit-assets:组件库、kit 聚合层与资源 crate;rustybuzz:OpenType 文本整形引擎;taffy:布局引擎;ttf-parser:TrueType 字体解析器。
其中文字排版相关的rustybuzz、taffy、ttf-parser与 GPU 无关但计算密集,opt-level = 3能显著改善文本渲染性能。仓库根目录的 Cargo.toml 中实际生效的 profile 还包含resvg、smol、tree-sitter、ropey、lsp-types、markdown等更多条目,并设置了codegen-units = 16、debug = "limited"、split-debuginfo = "unpacked",可作为优化调试体验的参考。
该配置只影响列出的依赖 crate,不会拖慢你自己的代码编译,也不会影响cargo build --release的产物。
验证安装
完成以上步骤后,可以通过以下方式验证环境是否就绪:
# 1. 确认 Rust 版本 rustc --version # 应不低于 1.90 # 2. 新建项目并引入 gpui-kit cargo new my_app && cd my_app # 在 Cargo.toml 的 [dependencies] 中添加 gpui-kit = "0.6" # 3. 编写一个最小窗口程序(参考上文示例) # 4. 运行 cargo run若构建过程中遇到缺库报错,回到对应平台的依赖安装脚本核对是否全部安装。仓库中的 examples/hello_world 等示例也可作为现成的验证工程。
至此,你已经完成了 GPUI Kit 的全平台环境搭建,可以基于gpui_kit::component开始构建跨平台桌面应用了。更完整的组件用法可继续阅读 基础组件文档 与 快速上手。
【免费下载链接】gpui-kitRust GUI components for building fantastic cross-platform desktop application by using GPUI.项目地址: https://gitcode.com/GitHub_Trending/gp/gpui-kit
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考