17
17
#import " imgui.h"
18
18
#import " imgui_impl_osx.h"
19
19
#import < Cocoa/Cocoa.h>
20
- #import < mach/mach_time.h>
21
20
#import < Carbon/Carbon.h>
22
21
#import < GameController/GameController.h>
22
+ #import < time.h>
23
23
24
24
// CHANGELOG
25
25
// (minor and older changes stripped away, please see git history for details)
54
54
#define APPLE_HAS_CONTROLLER (__IPHONE_OS_VERSION_MIN_REQUIRED >= 140000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 || __TV_OS_VERSION_MIN_REQUIRED >= 140000 )
55
55
#define APPLE_HAS_THUMBSTICKS (__IPHONE_OS_VERSION_MIN_REQUIRED >= 120100 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101401 || __TV_OS_VERSION_MIN_REQUIRED >= 120100 )
56
56
57
- @class ImFocusObserver ;
57
+ @class ImGuiObserver ;
58
58
@class KeyEventResponder;
59
59
60
60
// Data
61
61
struct ImGui_ImplOSX_Data
62
62
{
63
- double HostClockPeriod;
64
- double Time;
63
+ CFTimeInterval Time;
65
64
NSCursor * MouseCursors[ImGuiMouseCursor_COUNT];
66
65
bool MouseCursorHidden;
67
- ImFocusObserver * FocusObserver ;
66
+ ImGuiObserver * Observer ;
68
67
KeyEventResponder* KeyEventResponder;
69
68
NSTextInputContext * InputContext;
70
69
75
74
static ImGui_ImplOSX_Data* ImGui_ImplOSX_GetBackendData () { return (ImGui_ImplOSX_Data*)ImGui::GetIO ().BackendPlatformUserData ; }
76
75
static void ImGui_ImplOSX_DestroyBackendData () { IM_DELETE (ImGui_ImplOSX_GetBackendData ()); }
77
76
77
+ static inline CFTimeInterval GetMachAbsoluteTimeInSeconds () { return static_cast <CFTimeInterval >(static_cast <double >(clock_gettime_nsec_np (CLOCK_UPTIME_RAW)) / 1e9 ); }
78
+
78
79
// Undocumented methods for creating cursors.
79
80
@interface NSCursor ()
80
81
+ (id )_windowResizeNorthWestSouthEastCursor ;
@@ -83,20 +84,6 @@ + (id)_windowResizeNorthSouthCursor;
83
84
+ (id )_windowResizeEastWestCursor ;
84
85
@end
85
86
86
- static void InitHostClockPeriod ()
87
- {
88
- ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData ();
89
- struct mach_timebase_info info;
90
- mach_timebase_info (&info);
91
- bd->HostClockPeriod = 1e-9 * ((double )info.denom / (double )info.numer ); // Period is the reciprocal of frequency.
92
- }
93
-
94
- static double GetMachAbsoluteTimeInSeconds ()
95
- {
96
- ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData ();
97
- return (double )mach_absolute_time () * bd->HostClockPeriod ;
98
- }
99
-
100
87
/* *
101
88
KeyEventResponder implements the NSTextInputClient protocol as is required by the macOS text input manager.
102
89
@@ -225,14 +212,14 @@ - (void)unmarkText
225
212
226
213
@end
227
214
228
- @interface ImFocusObserver : NSObject
215
+ @interface ImGuiObserver : NSObject
229
216
230
217
- (void )onApplicationBecomeActive : (NSNotification *)aNotification ;
231
218
- (void )onApplicationBecomeInactive : (NSNotification *)aNotification ;
232
219
233
220
@end
234
221
235
- @implementation ImFocusObserver
222
+ @implementation ImGuiObserver
236
223
237
224
- (void )onApplicationBecomeActive : (NSNotification *)aNotification
238
225
{
@@ -385,6 +372,8 @@ bool ImGui_ImplOSX_Init(NSView* view)
385
372
// io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
386
373
io.BackendPlatformName = " imgui_impl_osx" ;
387
374
375
+ bd->Observer = [ImGuiObserver new ];
376
+
388
377
// Load cursors. Some of them are undocumented.
389
378
bd->MouseCursorHidden = false ;
390
379
bd->MouseCursors [ImGuiMouseCursor_Arrow] = [NSCursor arrowCursor ];
@@ -426,12 +415,11 @@ bool ImGui_ImplOSX_Init(NSView* view)
426
415
return s_clipboard.Data ;
427
416
};
428
417
429
- bd->FocusObserver = [[ImFocusObserver alloc ] init ];
430
- [[NSNotificationCenter defaultCenter ] addObserver: bd->FocusObserver
418
+ [[NSNotificationCenter defaultCenter ] addObserver: bd->Observer
431
419
selector: @selector (onApplicationBecomeActive: )
432
420
name: NSApplicationDidBecomeActiveNotification
433
421
object: nil ];
434
- [[NSNotificationCenter defaultCenter ] addObserver: bd->FocusObserver
422
+ [[NSNotificationCenter defaultCenter ] addObserver: bd->Observer
435
423
selector: @selector (onApplicationBecomeInactive: )
436
424
name: NSApplicationDidResignActiveNotification
437
425
object: nil ];
@@ -473,7 +461,7 @@ bool ImGui_ImplOSX_Init(NSView* view)
473
461
void ImGui_ImplOSX_Shutdown ()
474
462
{
475
463
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData ();
476
- bd->FocusObserver = NULL ;
464
+ bd->Observer = NULL ;
477
465
ImGui_ImplOSX_DestroyBackendData ();
478
466
}
479
467
@@ -592,10 +580,8 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
592
580
593
581
// Setup time step
594
582
if (bd->Time == 0.0 )
595
- {
596
- InitHostClockPeriod ();
597
583
bd->Time = GetMachAbsoluteTimeInSeconds ();
598
- }
584
+
599
585
double current_time = GetMachAbsoluteTimeInSeconds ();
600
586
io.DeltaTime = (float )(current_time - bd->Time );
601
587
bd->Time = current_time;
0 commit comments