MacDockIconHandler.mm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Cocoa/Cocoa.h>
  9. #include <native/utilities/MacDockIconHandler.h>
  10. #include <QTimer>
  11. @interface DockIconClickEventHandler : NSObject
  12. {
  13. MacDockIconHandler* dockIconHandler;
  14. }
  15. @end
  16. @implementation DockIconClickEventHandler
  17. - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
  18. {
  19. self = [super init];
  20. if (self)
  21. {
  22. dockIconHandler = aDockIconHandler;
  23. [[NSAppleEventManager sharedAppleEventManager]
  24. setEventHandler:self
  25. andSelector:@selector(handleDockClickEvent:withReplyEvent:)
  26. forEventClass:kCoreEventClass
  27. andEventID:kAEReopenApplication];
  28. }
  29. return self;
  30. }
  31. - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
  32. {
  33. Q_UNUSED(event)
  34. Q_UNUSED(replyEvent)
  35. dockIconHandler->dockIconClicked();
  36. }
  37. @end
  38. MacDockIconHandler::MacDockIconHandler(QObject* parent)
  39. : QObject(parent)
  40. {
  41. // this has to be delayed, since Qt installs the same handler
  42. // using 0 is timeout is not enough.
  43. QTimer* t = new QTimer;
  44. t->setSingleShot(true);
  45. t->start(1);
  46. connect(t, &QTimer::timeout, this, [t, this]()
  47. {
  48. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  49. m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
  50. [pool release];
  51. t->deleteLater();
  52. });
  53. }
  54. MacDockIconHandler::~MacDockIconHandler()
  55. {
  56. [m_dockIconClickEventHandler release];
  57. }
  58. #include <native/utilities/moc_MacDockIconHandler.cpp>