/*

 This manual test can be used to demonstrate that mouse clicks "just" inside (by 0-3 pixels)
 a window are no longer delivered on macOS 14. This problem is observed on all 4 edges of the window.

 To build and then test
 clang -framework Foundation -framework Cocoa MouseTest.m -o mousetest
 ./mousetest

  Once you start the test, a window appears with a blue panel to test mouse events.
  Start moving the mouse (horizontally across the window) very slowly one pixel at a time
  from outside the left edge of the window towards the edge of the window and then inside the window
  to about 5 pixels inside.  After each one pixel move perform a manual mouse click.
  If the click is received the application will print a message in the terminal.

  Optional: you can repeat from the bottom of the window moving upwards.

  Observe the x,y locations recorded for mouseUp & mouseDown events when test is run
  on older macOS version vs macOS 14.
  For older macOS versions mouse events are generated from x=0 but on the macOS 14
  mouse events start only when x=3 pixels or more (or y>3) in the upwards case.
  The pointer is clearly well inside the window at this point.
*/
 
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController

- (void)printLocation:(NSPoint)location;
@end

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (nonatomic, strong) NSWindow *window;
@property (nonatomic, strong) NSViewController *controller;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    int mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
    _window = [[NSWindow alloc] initWithContentRect:NSMakeRect(500, 500, 300, 200) styleMask:mask backing:2 defer:NO];
    [_window setBackgroundColor:NSColor.systemTealColor];
    [_window setTitle:@"Mouse Events"];
    _controller = [[ViewController alloc] init];

    [_window.contentView addSubview:_controller.view];
    [_window showsResizeIndicator];
    [_window isReleasedWhenClosed];
    [_window makeKeyWindow];
    [_window orderFrontRegardless];
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
    return YES;
}

@end

@implementation ViewController

-(void)loadView {
    self.view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 300, 200)];
}


- (void)mouseDown:(NSEvent *)event
{
    NSLog(@"mouseDown called");
    [self printLocation:[event locationInWindow]];
    NSLog(@"----------------------");
}

- (void)mouseUp:(NSEvent *)event
{
    NSLog(@"mouseUp called");
    [self printLocation:[event locationInWindow]];
    NSLog(@"----------------------");
}

- (void)printLocation:(NSPoint)location
{
    NSLog(@"x:%f y:%f",location.x, location.y);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        AppDelegate *delegate = [[AppDelegate alloc] init];
        NSApplication *app = [NSApplication sharedApplication];
        app.delegate = delegate;
        NSApplicationMain(argc, argv);
    }
    return 0;
}

