Skip to content

Commit

Permalink
bug fixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ALTaleX531 committed Jun 12, 2024
1 parent 177e04b commit c924e3a
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 239 deletions.
5 changes: 0 additions & 5 deletions OpenGlass/AeroBackdrop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ namespace OpenGlass::AeroBackdrop
float blurAmount
)
{
if (static_cast<float>(color.A) * colorBalance == 255.f)
{
return compositor.CreateColorBrush(color);
}

wuc::CompositionBrush blurredBackdropBrush
{
[](auto compositor, auto blurAmount) -> wuc::CompositionBrush
Expand Down
2 changes: 1 addition & 1 deletion OpenGlass/BackdropFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ wuc::CompositionBrush BackdropFactory::GetOrCreateBackdropBrush(

if (
policy->AccentState == 4 &&
(policy->dwGradientColor << 24) == 0
policy->dwGradientColor == 0
)
{
brush = compositor.CreateColorBrush({});
Expand Down
152 changes: 95 additions & 57 deletions OpenGlass/BackdropManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,6 @@ namespace OpenGlass::BackdropManager
{
return Configuration::g_overrideBorder || m_data->IsFrameExtendedIntoClientAreaLRB();
}
bool ShouldUpdateBackdropRegion() const
{
// window probably minimized or not visible on the screen,
// no need to update
RECT borderRect{};
THROW_HR_IF_NULL(E_INVALIDARG, m_window->GetActualWindowRect(&borderRect, false, true, false));
auto virtualScreenX{ GetSystemMetrics(SM_XVIRTUALSCREEN) };
auto virtualScreenY{ GetSystemMetrics(SM_YVIRTUALSCREEN) };
auto virtualScreenCX{ GetSystemMetrics(SM_CXVIRTUALSCREEN) };
auto virtualScreenCY{ GetSystemMetrics(SM_CYVIRTUALSCREEN) };
if (
borderRect.right <= virtualScreenX ||
borderRect.bottom <= virtualScreenY ||
borderRect.left >= virtualScreenX + virtualScreenCX ||
borderRect.top >= virtualScreenY + virtualScreenCY ||
!m_data->IsWindowVisibleAndUncloaked()
)
{
return false;
}
#ifdef _DEBUG
OutputDebugStringW(
std::format(
L"borderRect: [{},{},{},{}]\n",
borderRect.left,
borderRect.top,
borderRect.right,
borderRect.bottom
).c_str()
);
#endif
return true;
}
wil::unique_hrgn CompositeNewBackdropRegion() const
{
wil::unique_hrgn compositedRgn{ CreateRectRgn(0, 0, 0, 0) };
Expand Down Expand Up @@ -174,23 +141,15 @@ namespace OpenGlass::BackdropManager
CombineRgn(compositedRgn.get(), compositedRgn.get(), accentRgn.get(), RGN_AND);
}

// TO-DO: round corner specialization for SIB task thumbnail window
}

CombineRgn(compositedRgn.get(), compositedRgn.get(), windowRgn.get(), RGN_AND);
if (
m_gdiWindowRgn &&
(
m_kind != CompositedBackdropKind::Accent ||
(
m_kind == CompositedBackdropKind::Accent &&
m_data->GetAccentPolicy()->IsGdiRegionRespected()
)
)
if (
m_gdiWindowRgn &&
m_data->GetAccentPolicy()->IsGdiRegionRespected()
)
{
CombineRgn(compositedRgn.get(), compositedRgn.get(), m_gdiWindowRgn.get(), RGN_AND);
{
CombineRgn(compositedRgn.get(), compositedRgn.get(), m_gdiWindowRgn.get(), RGN_AND);
}
}
CombineRgn(compositedRgn.get(), compositedRgn.get(), windowRgn.get(), RGN_AND);

return compositedRgn;
}
Expand All @@ -209,6 +168,23 @@ namespace OpenGlass::BackdropManager
{
return m_udwmVisual.get();
}
bool CanBeTrimmed() override
{
if (m_kind != CompositedBackdropKind::Accent || !m_visible)
{
return true;
}
if (m_accentRect.has_value())
{
return false;
}
if (m_gdiWindowRgn && m_data->GetAccentPolicy()->IsGdiRegionRespected())
{
return false;
}

return true;
}
};

// temporary workaround for aero peek/live preview
Expand Down Expand Up @@ -298,6 +274,10 @@ namespace OpenGlass::BackdropManager
void SetGdiWindowRegion(HRGN region) override {}
void ValidateVisual() override {}
void UpdateNCBackground() override {}
bool CanBeTrimmed() override
{
return false;
}
};
class CClonedCompositedBackdropVisual : public winrt::implements<CClonedCompositedBackdropVisual, ICompositedBackdropVisual>, uDwm::CClonedBackdropVisual
{
Expand All @@ -321,6 +301,10 @@ namespace OpenGlass::BackdropManager
void SetGdiWindowRegion(HRGN region) override {}
void ValidateVisual() override {}
void UpdateNCBackground() override {}
bool CanBeTrimmed() override
{
return false;
}
};
}

