From 91c31f646e12ecbdafd4062e5ea10122f1eb5fa9 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 1 Oct 2013 16:27:33 -0700 Subject: [PATCH] Opt out of AppNap while progress bars are active --- aegisub/libaegisub/common/util.cpp | 11 ++++++++++- aegisub/libaegisub/include/libaegisub/util_osx.h | 14 +++++++++----- aegisub/libaegisub/osx/util.mm | 16 ++++++++++++++++ aegisub/src/dialog_progress.cpp | 3 +++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/aegisub/libaegisub/common/util.cpp b/aegisub/libaegisub/common/util.cpp index 7c2191f6b..8f96b4715 100644 --- a/aegisub/libaegisub/common/util.cpp +++ b/aegisub/libaegisub/common/util.cpp @@ -17,6 +17,7 @@ #include "../config.h" #include "libaegisub/util.h" +#include "libaegisub/util_osx.h" #include "libaegisub/exception.h" @@ -132,4 +133,12 @@ std::pair ifind(std::string const& haystack, std::string const& return ret; } -} } +} + +#ifndef __APPLE__ +namespace osx { +AppNapDisabler::AppNapDisabler(std::string const& reason) { } +AppNapDisabler::~AppNapDisabler() { } +} +#endif +} diff --git a/aegisub/libaegisub/include/libaegisub/util_osx.h b/aegisub/libaegisub/include/libaegisub/util_osx.h index 5ec61a027..1e17e83d9 100644 --- a/aegisub/libaegisub/include/libaegisub/util_osx.h +++ b/aegisub/libaegisub/include/libaegisub/util_osx.h @@ -28,11 +28,17 @@ /// When linking with this library, be sure to add '-framework CoreFoundation' /// to the GCC commandline. -#ifdef __APPLE__ - #include namespace agi { + namespace osx { + class AppNapDisabler { + void *handle; + public: + AppNapDisabler(std::string const& reason); + ~AppNapDisabler(); + }; + } namespace util { /// @brief Get the full name of bundle. /// @return Full name of bundle. @@ -91,7 +97,5 @@ std::string OSX_GetBundleAuxillaryExecutablePath(std::string const& executableNa /// @param location URI of file /// @note If this is a FILE or DIRECTORY the path must be ABSOLUTE no 'file://' void OSX_OpenLocation(std::string const& location); - } // namespace io + } // namespace util } // namespace agi - -#endif diff --git a/aegisub/libaegisub/osx/util.mm b/aegisub/libaegisub/osx/util.mm index 9ed4f6274..28162902c 100644 --- a/aegisub/libaegisub/osx/util.mm +++ b/aegisub/libaegisub/osx/util.mm @@ -28,6 +28,22 @@ static std::string EmptyIfNil(NSString *string) { } namespace agi { + namespace osx { +AppNapDisabler::AppNapDisabler(std::string const& reason) : handle(nullptr) { + auto processInfo = [NSProcessInfo processInfo]; + if ([processInfo respondsToSelector:@selector(beginActivityWithOptions:reason:)]) + handle = [[processInfo beginActivityWithOptions:NSActivityUserInitiatedAllowingIdleSystemSleep + reason:[NSString stringWithUTF8String:reason.c_str()]] + retain]; +} + +AppNapDisabler::~AppNapDisabler() { + auto processInfo = [NSProcessInfo processInfo]; + if (handle && [processInfo respondsToSelector:@selector(endActivity:)]) + [processInfo endActivity:(id)handle]; + [(id)handle release]; +} + } namespace util { std::string OSX_GetBundlePath() { diff --git a/aegisub/src/dialog_progress.cpp b/aegisub/src/dialog_progress.cpp index 8d37e7333..9f4cf5efa 100644 --- a/aegisub/src/dialog_progress.cpp +++ b/aegisub/src/dialog_progress.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -115,7 +116,9 @@ void DialogProgress::Run(std::function task, int prior DialogProgressSink ps(this); this->ps = &ps; + auto current_title = from_wx(title->GetLabelText()); agi::dispatch::Background().Async([=]{ + agi::osx::AppNapDisabler app_nap_disabler(current_title); try { task(this->ps); }