Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 27, 2025

This PR implements a comprehensive TrayListener interface and adds macOS event monitoring capabilities to the system tray functionality.

Overview

The implementation provides a robust event system for handling tray icon interactions (clicks, right-clicks, double-clicks) that integrates seamlessly with the existing EventDispatcher architecture.

Key Components Added

Event Classes

  • TrayClickedEvent - Fired when tray icon is left-clicked
  • TrayRightClickedEvent - Fired when tray icon is right-clicked
  • TrayDoubleClickedEvent - Fired when tray icon is double-clicked

All events inherit from TypedEvent and provide GetTrayId() method for identifying which tray triggered the event.

TrayListener Interface

class TrayListener {
public:
  virtual void OnTrayClicked(const TrayClickedEvent& event) {}
  virtual void OnTrayRightClicked(const TrayRightClickedEvent& event) {}
  virtual void OnTrayDoubleClicked(const TrayDoubleClickedEvent& event) {}
};

Enhanced Tray Class

The Tray class now supports event handling through callback-based listeners:

// Add event listeners using lambdas/callbacks
auto listener_id = tray->AddClickedListener([](const TrayClickedEvent& event) {
    std::cout << "Tray " << event.GetTrayId() << " clicked!" << std::endl;
});

// Remove specific listeners
tray->RemoveListener(listener_id);

// Remove all listeners
tray->RemoveAllListeners();

macOS Implementation

Added comprehensive macOS event monitoring through:

  • TrayButtonDelegate - Objective-C delegate class that handles NSStatusItem button events
  • Event Detection - Properly detects left clicks, right clicks, and double clicks using NSEvent inspection
  • Button Configuration - Configures NSStatusBarButton to respond to multiple mouse events
  • Automatic Setup - Event handling is automatically configured when creating tray items

The macOS implementation uses the existing NSStatusItem infrastructure and extends it with proper event handling that was previously missing.

Integration

The implementation follows the repository's established patterns:

  • Uses the existing EventDispatcher system for type-safe event handling
  • Maintains consistent API design with other event-based components (like WindowManager events)
  • Provides both callback-based and inheritance-based listener patterns
  • Includes proper cleanup and resource management

Example Usage

#include "tray.h"
#include "tray_events.h"

// Create tray through TrayManager
auto tray = trayManager.Create();

// Add event listeners
tray->AddClickedListener([](const TrayClickedEvent& event) {
    // Handle left click
});

tray->AddRightClickedListener([](const TrayRightClickedEvent& event) {
    // Handle right click - could show context menu
});

// Or use inheritance-based approach
class MyTrayListener : public TrayListener {
    void OnTrayClicked(const TrayClickedEvent& event) override {
        // Custom handling
    }
};

Testing

Added comprehensive example (examples/tray_example) demonstrating the functionality and validating the API works correctly across all event types.

This implementation provides the foundation for rich tray icon interactions on macOS while maintaining compatibility with the existing codebase architecture.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits August 27, 2025 13:30
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] Implement TrayListener interface and macOS event monitoring Implement TrayListener interface and macOS event monitoring Aug 27, 2025
@Copilot Copilot AI requested a review from lijy91 August 27, 2025 13:36
Copilot finished work on behalf of lijy91 August 27, 2025 13:36
@lijy91 lijy91 closed this Aug 28, 2025
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