Skip to content

Commit

Permalink
Use native rounded corners and title colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Sep 28, 2021
1 parent 40fc5b3 commit a827d94
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
6 changes: 3 additions & 3 deletions ui/colors.palette
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ tooltipBg: #eef2f5; // tooltip background (like when you put mouse over the mess
tooltipFg: #5d6c80; // tooltip text
tooltipBorderFg: #c9d1db; // tooltip border

// custom title bar for Windows and macOS
// custom title bar
titleShadow: #00000003; // one pixel line shadow at the bottom of custom window title
titleBg: windowBgOver; // custom window title background when window is inactive
titleBgActive: titleBg; // custom window title background when window is active
Expand All @@ -109,8 +109,8 @@ titleButtonCloseBgActive: titleButtonCloseBg; // custom window title close butto
titleButtonCloseFgActive: titleButtonCloseFg; // custom window title close button icon when window is active (Windows only)
titleButtonCloseBgActiveOver: titleButtonCloseBgOver; // custom window title close button background with mouse over when window is active (Windows only)
titleButtonCloseFgActiveOver: titleButtonCloseFgOver; // custom window title close button icon with mouse over when window is active (Windows only)
titleFg: #acacac; // custom window title text when window is inactive (macOS only)
titleFgActive: #3e3c3e; // custom window title text when window is active (macOS only)
titleFg: #acacac; // custom window title text when window is inactive (Windows 11 and macOS)
titleFgActive: #3e3c3e; // custom window title text when window is active (Windows 11 and macOS)

// tray icon
trayCounterBg: #f23c34; // tray icon counter background
Expand Down
6 changes: 4 additions & 2 deletions ui/platform/win/ui_window_shadow_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ui/rp_widget.h"
#include "ui/platform/win/ui_window_win.h"
#include "base/platform/base_platform_info.h"
#include "styles/style_widgets.h"

#include <QtGui/QPainter>
Expand Down Expand Up @@ -42,7 +43,8 @@ base::flat_map<HWND, not_null<WindowShadow*>> ShadowByHandle;

