Skip to content

Commit

Permalink
refactor: 优化获取窗口图标
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Jun 30, 2024
1 parent bc3f7a9 commit 4f96625
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Magpie.App/AboutViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ AboutViewModel::AboutViewModel() {
([](AboutViewModel* that)->fire_and_forget {
auto weakThis = that->get_weak();
SoftwareBitmapSource bitmap;
co_await bitmap.SetBitmapAsync(IconHelper::ExtractIconFromExe(
Win32Utils::GetExePath().c_str(), 256, USER_DEFAULT_SCREEN_DPI));
co_await bitmap.SetBitmapAsync(
IconHelper::ExtractIconFromExe(Win32Utils::GetExePath().c_str(), 256));

if (!weakThis.get()) {
co_return;
Expand Down
5 changes: 3 additions & 2 deletions src/Magpie.App/CandidateWindowItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ fire_and_forget CandidateWindowItem::_ResolveWindow(bool resolveIcon, bool resol
}

SoftwareBitmap iconBitmap{ nullptr };
const uint32_t iconSize = (uint32_t)std::lround(16 * dpi / double(USER_DEFAULT_SCREEN_DPI));

if (isPackaged) {
std::variant<std::wstring, SoftwareBitmap> uwpIcon =
reader.GetIcon((uint32_t)std::ceil(dpi * 16.0 / USER_DEFAULT_SCREEN_DPI), isLightTheme, true);
reader.GetIcon(iconSize, isLightTheme, true);
if (uwpIcon.index() == 1) {
iconBitmap = std::get<1>(uwpIcon);
}
} else {
iconBitmap = IconHelper::ExtractIconFormWnd(hWnd, 16, dpi);
iconBitmap = IconHelper::ExtractIconFormWnd(hWnd, iconSize);
}

// 切换到主线程
Expand Down
11 changes: 4 additions & 7 deletions src/Magpie.App/IconHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ static SIZE GetSizeOfIcon(HICON hIcon) noexcept {

static HICON GetHIconOfWnd(HWND hWnd, LONG preferredSize) noexcept {
HICON result = NULL;

HICON candidateSmallIcon = NULL;

result = (HICON)SendMessage(hWnd, WM_GETICON, ICON_SMALL, 0);
Expand Down Expand Up @@ -180,17 +179,15 @@ static HICON GetHIconOfWnd(HWND hWnd, LONG preferredSize) noexcept {
return GetHIconOfWnd(hwndOwner, preferredSize);
}

SoftwareBitmap IconHelper::ExtractIconFormWnd(HWND hWnd, uint32_t preferredSize, uint32_t dpi) {
if (HICON hIcon = GetHIconOfWnd(hWnd, std::lround(preferredSize * dpi / double(USER_DEFAULT_SCREEN_DPI)))) {
SoftwareBitmap IconHelper::ExtractIconFormWnd(HWND hWnd, uint32_t preferredSize) {
if (HICON hIcon = GetHIconOfWnd(hWnd, (LONG)preferredSize)) {
return HIcon2SoftwareBitmap(hIcon);
}

return ExtractIconFromExe(Win32Utils::GetPathOfWnd(hWnd).c_str(), preferredSize, dpi);
return ExtractIconFromExe(Win32Utils::GetPathOfWnd(hWnd).c_str(), preferredSize);
}

SoftwareBitmap IconHelper::ExtractIconFromExe(const wchar_t* fileName, uint32_t preferredSize, uint32_t dpi) {
preferredSize = (uint32_t)std::lround(preferredSize * dpi / double(USER_DEFAULT_SCREEN_DPI));

SoftwareBitmap IconHelper::ExtractIconFromExe(const wchar_t* fileName, uint32_t preferredSize) {
{
wil::unique_hicon hIcon = NULL;
SHDefExtractIcon(fileName, 0, 0, hIcon.put(), NULL, preferredSize);
Expand Down
4 changes: 2 additions & 2 deletions src/Magpie.App/IconHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace winrt::Magpie::App {

struct IconHelper {
static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFormWnd(HWND hWnd, uint32_t preferredSize, uint32_t dpi);
static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFromExe(const wchar_t* fileName, uint32_t preferredSize, uint32_t dpi);
static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFormWnd(HWND hWnd, uint32_t preferredSize);
static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFromExe(const wchar_t* fileName, uint32_t preferredSize);
static Windows::Graphics::Imaging::SoftwareBitmap ExtractAppIcon(uint32_t preferredSize);
};

Expand Down
7 changes: 3 additions & 4 deletions src/Magpie.App/ProfileViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,25 +773,24 @@ fire_and_forget ProfileViewModel::_LoadIcon(FrameworkElement const& rootPage) {
const bool isPackaged = _data->isPackaged;
const std::wstring path = _data->pathRule;
CoreDispatcher dispatcher = rootPage.Dispatcher();
const uint32_t dpi = (uint32_t)std::lroundf(_displayInformation.LogicalDpi());
const uint32_t iconSize = (uint32_t)std::lroundf(32 * _displayInformation.LogicalDpi() / USER_DEFAULT_SCREEN_DPI);

co_await resume_background();

static constexpr UINT ICON_SIZE = 32;
if (isPackaged) {
AppXReader appxReader;
[[maybe_unused]] bool result = appxReader.Initialize(path);
assert(result);

std::variant<std::wstring, SoftwareBitmap> uwpIcon =
appxReader.GetIcon((uint32_t)std::ceil(dpi * ICON_SIZE / double(USER_DEFAULT_SCREEN_DPI)), preferLightTheme);
appxReader.GetIcon(iconSize, preferLightTheme);
if (uwpIcon.index() == 0) {
iconPath = std::get<0>(uwpIcon);
} else {
iconBitmap = std::get<1>(uwpIcon);
}
} else {
iconBitmap = IconHelper::ExtractIconFromExe(path.c_str(), ICON_SIZE, dpi);
iconBitmap = IconHelper::ExtractIconFromExe(path.c_str(), iconSize);
}

co_await dispatcher;
Expand Down
6 changes: 3 additions & 3 deletions src/Magpie.App/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ fire_and_forget RootPage::_LoadIcon(MUXC::NavigationViewItem const& item, const
bool isPackaged = profile.isPackaged;
std::wstring path = profile.pathRule;
CoreDispatcher dispatcher = Dispatcher();
uint32_t dpi = (uint32_t)std::lroundf(_displayInformation.LogicalDpi());
const uint32_t iconSize = (uint32_t)std::lroundf(16 * _displayInformation.LogicalDpi() / USER_DEFAULT_SCREEN_DPI);

co_await resume_background();

Expand All @@ -329,15 +329,15 @@ fire_and_forget RootPage::_LoadIcon(MUXC::NavigationViewItem const& item, const
AppXReader reader;
if (reader.Initialize(path)) {
std::variant<std::wstring, SoftwareBitmap> uwpIcon =
reader.GetIcon((uint32_t)std::ceil(dpi * 16.0 / USER_DEFAULT_SCREEN_DPI), preferLightTheme);
reader.GetIcon(iconSize, preferLightTheme);
if (uwpIcon.index() == 0) {
iconPath = std::get<0>(uwpIcon);
} else {
iconBitmap = std::get<1>(uwpIcon);
}
}
} else {
iconBitmap = IconHelper::ExtractIconFromExe(path.c_str(), 16, dpi);
iconBitmap = IconHelper::ExtractIconFromExe(path.c_str(), iconSize);
}

co_await dispatcher;
Expand Down

0 comments on commit 4f96625

Please sign in to comment.