[Always on top] Transparent border (#28127)

This commit is contained in:
Seraphima Zykova
2023-08-25 11:31:51 +03:00
committed by GitHub
parent 738072f508
commit b3532b8c91
10 changed files with 66 additions and 18 deletions

View File

@@ -90,10 +90,10 @@ void FrameDrawer::Show()
Render();
}
void FrameDrawer::SetBorderRect(RECT windowRect, COLORREF color, int thickness, float radius)
void FrameDrawer::SetBorderRect(RECT windowRect, COLORREF rgb, float alpha, int thickness, float radius)
{
auto newSceneRect = DrawableRect{
.borderColor = ConvertColor(color),
.borderColor = ConvertColor(rgb, alpha),
.thickness = thickness,
};
@@ -175,12 +175,12 @@ IDWriteFactory* FrameDrawer::GetWriteFactory()
return pDWriteFactory;
}
D2D1_COLOR_F FrameDrawer::ConvertColor(COLORREF color)
D2D1_COLOR_F FrameDrawer::ConvertColor(COLORREF color, float alpha)
{
return D2D1::ColorF(GetRValue(color) / 255.f,
GetGValue(color) / 255.f,
GetBValue(color) / 255.f,
1.f);
alpha);
}
D2D1_ROUNDED_RECT FrameDrawer::ConvertRect(RECT rect, int thickness, float radius)

View File

@@ -18,7 +18,7 @@ public:
void Show();
void Hide();
void SetBorderRect(RECT windowRect, COLORREF color, int thickness, float radius);
void SetBorderRect(RECT windowRect, COLORREF rgb, float alpha, int thickness, float radius);
private:
bool CreateRenderTargets(const RECT& clientRect);
@@ -33,7 +33,7 @@ private:
static ID2D1Factory* GetD2DFactory();
static IDWriteFactory* GetWriteFactory();
static D2D1_COLOR_F ConvertColor(COLORREF color);
static D2D1_COLOR_F ConvertColor(COLORREF color, float alpha);
static D2D1_ROUNDED_RECT ConvertRect(RECT rect, int thickness, float radius);
static D2D1_RECT_F ConvertRect(RECT rect, int thickness);
void Render();

View File

@@ -17,6 +17,7 @@ namespace NonLocalizable
const static wchar_t* FrameEnabledID = L"frame-enabled";
const static wchar_t* FrameThicknessID = L"frame-thickness";
const static wchar_t* FrameColorID = L"frame-color";
const static wchar_t* FrameOpacityID = L"frame-opacity";
const static wchar_t* BlockInGameModeID = L"do-not-activate-on-game-mode";
const static wchar_t* ExcludedAppsID = L"excluded-apps";
const static wchar_t* FrameAccentColor = L"frame-accent-color";
@@ -134,6 +135,16 @@ void AlwaysOnTopSettings::LoadSettings()
}
}
if (const auto jsonVal = values.get_int_value(NonLocalizable::FrameOpacityID))
{
auto val = *jsonVal;
if (m_settings.frameOpacity != val)
{
m_settings.frameOpacity = val;
NotifyObservers(SettingId::FrameOpacity);
}
}
if (const auto jsonVal = values.get_bool_value(NonLocalizable::FrameEnabledID))
{
auto val = *jsonVal;

View File

@@ -21,6 +21,7 @@ struct Settings
bool blockInGameMode = true;
bool frameAccentColor = true;
int frameThickness = 15;
int frameOpacity = 100;
COLORREF frameColor = RGB(0, 173, 239);
std::vector<std::wstring> excludedApps{};
};

View File

@@ -7,6 +7,7 @@ enum class SettingId
FrameEnabled,
FrameThickness,
FrameColor,
FrameOpacity,
BlockInGameMode,
ExcludeApps,
FrameAccentColor,

View File

@@ -33,7 +33,7 @@ std::optional<RECT> GetFrameRect(HWND window)
}
WindowBorder::WindowBorder(HWND window) :
SettingsObserver({ SettingId::FrameColor, SettingId::FrameThickness, SettingId::FrameAccentColor, SettingId::RoundCornersEnabled }),
SettingsObserver({ SettingId::FrameColor, SettingId::FrameThickness, SettingId::FrameAccentColor, SettingId::FrameOpacity, SettingId::RoundCornersEnabled }),
m_window(nullptr),
m_trackingWindow(window),
m_frameDrawer(nullptr)
@@ -113,6 +113,14 @@ bool WindowBorder::Init(HINSTANCE hinstance)
return false;
}
// make window transparent
int const pos = -GetSystemMetrics(SM_CXVIRTUALSCREEN) - 8;
if (wil::unique_hrgn hrgn{ CreateRectRgn(pos, 0, (pos + 1), 1) })
{
DWM_BLURBEHIND bh = { DWM_BB_ENABLE | DWM_BB_BLURREGION, TRUE, hrgn.get(), FALSE };
DwmEnableBlurBehindWindow(m_window, &bh);
}
if (!SetLayeredWindowAttributes(m_window, RGB(0, 0, 0), 0, LWA_COLORKEY))
{
return false;
@@ -193,6 +201,7 @@ void WindowBorder::UpdateBorderProperties() const
color = AlwaysOnTopSettings::settings().frameColor;
}
float opacity = AlwaysOnTopSettings::settings().frameOpacity / 100.0f;
float scalingFactor = ScalingUtils::ScalingFactor(m_trackingWindow);
float thickness = AlwaysOnTopSettings::settings().frameThickness * scalingFactor;
float cornerRadius = 0.0;
@@ -201,7 +210,7 @@ void WindowBorder::UpdateBorderProperties() const
cornerRadius = WindowCornerUtils::CornersRadius(m_trackingWindow) * scalingFactor;
}
m_frameDrawer->SetBorderRect(frameRect, color, static_cast<int>(thickness), cornerRadius);
m_frameDrawer->SetBorderRect(frameRect, color, opacity, static_cast<int>(thickness), cornerRadius);
}
LRESULT WindowBorder::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
@@ -267,22 +276,14 @@ void WindowBorder::SettingsUpdate(SettingId id)
break;
case SettingId::FrameColor:
{
UpdateBorderProperties();
}
break;
case SettingId::FrameAccentColor:
{
UpdateBorderProperties();
}
break;
case SettingId::FrameOpacity:
case SettingId::RoundCornersEnabled:
{
UpdateBorderProperties();
}
break;
default:
break;
}