diff --git a/src/common/SettingsAPI/FileWatcher.h b/src/common/SettingsAPI/FileWatcher.h index 073a33a0c81c..5ac2a55f0ef8 100644 --- a/src/common/SettingsAPI/FileWatcher.h +++ b/src/common/SettingsAPI/FileWatcher.h @@ -9,6 +9,9 @@ #include #include +#include +#include + class FileWatcher { std::wstring m_path; diff --git a/src/common/SettingsAPI/pch.h b/src/common/SettingsAPI/pch.h index 86f6eff28321..8644dfaaafc0 100644 --- a/src/common/SettingsAPI/pch.h +++ b/src/common/SettingsAPI/pch.h @@ -10,4 +10,3 @@ #include #include -#include diff --git a/src/common/notifications/NotificationUtil.cpp b/src/common/notifications/NotificationUtil.cpp new file mode 100644 index 000000000000..04781e46df40 --- /dev/null +++ b/src/common/notifications/NotificationUtil.cpp @@ -0,0 +1,56 @@ +#include "pch.h" +#include "NotificationUtil.h" + +#include +#include +#include +#include + +// 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(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 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); + } +} diff --git a/src/common/notifications/NotificationUtil.h b/src/common/notifications/NotificationUtil.h index 8fcf3d91d8c3..736eabaf0c7d 100644 --- a/src/common/notifications/NotificationUtil.h +++ b/src/common/notifications/NotificationUtil.h @@ -1,40 +1,22 @@ #pragma once -#include -#include -#include -#include - -#include "Generated Files/resource.h" +#include 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 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 actions = { - link_button{ button1, RunAsAdminInfoPage }, - link_button{ button2, ToastNotificationButtonUrl } - }; - show_toast_with_activations(message, - title, - {}, - std::move(actions)); - warning_shown = true; - } - } -} \ No newline at end of file + void ReadSettings(); + }; +} diff --git a/src/common/notifications/notifications.vcxproj b/src/common/notifications/notifications.vcxproj index 8c6808cf6c9e..b55d67e7b344 100644 --- a/src/common/notifications/notifications.vcxproj +++ b/src/common/notifications/notifications.vcxproj @@ -27,13 +27,14 @@ - + + Create diff --git a/src/modules/Workspaces/WorkspacesSnapshotTool/SnapshotUtils.cpp b/src/modules/Workspaces/WorkspacesSnapshotTool/SnapshotUtils.cpp index ddbf0f3cf780..c7cb45fc1558 100644 --- a/src/modules/Workspaces/WorkspacesSnapshotTool/SnapshotUtils.cpp +++ b/src/modules/Workspaces/WorkspacesSnapshotTool/SnapshotUtils.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -11,6 +12,8 @@ #include #include +#include "Generated Files/resource.h" + #pragma comment(lib, "ntdll.lib") namespace SnapshotUtils @@ -73,7 +76,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(); + + 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)); diff --git a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp index 491a0bdf4272..ea4b6a23d345 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp +++ b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -36,7 +35,8 @@ AlwaysOnTop::AlwaysOnTop(bool useLLKH, DWORD mainThreadId) : SettingsObserver({SettingId::FrameEnabled, SettingId::Hotkey, SettingId::ExcludeApps}), m_hinstance(reinterpret_cast(&__ImageBase)), m_useCentralizedLLKH(useLLKH), - m_mainThreadId(mainThreadId) + m_mainThreadId(mainThreadId), + m_notificationUtil(std::make_unique()) { 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,7 @@ 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(); } diff --git a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h index da14d508b2c1..0505c837a282 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h +++ b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h @@ -9,6 +9,7 @@ #include #include +#include class AlwaysOnTop : public SettingsObserver { @@ -53,6 +54,7 @@ class AlwaysOnTop : public SettingsObserver std::thread m_thread; const bool m_useCentralizedLLKH; bool m_running = true; + std::unique_ptr m_notificationUtil; LRESULT WndProc(HWND, UINT, WPARAM, LPARAM) noexcept; void HandleWinHookEvent(WinHookEvent* data) noexcept; diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp b/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp index d0839f2844d8..1956c57a97fb 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp +++ b/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -185,6 +186,8 @@ struct FancyZones : public winrt::implements m_notificationUtil; + // If non-recoverable error occurs, trigger disabling of entire FancyZones. static std::function disableModuleCallback; @@ -266,6 +269,8 @@ FancyZones::Run() noexcept } }); + m_notificationUtil = std::make_unique(); + 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) diff --git a/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.cpp b/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.cpp index e127371347fb..71cf53ef87da 100644 --- a/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.cpp +++ b/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.cpp @@ -11,7 +11,7 @@ #include #include -#include +#include WindowMouseSnap::WindowMouseSnap(HWND window, const std::unordered_map>& activeWorkAreas) : m_window(window), @@ -27,7 +27,7 @@ WindowMouseSnap::~WindowMouseSnap() ResetWindowTransparency(); } -std::unique_ptr WindowMouseSnap::Create(HWND window, const std::unordered_map>& activeWorkAreas) +std::unique_ptr WindowMouseSnap::Create(HWND window, const std::unordered_map>& activeWorkAreas, notifications::NotificationUtil* notificationUtil) { if (FancyZonesWindowUtils::IsCursorTypeIndicatingSizeEvent() || !FancyZonesWindowProcessing::IsProcessableManually(window)) { @@ -36,8 +36,12 @@ std::unique_ptr WindowMouseSnap::Create(HWND window, const std: if (!is_process_elevated() && IsProcessOfWindowElevated(window)) { - // 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)); + if (notificationUtil != nullptr) + { + // Notifies user if unable to drag elevated window + 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; } diff --git a/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.h b/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.h index cf8f2615ac2a..e7e5365b19d9 100644 --- a/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.h +++ b/src/modules/fancyzones/FancyZonesLib/WindowMouseSnap.h @@ -1,6 +1,7 @@ #pragma once #include +#include class WorkArea; @@ -9,7 +10,7 @@ class WindowMouseSnap WindowMouseSnap(HWND window, const std::unordered_map>& activeWorkAreas); public: - static std::unique_ptr Create(HWND window, const std::unordered_map>& activeWorkAreas); + static std::unique_ptr Create(HWND window, const std::unordered_map>& activeWorkAreas, notifications::NotificationUtil* notificationUtil); ~WindowMouseSnap(); bool MoveSizeStart(HMONITOR monitor, bool isSnapping);