-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Common]NotificationUtil helper class with FileWatcher (#36720)
* add NotificationUtil helper with file watcher and cache * fix spellcheck * indentation
- Loading branch information
1 parent
403060e
commit 9e1242e
Showing
11 changed files
with
113 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,3 @@ | |
#include <fstream> | ||
|
||
#include <common/logger/logger.h> | ||
#include <wil/filesystem.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "pch.h" | ||
#include "NotificationUtil.h" | ||
|
||
#include <common/notifications/notifications.h> | ||
#include <common/notifications/dont_show_again.h> | ||
#include <common/utils/resources.h> | ||
#include <common/SettingsAPI/settings_helpers.h> | ||
|
||
// Non-Localizable strings | ||
namespace NonLocalizable | ||
{ | ||
const wchar_t RunAsAdminInfoPage[] = L"https://aka.ms/powertoysDetectedElevatedHelp"; | ||
const wchar_t ToastNotificationButtonUrl[] = L"powertoys://cant_drag_elevated_disable/"; | ||
} | ||
|
||
namespace notifications | ||
{ | ||
NotificationUtil::NotificationUtil() | ||
{ | ||
ReadSettings(); | ||
auto settingsFileName = PTSettingsHelper::get_powertoys_general_save_file_location(); | ||
|
||
m_settingsFileWatcher = std::make_unique<FileWatcher>(settingsFileName, [this]() { | ||
ReadSettings(); | ||
}); | ||
} | ||
|
||
NotificationUtil::~NotificationUtil() | ||
{ | ||
m_settingsFileWatcher.reset(); | ||
} | ||
|
||
void NotificationUtil::WarnIfElevationIsRequired(std::wstring title, std::wstring message, std::wstring button1, std::wstring button2) | ||
{ | ||
if (m_warningsElevatedApps && !m_warningShown && !is_toast_disabled(ElevatedDontShowAgainRegistryPath, ElevatedDisableIntervalInDays)) | ||
{ | ||
std::vector<action_t> actions = { | ||
link_button{ button1, NonLocalizable::RunAsAdminInfoPage }, | ||
link_button{ button2, NonLocalizable::ToastNotificationButtonUrl } | ||
}; | ||
|
||
show_toast_with_activations(message, | ||
title, | ||
{}, | ||
std::move(actions)); | ||
|
||
m_warningShown = true; | ||
} | ||
} | ||
|
||
void NotificationUtil::ReadSettings() | ||
{ | ||
auto settings = PTSettingsHelper::load_general_settings(); | ||
m_warningsElevatedApps = settings.GetNamedBoolean(L"enable_warnings_elevated_apps", true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,22 @@ | ||
#pragma once | ||
|
||
#include <common/notifications/notifications.h> | ||
#include <common/notifications/dont_show_again.h> | ||
#include <common/utils/resources.h> | ||
#include <common/SettingsAPI/settings_helpers.h> | ||
|
||
#include "Generated Files/resource.h" | ||
#include <common/SettingsAPI/FileWatcher.h> | ||
|
||
namespace notifications | ||
{ | ||
// Non-Localizable strings | ||
namespace NonLocalizable | ||
class NotificationUtil | ||
{ | ||
const wchar_t RunAsAdminInfoPage[] = L"https://aka.ms/powertoysDetectedElevatedHelp"; | ||
const wchar_t ToastNotificationButtonUrl[] = L"powertoys://cant_drag_elevated_disable/"; | ||
} | ||
public: | ||
NotificationUtil(); | ||
~NotificationUtil(); | ||
|
||
inline void WarnIfElevationIsRequired(std::wstring title, std::wstring message, std::wstring button1, std::wstring button2) | ||
{ | ||
using namespace NonLocalizable; | ||
void WarnIfElevationIsRequired(std::wstring title, std::wstring message, std::wstring button1, std::wstring button2); | ||
|
||
auto settings = PTSettingsHelper::load_general_settings(); | ||
auto enableWarningsElevatedApps = settings.GetNamedBoolean(L"enable_warnings_elevated_apps", true); | ||
private: | ||
std::unique_ptr<FileWatcher> m_settingsFileWatcher; | ||
bool m_warningsElevatedApps; | ||
bool m_warningShown = false; | ||
|
||
static bool warning_shown = false; | ||
if (enableWarningsElevatedApps && !warning_shown && !is_toast_disabled(ElevatedDontShowAgainRegistryPath, ElevatedDisableIntervalInDays)) | ||
{ | ||
std::vector<action_t> actions = { | ||
link_button{ button1, RunAsAdminInfoPage }, | ||
link_button{ button2, ToastNotificationButtonUrl } | ||
}; | ||
show_toast_with_activations(message, | ||
title, | ||
{}, | ||
std::move(actions)); | ||
warning_shown = true; | ||
} | ||
} | ||
} | ||
void ReadSettings(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters