Skip to content

Commit

Permalink
chore: 杂项优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Jun 4, 2024
1 parent 606ac5d commit 66130ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
36 changes: 19 additions & 17 deletions src/XamlIslandsCpp/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ LRESULT MainWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noex
case WM_GETMINMAXINFO:
{
// 设置窗口最小尺寸
MINMAXINFO* mmi = (MINMAXINFO*)lParam;
mmi->ptMinTrackSize = {
std::lroundf(500 * _CurrentDpi() / float(USER_DEFAULT_SCREEN_DPI)),
std::lroundf(300 * _CurrentDpi() / float(USER_DEFAULT_SCREEN_DPI))
const double dpiScale = _CurrentDpi() / double(USER_DEFAULT_SCREEN_DPI);
((MINMAXINFO*)lParam)->ptMinTrackSize = {
std::lround(500 * dpiScale),
std::lround(300 * dpiScale)
};
return 0;
}
Expand All @@ -185,12 +185,13 @@ LRESULT MainWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noex
HMENU systemMenu = GetSystemMenu(Handle(), FALSE);

// 根据窗口状态更新选项
MENUITEMINFO mii{};
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STATE;
mii.fType = MFT_STRING;
auto setState = [&](UINT item, bool enabled) {
mii.fState = enabled ? MF_ENABLED : MF_DISABLED;
MENUITEMINFO mii{
.cbSize = sizeof(MENUITEMINFO),
.fMask = MIIM_STATE,
.fType = MFT_STRING,
.fState = UINT(enabled ? MF_ENABLED : MF_DISABLED)
};
SetMenuItemInfo(systemMenu, item, FALSE, &mii);
};
const bool isMaximized = _IsMaximized();
Expand Down Expand Up @@ -267,8 +268,9 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
return Content().TitleBar().CaptionButtons().CaptionButtonSize();
}();

const float buttonWidthInPixels = buttonSizeInDips.Width * _CurrentDpi() / USER_DEFAULT_SCREEN_DPI;
const float buttonHeightInPixels = buttonSizeInDips.Height * _CurrentDpi() / USER_DEFAULT_SCREEN_DPI;
const double dpiScale = _CurrentDpi() / double(USER_DEFAULT_SCREEN_DPI);
const double buttonWidthInPixels = buttonSizeInDips.Width * dpiScale;
const double buttonHeightInPixels = buttonSizeInDips.Height * dpiScale;

if (cursorPos.y >= buttonHeightInPixels) {
// 鼠标位于标题按钮下方,如果标题栏很宽,这里也可以拖动
Expand All @@ -281,7 +283,6 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
return HTCLOSE;
} else if (cursorToRight < buttonWidthInPixels * 2) {
// 支持 Win11 的贴靠布局
// FIXME: 最大化时贴靠布局的位置不对,目前没有找到解决方案。似乎只适配了系统原生框架和 UWP
return HTMAXBUTTON;
} else if (cursorToRight < buttonWidthInPixels * 3) {
return HTMINBUTTON;
Expand Down Expand Up @@ -321,11 +322,12 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
// 追踪鼠标以确保鼠标离开标题栏时我们能收到 WM_NCMOUSELEAVE 消息,否则无法
// 可靠的收到这个消息,尤其是在用户快速移动鼠标的时候。
if (!_trackingMouse && msg == WM_NCMOUSEMOVE) {
TRACKMOUSEEVENT ev{};
ev.cbSize = sizeof(TRACKMOUSEEVENT);
ev.dwFlags = TME_LEAVE | TME_NONCLIENT;
ev.hwndTrack = _hwndTitleBar;
ev.dwHoverTime = HOVER_DEFAULT; // 不关心 HOVER 消息
TRACKMOUSEEVENT ev{
.cbSize = sizeof(TRACKMOUSEEVENT),
.dwFlags = TME_LEAVE | TME_NONCLIENT,
.hwndTrack = _hwndTitleBar,
.dwHoverTime = HOVER_DEFAULT // 不关心 HOVER 消息
};
TrackMouseEvent(&ev);
_trackingMouse = true;
}
Expand Down
12 changes: 8 additions & 4 deletions src/XamlIslandsCpp/ThemeHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ static fnAllowDarkModeForWindow AllowDarkModeForWindow = nullptr;
static fnRefreshImmersiveColorPolicyState RefreshImmersiveColorPolicyState = nullptr;
static fnFlushMenuThemes FlushMenuThemes = nullptr;

static bool IsInitialized() {
return SetPreferredAppMode;
}

static void InitApis() noexcept {
if (SetPreferredAppMode) {
if (IsInitialized()) {
return;
}

HMODULE hUxtheme = LoadLibrary(L"uxtheme.dll");
HMODULE hUxtheme = LoadLibraryEx(L"uxtheme.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
assert(hUxtheme);

SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
Expand All @@ -46,15 +50,15 @@ void ThemeHelper::Initialize() noexcept {
}

void ThemeHelper::SetWindowTheme(HWND hWnd, bool darkBorder, bool darkMenu) noexcept {
InitApis();
assert(IsInitialized());

SetPreferredAppMode(darkMenu ? PreferredAppMode::ForceDark : PreferredAppMode::ForceLight);
AllowDarkModeForWindow(hWnd, darkMenu);

// 使标题栏适应黑暗模式
// build 18985 之前 DWMWA_USE_IMMERSIVE_DARK_MODE 的值不同
// https://github.com/MicrosoftDocs/sdk-api/pull/966/files
constexpr const DWORD DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
static constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
BOOL value = darkBorder;
DwmSetWindowAttribute(
hWnd,
Expand Down
4 changes: 2 additions & 2 deletions src/XamlIslandsCpp/XamlApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace XamlIslandsCpp {
// 来自 https://github.com/CommunityToolkit/Microsoft.Toolkit.Win32/blob/6fb2c3e00803ea563af20f6bc9363091b685d81f/Microsoft.Toolkit.Win32.UI.XamlApplication/XamlApplication.cpp#L140
// 参见:https://github.com/microsoft/microsoft-ui-xaml/issues/7260#issuecomment-1231314776
static void FixThreadPoolCrash() noexcept {
LoadLibraryEx(L"twinapi.appcore.dll", nullptr, 0);
LoadLibraryEx(L"threadpoolwinrt.dll", nullptr, 0);
LoadLibraryEx(L"twinapi.appcore.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
LoadLibraryEx(L"threadpoolwinrt.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
}

bool XamlApp::Initialize(HINSTANCE hInstance) {
Expand Down

0 comments on commit 66130ea

Please sign in to comment.