-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Common] NotificationUtil helper class with FileWatcher #36720
Open
davidegiacometti
wants to merge
3
commits into
main
Choose a base branch
from
users/davidegiacometti/issue-36586
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−46
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]() { | ||
Check failure Code scanning / check-spelling Unrecognized Spelling
[settingsfile](#security-tab) is not a recognized word. \(unrecognized-spelling\)
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / check-spelling
Unrecognized Spelling