Macro panel_event

Source
macro_rules! panel_event {
    (
        $handler_name:ident {
            $(
                $method:ident ( $first_param:ident : $first_type:ty $(, $param:ident : $param_type:ty)* $(,)? ) -> $return_type:ty
            ),* $(,)?
        }
    ) => { ... };
}
Expand description

Macro to create a custom event handler for panels

This creates an NSWindowDelegate that responds to window events with type-safe callbacks.

This macro generates a type-safe delegate class with strongly typed callbacks. Users specify parameter types directly in the macro, and callbacks receive properly typed arguments instead of raw pointers.

§References

§Selector Generation Rules

The macro generates Objective-C selectors based on how you declare the methods:

  • Single parameter: method_name(param: Type)methodName:

    • Example: window_did_become_key(notification: &NSNotification)windowDidBecomeKey:
  • Multiple parameters: method_name(first: Type1, second: Type2)methodName:second:

    • Example: window_will_resize(window: &NSWindow, to_size: &NSSize)windowWillResize:toSize:
    • The first parameter is always anonymous (:) in the selector
    • Subsequent parameters use their names as selector parts

Parameter Naming: The macro automatically converts snake_case to camelCase:

  • to_sizetoSize
  • to_objecttoObject
  • for_clientforClient

You can use Rust’s conventional snake_case naming and the macro will generate the correct camelCase selectors to match Apple’s NSWindowDelegate signatures.

Important: Always check the references above to ensure your method names and parameter names match the exact NSWindowDelegate protocol signatures.

Usage:

use tauri_nspanel::tauri_panel;

tauri_panel! {
    panel_event!(MyPanelEventHandler {
        window_did_become_key(notification: &NSNotification) -> (),
        window_should_close(window: &NSWindow) -> Bool,
        window_will_resize(window: &NSWindow, to_size: &NSSize) -> NSSize,
        window_will_return_field_editor(sender: &NSWindow, client: Option<&AnyObject>) -> Option<Retained<NSObject>>
    })
}

let handler = MyPanelEventHandler::new();

handler.window_did_become_key(|notification| {
    // notification is already typed as &NSNotification
    println!("Window became key: {:?}", notification);
});

handler.window_should_close(|window| {
    // window is already typed as &NSWindow
    println!("Should close window: {:?}?", window);
    Bool::new(true) // Allow closing
});

§Type Specification

You must specify the exact types for parameters, including references:

  • Object types usually take references: &NSWindow, &NSNotification
  • Value types also take references: &NSSize, &NSRect, &NSPoint
  • Optional parameters: Option<&AnyObject>
  • The macro does NOT automatically add references

§Return Values

Methods must specify their return type explicitly:

  • -> () for void methods (no return value)
  • -> Bool for BOOL returns (objc2 Bool type)
  • -> Option<Retained<NSObject>> for nullable object returns
  • -> NSSize for NSSize value returns
  • -> NSRect for NSRect value returns
  • -> NSPoint for NSPoint value returns
  • Other types as needed by the delegate method

The macro handles conversion between Rust types and Objective-C types automatically.