Respect macOS do not disturb settings. Fix #3095.

This commit is contained in:
John Preston 2017-03-08 10:31:29 +03:00
parent b5d9eee489
commit a5c83467d6
3 changed files with 35 additions and 10 deletions

View File

@ -25,13 +25,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace Platform {
namespace Notifications {
inline bool SkipAudio() {
return false;
}
inline bool SkipToast() {
return false;
}
bool SkipAudio();
bool SkipToast();
class Manager : public Window::Notifications::NativeManager {
public:

View File

@ -30,7 +30,22 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace {
NeverFreedPointer<Platform::Notifications::Manager> ManagerInstance;
static constexpr auto kQuerySettingsEachMs = 1000;
auto DoNotDisturbEnabled = false;
auto LastSettingsQueryMs = 0;
void queryDoNotDisturbState() {
auto ms = getms(true);
if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + kQuerySettingsEachMs) {
return;
}
LastSettingsQueryMs = ms;
id userDefaultsValue = [[[NSUserDefaults alloc] initWithSuiteName:@"com.apple.notificationcenterui"] objectForKey:@"doNotDisturb"];
DoNotDisturbEnabled = [userDefaultsValue boolValue];
}
using Manager = Platform::Notifications::Manager;
} // namespace
@ -98,6 +113,21 @@ std::weak_ptr<Manager*> _manager;
namespace Platform {
namespace Notifications {
bool SkipAudio() {
queryDoNotDisturbState();
return DoNotDisturbEnabled;
}
bool SkipToast() {
if (Supported()) {
// Do not skip native notifications because of Do not disturb.
// They respect this setting anyway.
return false;
}
queryDoNotDisturbState();
return DoNotDisturbEnabled;
}
bool Supported() {
return (cPlatform() != dbipMacOld);
}

View File

@ -637,12 +637,12 @@ void queryUserNotificationState() {
}
}
static constexpr int QuerySettingsEachMs = 1000;
static constexpr auto kQuerySettingsEachMs = 1000;
TimeMs LastSettingsQueryMs = 0;
void querySystemNotificationSettings() {
auto ms = getms(true);
if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + QuerySettingsEachMs) {
if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + kQuerySettingsEachMs) {
return;
}
LastSettingsQueryMs = ms;