鱼皮AI导航
鱼皮AI导航
  • 主页
  • 社区
  • AI工具
  • AI知识库
  • AI提示词
  • MCP
  • Skills
  • AI学习资源
  • AI资讯
  • GitHub开源
登录/注册

gpui-global

提供 GPUI 应用中跨组件共享全局状态的能力,支持在任意上下文读取和安全更新应用级配置、功能开关及共享服务实例,适用于需统一管理且生命周期贯穿整个应用的只读或受控可变数据。

软件开发
访问 GitHub
文档Skills.MD
name
gpui-global
description
Global state management in GPUI. Use when implementing global state, app-wide configuration, or shared resources.

Overview

Global state in GPUI provides app-wide shared data accessible from any context.

Key Trait: Global - Implement on types to make them globally accessible

Quick Start

Define Global State

▼
rust
复制代码
use gpui::Global; #[derive(Clone)] struct AppSettings { theme: Theme, language: String, } impl Global for AppSettings {}

Set and Access Globals

▼
rust
复制代码
fn main() { let app = Application::new(); app.run(|cx: &mut App| { // Set global cx.set_global(AppSettings { theme: Theme::Dark, language: "en".to_string(), }); // Access global (read-only) let settings = cx.global::<AppSettings>(); println!("Theme: {:?}", settings.theme); }); }

Update Globals

▼
rust
复制代码
impl MyComponent { fn change_theme(&mut self, new_theme: Theme, cx: &mut Context<Self>) { cx.update_global::<AppSettings, _>(|settings, cx| { settings.theme = new_theme; // Global updates don't trigger automatic notifications // Manually notify components that care }); cx.notify(); // Re-render this component } }

Common Use Cases

1. App Configuration

▼
rust
复制代码
#[derive(Clone)] struct AppConfig { api_endpoint: String, max_retries: u32, timeout: Duration, } impl Global for AppConfig {} // Set once at startup cx.set_global(AppConfig { api_endpoint: "https://api.example.com".to_string(), max_retries: 3, timeout: Duration::from_secs(30), }); // Access anywhere let config = cx.global::<AppConfig>();

2. Feature Flags

▼
rust
复制代码
#[derive(Clone)] struct FeatureFlags { enable_beta_features: bool, enable_analytics: bool, } impl Global for FeatureFlags {} impl MyComponent { fn render_beta_feature(&self, cx: &App) -> Option<impl IntoElement> { let flags = cx.global::<FeatureFlags>(); if flags.enable_beta_features { Some(div().child("Beta feature")) } else { None } } }

3. Shared Services

▼
rust
复制代码
#[derive(Clone)] struct ServiceRegistry { http_client: Arc<HttpClient>, logger: Arc<Logger>, } impl Global for ServiceRegistry {} impl MyComponent { fn fetch_data(&mut self, cx: &mut Context<Self>) { let registry = cx.global::<ServiceRegistry>(); let client = registry.http_client.clone(); cx.spawn(async move |cx| { let data = client.get("api/data").await?; // Process data... Ok::<_, anyhow::Error>(()) }).detach(); } }

Best Practices

✅ Use Arc for Shared Resources

▼
rust
复制代码
#[derive(Clone)] struct GlobalState { database: Arc<Database>, // Cheap to clone cache: Arc<RwLock<Cache>>, } impl Global for GlobalState {}

✅ Immutable by Default

Globals are read-only by default. Use interior mutability when needed:

▼
rust
复制代码
#[derive(Clone)] struct Counter { count: Arc<AtomicUsize>, } impl Global for Counter {} impl Counter { fn increment(&self) { self.count.fetch_add(1, Ordering::SeqCst); } fn get(&self) -> usize { self.count.load(Ordering::SeqCst) } }

❌ Don't: Overuse Globals

▼
rust
复制代码
// ❌ Bad: Too many globals cx.set_global(UserState { ... }); cx.set_global(CartState { ... }); cx.set_global(CheckoutState { ... }); // ✅ Good: Use entities for component state let user_entity = cx.new(|_| UserState { ... });

When to Use

Use Globals for:

  • App-wide configuration
  • Feature flags
  • Shared services (HTTP client, logger)
  • Read-only reference data

Use Entities for:

  • Component-specific state
  • State that changes frequently
  • State that needs notifications

Reference Documentation

  • API Reference: See api-reference.md
    • Global trait, set_global, update_global
    • Interior mutability patterns
    • Best practices and anti-patterns
软件开发

快捷安装

复制到终端去安装 Skill

npxskillsaddlongbridge/gpui-component--skill"gpui-global"
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
🔥热门工具
朱雀AI检测朱雀AI检测
朱雀AI检测助手朱雀AI检测助手
LovartLovart
老鱼简历老鱼简历
WritePass官网WritePass官网
面多多面多多
CursorCursor
星月写作星月写作
朱雀AI检测助手朱雀AI检测助手
零感AI零感AI
Skills 推荐
debug
debug 是一个系统化定位软件缺陷根因的技能,适用于用户报告 'fix'、'broken'、'error' 等问题或测试意外失败时。它通过可验证的假设、最小复现、因果链分析和回归测试,确保在修复前准确定位根本原因。
retrieving-developer-knowledge
retrieving-developer-knowledge 用于搜索、获取和整合 Google 官方开发者文档,覆盖 Google Cloud、AI/Gemini、Android、Chrome、Web、Flutter、Go、Firebase 等平台,适用于查询 gcloud CLI 命令、API 语法、IAM 权限或架构对比等场景。
diff-intake
diff-intake 读取 PR 或分支差异,生成结构化 YAML 变更摘要,用于下游 analytics instrumentation 技能。适用于用户提交 PR 链接、分支对比或原始 diff 后需理解变更内容、评估分析覆盖或启动埋点流程的场景。
implement
实现 Linus Dashboard 的功能开发与集成,基于 TypeScript 和 Python 构建前后端组件,遵循严格的代码规范与架构模式,完成视图、卡片、服务及传感器等模块的创建与验证,确保高质量交付并兼容 Home Assistant 生态。
pypi-server
支持从源码构建 Python 软件包,并搭建本地 PyPI 服务,实现私有包的托管与分发,使团队可通过 pip 直接安装内部开发的模块。