Hash :
d193d51b
Author :
Date :
2024-06-17T22:46:08
Replace issue ids post migration to new issue tracker This change replaces anglebug.com/NNNN links. Bug: None Change-Id: I8ac3aec8d2a8a844b3d7b99fc0a6b2be8da31761 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5637912 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// OSXWindow.mm: Implementation of OSWindow for OSX
#include "util/osx/OSXWindow.h"
#include <set>
// Include Carbon to use the keycode names in Carbon's Event.h
#include <Carbon/Carbon.h>
#include "anglebase/no_destructor.h"
#include "common/debug.h"
// On OSX 10.12 a number of AppKit interfaces have been renamed for consistency, and the previous
// symbols tagged as deprecated. However we can't simply use the new symbols as it would break
// compilation on our automated testing that doesn't use OSX 10.12 yet. So we just ignore the
// warnings.
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// Some events such as "ShouldTerminate" are sent to the whole application so we keep a list of
// all the windows in order to forward the event to each of them. However this and calling pushEvent
// in ApplicationDelegate is inherently unsafe in a multithreaded environment.
static std::set<OSXWindow *> &AllWindows()
{
static angle::base::NoDestructor<std::set<OSXWindow *>> allWindows;
return *allWindows;
}
@interface Application : NSApplication
@end
@implementation Application
- (void)sendEvent:(NSEvent *)nsEvent
{
if ([nsEvent type] == NSApplicationDefined)
{
for (auto window : AllWindows())
{
if ([window->getNSWindow() windowNumber] == [nsEvent windowNumber])
{
Event event;
event.Type = Event::EVENT_TEST;
window->pushEvent(event);
}
}
}
[super sendEvent:nsEvent];
}
// Override internal method to try to diagnose unexpected crashes in Core Animation.
// anglebug.com/42265067
// See also:
// https://github.com/microsoft/appcenter-sdk-apple/issues/1944
// https://stackoverflow.com/questions/220159/how-do-you-print-out-a-stack-trace-to-the-console-log-in-cocoa
- (void)_crashOnException:(NSException *)exception
{
NSLog(@"*** OSXWindow aborting on exception: <%@> %@", [exception name], [exception reason]);
NSLog(@"%@", [exception callStackSymbols]);
abort();
}
@end
// The Delegate receiving application-wide events.
@interface ApplicationDelegate : NSObject
@end
@implementation ApplicationDelegate
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
Event event;
event.Type = Event::EVENT_CLOSED;
for (auto window : AllWindows())
{
window->pushEvent(event);
}
return NSTerminateCancel;
}
@end
static ApplicationDelegate *gApplicationDelegate = nil;
static bool InitializeAppKit()
{
if (NSApp != nil)
{
return true;
}
// Initialize the global variable "NSApp"
[Application sharedApplication];
// Make us appear in the dock
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// Register our global event handler
gApplicationDelegate = [[ApplicationDelegate alloc] init];
if (gApplicationDelegate == nil)
{
return false;
}
[NSApp setDelegate:static_cast<id>(gApplicationDelegate)];
// Set our status to "started" so we are not bouncing in the doc and can activate
[NSApp finishLaunching];
return true;
}
// NS's and CG's coordinate systems start at the bottom left, while OSWindow's coordinate
// system starts at the top left. This function converts the Y coordinate accordingly.
static float YCoordToFromCG(float y)
{
float screenHeight = CGDisplayBounds(CGMainDisplayID()).size.height;
return screenHeight - y;
}
// Delegate for window-wide events, note that the protocol doesn't contain anything input related.
@implementation WindowDelegate
- (id)initWithWindow:(OSXWindow *)window
{
self = [super init];
if (self != nil)
{
mWindow = window;
}
return self;
}
- (void)onOSXWindowDeleted
{
mWindow = nil;
}
- (BOOL)windowShouldClose:(id)sender
{
Event event;
event.Type = Event::EVENT_CLOSED;
mWindow->pushEvent(event);
return NO;
}
- (void)windowDidResize:(NSNotification *)notification
{
NSSize windowSize = [[mWindow->getNSWindow() contentView] frame].size;
Event event;
event.Type = Event::EVENT_RESIZED;
event.Size.Width = (int)windowSize.width;
event.Size.Height = (int)windowSize.height;
mWindow->pushEvent(event);
}
- (void)windowDidMove:(NSNotification *)notification
{
NSRect screenspace = [mWindow->getNSWindow() frame];
Event event;
event.Type = Event::EVENT_MOVED;
event.Move.X = (int)screenspace.origin.x;
event.Move.Y = (int)YCoordToFromCG(screenspace.origin.y + screenspace.size.height);
mWindow->pushEvent(event);
}
- (void)windowDidBecomeKey:(NSNotification *)notification
{
Event event;
event.Type = Event::EVENT_GAINED_FOCUS;
mWindow->pushEvent(event);
[self retain];
}
- (void)windowDidResignKey:(NSNotification *)notification
{
if (mWindow != nil)
{
Event event;
event.Type = Event::EVENT_LOST_FOCUS;
mWindow->pushEvent(event);
}
[self release];
}
@end
static Key NSCodeToKey(int keyCode)
{
// Missing KEY_PAUSE
switch (keyCode)
{
case kVK_Shift:
return KEY_LSHIFT;
case kVK_RightShift:
return KEY_RSHIFT;
case kVK_Option:
return KEY_LALT;
case kVK_RightOption:
return KEY_RALT;
case kVK_Control:
return KEY_LCONTROL;
case kVK_RightControl:
return KEY_RCONTROL;
case kVK_Command:
return KEY_LSYSTEM;
// Right System doesn't have a name, but shows up as 0x36.
case 0x36:
return KEY_RSYSTEM;
case kVK_Function:
return KEY_MENU;
case kVK_ANSI_Semicolon:
return KEY_SEMICOLON;
case kVK_ANSI_Slash:
return KEY_SLASH;
case kVK_ANSI_Equal:
return KEY_EQUAL;
case kVK_ANSI_Minus:
return KEY_DASH;
case kVK_ANSI_LeftBracket:
return KEY_LBRACKET;
case kVK_ANSI_RightBracket:
return KEY_RBRACKET;
case kVK_ANSI_Comma:
return KEY_COMMA;
case kVK_ANSI_Period:
return KEY_PERIOD;
case kVK_ANSI_Backslash:
return KEY_BACKSLASH;
case kVK_ANSI_Grave:
return KEY_TILDE;
case kVK_Escape:
return KEY_ESCAPE;
case kVK_Space:
return KEY_SPACE;
case kVK_Return:
return KEY_RETURN;
case kVK_Delete:
return KEY_BACK;
case kVK_Tab:
return KEY_TAB;
case kVK_PageUp:
return KEY_PAGEUP;
case kVK_PageDown:
return KEY_PAGEDOWN;
case kVK_End:
return KEY_END;
case kVK_Home:
return KEY_HOME;
case kVK_Help:
return KEY_INSERT;
case kVK_ForwardDelete:
return KEY_DELETE;
case kVK_ANSI_KeypadPlus:
return KEY_ADD;
case kVK_ANSI_KeypadMinus:
return KEY_SUBTRACT;
case kVK_ANSI_KeypadMultiply:
return KEY_MULTIPLY;
case kVK_ANSI_KeypadDivide:
return KEY_DIVIDE;
case kVK_F1:
return KEY_F1;
case kVK_F2:
return KEY_F2;
case kVK_F3:
return KEY_F3;
case kVK_F4:
return KEY_F4;
case kVK_F5:
return KEY_F5;
case kVK_F6:
return KEY_F6;
case kVK_F7:
return KEY_F7;
case kVK_F8:
return KEY_F8;
case kVK_F9:
return KEY_F9;
case kVK_F10:
return KEY_F10;
case kVK_F11:
return KEY_F11;
case kVK_F12:
return KEY_F12;
case kVK_F13:
return KEY_F13;
case kVK_F14:
return KEY_F14;
case kVK_F15:
return KEY_F15;
case kVK_LeftArrow:
return KEY_LEFT;
case kVK_RightArrow:
return KEY_RIGHT;
case kVK_DownArrow:
return KEY_DOWN;
case kVK_UpArrow:
return KEY_UP;
case kVK_ANSI_Keypad0:
return KEY_NUMPAD0;
case kVK_ANSI_Keypad1:
return KEY_NUMPAD1;
case kVK_ANSI_Keypad2:
return KEY_NUMPAD2;
case kVK_ANSI_Keypad3:
return KEY_NUMPAD3;
case kVK_ANSI_Keypad4:
return KEY_NUMPAD4;
case kVK_ANSI_Keypad5:
return KEY_NUMPAD5;
case kVK_ANSI_Keypad6:
return KEY_NUMPAD6;
case kVK_ANSI_Keypad7:
return KEY_NUMPAD7;
case kVK_ANSI_Keypad8:
return KEY_NUMPAD8;
case kVK_ANSI_Keypad9:
return KEY_NUMPAD9;
case kVK_ANSI_A:
return KEY_A;
case kVK_ANSI_B:
return KEY_B;
case kVK_ANSI_C:
return KEY_C;
case kVK_ANSI_D:
return KEY_D;
case kVK_ANSI_E:
return KEY_E;
case kVK_ANSI_F:
return KEY_F;
case kVK_ANSI_G:
return KEY_G;
case kVK_ANSI_H:
return KEY_H;
case kVK_ANSI_I:
return KEY_I;
case kVK_ANSI_J:
return KEY_J;
case kVK_ANSI_K:
return KEY_K;
case kVK_ANSI_L:
return KEY_L;
case kVK_ANSI_M:
return KEY_M;
case kVK_ANSI_N:
return KEY_N;
case kVK_ANSI_O:
return KEY_O;
case kVK_ANSI_P:
return KEY_P;
case kVK_ANSI_Q:
return KEY_Q;
case kVK_ANSI_R:
return KEY_R;
case kVK_ANSI_S:
return KEY_S;
case kVK_ANSI_T:
return KEY_T;
case kVK_ANSI_U:
return KEY_U;
case kVK_ANSI_V:
return KEY_V;
case kVK_ANSI_W:
return KEY_W;
case kVK_ANSI_X:
return KEY_X;
case kVK_ANSI_Y:
return KEY_Y;
case kVK_ANSI_Z:
return KEY_Z;
case kVK_ANSI_1:
return KEY_NUM1;
case kVK_ANSI_2:
return KEY_NUM2;
case kVK_ANSI_3:
return KEY_NUM3;
case kVK_ANSI_4:
return KEY_NUM4;
case kVK_ANSI_5:
return KEY_NUM5;
case kVK_ANSI_6:
return KEY_NUM6;
case kVK_ANSI_7:
return KEY_NUM7;
case kVK_ANSI_8:
return KEY_NUM8;
case kVK_ANSI_9:
return KEY_NUM9;
case kVK_ANSI_0:
return KEY_NUM0;
}
return Key(0);
}
static void AddNSKeyStateToEvent(Event *event, NSEventModifierFlags state)
{
event->Key.Shift = state & NSShiftKeyMask;
event->Key.Control = state & NSControlKeyMask;
event->Key.Alt = state & NSAlternateKeyMask;
event->Key.System = state & NSCommandKeyMask;
}
static MouseButton TranslateMouseButton(NSInteger button)
{
switch (button)
{
case 2:
return MOUSEBUTTON_MIDDLE;
case 3:
return MOUSEBUTTON_BUTTON4;
case 4:
return MOUSEBUTTON_BUTTON5;
default:
return MOUSEBUTTON_UNKNOWN;
}
}
// Delegate for NSView events, mostly the input events
@implementation ContentView
- (id)initWithWindow:(OSXWindow *)window
{
self = [super init];
if (self != nil)
{
mWindow = window;
mTrackingArea = nil;
mCurrentModifier = 0;
[self updateTrackingAreas];
}
return self;
}
- (void)dealloc
{
[mTrackingArea release];
[super dealloc];
}
- (void)updateTrackingAreas
{
if (mTrackingArea != nil)
{
[self removeTrackingArea:mTrackingArea];
[mTrackingArea release];
mTrackingArea = nil;
}
NSRect bounds = [self bounds];
NSTrackingAreaOptions flags = NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow |
NSTrackingCursorUpdate | NSTrackingInVisibleRect |
NSTrackingAssumeInside;
mTrackingArea = [[NSTrackingArea alloc] initWithRect:bounds
options:flags
owner:self
userInfo:nil];
[self addTrackingArea:mTrackingArea];
[super updateTrackingAreas];
}
// Helps with performance
- (BOOL)isOpaque
{
return YES;
}
- (BOOL)canBecomeKeyView
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
// Handle mouse events from the NSResponder protocol
- (float)translateMouseY:(float)y
{
return [self frame].size.height - y;
}
- (void)addButtonEvent:(NSEvent *)nsEvent
type:(Event::EventType)eventType
button:(MouseButton)button
{
Event event;
event.Type = eventType;
event.MouseButton.Button = button;
event.MouseButton.X = (int)[nsEvent locationInWindow].x;
event.MouseButton.Y = (int)[self translateMouseY:[nsEvent locationInWindow].y];
mWindow->pushEvent(event);
}
- (void)mouseDown:(NSEvent *)event
{
[self addButtonEvent:event type:Event::EVENT_MOUSE_BUTTON_PRESSED button:MOUSEBUTTON_LEFT];
}
- (void)mouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)mouseUp:(NSEvent *)event
{
[self addButtonEvent:event type:Event::EVENT_MOUSE_BUTTON_RELEASED button:MOUSEBUTTON_LEFT];
}
- (void)mouseMoved:(NSEvent *)nsEvent
{
Event event;
event.Type = Event::EVENT_MOUSE_MOVED;
event.MouseMove.X = (int)[nsEvent locationInWindow].x;
event.MouseMove.Y = (int)[self translateMouseY:[nsEvent locationInWindow].y];
mWindow->pushEvent(event);
}
- (void)mouseEntered:(NSEvent *)nsEvent
{
Event event;
event.Type = Event::EVENT_MOUSE_ENTERED;
mWindow->pushEvent(event);
}
- (void)mouseExited:(NSEvent *)nsEvent
{
Event event;
event.Type = Event::EVENT_MOUSE_LEFT;
mWindow->pushEvent(event);
}
- (void)rightMouseDown:(NSEvent *)event
{
[self addButtonEvent:event type:Event::EVENT_MOUSE_BUTTON_PRESSED button:MOUSEBUTTON_RIGHT];
}
- (void)rightMouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)rightMouseUp:(NSEvent *)event
{
[self addButtonEvent:event type:Event::EVENT_MOUSE_BUTTON_RELEASED button:MOUSEBUTTON_RIGHT];
}
- (void)otherMouseDown:(NSEvent *)event
{
[self addButtonEvent:event
type:Event::EVENT_MOUSE_BUTTON_PRESSED
button:TranslateMouseButton([event buttonNumber])];
}
- (void)otherMouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)otherMouseUp:(NSEvent *)event
{
[self addButtonEvent:event
type:Event::EVENT_MOUSE_BUTTON_RELEASED
button:TranslateMouseButton([event buttonNumber])];
}
- (void)scrollWheel:(NSEvent *)nsEvent
{
if (static_cast<int>([nsEvent deltaY]) == 0)
{
return;
}
Event event;
event.Type = Event::EVENT_MOUSE_WHEEL_MOVED;
event.MouseWheel.Delta = (int)[nsEvent deltaY];
mWindow->pushEvent(event);
}
// Handle key events from the NSResponder protocol
- (void)keyDown:(NSEvent *)nsEvent
{
// TODO(cwallez) also send text events
Event event;
event.Type = Event::EVENT_KEY_PRESSED;
event.Key.Code = NSCodeToKey([nsEvent keyCode]);
AddNSKeyStateToEvent(&event, [nsEvent modifierFlags]);
mWindow->pushEvent(event);
}
- (void)keyUp:(NSEvent *)nsEvent
{
Event event;
event.Type = Event::EVENT_KEY_RELEASED;
event.Key.Code = NSCodeToKey([nsEvent keyCode]);
AddNSKeyStateToEvent(&event, [nsEvent modifierFlags]);
mWindow->pushEvent(event);
}
// Modifier keys do not trigger keyUp/Down events but only flagsChanged events.
- (void)flagsChanged:(NSEvent *)nsEvent
{
Event event;
// Guess if the key has been pressed or released with the change of modifiers
// It currently doesn't work when modifiers are unchanged, such as when pressing
// both shift keys. GLFW has a solution for this but it requires tracking the
// state of the keys. Implementing this is still TODO(cwallez)
int modifier = [nsEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
if (modifier < mCurrentModifier)
{
event.Type = Event::EVENT_KEY_RELEASED;
}
else
{
event.Type = Event::EVENT_KEY_PRESSED;
}
mCurrentModifier = modifier;
event.Key.Code = NSCodeToKey([nsEvent keyCode]);
AddNSKeyStateToEvent(&event, [nsEvent modifierFlags]);
mWindow->pushEvent(event);
}
@end
OSXWindow::OSXWindow() : mWindow(nil), mDelegate(nil), mView(nil) {}
OSXWindow::~OSXWindow()
{
destroy();
}
bool OSXWindow::initializeImpl(const std::string &name, int width, int height)
{
if (!InitializeAppKit())
{
return false;
}
unsigned int styleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask |
NSMiniaturizableWindowMask;
mWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
if (mWindow == nil)
{
return false;
}
mDelegate = [[WindowDelegate alloc] initWithWindow:this];
if (mDelegate == nil)
{
return false;
}
[mWindow setDelegate:static_cast<id>(mDelegate)];
mView = [[ContentView alloc] initWithWindow:this];
if (mView == nil)
{
return false;
}
[mView setWantsLayer:YES];
// Disable scaling for this view. If scaling is enabled, the metal backend's
// frame buffer's size will be this window's size multiplied by contentScale.
// It will cause inconsistent testing & example apps' results.
mView.layer.contentsScale = 1;
[mWindow setContentView:mView];
[mWindow setTitle:[NSString stringWithUTF8String:name.c_str()]];
[mWindow setAcceptsMouseMovedEvents:YES];
[mWindow center];
[NSApp activateIgnoringOtherApps:YES];
mX = 0;
mY = 0;
mWidth = width;
mHeight = height;
AllWindows().insert(this);
return true;
}
void OSXWindow::disableErrorMessageDialog() {}
void OSXWindow::destroy()
{
AllWindows().erase(this);
[mView release];
mView = nil;
[mDelegate onOSXWindowDeleted];
[mDelegate release];
mDelegate = nil;
// NSWindow won't be completely released unless its content view is set to nil:
[mWindow setContentView:nil];
[mWindow release];
mWindow = nil;
}
void OSXWindow::resetNativeWindow() {}
EGLNativeWindowType OSXWindow::getNativeWindow() const
{
return [mView layer];
}
EGLNativeDisplayType OSXWindow::getNativeDisplay() const
{
// TODO(cwallez): implement it once we have defined what EGLNativeDisplayType is
return static_cast<EGLNativeDisplayType>(0);
}
void OSXWindow::messageLoop()
{
@autoreleasepool
{
while (true)
{
// TODO(http://anglebug.com/42265067): @try/@catch is a workaround for
// exceptions being thrown from Cocoa-internal function
// NS_setFlushesWithDisplayLink starting in macOS 11.
@try
{
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil)
{
break;
}
if ([event type] == NSAppKitDefined)
{
continue;
}
[NSApp sendEvent:event];
}
@catch (NSException *localException)
{
NSLog(@"*** OSXWindow discarding exception: <%@> %@", [localException name],
[localException reason]);
}
}
}
}
void OSXWindow::setMousePosition(int x, int y)
{
y = (int)([mWindow frame].size.height) - y - 1;
NSPoint screenspace;
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
screenspace = [mWindow convertBaseToScreen:NSMakePoint(x, y)];
#else
screenspace = [mWindow convertRectToScreen:NSMakeRect(x, y, 0, 0)].origin;
#endif
CGWarpMouseCursorPosition(CGPointMake(screenspace.x, YCoordToFromCG(screenspace.y)));
}
bool OSXWindow::setOrientation(int width, int height)
{
UNIMPLEMENTED();
return false;
}
bool OSXWindow::setPosition(int x, int y)
{
// Given CG and NS's coordinate system, the "Y" position of a window is the Y coordinate
// of the bottom of the window.
int newBottom = (int)([mWindow frame].size.height) + y;
NSRect emptyRect = NSMakeRect(x, YCoordToFromCG(newBottom), 0, 0);
[mWindow setFrameOrigin:[mWindow frameRectForContentRect:emptyRect].origin];
return true;
}
bool OSXWindow::resize(int width, int height)
{
[mWindow setContentSize:NSMakeSize(width, height)];
return true;
}
void OSXWindow::setVisible(bool isVisible)
{
if (isVisible)
{
[mWindow makeKeyAndOrderFront:nil];
}
else
{
[mWindow orderOut:nil];
}
}
void OSXWindow::signalTestEvent()
{
@autoreleasepool
{
NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
location:NSMakePoint(0, 0)
modifierFlags:0
timestamp:0.0
windowNumber:[mWindow windowNumber]
context:nil
subtype:0
data1:0
data2:0];
[NSApp postEvent:event atStart:YES];
}
}
NSWindow *OSXWindow::getNSWindow() const
{
return mWindow;
}
// static
OSWindow *OSWindow::New()
{
return new OSXWindow;
}