diff --git a/widget/cocoa/nsCocoaUtils.h b/widget/cocoa/nsCocoaUtils.h index c1cc7fbc6949..a9f267b62d7c 100644 --- a/widget/cocoa/nsCocoaUtils.h +++ b/widget/cocoa/nsCocoaUtils.h @@ -34,6 +34,7 @@ enum { class nsIWidget; namespace mozilla { +class TimeStamp; namespace gfx { class SourceSurface; } // namespace gfx @@ -377,6 +378,12 @@ public: const nsTArray& aFontRanges, const bool aIsVertical, const CGFloat aBackingScaleFactor); + + /** + * Compute TimeStamp from an event's timestamp. + * If aEventTime is 0, this returns current timestamp. + */ + static mozilla::TimeStamp GetEventTimeStamp(NSTimeInterval aEventTime); }; #endif // nsCocoaUtils_h_ diff --git a/widget/cocoa/nsCocoaUtils.mm b/widget/cocoa/nsCocoaUtils.mm index 3138245aa785..4ed439592930 100644 --- a/widget/cocoa/nsCocoaUtils.mm +++ b/widget/cocoa/nsCocoaUtils.mm @@ -1020,3 +1020,22 @@ nsCocoaUtils::GetNSMutableAttributedString( NS_OBJC_END_TRY_ABORT_BLOCK_NIL } + +TimeStamp +nsCocoaUtils::GetEventTimeStamp(NSTimeInterval aEventTime) +{ + if (!aEventTime) { + // If the event is generated by a 3rd party application, its timestamp + // may be 0. In this case, just return current timestamp. + // XXX Should we cache last event time? + return TimeStamp::Now(); + } + // The internal value of the macOS implementation of TimeStamp is based on + // mach_absolute_time(), which measures "ticks" since boot. + // Event timestamps are NSTimeIntervals (seconds) since boot. So the two time + // representations already have the same base; we only need to convert + // seconds into ticks. + int64_t tick = + BaseTimeDurationPlatformUtils::TicksFromMilliseconds(aEventTime * 1000.0); + return TimeStamp::FromSystemTime(tick); +}