Implement Generic Event Handling System for C++#13
Merged
Conversation
Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com>
Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] 设计并实现一个通用的事件处理机制,仅需提供 C++ 版本的实现。
Implement Generic Event Handling System for C++
Aug 26, 2025
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
DisplayManageruses an observer pattern withDisplayListenerinterfaceKeyboardEventHandleruses callback-based handling withstd::functionThese 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 identificationEventListener&TypedEventListener<T>: Observer pattern interfaces for event handlingCallbackEventListener<T>: Wrapper forstd::functioncallbacksEventDispatcher: Thread-safe event dispatcher supporting both synchronous and asynchronous dispatchEventListenerGuard: RAII helper for automatic listener cleanupKey Features
Usage Examples
Observer pattern:
Callback pattern:
Event dispatch:
Integration
The system is designed to work alongside existing patterns without breaking changes. The
EventAwareDisplayManagerexample 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:
All tests pass and demonstrate the system's reliability and performance.
Documentation
Complete documentation is provided in
docs/EVENT_SYSTEM.mdwith 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.