Backends: OSX: Use mach_absolute_time as CFAbsoluteTimeGetCurrent can jump backwards. (#4557, #4563)

This commit is contained in:
James McCartney 2021-09-20 00:20:04 -07:00 committed by ocornut
parent c6ca327fb2
commit bc3d267c51
2 changed files with 22 additions and 3 deletions

View File

@ -16,9 +16,11 @@
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_osx.h" #include "imgui_impl_osx.h"
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include <mach/mach_time.h>
// CHANGELOG // CHANGELOG
// (minor and older changes stripped away, please see git history for details) // (minor and older changes stripped away, please see git history for details)
// 2021-09-21: Use mach_absolute_time as CFAbsoluteTimeGetCurrent can jump backwards.
// 2021-08-17: Calling io.AddFocusEvent() on NSApplicationDidBecomeActiveNotification/NSApplicationDidResignActiveNotification events. // 2021-08-17: Calling io.AddFocusEvent() on NSApplicationDidBecomeActiveNotification/NSApplicationDidResignActiveNotification events.
// 2021-06-23: Inputs: Added a fix for shortcuts using CTRL key instead of CMD key. // 2021-06-23: Inputs: Added a fix for shortcuts using CTRL key instead of CMD key.
// 2021-04-19: Inputs: Added a fix for keys remaining stuck in pressed state when CMD-tabbing into different application. // 2021-04-19: Inputs: Added a fix for keys remaining stuck in pressed state when CMD-tabbing into different application.
@ -37,7 +39,8 @@
@class ImFocusObserver; @class ImFocusObserver;
// Data // Data
static CFAbsoluteTime g_Time = 0.0; static double g_HostClockPeriod = 0.0;
static double g_Time = 0.0;
static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
static bool g_MouseCursorHidden = false; static bool g_MouseCursorHidden = false;
static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {}; static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {};
@ -52,6 +55,18 @@ static ImFocusObserver* g_FocusObserver = NULL;
+ (id)_windowResizeEastWestCursor; + (id)_windowResizeEastWestCursor;
@end @end
static void InitHostClockPeriod()
{
struct mach_timebase_info info;
mach_timebase_info(&info);
g_HostClockPeriod = 1e-9 * ((double)info.denom / (double)info.numer); // Period is the reciprocal of frequency.
}
static double GetMachAbsoluteTimeInSeconds()
{
return (double)mach_absolute_time() * g_HostClockPeriod;
}
static void resetKeys() static void resetKeys()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
@ -232,8 +247,11 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
// Setup time step // Setup time step
if (g_Time == 0.0) if (g_Time == 0.0)
g_Time = CFAbsoluteTimeGetCurrent(); {
CFAbsoluteTime current_time = CFAbsoluteTimeGetCurrent(); InitHostClockPeriod();
g_Time = GetMachAbsoluteTimeInSeconds();
}
double current_time = GetMachAbsoluteTimeInSeconds();
io.DeltaTime = (float)(current_time - g_Time); io.DeltaTime = (float)(current_time - g_Time);
g_Time = current_time; g_Time = current_time;

View File

@ -78,6 +78,7 @@ Other Changes:
- Backends: WebGPU: Fixed for latest specs. (#4472) [@Kangz] - Backends: WebGPU: Fixed for latest specs. (#4472) [@Kangz]
- Backends: Metal: Fixed a crash when clipping rect larger than framebuffer is submitted via - Backends: Metal: Fixed a crash when clipping rect larger than framebuffer is submitted via
a direct unclipped PushClipRect() call. (#4464) a direct unclipped PushClipRect() call. (#4464)
- Backends: OSX: Use mach_absolute_time as CFAbsoluteTimeGetCurrent can jump backwards. (#4557, #4563) [@lfnoise]
- Backends: All renderers: Normalize clipping rect handling across backends. (#4464) - Backends: All renderers: Normalize clipping rect handling across backends. (#4464)