Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:支持通过检测Windows小组件是否安装来调整任务栏窗口靠左显示的位置 #1582

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TrafficMonitor/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void DispStrings::operator=(const DispStrings& disp_str)
{
map_str = disp_str.map_str;
//如果赋值的字符串是定义的无效字符串,则不赋值
for (auto& iter = map_str.begin(); iter != map_str.end(); ++iter)
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
{
if (iter->second == NONE_STR)
iter->second.clear();
Expand All @@ -78,7 +78,7 @@ void DispStrings::operator=(const DispStrings& disp_str)

bool DispStrings::IsInvalid() const
{
for (auto& iter = map_str.begin(); iter != map_str.end(); ++iter)
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
{
if (iter->second == NONE_STR)
return true;
Expand Down
2 changes: 2 additions & 0 deletions TrafficMonitor/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ struct TaskBarSettingData : public PublicSettingData

bool disable_d2d{ false };//是否禁用d2d绘图
DWORD update_layered_window_error_code{0}; // 使用D2D1渲染时,UpdateLayeredWindowIndirect失败的错误代码,会在关闭任务栏窗口时被重置为0

bool is_windows_web_experience_detected{ false }; //是否检测到Windows Web Experience小组件安装信息
};

//选项设置中“常规设置”的数据
Expand Down
14 changes: 8 additions & 6 deletions TrafficMonitor/DrawCommonFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ using UniqueIDrawBuffer = std::unique_ptr<void, StackObjectDeleter<IDrawBuffer>>
* @brief 新增渲染器类型后,将新类型写到下面类型的模板参数中
*
*/
using DrawCommonUnionStorage = variant_storage<CDrawCommon, CTaskBarDlgDrawCommon>;
using DrawCommonUnionStorage = AlignedUnionStorage<CDrawCommon, CTaskBarDlgDrawCommon>;
/**
* @brief 新增缓冲器(即析构时提交窗口绘制内容到系统的对象)类型后,将新类型写入到下面类型的模板参数中
*
*/
using DrawBufferUnionStorage = variant_storage<CDrawDoubleBuffer, CTaskBarDlgDrawBuffer, CTaskBarDlgDrawBufferUseDComposition>;
using DrawBufferUnionStorage = AlignedUnionStorage<CDrawDoubleBuffer, CTaskBarDlgDrawBuffer, CTaskBarDlgDrawBufferUseDComposition>;
struct InvolvedDrawCommonStorages
{
DrawBufferUnionStorage m_draw_buffer_union_storage;
DrawCommonUnionStorage m_draw_common_union_storage;
~InvolvedDrawCommonStorages() = delete;
DrawBufferUnionStorage m_draw_buffer_union_storage{};
DrawCommonUnionStorage m_draw_common_union_storage{};
};
/**
* @brief 具有DrawCommon和DrawBuffer栈内存以及它们对应独占指针的栈内存块,
Expand All @@ -43,10 +42,13 @@ struct InvolvedDrawCommonStorages
*/
struct AllInvolvedDrawCommonObjectsStorage
{
std_aligned_storage<InvolvedDrawCommonStorages> m_storage{};
InvolvedDrawCommonStorages m_storage{};
// 先析构DrawCommon再析构DrawBuffer
UniqueIDrawBuffer m_unique_draw_buffer{};
UniqueIDrawCommon m_unique_draw_common{};

AllInvolvedDrawCommonObjectsStorage() = default;
~AllInvolvedDrawCommonObjectsStorage() = default;
// 禁用复制移动
AllInvolvedDrawCommonObjectsStorage(const AllInvolvedDrawCommonObjectsStorage&) = delete;
AllInvolvedDrawCommonObjectsStorage& operator=(const AllInvolvedDrawCommonObjectsStorage&) = delete;
Expand Down
23 changes: 11 additions & 12 deletions TrafficMonitor/IDrawCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,17 @@ namespace DrawCommonHelper
constexpr BYTE GDI_MODIFIED_FLAG = 0x00;
}

template <class... Types>
struct variant_storage
template <class... Ts>
class AlignedUnionStorage
{
~variant_storage() = delete;
};
template <class First, class... Other>
struct variant_storage<First, Other...>
{
union
private:
alignas(Ts...) std::byte m_buffer[(std::max)({sizeof(Ts)...})]{};

public:
AlignedUnionStorage() = default;
~AlignedUnionStorage() = default;
std::byte* operator&() noexcept
{
First m_head;
variant_storage<Other...> m_tail;
};
~variant_storage() = delete;
return m_buffer;
}
};
46 changes: 28 additions & 18 deletions TrafficMonitor/Nullable.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
#pragma once
#include <cstddef>
#include <functional>
#include <type_traits>
#include <stdexcept>
#include <tuple>

template <class T>
using std_aligned_storage = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
class AlignedStorage
{
private:
alignas(T) std::byte m_buffer[sizeof(T)]{};

public:
AlignedStorage() = default;
~AlignedStorage() = default;
T& Get() noexcept
{
return *reinterpret_cast<T*>(m_buffer);
};
const T& Get() const noexcept
{
return *reinterpret_cast<T*>(m_buffer);
};
std::byte* operator&() noexcept
{
return m_buffer;
};
};

//皆不保证多线程操作安全
// 皆不保证多线程操作安全
/**
* @brief CNullable<T>的默认删除器,注意重载operator()的参数为T*
*
Expand All @@ -30,7 +51,7 @@ struct NullableDefaultDeleter
template <class T, class Deleter = NullableDefaultDeleter<T>>
class CNullable
{
using StorageType = std_aligned_storage<T>;
using StorageType = AlignedStorage<T>;
using RawType = std::decay_t<T>;
template <class... Args>
static void EmplaceAt(RawType* p_object, Args&&... args)
Expand Down Expand Up @@ -152,17 +173,6 @@ class CNullable
private:
StorageType m_storage;

auto GetStoragePointer() const noexcept
-> const StorageType*
{
return std::addressof(m_storage);
}
auto GetStoragePointer() noexcept
-> StorageType*
{
return std::addressof(m_storage);
}

public:
explicit StorageAndEboDeleter(Deleter deleter)
: Deleter{deleter} {}
Expand All @@ -172,19 +182,19 @@ class CNullable

const T& GetUnsafe() const noexcept
{
return *static_cast<const T*>(static_cast<const void*>(GetStoragePointer()));
return *static_cast<const T*>(static_cast<const void*>(&m_storage));
}
T& GetUnsafe() noexcept
{
return *static_cast<T*>(static_cast<void*>(GetStoragePointer()));
return *static_cast<T*>(static_cast<void*>(&m_storage));
}
const T* GetPointerUnsafe() const noexcept
{
return static_cast<const T*>(static_cast<const void*>(GetStoragePointer()));
return static_cast<const T*>(static_cast<const void*>(&m_storage));
}
T* GetPointerUnsafe() noexcept
{
return static_cast<T*>(static_cast<void*>(GetStoragePointer()));
return static_cast<T*>(static_cast<void*>(&m_storage));
}
};
bool m_has_value{false};
Expand Down
11 changes: 10 additions & 1 deletion TrafficMonitor/TaskBarDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "WIC.h"
#include "Nullable.hpp"
#include "DrawCommonFactory.h"
#include "WindowsWebExperienceDetector.h"

#ifdef DEBUG
// DX调试信息捕获
Expand Down Expand Up @@ -93,7 +94,7 @@ void CTaskBarDlg::ShowInfo(CDC* pDC)
#endif
// 初始化DrawBuffer和DrawCommon栈内存
AllInvolvedDrawCommonObjectsStorage all_involved_draw_common_objects{};
IDrawCommon* draw_common_interface;
IDrawCommon* draw_common_interface{nullptr};
std::tie(std::ignore, draw_common_interface) =
GetInterfaceFromAllInvolvedDrawCommonObjects(
all_involved_draw_common_objects,
Expand Down Expand Up @@ -792,6 +793,11 @@ int CTaskBarDlg::DPI(int pixel) const
return static_cast<int>(m_taskbar_dpi) * pixel / 96;
}

LONG CTaskBarDlg::DPI(LONG pixel) const
{
return static_cast<LONG>(m_taskbar_dpi) * pixel / 96;
}

void CTaskBarDlg::DPI(CRect& rect) const
{
rect.left = DPI(rect.left);
Expand Down Expand Up @@ -1193,6 +1199,9 @@ BOOL CTaskBarDlg::OnInitDialog()
CDialogEx::OnInitDialog();

// TODO: 在此添加额外的初始化
// 检测系统是否安装了 MicrosoftWindows.Client.WebExperience (aka Windows Web Experience Pack)
theApp.m_taskbar_data.is_windows_web_experience_detected =
WindowsWebExperienceDetector::IsDetected();
// 根据任务栏窗口的设置禁用必要的渲染选项,仅透明且支持D2D渲染时才会使用D2D渲染
DisableRenderFeatureIfNecessary(m_supported_render_enums);
//设置隐藏任务栏图标
Expand Down
1 change: 1 addition & 0 deletions TrafficMonitor/TaskBarDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CTaskBarDlg : public CDialogEx
void SetDPI(UINT dpi);
UINT DPI(UINT pixel) const;
int DPI(int pixel) const;
LONG DPI(LONG pixel) const;
void DPI(CRect& rect) const;

static void DPIFromRect(const RECT& rect, UINT* out_dpi_x, UINT* out_dpi_y);
Expand Down
4 changes: 2 additions & 2 deletions TrafficMonitor/TaskBarDlgDrawCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ class CD2D1DeviceContextWindowSupport
auto GetD3D10Device1()
-> Microsoft::WRL::ComPtr<ID3D10Device1>;
};
using VariantStorage = std_aligned_storage<variant_storage<
using VariantStorage = AlignedUnionStorage<
CNullable<CDCompositionHelper>,
CNullable<CD3DHelper>>>;
CNullable<CD3DHelper>>;

CTaskBarDlgDrawCommonSupport& m_ref_task_bar_dlg_draw_common_support;
D2D1_SIZE_U m_size{0, 0};
Expand Down
2 changes: 2 additions & 0 deletions TrafficMonitor/TrafficMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "auto_start_helper.h"
#include "AppAlreadyRuningDlg.h"
#include "WindowsSettingHelper.h"
#include "winrt/base.h"

#ifdef _DEBUG
#define new DEBUG_NEW
Expand Down Expand Up @@ -41,6 +42,7 @@ CTrafficMonitorApp::CTrafficMonitorApp()
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
CRASHREPORT::StartCrashReport();
winrt::init_apartment();
}

void CTrafficMonitorApp::LoadConfig()
Expand Down
10 changes: 10 additions & 0 deletions TrafficMonitor/TrafficMonitor.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<SDLCheck>true</SDLCheck>
<TreatWarningAsError>false</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -234,6 +235,7 @@
<SDLCheck>true</SDLCheck>
<TreatWarningAsError>false</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -270,6 +272,7 @@
<PreprocessorDefinitions>_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -307,6 +310,7 @@
<PreprocessorDefinitions>_WINDOWS;_DEBUG;%(PreprocessorDefinitions);WITHOUT_TEMPERATURE</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -346,6 +350,7 @@
<SDLCheck>true</SDLCheck>
<AdditionalOptions>$(ExternalCompilerOptions) %(AdditionalOptions)</AdditionalOptions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -388,6 +393,7 @@
<SDLCheck>true</SDLCheck>
<AdditionalOptions>$(ExternalCompilerOptions) %(AdditionalOptions)</AdditionalOptions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -428,6 +434,7 @@
<PreprocessorDefinitions>_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -469,6 +476,7 @@
<PreprocessorDefinitions>_WINDOWS;NDEBUG;%(PreprocessorDefinitions);WITHOUT_TEMPERATURE</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -588,6 +596,7 @@
<ClInclude Include="UpdateHelper.h" />
<ClInclude Include="WIC.h" />
<ClInclude Include="WindowsSettingHelper.h" />
<ClInclude Include="WindowsWebExperienceDetector.h" />
<ClInclude Include="WinVersionHelper.h" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -684,6 +693,7 @@
<ClCompile Include="UpdateHelper.cpp" />
<ClCompile Include="WIC.cpp" />
<ClCompile Include="WindowsSettingHelper.cpp" />
<ClCompile Include="WindowsWebExperienceDetector.cpp" />
<ClCompile Include="WinVersionHelper.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions TrafficMonitor/TrafficMonitor.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@
<Filter Include="源文件和头文件\公共的类\RenderAPISupport\DxgiSupport2">
<UniqueIdentifier>{059ab3fb-f8b3-4cae-b5d3-72bd857e5197}</UniqueIdentifier>
</Filter>
<Filter Include="源文件和头文件\公共的类\WindowsWebExperienceDetector">
<UniqueIdentifier>{4f233c31-4fef-48c1-8176-845ddd21debb}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
Expand Down Expand Up @@ -484,6 +487,9 @@
<ClInclude Include="SupportedRenderEnums.h">
<Filter>源文件和头文件\公共的类\DrawCommon</Filter>
</ClInclude>
<ClInclude Include="WindowsWebExperienceDetector.h">
<Filter>源文件和头文件\公共的类\WindowsWebExperienceDetector</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="StaticEx.cpp">
Expand Down Expand Up @@ -714,6 +720,9 @@
<ClCompile Include="SupportedRenderEnums.cpp">
<Filter>源文件和头文件\公共的类\DrawCommon</Filter>
</ClCompile>
<ClCompile Include="WindowsWebExperienceDetector.cpp">
<Filter>源文件和头文件\公共的类\WindowsWebExperienceDetector</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="TrafficMonitor.rc">
Expand Down
2 changes: 2 additions & 0 deletions TrafficMonitor/WindowsSettingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ bool CWindowsSettingHelper::IsDotNetFramework4Point5Installed()

bool CWindowsSettingHelper::IsTaskbarWidgetsBtnShown()
{
if (!theApp.m_taskbar_data.is_windows_web_experience_detected)
return false;
DWORD data{};
if (!GetDWORDRegKeyData(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", L"TaskbarDa", data))
return true;
Expand Down
23 changes: 23 additions & 0 deletions TrafficMonitor/WindowsWebExperienceDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "stdafx.h"
#include "WindowsWebExperienceDetector.h"
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.Management.Deployment.h>
#include <winrt/Windows.Storage.h>
#include "TrafficMonitor.h"

bool WindowsWebExperienceDetector::IsDetected() noexcept
{
if (theApp.m_win_version.IsWindows11OrLater())
{
auto manager = winrt::Windows::Management::Deployment::PackageManager{};
auto packages = manager.FindPackagesForUser(
{},
winrt::hstring{L"MicrosoftWindows.Client.WebExperience"},
winrt::hstring{
L"CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US"});
auto result = packages.First().HasCurrent();
return result;
}
return false;
}
Loading