Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 26, 2025

This PR introduces a comprehensive, type-safe generic event handling system for C++ that supports both observer patterns and callback-based event handling. The system is designed to unify and extend the existing event handling patterns found in the codebase.

Background

The existing codebase uses different event handling approaches:

  • DisplayManager uses an observer pattern with DisplayListener interface
  • KeyboardEventHandler uses callback-based handling with std::function

These patterns work well but are not generic or unified, leading to code duplication and inconsistent approaches across different components.

Solution

The new generic event system provides:

Core Components

  • Event & TypedEvent<T>: Base classes for type-safe events with automatic type identification
  • EventListener & TypedEventListener<T>: Observer pattern interfaces for event handling
  • CallbackEventListener<T>: Wrapper for std::function callbacks
  • EventDispatcher: Thread-safe event dispatcher supporting both synchronous and asynchronous dispatch
  • EventListenerGuard: RAII helper for automatic listener cleanup

Key Features

  • Type Safety: Template-based design ensures compile-time type checking
  • Thread Safety: Mutex-protected operations with efficient async event processing
  • Multiple Patterns: Supports both observer pattern and lambda/callback approaches
  • Memory Management: Automatic cleanup and RAII-based listener management
  • Performance: Minimal overhead for sync dispatch, background thread for async processing
  • Exception Safety: Graceful handling of exceptions in event listeners

Usage Examples

Observer pattern:

class MyObserver : public TypedEventListener<KeyPressedEvent> {
  void OnTypedEvent(const KeyPressedEvent& event) override {
    // Handle event
  }
};

EventDispatcher dispatcher;
MyObserver observer;
dispatcher.AddListener<KeyPressedEvent>(&observer);

Callback pattern:

auto guard = AddScopedListener<KeyPressedEvent>(dispatcher,
    [](const KeyPressedEvent& event) {
        // Handle event with automatic cleanup
    });

Event dispatch:

// Synchronous - immediate dispatch
dispatcher.DispatchSync<KeyPressedEvent>(keycode);

// Asynchronous - background thread processing
dispatcher.DispatchAsync<KeyPressedEvent>(keycode);

Integration

The system is designed to work alongside existing patterns without breaking changes. The EventAwareDisplayManager example shows how existing components can be enhanced to use the new system while maintaining backward compatibility.

Common predefined events are provided (DisplayAddedEvent, KeyPressedEvent, ApplicationStartedEvent, etc.) that can be used immediately or serve as examples for custom events.

Testing

The implementation includes comprehensive tests covering:

  • Basic observer and callback patterns
  • Multi-event observers
  • Synchronous and asynchronous dispatch
  • Listener management and cleanup
  • Thread safety and exception handling
  • RAII-based automatic cleanup

All tests pass and demonstrate the system's reliability and performance.

Documentation

Complete documentation is provided in docs/EVENT_SYSTEM.md with usage examples, best practices, and integration guidance.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 26, 2025 23:28
Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com>
Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] 设计并实现一个通用的事件处理机制,仅需提供 C++ 版本的实现。 Implement Generic Event Handling System for C++ Aug 26, 2025
@Copilot Copilot AI requested a review from lijy91 August 26, 2025 23:31
Copilot finished work on behalf of lijy91 August 26, 2025 23:31
- Delete event_system and related demo/test files - Remove common_events
and integration examples - Update CMakeLists.txt to require C++17 -
Update nativeapi.h to use event_dispatcher/event headers - Implement
GetGlobalEventDispatcher in event_dispatcher.cpp/h
@lijy91 lijy91 marked this pull request as ready for review August 27, 2025 12:00
@lijy91 lijy91 merged commit bdc2c44 into main Aug 27, 2025
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants