imgui_impl_osx.mm 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. // dear imgui: Platform Backend for OSX / Cocoa
  2. // This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..)
  3. // - Not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac.
  4. // - Requires linking with the GameController framework ("-framework GameController").
  5. // Implemented features:
  6. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  7. // [X] Platform: Mouse support. Can discriminate Mouse/Pen.
  8. // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy kVK_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
  9. // [X] Platform: OSX clipboard is supported within core Dear ImGui (no specific code in this backend).
  10. // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  11. // [X] Platform: IME support.
  12. // [X] Platform: Multi-viewport / platform windows.
  13. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  14. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  15. // Learn about Dear ImGui:
  16. // - FAQ https://dearimgui.com/faq
  17. // - Getting Started https://dearimgui.com/getting-started
  18. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  19. // - Introduction, links and more at the top of imgui.cpp
  20. #import "imgui.h"
  21. #ifndef IMGUI_DISABLE
  22. #import "imgui_impl_osx.h"
  23. #import <Cocoa/Cocoa.h>
  24. #import <Carbon/Carbon.h>
  25. #import <GameController/GameController.h>
  26. #import <time.h>
  27. // CHANGELOG
  28. // (minor and older changes stripped away, please see git history for details)
  29. // 2023-XX-XX: Added support for multiple windows via the ImGuiPlatformIO interface.
  30. // 2023-04-09: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_Pen.
  31. // 2023-02-01: Fixed scroll wheel scaling for devices emitting events with hasPreciseScrollingDeltas==false (e.g. non-Apple mices).
  32. // 2022-11-02: Fixed mouse coordinates before clicking the host window.
  33. // 2022-10-06: Fixed mouse inputs on flipped views.
  34. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
  35. // 2022-05-03: Inputs: Removed ImGui_ImplOSX_HandleEvent() from backend API in favor of backend automatically handling event capture.
  36. // 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts.
  37. // 2022-03-22: Inputs: Monitor NSKeyUp events to catch missing keyUp for key when user press Cmd + key
  38. // 2022-02-07: Inputs: Forward keyDown/keyUp events to OS when unused by dear imgui.
  39. // 2022-01-31: Fixed building with old Xcode versions that are missing gamepad features.
  40. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
  41. // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
  42. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
  43. // 2022-01-12: Inputs: Added basic Platform IME support, hooking the io.SetPlatformImeDataFn() function.
  44. // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
  45. // 2021-12-13: *BREAKING CHANGE* Add NSView parameter to ImGui_ImplOSX_Init(). Generally fix keyboard support. Using kVK_* codes for keyboard keys.
  46. // 2021-12-13: Add game controller support.
  47. // 2021-09-21: Use mach_absolute_time as CFAbsoluteTimeGetCurrent can jump backwards.
  48. // 2021-08-17: Calling io.AddFocusEvent() on NSApplicationDidBecomeActiveNotification/NSApplicationDidResignActiveNotification events.
  49. // 2021-06-23: Inputs: Added a fix for shortcuts using CTRL key instead of CMD key.
  50. // 2021-04-19: Inputs: Added a fix for keys remaining stuck in pressed state when CMD-tabbing into different application.
  51. // 2021-01-27: Inputs: Added a fix for mouse position not being reported when mouse buttons other than left one are down.
  52. // 2020-10-28: Inputs: Added a fix for handling keypad-enter key.
  53. // 2020-05-25: Inputs: Added a fix for missing trackpad clicks when done with "soft tap".
  54. // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
  55. // 2019-10-11: Inputs: Fix using Backspace key.
  56. // 2019-07-21: Re-added clipboard handlers as they are not enabled by default in core imgui.cpp (reverted 2019-05-18 change).
  57. // 2019-05-28: Inputs: Added mouse cursor shape and visibility support.
  58. // 2019-05-18: Misc: Removed clipboard handlers as they are now supported by core imgui.cpp.
  59. // 2019-05-11: Inputs: Don't filter character values before calling AddInputCharacter() apart from 0xF700..0xFFFF range.
  60. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  61. // 2018-07-07: Initial version.
  62. #define APPLE_HAS_BUTTON_OPTIONS (__IPHONE_OS_VERSION_MIN_REQUIRED >= 130000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500 || __TV_OS_VERSION_MIN_REQUIRED >= 130000)
  63. #define APPLE_HAS_CONTROLLER (__IPHONE_OS_VERSION_MIN_REQUIRED >= 140000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 || __TV_OS_VERSION_MIN_REQUIRED >= 140000)
  64. #define APPLE_HAS_THUMBSTICKS (__IPHONE_OS_VERSION_MIN_REQUIRED >= 120100 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101401 || __TV_OS_VERSION_MIN_REQUIRED >= 120100)
  65. @class ImGuiObserver;
  66. @class KeyEventResponder;
  67. // Data
  68. struct ImGui_ImplOSX_Data
  69. {
  70. CFTimeInterval Time;
  71. NSCursor* MouseCursors[ImGuiMouseCursor_COUNT];
  72. bool MouseCursorHidden;
  73. ImGuiObserver* Observer;
  74. KeyEventResponder* KeyEventResponder;
  75. NSTextInputContext* InputContext;
  76. id Monitor;
  77. NSWindow* Window;
  78. ImGui_ImplOSX_Data() { memset(this, 0, sizeof(*this)); }
  79. };
  80. static ImGui_ImplOSX_Data* ImGui_ImplOSX_CreateBackendData() { return IM_NEW(ImGui_ImplOSX_Data)(); }
  81. static ImGui_ImplOSX_Data* ImGui_ImplOSX_GetBackendData() { return (ImGui_ImplOSX_Data*)ImGui::GetIO().BackendPlatformUserData; }
  82. static void ImGui_ImplOSX_DestroyBackendData() { IM_DELETE(ImGui_ImplOSX_GetBackendData()); }
  83. static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return (CFTimeInterval)(double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1e9); }
  84. // Forward Declarations
  85. static void ImGui_ImplOSX_InitPlatformInterface();
  86. static void ImGui_ImplOSX_ShutdownPlatformInterface();
  87. static void ImGui_ImplOSX_UpdateMonitors();
  88. static void ImGui_ImplOSX_AddTrackingArea(NSView* _Nonnull view);
  89. static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view);
  90. // Undocumented methods for creating cursors.
  91. @interface NSCursor()
  92. + (id)_windowResizeNorthWestSouthEastCursor;
  93. + (id)_windowResizeNorthEastSouthWestCursor;
  94. + (id)_windowResizeNorthSouthCursor;
  95. + (id)_windowResizeEastWestCursor;
  96. @end
  97. /**
  98. KeyEventResponder implements the NSTextInputClient protocol as is required by the macOS text input manager.
  99. The macOS text input manager is invoked by calling the interpretKeyEvents method from the keyDown method.
  100. Keyboard events are then evaluated by the macOS input manager and valid text input is passed back via the
  101. insertText:replacementRange method.
  102. This is the same approach employed by other cross-platform libraries such as SDL2:
  103. https://github.com/spurious/SDL-mirror/blob/e17aacbd09e65a4fd1e166621e011e581fb017a8/src/video/cocoa/SDL_cocoakeyboard.m#L53
  104. and GLFW:
  105. https://github.com/glfw/glfw/blob/b55a517ae0c7b5127dffa79a64f5406021bf9076/src/cocoa_window.m#L722-L723
  106. */
  107. @interface KeyEventResponder: NSView<NSTextInputClient>
  108. @end
  109. @implementation KeyEventResponder
  110. {
  111. float _posX;
  112. float _posY;
  113. NSRect _imeRect;
  114. }
  115. #pragma mark - Public
  116. - (void)setImePosX:(float)posX imePosY:(float)posY
  117. {
  118. _posX = posX;
  119. _posY = posY;
  120. }
  121. - (void)updateImePosWithView:(NSView *)view
  122. {
  123. NSWindow *window = view.window;
  124. if (!window)
  125. return;
  126. NSRect contentRect = [window contentRectForFrameRect:window.frame];
  127. NSRect rect = NSMakeRect(_posX, contentRect.size.height - _posY, 0, 0);
  128. _imeRect = [window convertRectToScreen:rect];
  129. }
  130. - (void)viewDidMoveToWindow
  131. {
  132. // Ensure self is a first responder to receive the input events.
  133. [self.window makeFirstResponder:self];
  134. }
  135. - (void)keyDown:(NSEvent*)event
  136. {
  137. if (!ImGui_ImplOSX_HandleEvent(event, self))
  138. [super keyDown:event];
  139. // Call to the macOS input manager system.
  140. [self interpretKeyEvents:@[event]];
  141. }
  142. - (void)keyUp:(NSEvent*)event
  143. {
  144. if (!ImGui_ImplOSX_HandleEvent(event, self))
  145. [super keyUp:event];
  146. }
  147. - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange
  148. {
  149. ImGuiIO& io = ImGui::GetIO();
  150. NSString* characters;
  151. if ([aString isKindOfClass:[NSAttributedString class]])
  152. characters = [aString string];
  153. else
  154. characters = (NSString*)aString;
  155. io.AddInputCharactersUTF8(characters.UTF8String);
  156. }
  157. - (BOOL)acceptsFirstResponder
  158. {
  159. return YES;
  160. }
  161. - (void)doCommandBySelector:(SEL)myselector
  162. {
  163. }
  164. - (nullable NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range actualRange:(nullable NSRangePointer)actualRange
  165. {
  166. return nil;
  167. }
  168. - (NSUInteger)characterIndexForPoint:(NSPoint)point
  169. {
  170. return 0;
  171. }
  172. - (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(nullable NSRangePointer)actualRange
  173. {
  174. return _imeRect;
  175. }
  176. - (BOOL)hasMarkedText
  177. {
  178. return NO;
  179. }
  180. - (NSRange)markedRange
  181. {
  182. return NSMakeRange(NSNotFound, 0);
  183. }
  184. - (NSRange)selectedRange
  185. {
  186. return NSMakeRange(NSNotFound, 0);
  187. }
  188. - (void)setMarkedText:(nonnull id)string selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange
  189. {
  190. }
  191. - (void)unmarkText
  192. {
  193. }
  194. - (nonnull NSArray<NSAttributedStringKey>*)validAttributesForMarkedText
  195. {
  196. return @[];
  197. }
  198. @end
  199. @interface ImGuiObserver : NSObject
  200. - (void)onApplicationBecomeActive:(NSNotification*)aNotification;
  201. - (void)onApplicationBecomeInactive:(NSNotification*)aNotification;
  202. - (void)displaysDidChange:(NSNotification*)aNotification;
  203. @end
  204. @implementation ImGuiObserver
  205. - (void)onApplicationBecomeActive:(NSNotification*)aNotification
  206. {
  207. ImGuiIO& io = ImGui::GetIO();
  208. io.AddFocusEvent(true);
  209. }
  210. - (void)onApplicationBecomeInactive:(NSNotification*)aNotification
  211. {
  212. ImGuiIO& io = ImGui::GetIO();
  213. io.AddFocusEvent(false);
  214. }
  215. - (void)displaysDidChange:(NSNotification*)aNotification
  216. {
  217. ImGui_ImplOSX_UpdateMonitors();
  218. }
  219. @end
  220. // Functions
  221. static ImGuiKey ImGui_ImplOSX_KeyCodeToImGuiKey(int key_code)
  222. {
  223. switch (key_code)
  224. {
  225. case kVK_ANSI_A: return ImGuiKey_A;
  226. case kVK_ANSI_S: return ImGuiKey_S;
  227. case kVK_ANSI_D: return ImGuiKey_D;
  228. case kVK_ANSI_F: return ImGuiKey_F;
  229. case kVK_ANSI_H: return ImGuiKey_H;
  230. case kVK_ANSI_G: return ImGuiKey_G;
  231. case kVK_ANSI_Z: return ImGuiKey_Z;
  232. case kVK_ANSI_X: return ImGuiKey_X;
  233. case kVK_ANSI_C: return ImGuiKey_C;
  234. case kVK_ANSI_V: return ImGuiKey_V;
  235. case kVK_ANSI_B: return ImGuiKey_B;
  236. case kVK_ANSI_Q: return ImGuiKey_Q;
  237. case kVK_ANSI_W: return ImGuiKey_W;
  238. case kVK_ANSI_E: return ImGuiKey_E;
  239. case kVK_ANSI_R: return ImGuiKey_R;
  240. case kVK_ANSI_Y: return ImGuiKey_Y;
  241. case kVK_ANSI_T: return ImGuiKey_T;
  242. case kVK_ANSI_1: return ImGuiKey_1;
  243. case kVK_ANSI_2: return ImGuiKey_2;
  244. case kVK_ANSI_3: return ImGuiKey_3;
  245. case kVK_ANSI_4: return ImGuiKey_4;
  246. case kVK_ANSI_6: return ImGuiKey_6;
  247. case kVK_ANSI_5: return ImGuiKey_5;
  248. case kVK_ANSI_Equal: return ImGuiKey_Equal;
  249. case kVK_ANSI_9: return ImGuiKey_9;
  250. case kVK_ANSI_7: return ImGuiKey_7;
  251. case kVK_ANSI_Minus: return ImGuiKey_Minus;
  252. case kVK_ANSI_8: return ImGuiKey_8;
  253. case kVK_ANSI_0: return ImGuiKey_0;
  254. case kVK_ANSI_RightBracket: return ImGuiKey_RightBracket;
  255. case kVK_ANSI_O: return ImGuiKey_O;
  256. case kVK_ANSI_U: return ImGuiKey_U;
  257. case kVK_ANSI_LeftBracket: return ImGuiKey_LeftBracket;
  258. case kVK_ANSI_I: return ImGuiKey_I;
  259. case kVK_ANSI_P: return ImGuiKey_P;
  260. case kVK_ANSI_L: return ImGuiKey_L;
  261. case kVK_ANSI_J: return ImGuiKey_J;
  262. case kVK_ANSI_Quote: return ImGuiKey_Apostrophe;
  263. case kVK_ANSI_K: return ImGuiKey_K;
  264. case kVK_ANSI_Semicolon: return ImGuiKey_Semicolon;
  265. case kVK_ANSI_Backslash: return ImGuiKey_Backslash;
  266. case kVK_ANSI_Comma: return ImGuiKey_Comma;
  267. case kVK_ANSI_Slash: return ImGuiKey_Slash;
  268. case kVK_ANSI_N: return ImGuiKey_N;
  269. case kVK_ANSI_M: return ImGuiKey_M;
  270. case kVK_ANSI_Period: return ImGuiKey_Period;
  271. case kVK_ANSI_Grave: return ImGuiKey_GraveAccent;
  272. case kVK_ANSI_KeypadDecimal: return ImGuiKey_KeypadDecimal;
  273. case kVK_ANSI_KeypadMultiply: return ImGuiKey_KeypadMultiply;
  274. case kVK_ANSI_KeypadPlus: return ImGuiKey_KeypadAdd;
  275. case kVK_ANSI_KeypadClear: return ImGuiKey_NumLock;
  276. case kVK_ANSI_KeypadDivide: return ImGuiKey_KeypadDivide;
  277. case kVK_ANSI_KeypadEnter: return ImGuiKey_KeypadEnter;
  278. case kVK_ANSI_KeypadMinus: return ImGuiKey_KeypadSubtract;
  279. case kVK_ANSI_KeypadEquals: return ImGuiKey_KeypadEqual;
  280. case kVK_ANSI_Keypad0: return ImGuiKey_Keypad0;
  281. case kVK_ANSI_Keypad1: return ImGuiKey_Keypad1;
  282. case kVK_ANSI_Keypad2: return ImGuiKey_Keypad2;
  283. case kVK_ANSI_Keypad3: return ImGuiKey_Keypad3;
  284. case kVK_ANSI_Keypad4: return ImGuiKey_Keypad4;
  285. case kVK_ANSI_Keypad5: return ImGuiKey_Keypad5;
  286. case kVK_ANSI_Keypad6: return ImGuiKey_Keypad6;
  287. case kVK_ANSI_Keypad7: return ImGuiKey_Keypad7;
  288. case kVK_ANSI_Keypad8: return ImGuiKey_Keypad8;
  289. case kVK_ANSI_Keypad9: return ImGuiKey_Keypad9;
  290. case kVK_Return: return ImGuiKey_Enter;
  291. case kVK_Tab: return ImGuiKey_Tab;
  292. case kVK_Space: return ImGuiKey_Space;
  293. case kVK_Delete: return ImGuiKey_Backspace;
  294. case kVK_Escape: return ImGuiKey_Escape;
  295. case kVK_CapsLock: return ImGuiKey_CapsLock;
  296. case kVK_Control: return ImGuiKey_LeftCtrl;
  297. case kVK_Shift: return ImGuiKey_LeftShift;
  298. case kVK_Option: return ImGuiKey_LeftAlt;
  299. case kVK_Command: return ImGuiKey_LeftSuper;
  300. case kVK_RightControl: return ImGuiKey_RightCtrl;
  301. case kVK_RightShift: return ImGuiKey_RightShift;
  302. case kVK_RightOption: return ImGuiKey_RightAlt;
  303. case kVK_RightCommand: return ImGuiKey_RightSuper;
  304. // case kVK_Function: return ImGuiKey_;
  305. // case kVK_F17: return ImGuiKey_;
  306. // case kVK_VolumeUp: return ImGuiKey_;
  307. // case kVK_VolumeDown: return ImGuiKey_;
  308. // case kVK_Mute: return ImGuiKey_;
  309. // case kVK_F18: return ImGuiKey_;
  310. // case kVK_F19: return ImGuiKey_;
  311. // case kVK_F20: return ImGuiKey_;
  312. case kVK_F5: return ImGuiKey_F5;
  313. case kVK_F6: return ImGuiKey_F6;
  314. case kVK_F7: return ImGuiKey_F7;
  315. case kVK_F3: return ImGuiKey_F3;
  316. case kVK_F8: return ImGuiKey_F8;
  317. case kVK_F9: return ImGuiKey_F9;
  318. case kVK_F11: return ImGuiKey_F11;
  319. case kVK_F13: return ImGuiKey_PrintScreen;
  320. // case kVK_F16: return ImGuiKey_;
  321. // case kVK_F14: return ImGuiKey_;
  322. case kVK_F10: return ImGuiKey_F10;
  323. case 0x6E: return ImGuiKey_Menu;
  324. case kVK_F12: return ImGuiKey_F12;
  325. // case kVK_F15: return ImGuiKey_;
  326. case kVK_Help: return ImGuiKey_Insert;
  327. case kVK_Home: return ImGuiKey_Home;
  328. case kVK_PageUp: return ImGuiKey_PageUp;
  329. case kVK_ForwardDelete: return ImGuiKey_Delete;
  330. case kVK_F4: return ImGuiKey_F4;
  331. case kVK_End: return ImGuiKey_End;
  332. case kVK_F2: return ImGuiKey_F2;
  333. case kVK_PageDown: return ImGuiKey_PageDown;
  334. case kVK_F1: return ImGuiKey_F1;
  335. case kVK_LeftArrow: return ImGuiKey_LeftArrow;
  336. case kVK_RightArrow: return ImGuiKey_RightArrow;
  337. case kVK_DownArrow: return ImGuiKey_DownArrow;
  338. case kVK_UpArrow: return ImGuiKey_UpArrow;
  339. default: return ImGuiKey_None;
  340. }
  341. }
  342. #ifdef IMGUI_IMPL_METAL_CPP_EXTENSIONS
  343. IMGUI_IMPL_API bool ImGui_ImplOSX_Init(void* _Nonnull view) {
  344. return ImGui_ImplOSX_Init((__bridge NSView*)(view));
  345. }
  346. IMGUI_IMPL_API void ImGui_ImplOSX_NewFrame(void* _Nullable view) {
  347. return ImGui_ImplOSX_NewFrame((__bridge NSView*)(view));
  348. }
  349. #endif
  350. bool ImGui_ImplOSX_Init(NSView* view)
  351. {
  352. ImGuiIO& io = ImGui::GetIO();
  353. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_CreateBackendData();
  354. io.BackendPlatformUserData = (void*)bd;
  355. // Setup backend capabilities flags
  356. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  357. //io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  358. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  359. //io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional)
  360. io.BackendPlatformName = "imgui_impl_osx";
  361. bd->Observer = [ImGuiObserver new];
  362. bd->Window = view.window ?: NSApp.orderedWindows.firstObject;
  363. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  364. main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (__bridge_retained void*)bd->Window;
  365. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  366. ImGui_ImplOSX_InitPlatformInterface();
  367. // Load cursors. Some of them are undocumented.
  368. bd->MouseCursorHidden = false;
  369. bd->MouseCursors[ImGuiMouseCursor_Arrow] = [NSCursor arrowCursor];
  370. bd->MouseCursors[ImGuiMouseCursor_TextInput] = [NSCursor IBeamCursor];
  371. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = [NSCursor closedHandCursor];
  372. bd->MouseCursors[ImGuiMouseCursor_Hand] = [NSCursor pointingHandCursor];
  373. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = [NSCursor operationNotAllowedCursor];
  374. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = [NSCursor respondsToSelector:@selector(_windowResizeNorthSouthCursor)] ? [NSCursor _windowResizeNorthSouthCursor] : [NSCursor resizeUpDownCursor];
  375. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = [NSCursor respondsToSelector:@selector(_windowResizeEastWestCursor)] ? [NSCursor _windowResizeEastWestCursor] : [NSCursor resizeLeftRightCursor];
  376. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = [NSCursor respondsToSelector:@selector(_windowResizeNorthEastSouthWestCursor)] ? [NSCursor _windowResizeNorthEastSouthWestCursor] : [NSCursor closedHandCursor];
  377. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = [NSCursor respondsToSelector:@selector(_windowResizeNorthWestSouthEastCursor)] ? [NSCursor _windowResizeNorthWestSouthEastCursor] : [NSCursor closedHandCursor];
  378. // Note that imgui.cpp also include default OSX clipboard handlers which can be enabled
  379. // by adding '#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS' in imconfig.h and adding '-framework ApplicationServices' to your linker command-line.
  380. // Since we are already in ObjC land here, it is easy for us to add a clipboard handler using the NSPasteboard api.
  381. io.SetClipboardTextFn = [](void*, const char* str) -> void
  382. {
  383. NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
  384. [pasteboard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
  385. [pasteboard setString:[NSString stringWithUTF8String:str] forType:NSPasteboardTypeString];
  386. };
  387. io.GetClipboardTextFn = [](void*) -> const char*
  388. {
  389. NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
  390. NSString* available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:NSPasteboardTypeString]];
  391. if (![available isEqualToString:NSPasteboardTypeString])
  392. return nullptr;
  393. NSString* string = [pasteboard stringForType:NSPasteboardTypeString];
  394. if (string == nil)
  395. return nullptr;
  396. const char* string_c = (const char*)[string UTF8String];
  397. size_t string_len = strlen(string_c);
  398. static ImVector<char> s_clipboard;
  399. s_clipboard.resize((int)string_len + 1);
  400. strcpy(s_clipboard.Data, string_c);
  401. return s_clipboard.Data;
  402. };
  403. [[NSNotificationCenter defaultCenter] addObserver:bd->Observer
  404. selector:@selector(onApplicationBecomeActive:)
  405. name:NSApplicationDidBecomeActiveNotification
  406. object:nil];
  407. [[NSNotificationCenter defaultCenter] addObserver:bd->Observer
  408. selector:@selector(onApplicationBecomeInactive:)
  409. name:NSApplicationDidResignActiveNotification
  410. object:nil];
  411. // Add the NSTextInputClient to the view hierarchy,
  412. // to receive keyboard events and translate them to input text.
  413. bd->KeyEventResponder = [[KeyEventResponder alloc] initWithFrame:NSZeroRect];
  414. bd->InputContext = [[NSTextInputContext alloc] initWithClient:bd->KeyEventResponder];
  415. [view addSubview:bd->KeyEventResponder];
  416. ImGui_ImplOSX_AddTrackingArea(view);
  417. io.SetPlatformImeDataFn = [](ImGuiViewport* viewport, ImGuiPlatformImeData* data) -> void
  418. {
  419. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  420. if (data->WantVisible)
  421. {
  422. [bd->InputContext activate];
  423. }
  424. else
  425. {
  426. [bd->InputContext discardMarkedText];
  427. [bd->InputContext invalidateCharacterCoordinates];
  428. [bd->InputContext deactivate];
  429. }
  430. [bd->KeyEventResponder setImePosX:data->InputPos.x imePosY:data->InputPos.y + data->InputLineHeight];
  431. };
  432. return true;
  433. }
  434. void ImGui_ImplOSX_Shutdown()
  435. {
  436. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  437. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  438. bd->Observer = nullptr;
  439. if (bd->Monitor != nullptr)
  440. {
  441. [NSEvent removeMonitor:bd->Monitor];
  442. bd->Monitor = nullptr;
  443. }
  444. ImGui_ImplOSX_ShutdownPlatformInterface();
  445. ImGui_ImplOSX_DestroyBackendData();
  446. ImGuiIO& io = ImGui::GetIO();
  447. io.BackendPlatformName = nullptr;
  448. io.BackendPlatformUserData = nullptr;
  449. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports);
  450. }
  451. static void ImGui_ImplOSX_UpdateMouseCursor()
  452. {
  453. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  454. ImGuiIO& io = ImGui::GetIO();
  455. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  456. return;
  457. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  458. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  459. {
  460. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  461. if (!bd->MouseCursorHidden)
  462. {
  463. bd->MouseCursorHidden = true;
  464. [NSCursor hide];
  465. }
  466. }
  467. else
  468. {
  469. NSCursor* desired = bd->MouseCursors[imgui_cursor] ?: bd->MouseCursors[ImGuiMouseCursor_Arrow];
  470. // -[NSCursor set] generates measureable overhead if called unconditionally.
  471. if (desired != NSCursor.currentCursor)
  472. {
  473. [desired set];
  474. }
  475. if (bd->MouseCursorHidden)
  476. {
  477. bd->MouseCursorHidden = false;
  478. [NSCursor unhide];
  479. }
  480. }
  481. }
  482. static void ImGui_ImplOSX_UpdateGamepads()
  483. {
  484. ImGuiIO& io = ImGui::GetIO();
  485. memset(io.NavInputs, 0, sizeof(io.NavInputs));
  486. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  487. return;
  488. #if APPLE_HAS_CONTROLLER
  489. GCController* controller = GCController.current;
  490. #else
  491. GCController* controller = GCController.controllers.firstObject;
  492. #endif
  493. if (controller == nil || controller.extendedGamepad == nil)
  494. {
  495. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  496. return;
  497. }
  498. GCExtendedGamepad* gp = controller.extendedGamepad;
  499. // Update gamepad inputs
  500. #define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
  501. #define MAP_BUTTON(KEY_NO, BUTTON_NAME) { io.AddKeyEvent(KEY_NO, gp.BUTTON_NAME.isPressed); }
  502. #define MAP_ANALOG(KEY_NO, AXIS_NAME, V0, V1) { float vn = (float)(gp.AXIS_NAME.value - V0) / (float)(V1 - V0); vn = IM_SATURATE(vn); io.AddKeyAnalogEvent(KEY_NO, vn > 0.1f, vn); }
  503. const float thumb_dead_zone = 0.0f;
  504. #if APPLE_HAS_BUTTON_OPTIONS
  505. MAP_BUTTON(ImGuiKey_GamepadBack, buttonOptions);
  506. #endif
  507. MAP_BUTTON(ImGuiKey_GamepadFaceLeft, buttonX); // Xbox X, PS Square
  508. MAP_BUTTON(ImGuiKey_GamepadFaceRight, buttonB); // Xbox B, PS Circle
  509. MAP_BUTTON(ImGuiKey_GamepadFaceUp, buttonY); // Xbox Y, PS Triangle
  510. MAP_BUTTON(ImGuiKey_GamepadFaceDown, buttonA); // Xbox A, PS Cross
  511. MAP_BUTTON(ImGuiKey_GamepadDpadLeft, dpad.left);
  512. MAP_BUTTON(ImGuiKey_GamepadDpadRight, dpad.right);
  513. MAP_BUTTON(ImGuiKey_GamepadDpadUp, dpad.up);
  514. MAP_BUTTON(ImGuiKey_GamepadDpadDown, dpad.down);
  515. MAP_ANALOG(ImGuiKey_GamepadL1, leftShoulder, 0.0f, 1.0f);
  516. MAP_ANALOG(ImGuiKey_GamepadR1, rightShoulder, 0.0f, 1.0f);
  517. MAP_ANALOG(ImGuiKey_GamepadL2, leftTrigger, 0.0f, 1.0f);
  518. MAP_ANALOG(ImGuiKey_GamepadR2, rightTrigger, 0.0f, 1.0f);
  519. #if APPLE_HAS_THUMBSTICKS
  520. MAP_BUTTON(ImGuiKey_GamepadL3, leftThumbstickButton);
  521. MAP_BUTTON(ImGuiKey_GamepadR3, rightThumbstickButton);
  522. #endif
  523. MAP_ANALOG(ImGuiKey_GamepadLStickLeft, leftThumbstick.xAxis, -thumb_dead_zone, -1.0f);
  524. MAP_ANALOG(ImGuiKey_GamepadLStickRight, leftThumbstick.xAxis, +thumb_dead_zone, +1.0f);
  525. MAP_ANALOG(ImGuiKey_GamepadLStickUp, leftThumbstick.yAxis, +thumb_dead_zone, +1.0f);
  526. MAP_ANALOG(ImGuiKey_GamepadLStickDown, leftThumbstick.yAxis, -thumb_dead_zone, -1.0f);
  527. MAP_ANALOG(ImGuiKey_GamepadRStickLeft, rightThumbstick.xAxis, -thumb_dead_zone, -1.0f);
  528. MAP_ANALOG(ImGuiKey_GamepadRStickRight, rightThumbstick.xAxis, +thumb_dead_zone, +1.0f);
  529. MAP_ANALOG(ImGuiKey_GamepadRStickUp, rightThumbstick.yAxis, +thumb_dead_zone, +1.0f);
  530. MAP_ANALOG(ImGuiKey_GamepadRStickDown, rightThumbstick.yAxis, -thumb_dead_zone, -1.0f);
  531. #undef MAP_BUTTON
  532. #undef MAP_ANALOG
  533. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  534. }
  535. static void ImGui_ImplOSX_UpdateImePosWithView(NSView* view)
  536. {
  537. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  538. ImGuiIO& io = ImGui::GetIO();
  539. if (io.WantTextInput)
  540. [bd->KeyEventResponder updateImePosWithView:view];
  541. }
  542. void ImGui_ImplOSX_NewFrame(NSView* view)
  543. {
  544. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  545. ImGuiIO& io = ImGui::GetIO();
  546. // Setup display size
  547. if (view)
  548. {
  549. const float dpi = (float)[view.window backingScaleFactor];
  550. io.DisplaySize = ImVec2((float)view.bounds.size.width, (float)view.bounds.size.height);
  551. io.DisplayFramebufferScale = ImVec2(dpi, dpi);
  552. }
  553. // Setup time step
  554. if (bd->Time == 0.0)
  555. bd->Time = GetMachAbsoluteTimeInSeconds();
  556. double current_time = GetMachAbsoluteTimeInSeconds();
  557. io.DeltaTime = (float)(current_time - bd->Time);
  558. bd->Time = current_time;
  559. ImGui_ImplOSX_UpdateMouseCursor();
  560. ImGui_ImplOSX_UpdateGamepads();
  561. ImGui_ImplOSX_UpdateImePosWithView(view);
  562. }
  563. // Must only be called for a mouse event, otherwise an exception occurs
  564. // (Note that NSEventTypeScrollWheel is considered "other input". Oddly enough an exception does not occur with it, but the value will sometimes be wrong!)
  565. static ImGuiMouseSource GetMouseSource(NSEvent* event)
  566. {
  567. switch (event.subtype)
  568. {
  569. case NSEventSubtypeTabletPoint:
  570. return ImGuiMouseSource_Pen;
  571. // macOS considers input from relative touch devices (like the trackpad or Apple Magic Mouse) to be touch input.
  572. // This doesn't really make sense for Dear ImGui, which expects absolute touch devices only.
  573. // There does not seem to be a simple way to disambiguate things here so we consider NSEventSubtypeTouch events to always come from mice.
  574. // See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW24
  575. //case NSEventSubtypeTouch:
  576. // return ImGuiMouseSource_TouchScreen;
  577. case NSEventSubtypeMouseEvent:
  578. default:
  579. return ImGuiMouseSource_Mouse;
  580. }
  581. }
  582. static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
  583. {
  584. ImGuiIO& io = ImGui::GetIO();
  585. if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown)
  586. {
  587. int button = (int)[event buttonNumber];
  588. if (button >= 0 && button < ImGuiMouseButton_COUNT)
  589. {
  590. io.AddMouseSourceEvent(GetMouseSource(event));
  591. io.AddMouseButtonEvent(button, true);
  592. }
  593. return io.WantCaptureMouse;
  594. }
  595. if (event.type == NSEventTypeLeftMouseUp || event.type == NSEventTypeRightMouseUp || event.type == NSEventTypeOtherMouseUp)
  596. {
  597. int button = (int)[event buttonNumber];
  598. if (button >= 0 && button < ImGuiMouseButton_COUNT)
  599. {
  600. io.AddMouseSourceEvent(GetMouseSource(event));
  601. io.AddMouseButtonEvent(button, false);
  602. }
  603. return io.WantCaptureMouse;
  604. }
  605. if (event.type == NSEventTypeMouseMoved || event.type == NSEventTypeLeftMouseDragged || event.type == NSEventTypeRightMouseDragged || event.type == NSEventTypeOtherMouseDragged)
  606. {
  607. NSPoint mousePoint;
  608. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  609. {
  610. mousePoint = NSEvent.mouseLocation;
  611. mousePoint.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - mousePoint.y; // Normalize y coordinate to top-left of main display.
  612. }
  613. else
  614. {
  615. mousePoint = event.locationInWindow;
  616. if (event.window == nil)
  617. mousePoint = [[view window] convertPointFromScreen:mousePoint];
  618. mousePoint = [view convertPoint:mousePoint fromView:nil]; // Convert to local coordinates of view
  619. if ([view isFlipped])
  620. mousePoint = NSMakePoint(mousePoint.x, mousePoint.y);
  621. else
  622. mousePoint = NSMakePoint(mousePoint.x, view.bounds.size.height - mousePoint.y);
  623. }
  624. io.AddMouseSourceEvent(GetMouseSource(event));
  625. io.AddMousePosEvent((float)mousePoint.x, (float)mousePoint.y);
  626. return io.WantCaptureMouse;
  627. }
  628. if (event.type == NSEventTypeScrollWheel)
  629. {
  630. // Ignore canceled events.
  631. //
  632. // From macOS 12.1, scrolling with two fingers and then decelerating
  633. // by tapping two fingers results in two events appearing:
  634. //
  635. // 1. A scroll wheel NSEvent, with a phase == NSEventPhaseMayBegin, when the user taps
  636. // two fingers to decelerate or stop the scroll events.
  637. //
  638. // 2. A scroll wheel NSEvent, with a phase == NSEventPhaseCancelled, when the user releases the
  639. // two-finger tap. It is this event that sometimes contains large values for scrollingDeltaX and
  640. // scrollingDeltaY. When these are added to the current x and y positions of the scrolling view,
  641. // it appears to jump up or down. It can be observed in Preview, various JetBrains IDEs and here.
  642. if (event.phase == NSEventPhaseCancelled)
  643. return false;
  644. double wheel_dx = 0.0;
  645. double wheel_dy = 0.0;
  646. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  647. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  648. {
  649. wheel_dx = [event scrollingDeltaX];
  650. wheel_dy = [event scrollingDeltaY];
  651. if ([event hasPreciseScrollingDeltas])
  652. {
  653. wheel_dx *= 0.01;
  654. wheel_dy *= 0.01;
  655. }
  656. }
  657. else
  658. #endif // MAC_OS_X_VERSION_MAX_ALLOWED
  659. {
  660. wheel_dx = [event deltaX] * 0.1;
  661. wheel_dy = [event deltaY] * 0.1;
  662. }
  663. if (wheel_dx != 0.0 || wheel_dy != 0.0)
  664. io.AddMouseWheelEvent((float)wheel_dx, (float)wheel_dy);
  665. return io.WantCaptureMouse;
  666. }
  667. if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp)
  668. {
  669. if ([event isARepeat])
  670. return io.WantCaptureKeyboard;
  671. int key_code = (int)[event keyCode];
  672. ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code);
  673. io.AddKeyEvent(key, event.type == NSEventTypeKeyDown);
  674. io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code)
  675. return io.WantCaptureKeyboard;
  676. }
  677. if (event.type == NSEventTypeFlagsChanged)
  678. {
  679. unsigned short key_code = [event keyCode];
  680. NSEventModifierFlags modifier_flags = [event modifierFlags];
  681. io.AddKeyEvent(ImGuiMod_Shift, (modifier_flags & NSEventModifierFlagShift) != 0);
  682. io.AddKeyEvent(ImGuiMod_Ctrl, (modifier_flags & NSEventModifierFlagControl) != 0);
  683. io.AddKeyEvent(ImGuiMod_Alt, (modifier_flags & NSEventModifierFlagOption) != 0);
  684. io.AddKeyEvent(ImGuiMod_Super, (modifier_flags & NSEventModifierFlagCommand) != 0);
  685. ImGuiKey key = ImGui_ImplOSX_KeyCodeToImGuiKey(key_code);
  686. if (key != ImGuiKey_None)
  687. {
  688. // macOS does not generate down/up event for modifiers. We're trying
  689. // to use hardware dependent masks to extract that information.
  690. // 'imgui_mask' is left as a fallback.
  691. NSEventModifierFlags mask = 0;
  692. switch (key)
  693. {
  694. case ImGuiKey_LeftCtrl: mask = 0x0001; break;
  695. case ImGuiKey_RightCtrl: mask = 0x2000; break;
  696. case ImGuiKey_LeftShift: mask = 0x0002; break;
  697. case ImGuiKey_RightShift: mask = 0x0004; break;
  698. case ImGuiKey_LeftSuper: mask = 0x0008; break;
  699. case ImGuiKey_RightSuper: mask = 0x0010; break;
  700. case ImGuiKey_LeftAlt: mask = 0x0020; break;
  701. case ImGuiKey_RightAlt: mask = 0x0040; break;
  702. default:
  703. return io.WantCaptureKeyboard;
  704. }
  705. NSEventModifierFlags modifier_flags = [event modifierFlags];
  706. io.AddKeyEvent(key, (modifier_flags & mask) != 0);
  707. io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code)
  708. }
  709. return io.WantCaptureKeyboard;
  710. }
  711. return false;
  712. }
  713. static void ImGui_ImplOSX_AddTrackingArea(NSView* _Nonnull view)
  714. {
  715. // If we want to receive key events, we either need to be in the responder chain of the key view,
  716. // or else we can install a local monitor. The consequence of this heavy-handed approach is that
  717. // we receive events for all controls, not just Dear ImGui widgets. If we had native controls in our
  718. // window, we'd want to be much more careful than just ingesting the complete event stream.
  719. // To match the behavior of other backends, we pass every event down to the OS.
  720. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  721. if (bd->Monitor)
  722. return;
  723. NSEventMask eventMask = 0;
  724. eventMask |= NSEventMaskMouseMoved | NSEventMaskScrollWheel;
  725. eventMask |= NSEventMaskLeftMouseDown | NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
  726. eventMask |= NSEventMaskRightMouseDown | NSEventMaskRightMouseUp | NSEventMaskRightMouseDragged;
  727. eventMask |= NSEventMaskOtherMouseDown | NSEventMaskOtherMouseUp | NSEventMaskOtherMouseDragged;
  728. eventMask |= NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged;
  729. bd->Monitor = [NSEvent addLocalMonitorForEventsMatchingMask:eventMask
  730. handler:^NSEvent* _Nullable(NSEvent* event)
  731. {
  732. ImGui_ImplOSX_HandleEvent(event, view);
  733. return event;
  734. }];
  735. }
  736. //--------------------------------------------------------------------------------------------------------
  737. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  738. // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
  739. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  740. //--------------------------------------------------------------------------------------------------------
  741. struct ImGuiViewportDataOSX
  742. {
  743. NSWindow* Window;
  744. bool WindowOwned;
  745. ImGuiViewportDataOSX() { WindowOwned = false; }
  746. ~ImGuiViewportDataOSX() { IM_ASSERT(Window == nil); }
  747. };
  748. @interface ImGui_ImplOSX_Window: NSWindow
  749. @end
  750. @implementation ImGui_ImplOSX_Window
  751. - (BOOL)canBecomeKeyWindow
  752. {
  753. return YES;
  754. }
  755. @end
  756. static void ConvertNSRect(NSScreen* screen, NSRect* r)
  757. {
  758. r->origin.y = screen.frame.size.height - r->origin.y - r->size.height;
  759. }
  760. static void ImGui_ImplOSX_CreateWindow(ImGuiViewport* viewport)
  761. {
  762. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  763. ImGuiViewportDataOSX* data = IM_NEW(ImGuiViewportDataOSX)();
  764. viewport->PlatformUserData = data;
  765. NSScreen* screen = bd->Window.screen;
  766. NSRect rect = NSMakeRect(viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y);
  767. ConvertNSRect(screen, &rect);
  768. NSWindowStyleMask styleMask = 0;
  769. if (viewport->Flags & ImGuiViewportFlags_NoDecoration)
  770. styleMask |= NSWindowStyleMaskBorderless;
  771. else
  772. styleMask |= NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
  773. NSWindow* window = [[ImGui_ImplOSX_Window alloc] initWithContentRect:rect
  774. styleMask:styleMask
  775. backing:NSBackingStoreBuffered
  776. defer:YES
  777. screen:screen];
  778. if (viewport->Flags & ImGuiViewportFlags_TopMost)
  779. [window setLevel:NSFloatingWindowLevel];
  780. window.title = @"Untitled";
  781. window.opaque = YES;
  782. KeyEventResponder* view = [[KeyEventResponder alloc] initWithFrame:rect];
  783. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  784. [view setWantsBestResolutionOpenGLSurface:YES];
  785. window.contentView = view;
  786. data->Window = window;
  787. data->WindowOwned = true;
  788. viewport->PlatformRequestResize = false;
  789. viewport->PlatformHandle = viewport->PlatformHandleRaw = (__bridge_retained void*)window;
  790. }
  791. static void ImGui_ImplOSX_DestroyWindow(ImGuiViewport* viewport)
  792. {
  793. NSWindow* window = (__bridge_transfer NSWindow*)viewport->PlatformHandleRaw;
  794. window = nil;
  795. if (ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData)
  796. {
  797. NSWindow* window = data->Window;
  798. if (window != nil && data->WindowOwned)
  799. {
  800. window.contentView = nil;
  801. window.contentViewController = nil;
  802. [window orderOut:nil];
  803. }
  804. data->Window = nil;
  805. IM_DELETE(data);
  806. }
  807. viewport->PlatformUserData = viewport->PlatformHandle = viewport->PlatformHandleRaw = nullptr;
  808. }
  809. static void ImGui_ImplOSX_ShowWindow(ImGuiViewport* viewport)
  810. {
  811. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  812. IM_ASSERT(data->Window != 0);
  813. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  814. [data->Window orderFront:nil];
  815. else
  816. [data->Window makeKeyAndOrderFront:nil];
  817. [data->Window setIsVisible:YES];
  818. }
  819. static ImVec2 ImGui_ImplOSX_GetWindowPos(ImGuiViewport* viewport)
  820. {
  821. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  822. IM_ASSERT(data->Window != 0);
  823. NSWindow* window = data->Window;
  824. NSScreen* screen = window.screen;
  825. NSSize size = screen.frame.size;
  826. NSRect frame = window.frame;
  827. NSRect rect = window.contentLayoutRect;
  828. return ImVec2(frame.origin.x, size.height - frame.origin.y - rect.size.height);
  829. }
  830. static void ImGui_ImplOSX_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  831. {
  832. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  833. IM_ASSERT(data->Window != 0);
  834. NSWindow* window = data->Window;
  835. NSSize size = window.frame.size;
  836. NSRect r = NSMakeRect(pos.x, pos.y, size.width, size.height);
  837. ConvertNSRect(window.screen, &r);
  838. [window setFrameOrigin:r.origin];
  839. }
  840. static ImVec2 ImGui_ImplOSX_GetWindowSize(ImGuiViewport* viewport)
  841. {
  842. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  843. IM_ASSERT(data->Window != 0);
  844. NSWindow* window = data->Window;
  845. NSSize size = window.contentLayoutRect.size;
  846. return ImVec2(size.width, size.height);
  847. }
  848. static void ImGui_ImplOSX_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  849. {
  850. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  851. IM_ASSERT(data->Window != 0);
  852. NSWindow* window = data->Window;
  853. NSRect rect = window.frame;
  854. rect.origin.y -= (size.y - rect.size.height);
  855. rect.size.width = size.x;
  856. rect.size.height = size.y;
  857. [window setFrame:rect display:YES];
  858. }
  859. static void ImGui_ImplOSX_SetWindowFocus(ImGuiViewport* viewport)
  860. {
  861. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  862. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  863. IM_ASSERT(data->Window != 0);
  864. [data->Window makeKeyAndOrderFront:bd->Window];
  865. }
  866. static bool ImGui_ImplOSX_GetWindowFocus(ImGuiViewport* viewport)
  867. {
  868. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  869. IM_ASSERT(data->Window != 0);
  870. return data->Window.isKeyWindow;
  871. }
  872. static bool ImGui_ImplOSX_GetWindowMinimized(ImGuiViewport* viewport)
  873. {
  874. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  875. IM_ASSERT(data->Window != 0);
  876. return data->Window.isMiniaturized;
  877. }
  878. static void ImGui_ImplOSX_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  879. {
  880. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  881. IM_ASSERT(data->Window != 0);
  882. data->Window.title = [NSString stringWithUTF8String:title];
  883. }
  884. static void ImGui_ImplOSX_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  885. {
  886. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  887. IM_ASSERT(data->Window != 0);
  888. IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);
  889. data->Window.alphaValue = alpha;
  890. }
  891. static float ImGui_ImplOSX_GetWindowDpiScale(ImGuiViewport* viewport)
  892. {
  893. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
  894. IM_ASSERT(data->Window != 0);
  895. return data->Window.backingScaleFactor;
  896. }
  897. static void ImGui_ImplOSX_UpdateMonitors()
  898. {
  899. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  900. platform_io.Monitors.resize(0);
  901. for (NSScreen* screen in NSScreen.screens)
  902. {
  903. NSRect frame = screen.frame;
  904. NSRect visibleFrame = screen.visibleFrame;
  905. ImGuiPlatformMonitor imgui_monitor;
  906. imgui_monitor.MainPos = ImVec2(frame.origin.x, frame.origin.y);
  907. imgui_monitor.MainSize = ImVec2(frame.size.width, frame.size.height);
  908. imgui_monitor.WorkPos = ImVec2(visibleFrame.origin.x, visibleFrame.origin.y);
  909. imgui_monitor.WorkSize = ImVec2(visibleFrame.size.width, visibleFrame.size.height);
  910. imgui_monitor.DpiScale = screen.backingScaleFactor;
  911. imgui_monitor.PlatformHandle = (__bridge_retained void*)screen;
  912. platform_io.Monitors.push_back(imgui_monitor);
  913. }
  914. }
  915. static void ImGui_ImplOSX_InitPlatformInterface()
  916. {
  917. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  918. ImGui_ImplOSX_UpdateMonitors();
  919. // Register platform interface (will be coupled with a renderer interface)
  920. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  921. platform_io.Platform_CreateWindow = ImGui_ImplOSX_CreateWindow;
  922. platform_io.Platform_DestroyWindow = ImGui_ImplOSX_DestroyWindow;
  923. platform_io.Platform_ShowWindow = ImGui_ImplOSX_ShowWindow;
  924. platform_io.Platform_SetWindowPos = ImGui_ImplOSX_SetWindowPos;
  925. platform_io.Platform_GetWindowPos = ImGui_ImplOSX_GetWindowPos;
  926. platform_io.Platform_SetWindowSize = ImGui_ImplOSX_SetWindowSize;
  927. platform_io.Platform_GetWindowSize = ImGui_ImplOSX_GetWindowSize;
  928. platform_io.Platform_SetWindowFocus = ImGui_ImplOSX_SetWindowFocus;
  929. platform_io.Platform_GetWindowFocus = ImGui_ImplOSX_GetWindowFocus;
  930. platform_io.Platform_GetWindowMinimized = ImGui_ImplOSX_GetWindowMinimized;
  931. platform_io.Platform_SetWindowTitle = ImGui_ImplOSX_SetWindowTitle;
  932. platform_io.Platform_SetWindowAlpha = ImGui_ImplOSX_SetWindowAlpha;
  933. platform_io.Platform_GetWindowDpiScale = ImGui_ImplOSX_GetWindowDpiScale; // FIXME-DPI
  934. // Register main window handle (which is owned by the main application, not by us)
  935. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  936. ImGuiViewportDataOSX* data = IM_NEW(ImGuiViewportDataOSX)();
  937. data->Window = bd->Window;
  938. data->WindowOwned = false;
  939. main_viewport->PlatformUserData = data;
  940. main_viewport->PlatformHandle = (__bridge void*)bd->Window;
  941. [NSNotificationCenter.defaultCenter addObserver:bd->Observer
  942. selector:@selector(displaysDidChange:)
  943. name:NSApplicationDidChangeScreenParametersNotification
  944. object:nil];
  945. }
  946. static void ImGui_ImplOSX_ShutdownPlatformInterface()
  947. {
  948. ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
  949. [NSNotificationCenter.defaultCenter removeObserver:bd->Observer
  950. name:NSApplicationDidChangeScreenParametersNotification
  951. object:nil];
  952. bd->Observer = nullptr;
  953. bd->Window = nullptr;
  954. if (bd->Monitor != nullptr)
  955. {
  956. [NSEvent removeMonitor:bd->Monitor];
  957. bd->Monitor = nullptr;
  958. }
  959. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  960. ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)main_viewport->PlatformUserData;
  961. IM_DELETE(data);
  962. main_viewport->PlatformUserData = nullptr;
  963. ImGui::DestroyPlatformWindows();
  964. }
  965. //-----------------------------------------------------------------------------
  966. #endif // #ifndef IMGUI_DISABLE