Hash :
7d6aa630
Author :
Date :
2025-08-13T15:50:24
Reland: Rename enums to fix GCC -Wchanges-meaning error This is a reland of commit dc24ecca9ed920c0269e9d62dfcac4b3f3e5a4d4 Original change's description: > Renamed enums Key and MouseButton to enum class KeyType and > MouseButtonType to avoid name conflicts with union members in > Event.h on GCC, and to follow the ANGLE style guide preference > for scoped enums. > > BUG: angleproject:438226513 > Change-Id: I8892b438b3b05a3ce4e14f8c0769b81722688da8 > Signed-off-by: Sungyong Choi <sywow.choi@samsung.com> > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6845484 > Commit-Queue: Geoff Lang <geofflang@chromium.org> > Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> > Reviewed-by: Geoff Lang <geofflang@chromium.org> BUG: angleproject:438226513 Change-Id: Ifc1a7bad661881a1a71bd5380fe431270bc68686 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6874206 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@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 830 831 832 833 834 835 836 837
//
// 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 angle::KeyType NSCodeToKey(int keyCode)
{
// Missing KEY_PAUSE
switch (keyCode)
{
case kVK_Shift:
return angle::KeyType::LSHIFT;
case kVK_RightShift:
return angle::KeyType::RSHIFT;
case kVK_Option:
return angle::KeyType::LALT;
case kVK_RightOption:
return angle::KeyType::RALT;
case kVK_Control:
return angle::KeyType::LCONTROL;
case kVK_RightControl:
return angle::KeyType::RCONTROL;
case kVK_Command:
return angle::KeyType::LSYSTEM;
// Right System doesn't have a name, but shows up as 0x36.
case 0x36:
return angle::KeyType::RSYSTEM;
case kVK_Function:
return angle::KeyType::MENU;
case kVK_ANSI_Semicolon:
return angle::KeyType::SEMICOLON;
case kVK_ANSI_Slash:
return angle::KeyType::SLASH;
case kVK_ANSI_Equal:
return angle::KeyType::EQUAL;
case kVK_ANSI_Minus:
return angle::KeyType::DASH;
case kVK_ANSI_LeftBracket:
return angle::KeyType::LBRACKET;
case kVK_ANSI_RightBracket:
return angle::KeyType::RBRACKET;
case kVK_ANSI_Comma:
return angle::KeyType::COMMA;
case kVK_ANSI_Period:
return angle::KeyType::PERIOD;
case kVK_ANSI_Backslash:
return angle::KeyType::BACKSLASH;
case kVK_ANSI_Grave:
return angle::KeyType::TILDE;
case kVK_Escape:
return angle::KeyType::ESCAPE;
case kVK_Space:
return angle::KeyType::SPACE;
case kVK_Return:
return angle::KeyType::RETURN;
case kVK_Delete:
return angle::KeyType::BACK;
case kVK_Tab:
return angle::KeyType::TAB;
case kVK_PageUp:
return angle::KeyType::PAGEUP;
case kVK_PageDown:
return angle::KeyType::PAGEDOWN;
case kVK_End:
return angle::KeyType::END;
case kVK_Home:
return angle::KeyType::HOME;
case kVK_Help:
return angle::KeyType::INSERT;
case kVK_ForwardDelete:
return angle::KeyType::DEL;
case kVK_ANSI_KeypadPlus:
return angle::KeyType::ADD;
case kVK_ANSI_KeypadMinus:
return angle::KeyType::SUBTRACT;
case kVK_ANSI_KeypadMultiply:
return angle::KeyType::MULTIPLY;
case kVK_ANSI_KeypadDivide:
return angle::KeyType::DIVIDE;
case kVK_F1:
return angle::KeyType::F1;
case kVK_F2:
return angle::KeyType::F2;
case kVK_F3:
return angle::KeyType::F3;
case kVK_F4:
return angle::KeyType::F4;
case kVK_F5:
return angle::KeyType::F5;
case kVK_F6:
return angle::KeyType::F6;
case kVK_F7:
return angle::KeyType::F7;
case kVK_F8:
return angle::KeyType::F8;
case kVK_F9:
return angle::KeyType::F9;
case kVK_F10:
return angle::KeyType::F10;
case kVK_F11:
return angle::KeyType::F11;
case kVK_F12:
return angle::KeyType::F12;
case kVK_F13:
return angle::KeyType::F13;
case kVK_F14:
return angle::KeyType::F14;
case kVK_F15:
return angle::KeyType::F15;
case kVK_LeftArrow:
return angle::KeyType::LEFT;
case kVK_RightArrow:
return angle::KeyType::RIGHT;
case kVK_DownArrow:
return angle::KeyType::DOWN;
case kVK_UpArrow:
return angle::KeyType::UP;
case kVK_ANSI_Keypad0:
return angle::KeyType::NUMPAD0;
case kVK_ANSI_Keypad1:
return angle::KeyType::NUMPAD1;
case kVK_ANSI_Keypad2:
return angle::KeyType::NUMPAD2;
case kVK_ANSI_Keypad3:
return angle::KeyType::NUMPAD3;
case kVK_ANSI_Keypad4:
return angle::KeyType::NUMPAD4;
case kVK_ANSI_Keypad5:
return angle::KeyType::NUMPAD5;
case kVK_ANSI_Keypad6:
return angle::KeyType::NUMPAD6;
case kVK_ANSI_Keypad7:
return angle::KeyType::NUMPAD7;
case kVK_ANSI_Keypad8:
return angle::KeyType::NUMPAD8;
case kVK_ANSI_Keypad9:
return angle::KeyType::NUMPAD9;
case kVK_ANSI_A:
return angle::KeyType::A;
case kVK_ANSI_B:
return angle::KeyType::B;
case kVK_ANSI_C:
return angle::KeyType::C;
case kVK_ANSI_D:
return angle::KeyType::D;
case kVK_ANSI_E:
return angle::KeyType::E;
case kVK_ANSI_F:
return angle::KeyType::F;
case kVK_ANSI_G:
return angle::KeyType::G;
case kVK_ANSI_H:
return angle::KeyType::H;
case kVK_ANSI_I:
return angle::KeyType::I;
case kVK_ANSI_J:
return angle::KeyType::J;
case kVK_ANSI_K:
return angle::KeyType::K;
case kVK_ANSI_L:
return angle::KeyType::L;
case kVK_ANSI_M:
return angle::KeyType::M;
case kVK_ANSI_N:
return angle::KeyType::N;
case kVK_ANSI_O:
return angle::KeyType::O;
case kVK_ANSI_P:
return angle::KeyType::P;
case kVK_ANSI_Q:
return angle::KeyType::Q;
case kVK_ANSI_R:
return angle::KeyType::R;
case kVK_ANSI_S:
return angle::KeyType::S;
case kVK_ANSI_T:
return angle::KeyType::T;
case kVK_ANSI_U:
return angle::KeyType::U;
case kVK_ANSI_V:
return angle::KeyType::V;
case kVK_ANSI_W:
return angle::KeyType::W;
case kVK_ANSI_X:
return angle::KeyType::X;
case kVK_ANSI_Y:
return angle::KeyType::Y;
case kVK_ANSI_Z:
return angle::KeyType::Z;
case kVK_ANSI_1:
return angle::KeyType::NUM1;
case kVK_ANSI_2:
return angle::KeyType::NUM2;
case kVK_ANSI_3:
return angle::KeyType::NUM3;
case kVK_ANSI_4:
return angle::KeyType::NUM4;
case kVK_ANSI_5:
return angle::KeyType::NUM5;
case kVK_ANSI_6:
return angle::KeyType::NUM6;
case kVK_ANSI_7:
return angle::KeyType::NUM7;
case kVK_ANSI_8:
return angle::KeyType::NUM8;
case kVK_ANSI_9:
return angle::KeyType::NUM9;
case kVK_ANSI_0:
return angle::KeyType::NUM0;
}
return angle::KeyType(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 angle::MouseButtonType TranslateMouseButton(NSInteger button)
{
switch (button)
{
case 2:
return angle::MouseButtonType::MIDDLE;
case 3:
return angle::MouseButtonType::BUTTON4;
case 4:
return angle::MouseButtonType::BUTTON5;
default:
return angle::MouseButtonType::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:(angle::MouseButtonType)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:angle::MouseButtonType::LEFT];
}
- (void)mouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)mouseUp:(NSEvent *)event
{
[self addButtonEvent:event
type:Event::EVENT_MOUSE_BUTTON_RELEASED
button:angle::MouseButtonType::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:angle::MouseButtonType::RIGHT];
}
- (void)rightMouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)rightMouseUp:(NSEvent *)event
{
[self addButtonEvent:event
type:Event::EVENT_MOUSE_BUTTON_RELEASED
button:angle::MouseButtonType::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;
}