mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
[Common]NotificationUtil helper class with FileWatcher (#36720)
* add NotificationUtil helper with file watcher and cache * fix spellcheck * indentation
This commit is contained in:
committed by
GitHub
parent
403060e109
commit
9e1242e8d5
56
src/common/notifications/NotificationUtil.cpp
Normal file
56
src/common/notifications/NotificationUtil.cpp
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,13 +27,14 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="notifications.h" />
|
||||
<ClInclude Include="NotificationUtil.h" />
|
||||
<ClInclude Include="dont_show_again.h" />
|
||||
<ClInclude Include="NotificationUtil.h" />
|
||||
<ClInclude Include="pch.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dont_show_again.cpp" />
|
||||
<ClCompile Include="notifications.cpp" />
|
||||
<ClCompile Include="NotificationUtil.cpp" />
|
||||
<ClCompile Include="pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(UsePrecompiledHeaders)' != 'false'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
|
||||
Reference in New Issue
Block a user