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