WindowShadow::WindowShadow(not_null<RpWidget*> window, QColor color)
: _window(window)
, _handle(GetWindowHandle(window)) {
, _handle(GetWindowHandle(window))
, _windows11(::Platform::IsWindows11OrGreater()) {
init(color);
}

Expand Down Expand Up @@ -315,7 +317,7 @@ void WindowShadow::horCorners(int w, Gdiplus::Graphics *pgraphics0, Gdiplus::Gra
}

Gdiplus::Color WindowShadow::getColor(uchar alpha) const {
return Gdiplus::Color(BYTE(alpha), _r, _g, _b);
return Gdiplus::Color(BYTE(_windows11 ? 1 : alpha), _r, _g, _b);
}

Gdiplus::SolidBrush WindowShadow::getBrush(uchar alpha) const {
Expand Down
1 change: 1 addition & 0 deletions ui/platform/win/ui_window_shadow_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class WindowShadow final {

const not_null<RpWidget*> _window;
const HWND _handle;
const bool _windows11 = false;

int _x = 0;
int _y = 0;
Expand Down
4 changes: 4 additions & 0 deletions ui/platform/win/ui_window_title_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ void TitleWidget::setStyle(const style::WindowTitle &st) {
update();
}

not_null<const style::WindowTitle*> TitleWidget::st() const {
return _controls.st();
}

void TitleWidget::setResizeEnabled(bool enabled) {
_controls.setResizeEnabled(enabled);
}
Expand Down
1 change: 1 addition & 0 deletions ui/platform/win/ui_window_title_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TitleWidget : public RpWidget {

void setText(const QString &text);
void setStyle(const style::WindowTitle &st);
[[nodiscard]] not_null<const style::WindowTitle*> st() const;
[[nodiscard]] HitTestResult hitTest(QPoint point) const;
void setResizeEnabled(bool enabled);

Expand Down
78 changes: 58 additions & 20 deletions ui/platform/win/ui_window_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/integration.h"
#include "base/debug_log.h"
#include "styles/palette.h"
#include "styles/style_widgets.h"

#include <QtCore/QAbstractNativeEventFilter>
#include <QtGui/QWindow>
Expand All @@ -29,7 +30,13 @@ namespace Ui {
namespace Platform {
namespace {

bool IsCompositionEnabled() {
constexpr auto kDWMWCP_ROUND = DWORD(2);
constexpr auto kDWMWA_WINDOW_CORNER_PREFERENCE = DWORD(33);
constexpr auto kDWMWA_BORDER_COLOR = DWORD(34);
constexpr auto kDWMWA_CAPTION_COLOR = DWORD(35);
constexpr auto kDWMWA_TEXT_COLOR = DWORD(36);

[[nodiscard]] bool IsCompositionEnabled() {
auto result = BOOL(FALSE);
const auto success = (DwmIsCompositionEnabled(&result) == S_OK);
return success && result;
Expand Down Expand Up @@ -85,23 +92,6 @@ bool IsTaskbarAutoHidden(LPRECT rcMon = nullptr, PUINT pEdge = nullptr) {
return bAutoHidden;
}

HRESULT WinApiSetWindowTheme(
HWND hWnd,
LPCWSTR pszSubAppName,
LPCWSTR pszSubIdList) {
static const auto method = [&] {
using f_SetWindowTheme = HRESULT(FAR STDAPICALLTYPE*)(
HWND hWnd,
LPCWSTR pszSubAppName,
LPCWSTR pszSubIdList);
auto result = f_SetWindowTheme();
const auto loaded = base::Platform::SafeLoadLibrary(L"uxtheme.dll");
base::Platform::LoadMethod(loaded, "SetWindowTheme", result);
return result;
}();
return method ? method(hWnd, pszSubAppName, pszSubIdList) : HRESULT();
}

void FixAeroSnap(HWND handle) {
SetWindowLongPtr(
handle,
Expand Down Expand Up @@ -198,6 +188,7 @@ void WindowHelper::setTitle(const QString &title) {

void WindowHelper::setTitleStyle(const style::WindowTitle &st) {
_title->setStyle(st);
updateWindowFrameColors();
}

void WindowHelper::setNativeFrame(bool enabled) {
Expand All @@ -216,6 +207,7 @@ void WindowHelper::setNativeFrame(bool enabled) {
initialShadowUpdate();
}
updateMargins();
updateWindowFrameColors();
fixMaximizedWindow();
}

Expand All @@ -227,6 +219,15 @@ void WindowHelper::initialShadowUpdate() {
} else {
_shadow->update(Change::Moved | Change::Resized | Change::Shown);
}

if (::Platform::IsWindows11OrGreater()) {
auto preference = kDWMWCP_ROUND;
DwmSetWindowAttribute(
_handle,
kDWMWA_WINDOW_CORNER_PREFERENCE,
&preference,
sizeof(preference));
}
}

void WindowHelper::setMinimumSize(QSize size) {
Expand Down Expand Up @@ -270,6 +271,7 @@ void WindowHelper::init() {
if (_shadow) {
_shadow->setColor(st::windowShadowFg->c);
}
updateWindowFrameColors();
Ui::ForceFullRepaint(window());
}, window()->lifetime());

Expand All @@ -291,9 +293,10 @@ void WindowHelper::init() {
updateMargins();

if (!::Platform::IsWindows8OrGreater()) {
WinApiSetWindowTheme(_handle, L" ", L" ");
SetWindowTheme(_handle, L" ", L" ");
QApplication::setStyle(QStyleFactory::create("Windows"));
}
updateWindowFrameColors();

_menu = GetSystemMenu(_handle, FALSE);
updateSystemMenu();
Expand Down Expand Up @@ -326,13 +329,15 @@ bool WindowHelper::handleNativeEvent(
if (LOWORD(wParam) == WA_CLICKACTIVE) {
Ui::MarkInactivePress(window(), true);
}
const auto active = (LOWORD(wParam) != WA_INACTIVE);
if (_shadow) {
if (LOWORD(wParam) != WA_INACTIVE) {
if (active) {
_shadow->update(WindowShadow::Change::Activate);
} else {
_shadow->update(WindowShadow::Change::Deactivate);
}
}
updateWindowFrameColors(active);
window()->update();
} return false;

Expand Down Expand Up @@ -553,6 +558,39 @@ int WindowHelper::titleHeight() const {
return _title->isHidden() ? 0 : _title->height();
}

void WindowHelper::updateWindowFrameColors() {
updateWindowFrameColors(window()->isActiveWindow());
}

void WindowHelper::updateWindowFrameColors(bool active) {
if (!::Platform::IsWindows11OrGreater()) {
return;
}
const auto bg = active
? _title->st()->bgActive->c
: _title->st()->bg->c;
COLORREF bgRef = RGB(bg.red(), bg.green(), bg.blue());
DwmSetWindowAttribute(
_handle,
kDWMWA_BORDER_COLOR,
&bgRef,
sizeof(COLORREF));
DwmSetWindowAttribute(
_handle,
kDWMWA_CAPTION_COLOR,
&bgRef,
sizeof(COLORREF));
const auto fg = active
? _title->st()->fgActive->c
: _title->st()->fg->c;
COLORREF fgRef = RGB(fg.red(), fg.green(), fg.blue());
DwmSetWindowAttribute(
_handle,
kDWMWA_TEXT_COLOR,
&fgRef,
sizeof(COLORREF));
}

void WindowHelper::updateMargins() {
if (_updatingMargins) return;

Expand Down
2 changes: 2 additions & 0 deletions ui/platform/win/ui_window_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class WindowHelper final : public BasicWindowHelper {

void init();
void updateMargins();
void updateWindowFrameColors();
void updateWindowFrameColors(bool active);
void updateSystemMenu();
void updateSystemMenu(Qt::WindowState state);
void initialShadowUpdate();
Expand Down

0 comments on commit a827d94

Please sign in to comment.