Expand Down Expand Up @@ -623,6 +607,18 @@ BackdropManager::CCompositedBackdropVisual::CCompositedBackdropVisual(uDwm::CTop

OnDeviceLost();
OnBackdropKindUpdated(GlassFramework::GetActualBackdropKind(window));
if (m_kind == CompositedBackdropKind::Accent)
{
wil::unique_hrgn clipRgn{ CreateRectRgn(0, 0, 0, 0) };
if (GetWindowRgn(m_data->GetHwnd(), clipRgn.get()) != ERROR)
{
SetGdiWindowRegion(clipRgn.get());
}
else
{
SetGdiWindowRegion(nullptr);
}
}
}

BackdropManager::CCompositedBackdropVisual::~CCompositedBackdropVisual()
Expand Down Expand Up @@ -665,10 +661,23 @@ void BackdropManager::CCompositedBackdropVisual::SetAccentRect(LPCRECT lprc)
if (lprc)
{
m_accentRect = *lprc;
m_gdiWindowRgn.reset();
}
else
else if (m_accentRect.has_value())
{
m_accentRect = std::nullopt;
if (m_kind == CompositedBackdropKind::Accent)
{
wil::unique_hrgn clipRgn{ CreateRectRgn(0, 0, 0, 0) };
if (GetWindowRgn(m_data->GetHwnd(), clipRgn.get()) != ERROR)
{
m_gdiWindowRgn.reset(clipRgn.release());
}
else
{
m_gdiWindowRgn.reset();
}
}
}
m_backdropDataChanged = true;
}
Expand All @@ -681,17 +690,19 @@ void BackdropManager::CCompositedBackdropVisual::SetGdiWindowRegion(HRGN region)
m_gdiWindowRgn.reset(CreateRectRgn(0, 0, 0, 0));
}
CopyRgn(m_gdiWindowRgn.get(), region);
m_accentRect = std::nullopt;
}
else
else if (m_gdiWindowRgn)
{
m_gdiWindowRgn.reset();
m_accentRect = std::nullopt;
}
m_backdropDataChanged = true;
}

