Feat: New tray-icon that adapts to theme change

This commit is contained in:
Peter 2024-06-10 23:52:30 +08:00
Родитель e3f5fba870
Коммит 012d7bc839
5 изменённых файлов: 47 добавлений и 1 удалений

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

@ -123,6 +123,28 @@
<ItemGroup>
<EmbeddedResource Include="Resources.resx" />
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="svgs\PowerToysDark.ico">
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">true</DeploymentContent>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">true</DeploymentContent>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">$(OutDir)\svgs</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">$(OutDir)\svgs</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)\svgs</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)\svgs</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="svgs\PowerToysWhite.ico">
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">true</DeploymentContent>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">true</DeploymentContent>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">$(OutDir)\svgs</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">$(OutDir)\svgs</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)\svgs</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)\svgs</DestinationFolders>
</CopyFileToFolders>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Import Project="..\..\deps\spdlog.props" />
<ImportGroup Label="ExtensionTargets">

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

@ -107,6 +107,8 @@
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="svgs\icon.ico" />
<CopyFileToFolders Include="svgs\PowerToysDark.ico" />
<CopyFileToFolders Include="svgs\PowerToysWhite.ico" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

Двоичные данные
src/runner/svgs/PowerToysDark.ico Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 37 KiB

Двоичные данные
src/runner/svgs/PowerToysWhite.ico Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 37 KiB

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

@ -10,6 +10,7 @@
#include <common/version/version.h>
#include <common/logger/logger.h>
#include <common/utils/elevation.h>
#include <common/Themes/theme_listener.h>
#include "bug_report.h"
namespace
@ -36,6 +37,7 @@ namespace
bool double_clicked = false;
POINT tray_icon_click_point;
static ThemeListener theme_listener;
}
// Struct to fill with callback and the data. The window_proc is responsible for cleaning it.
@ -259,10 +261,29 @@ LRESULT __stdcall tray_icon_window_proc(HWND window, UINT message, WPARAM wparam
return DefWindowProc(window, message, wparam, lparam);
}
static HICON get_icon(AppTheme theme)
{
return static_cast<HICON>(LoadImage(NULL,
theme == AppTheme::Dark ?
L"svgs/PowerToysWhite.ico" :
L"svgs/PowerToysDark.ico",
IMAGE_ICON,
0,
0,
LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_SHARED));
}
static void handle_theme_change()
{
tray_icon_data.hIcon = get_icon(theme_listener.AppTheme);
Shell_NotifyIcon(NIM_MODIFY, &tray_icon_data);
}
void start_tray_icon(bool isProcessElevated)
{
auto h_instance = reinterpret_cast<HINSTANCE>(&__ImageBase);
auto icon = LoadIcon(h_instance, MAKEINTRESOURCE(APPICON));
HICON const icon = get_icon(theme_listener.AppTheme);
if (icon)
{
UINT id_tray_icon = 1;
@ -309,6 +330,7 @@ void start_tray_icon(bool isProcessElevated)
ChangeWindowMessageFilterEx(hwnd, WM_COMMAND, MSGFLT_ALLOW, nullptr);
tray_icon_created = Shell_NotifyIcon(NIM_ADD, &tray_icon_data) == TRUE;
theme_listener.AddChangedHandler(&handle_theme_change);
}
}