🎯

gpui-event

🎯Skill

from longbridge/gpui-component

VibeIndex|
What it does

Enables type-safe event handling, subscriptions, and observations for coordinating component interactions in GPUI applications.

gpui-event

Installation

Install skill:
npx skills add https://github.com/longbridge/gpui-component --skill gpui-event
23
Last UpdatedJan 26, 2026

Skill Details

SKILL.md

Event handling and subscriptions in GPUI. Use when implementing events, observers, or event-driven patterns. Supports custom events, entity observations, and event subscriptions for coordinating between components.

Overview

GPUI provides event system for component coordination:

Event Mechanisms:

  • Custom Events: Define and emit type-safe events
  • Observations: React to entity state changes
  • Subscriptions: Listen to events from other entities
  • Global Events: App-wide event handling

Quick Start

Define and Emit Events

```rust

#[derive(Clone)]

enum MyEvent {

DataUpdated(String),

ActionTriggered,

}

impl MyComponent {

fn update_data(&mut self, data: String, cx: &mut Context) {

self.data = data.clone();

// Emit event

cx.emit(MyEvent::DataUpdated(data));

cx.notify();

}

}

```

Subscribe to Events

```rust

impl Listener {

fn new(source: Entity, cx: &mut App) -> Entity {

cx.new(|cx| {

// Subscribe to events

cx.subscribe(&source, |this, emitter, event: &MyEvent, cx| {

match event {

MyEvent::DataUpdated(data) => {

this.handle_update(data.clone(), cx);

}

MyEvent::ActionTriggered => {

this.handle_action(cx);

}

}

}).detach();

Self { source }

})

}

}

```

Observe Entity Changes

```rust

impl Observer {

fn new(target: Entity, cx: &mut App) -> Entity {

cx.new(|cx| {

// Observe entity for any changes

cx.observe(&target, |this, observed, cx| {

// Called when observed.update() calls cx.notify()

println!("Target changed");

cx.notify();

}).detach();

Self { target }

})

}

}

```

Common Patterns

1. Parent-Child Communication

```rust

// Parent emits events

impl Parent {

fn notify_children(&mut self, cx: &mut Context) {

cx.emit(ParentEvent::Updated);

cx.notify();

}

}

// Children subscribe

impl Child {

fn new(parent: Entity, cx: &mut App) -> Entity {

cx.new(|cx| {

cx.subscribe(&parent, |this, parent, event, cx| {

this.handle_parent_event(event, cx);

}).detach();

Self { parent }

})

}

}

```

2. Global Event Broadcasting

```rust

struct EventBus {

listeners: Vec>,

}

impl EventBus {

fn broadcast(&mut self, event: GlobalEvent, cx: &mut Context) {

self.listeners.retain(|weak| {

weak.update(cx, |listener, cx| {

listener.on_event(&event, cx);

}).is_ok()

});

}

}

```

3. Observer Pattern

```rust

cx.observe(&entity, |this, observed, cx| {

// React to any state change

let state = observed.read(cx);

this.sync_with_state(state, cx);

}).detach();

```

Best Practices

βœ… Detach Subscriptions

```rust

// βœ… Detach to keep alive

cx.subscribe(&entity, |this, source, event, cx| {

// Handle event

}).detach();

```

βœ… Clean Event Types

```rust

#[derive(Clone)]

enum AppEvent {

DataChanged { id: usize, value: String },

ActionPerformed(ActionType),

Error(String),

}

```

❌ Avoid Event Loops

```rust

// ❌ Don't create mutual subscriptions

entity1.subscribe(entity2) β†’ emits event

entity2.subscribe(entity1) β†’ emits event β†’ infinite loop!

```

Reference Documentation

  • API Reference: See [api-reference.md](references/api-reference.md)

- Event definition, emission, subscriptions

- Observations, global events

- Subscription lifecycle

  • Patterns: See [patterns.md](references/patterns.md)

- Event-driven architectures

- Communication patterns

- Best practices and pitfalls

More from this repository10

🎯
gpui-style-guide🎯Skill

Enforces consistent GPUI component design patterns by providing a comprehensive style guide for creating and reviewing Rust UI components.

🎯
gpui-async🎯Skill

Enables asynchronous operations and background tasks in GPUI, allowing seamless coordination between UI updates and concurrent computations.

🎯
gpui-layout-and-style🎯Skill

Provides CSS-like layout and styling for Rust GPUI components using type-safe, chainable methods for flexbox, sizing, colors, and spacing.

🎯
gpui-entity🎯Skill

Manages application state through safe, concurrent entities with read, update, and weak reference capabilities for reactive and async-friendly state handling in GPUI.

🎯
gpui-focus-handle🎯Skill

Enables keyboard-driven focus management and navigation between focusable elements in GPUI user interfaces.

🎯
gpui-action🎯Skill

Defines and manages keyboard-driven actions and key bindings for declarative UI interactions in GPUI applications.

🎯
gpui-context🎯Skill

gpui-context skill from longbridge/gpui-component

🎯
gpui-test🎯Skill

Enables comprehensive testing of GPUI applications with deterministic, single-threaded test execution for UI components and async operations.

🎯
gpui-element🎯Skill

Enables low-level, performance-critical custom UI element creation with precise control over layout, painting, and interaction phases in GPUI.

🎯
gpui-global🎯Skill

Manages global application state and configuration in GPUI, enabling centralized data access and sharing across components.