void BackdropManager::CCompositedBackdropVisual::ValidateVisual()
{
if (ShouldUpdateBackdropRegion())
if (!m_window->IsTrullyMinimized())
{
if (m_visible)
{
Expand All @@ -714,7 +725,7 @@ void BackdropManager::CCompositedBackdropVisual::ValidateVisual()
}
void BackdropManager::CCompositedBackdropVisual::UpdateNCBackground()
{
if (!ShouldUpdateBackdropRegion())
if (m_window->IsTrullyMinimized())
{
return;
}
Expand All @@ -730,12 +741,12 @@ void BackdropManager::CCompositedBackdropVisual::UpdateNCBackground()
}


size_t BackdropManager::GetBackdropCount()
size_t BackdropManager::GetCount()
{
return g_backdropMap.size();
}

winrt::com_ptr<BackdropManager::ICompositedBackdropVisual> BackdropManager::GetOrCreateBackdropVisual(uDwm::CTopLevelWindow* window, bool createIfNecessary, bool silent)
winrt::com_ptr<BackdropManager::ICompositedBackdropVisual> BackdropManager::GetOrCreate(uDwm::CTopLevelWindow* window, bool createIfNecessary, bool silent)
{
auto it{ g_backdropMap.find(window) };

Expand Down Expand Up @@ -770,10 +781,27 @@ winrt::com_ptr<BackdropManager::ICompositedBackdropVisual> BackdropManager::GetO
return it == g_backdropMap.end() ? nullptr : it->second;
}

void BackdropManager::TryCloneBackdropVisualForWindow(uDwm::CTopLevelWindow* src, uDwm::CTopLevelWindow* dst, ICompositedBackdropVisual** visual)
winrt::com_ptr<BackdropManager::ICompositedBackdropVisual> BackdropManager::GetOrCreateForAccentBlurRect(uDwm::CTopLevelWindow* window, LPCRECT accentBlurRect, bool createIfNecessary, bool silent)
{
auto it{ g_backdropMap.find(window) };

auto result{ BackdropManager::GetOrCreate(window, createIfNecessary, true) };
if (result && it == g_backdropMap.end())
{
result->SetAccentRect(accentBlurRect);
if (!silent)
{
RedrawTopLevelWindow(window);
}
}

return result;
}

void BackdropManager::TryClone(uDwm::CTopLevelWindow* src, uDwm::CTopLevelWindow* dst, ICompositedBackdropVisual** visual)
{
auto legacyVisual{ src->GetLegacyVisual() };
if (auto backdrop{ GetOrCreateBackdropVisual(src) }; backdrop && legacyVisual)
if (auto backdrop{ GetOrCreate(src) }; backdrop && legacyVisual)
{
auto it{ g_backdropMap.find(dst) };
if (it == g_backdropMap.end())
Expand All @@ -793,7 +821,7 @@ void BackdropManager::TryCloneBackdropVisualForWindow(uDwm::CTopLevelWindow* src
}
}

void BackdropManager::RemoveBackdrop(uDwm::CTopLevelWindow* window, bool silent)
void BackdropManager::Remove(uDwm::CTopLevelWindow* window, bool silent)
{
auto it{ g_backdropMap.find(window) };

Expand All @@ -808,6 +836,16 @@ void BackdropManager::RemoveBackdrop(uDwm::CTopLevelWindow* window, bool silent)
}
}

void BackdropManager::Trim(uDwm::CTopLevelWindow* window)
{
auto it{ g_backdropMap.find(window) };

if (it != g_backdropMap.end() && it->second->CanBeTrimmed())
{
g_backdropMap.erase(it);
}
}

void BackdropManager::Shutdown()
{
std::vector<uDwm::CTopLevelWindow*> windowCollection{};
Expand Down
11 changes: 7 additions & 4 deletions OpenGlass/BackdropManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace OpenGlass::BackdropManager

virtual void ValidateVisual() = 0;
virtual void UpdateNCBackground() = 0;
virtual bool CanBeTrimmed() = 0;
};

namespace Configuration
Expand All @@ -43,9 +44,11 @@ namespace OpenGlass::BackdropManager
inline wf::TimeSpan g_crossfadeTime{ std::chrono::milliseconds{ 87 } };
}

size_t GetBackdropCount();
winrt::com_ptr<ICompositedBackdropVisual> GetOrCreateBackdropVisual(uDwm::CTopLevelWindow* window, bool createIfNecessary = false, bool silent = false);
void TryCloneBackdropVisualForWindow(uDwm::CTopLevelWindow* src, uDwm::CTopLevelWindow* dst, ICompositedBackdropVisual** visual = nullptr);
void RemoveBackdrop(uDwm::CTopLevelWindow* window, bool silent = false);
size_t GetCount();
winrt::com_ptr<ICompositedBackdropVisual> GetOrCreate(uDwm::CTopLevelWindow* window, bool createIfNecessary = false, bool silent = false);
winrt::com_ptr<ICompositedBackdropVisual> GetOrCreateForAccentBlurRect(uDwm::CTopLevelWindow* window, LPCRECT accentBlurRect, bool createIfNecessary = false, bool silent = false);
void TryClone(uDwm::CTopLevelWindow* src, uDwm::CTopLevelWindow* dst, ICompositedBackdropVisual** visual = nullptr);
void Remove(uDwm::CTopLevelWindow* window, bool silent = false);
void Trim(uDwm::CTopLevelWindow* window);
void Shutdown();
}
Loading

0 comments on commit c924e3a

Please sign in to comment.