[Common]NotificationUtil helper class with FileWatcher (#36720)

* add NotificationUtil helper with file watcher and cache

* fix spellcheck

* indentation
This commit is contained in:
Davide Giacometti 2025-01-19 16:19:40 +01:00 коммит произвёл GitHub
Родитель 403060e109
Коммит 9e1242e8d5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
11 изменённых файлов: 113 добавлений и 46 удалений

Просмотреть файл

@ -9,6 +9,9 @@
#include <string>
#include <functional>
#include <wil/resource.h>
#include <wil/filesystem.h>
class FileWatcher
{
std::wstring m_path;

Просмотреть файл

@ -10,4 +10,3 @@
#include <fstream>
#include <common/logger/logger.h>
#include <wil/filesystem.h>

Просмотреть файл

@ -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 }
void ReadSettings();
};
show_toast_with_activations(message,
title,
{},
std::move(actions));
warning_shown = true;
}
}
}

Просмотреть файл

@ -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>

Просмотреть файл

@ -3,6 +3,7 @@
#include <common/utils/elevation.h>
#include <common/utils/process_path.h>
#include <common/utils/resources.h>
#include <common/notifications/NotificationUtil.h>
#include <workspaces-common/WindowEnumerator.h>
@ -12,6 +13,8 @@
#include <WorkspacesLib/PwaHelper.h>
#include <WindowProperties/WorkspacesWindowPropertyUtils.h>
#include "Generated Files/resource.h"
#pragma comment(lib, "ntdll.lib")
namespace SnapshotUtils
@ -74,7 +77,9 @@ namespace SnapshotUtils
// Notify the user that running as admin is required to process elevated windows.
if (!is_process_elevated() && IsProcessElevated(pid))
{
notifications::WarnIfElevationIsRequired(GET_RESOURCE_STRING(IDS_PROJECTS),
auto notificationUtil = std::make_unique<notifications::NotificationUtil>();
notificationUtil->WarnIfElevationIsRequired(GET_RESOURCE_STRING(IDS_PROJECTS),
GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED),
GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED_LEARN_MORE),
GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED_DIALOG_DONT_SHOW_AGAIN));

Просмотреть файл

@ -9,7 +9,6 @@
#include <common/utils/process_path.h>
#include <common/utils/elevation.h>
#include <common/notifications/NotificationUtil.h>
#include <Generated Files/resource.h>
#include <interop/shared_constants.h>
@ -36,7 +35,8 @@ AlwaysOnTop::AlwaysOnTop(bool useLLKH, DWORD mainThreadId) :
SettingsObserver({SettingId::FrameEnabled, SettingId::Hotkey, SettingId::ExcludeApps}),
m_hinstance(reinterpret_cast<HINSTANCE>(&__ImageBase)),
m_useCentralizedLLKH(useLLKH),
m_mainThreadId(mainThreadId)
m_mainThreadId(mainThreadId),
m_notificationUtil(std::make_unique<notifications::NotificationUtil>())
{
s_instance = this;
DPIAware::EnableDPIAwarenessForThisProcess();
@ -64,6 +64,7 @@ AlwaysOnTop::AlwaysOnTop(bool useLLKH, DWORD mainThreadId) :
AlwaysOnTop::~AlwaysOnTop()
{
m_running = false;
m_notificationUtil.reset();
if (m_hPinEvent)
{
@ -509,7 +510,10 @@ void AlwaysOnTop::HandleWinHookEvent(WinHookEvent* data) noexcept
{
if (!is_process_elevated() && IsProcessOfWindowElevated(data->hwnd))
{
notifications::WarnIfElevationIsRequired(GET_RESOURCE_STRING(IDS_ALWAYSONTOP), GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED), GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED_LEARN_MORE), GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED_DIALOG_DONT_SHOW_AGAIN));
m_notificationUtil->WarnIfElevationIsRequired(GET_RESOURCE_STRING(IDS_ALWAYSONTOP),
GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED),
GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED_LEARN_MORE),
GET_RESOURCE_STRING(IDS_SYSTEM_FOREGROUND_ELEVATED_DIALOG_DONT_SHOW_AGAIN));
}
RefreshBorders();
}

Просмотреть файл

@ -9,6 +9,7 @@
#include <WindowBorder.h>
#include <common/hooks/WinHookEvent.h>
#include <common/notifications/NotificationUtil.h>
class AlwaysOnTop : public SettingsObserver
{
@ -53,6 +54,7 @@ private:
std::thread m_thread;
const bool m_useCentralizedLLKH;
bool m_running = true;
std::unique_ptr<notifications::NotificationUtil> m_notificationUtil;
LRESULT WndProc(HWND, UINT, WPARAM, LPARAM) noexcept;
void HandleWinHookEvent(WinHookEvent* data) noexcept;

Просмотреть файл

@ -7,6 +7,7 @@
#include <common/utils/EventWaiter.h>
#include <common/utils/winapi_error.h>
#include <common/SettingsAPI/FileWatcher.h>
#include <common/notifications/NotificationUtil.h>
#include <FancyZonesLib/DraggingState.h>
#include <FancyZonesLib/EditorParameters.h>
@ -185,6 +186,8 @@ private:
EventWaiter m_toggleEditorEventWaiter;
std::unique_ptr<notifications::NotificationUtil> m_notificationUtil;
// If non-recoverable error occurs, trigger disabling of entire FancyZones.
static std::function<void()> disableModuleCallback;
@ -266,6 +269,8 @@ FancyZones::Run() noexcept
}
});
m_notificationUtil = std::make_unique<notifications::NotificationUtil>();
SyncVirtualDesktops();
// id format of applied-layouts and app-zone-history was changed in 0.60
@ -288,6 +293,8 @@ FancyZones::Destroy() noexcept
m_window = nullptr;
}
m_notificationUtil.reset();
CoUninitialize();
}
@ -302,7 +309,7 @@ FancyZones::VirtualDesktopChanged() noexcept
void FancyZones::MoveSizeStart(HWND window, HMONITOR monitor)
{
m_windowMouseSnapper = WindowMouseSnap::Create(window, m_workAreaConfiguration.GetAllWorkAreas());
m_windowMouseSnapper = WindowMouseSnap::Create(window, m_workAreaConfiguration.GetAllWorkAreas(), m_notificationUtil.get());
if (m_windowMouseSnapper)
{
if (FancyZonesSettings::settings().spanZonesAcrossMonitors)

Просмотреть файл

@ -11,7 +11,7 @@
#include <FancyZonesLib/trace.h>
#include <common/utils/elevation.h>
#include <common/notifications/NotificationUtil.h>
#include <common/utils/resources.h>
WindowMouseSnap::WindowMouseSnap(HWND window, const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& activeWorkAreas) :
m_window(window),
@ -27,7 +27,7 @@ WindowMouseSnap::~WindowMouseSnap()
ResetWindowTransparency();
}
std::unique_ptr<WindowMouseSnap> WindowMouseSnap::Create(HWND window, const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& activeWorkAreas)
std::unique_ptr<WindowMouseSnap> WindowMouseSnap::Create(HWND window, const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& activeWorkAreas, notifications::NotificationUtil* notificationUtil)
{
if (FancyZonesWindowUtils::IsCursorTypeIndicatingSizeEvent() || !FancyZonesWindowProcessing::IsProcessableManually(window))
{
@ -35,9 +35,16 @@ std::unique_ptr<WindowMouseSnap> WindowMouseSnap::Create(HWND window, const std:
}
if (!is_process_elevated() && IsProcessOfWindowElevated(window))
{
if (notificationUtil != nullptr)
{
// Notifies user if unable to drag elevated window
notifications::WarnIfElevationIsRequired(GET_RESOURCE_STRING(IDS_FANCYZONES), GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED), GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_LEARN_MORE), GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_DIALOG_DONT_SHOW_AGAIN));
notificationUtil->WarnIfElevationIsRequired(GET_RESOURCE_STRING(IDS_FANCYZONES),
GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED),
GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_LEARN_MORE),
GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_DIALOG_DONT_SHOW_AGAIN));
}
return nullptr;
}

Просмотреть файл

@ -1,6 +1,7 @@
#pragma once
#include <FancyZonesLib/HighlightedZones.h>
#include <common/notifications/NotificationUtil.h>
class WorkArea;
@ -9,7 +10,7 @@ class WindowMouseSnap
WindowMouseSnap(HWND window, const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& activeWorkAreas);
public:
static std::unique_ptr<WindowMouseSnap> Create(HWND window, const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& activeWorkAreas);
static std::unique_ptr<WindowMouseSnap> Create(HWND window, const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& activeWorkAreas, notifications::NotificationUtil* notificationUtil);
~WindowMouseSnap();
bool MoveSizeStart(HMONITOR monitor, bool isSnapping);