From 94d1ea7032095531842482807e9a578fa0688239 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 28 Jan 2015 16:11:14 +0300 Subject: [PATCH] improved borderless window --- Telegram/SourceFiles/gui/contextmenu.cpp | 4 +- Telegram/SourceFiles/gui/style_core.cpp | 1 - Telegram/SourceFiles/mediaview.cpp | 2 +- Telegram/SourceFiles/pspecific_wnd.cpp | 88 ++++++++++-------------- Telegram/SourceFiles/window.cpp | 1 - 5 files changed, 39 insertions(+), 57 deletions(-) diff --git a/Telegram/SourceFiles/gui/contextmenu.cpp b/Telegram/SourceFiles/gui/contextmenu.cpp index 4e370a52d..b70d47197 100644 --- a/Telegram/SourceFiles/gui/contextmenu.cpp +++ b/Telegram/SourceFiles/gui/contextmenu.cpp @@ -21,6 +21,8 @@ #include "flatbutton.h" #include "pspecific.h" +#include "application.h" + #include "lang.h" ContextMenu::ContextMenu(QWidget *parent, const style::iconedButton &st) : TWidget(0), @@ -241,7 +243,7 @@ void ContextMenu::deleteOnHide() { void ContextMenu::popup(const QPoint &p) { QPoint w = p - QPoint(st::dropdownPadding.left(), st::dropdownPadding.top()); - QRect r = QDesktopWidget().screenGeometry(p); + QRect r = App::app() ? App::app()->desktop()->screenGeometry(p) : QDesktopWidget().screenGeometry(p); if (w.x() + width() - st::dropdownPadding.right() > r.x() + r.width()) { w.setX(r.x() + r.width() - width() + st::dropdownPadding.right()); } diff --git a/Telegram/SourceFiles/gui/style_core.cpp b/Telegram/SourceFiles/gui/style_core.cpp index 4c6a57566..5571bee46 100644 --- a/Telegram/SourceFiles/gui/style_core.cpp +++ b/Telegram/SourceFiles/gui/style_core.cpp @@ -24,7 +24,6 @@ namespace { namespace style { FontData::FontData(uint32 size, uint32 flags, uint32 family, Font *other) : f(_fontFamilies[family]), m(f), _size(size), _flags(flags), _family(family) { -// f.setStyleStrategy(QFont::NoAntialias); if (other) { memcpy(modified, other, sizeof(modified)); } else { diff --git a/Telegram/SourceFiles/mediaview.cpp b/Telegram/SourceFiles/mediaview.cpp index 4debbc88c..42ef3e605 100644 --- a/Telegram/SourceFiles/mediaview.cpp +++ b/Telegram/SourceFiles/mediaview.cpp @@ -89,7 +89,7 @@ _saveMsgStarted(0), _saveMsgOpacity(0) void MediaView::moveToScreen() { QPoint wndCenter(App::wnd()->x() + App::wnd()->width() / 2, App::wnd()->y() + App::wnd()->height() / 2); - _avail = QDesktopWidget().screenGeometry(wndCenter); + _avail = App::app() ? App::app()->desktop()->screenGeometry(wndCenter) : QDesktopWidget().screenGeometry(wndCenter); if (_avail != geometry()) { setGeometry(_avail); } diff --git a/Telegram/SourceFiles/pspecific_wnd.cpp b/Telegram/SourceFiles/pspecific_wnd.cpp index b2c83343f..4c9be6d7c 100644 --- a/Telegram/SourceFiles/pspecific_wnd.cpp +++ b/Telegram/SourceFiles/pspecific_wnd.cpp @@ -33,8 +33,6 @@ Copyright (c) 2014 John Preston, https://desktop.telegram.org #include -#include - #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) < (b) ? (b) : (a)) @@ -56,7 +54,6 @@ namespace { QStringList _initLogs; bool frameless = true; - bool useDWM = false; bool useTheme = false; bool useOpenWith = false; bool useOpenAs = false; @@ -207,7 +204,7 @@ namespace { screenDC = GetDC(0); if (!screenDC) return false; - QRect avail(QDesktopWidget().availableGeometry()); + QRect avail(App::app() ? App::app()->desktop()->availableGeometry() : QDesktopWidget().availableGeometry()); max_w = avail.width(); if (max_w < st::wndMinWidth) max_w = st::wndMinWidth; max_h = avail.height(); @@ -610,15 +607,6 @@ namespace { QColor _shActive(0, 0, 0), _shInactive(0, 0, 0); - typedef BOOL (FAR STDAPICALLTYPE *f_dwmDefWindowProc)(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, _Out_ LRESULT *plResult); - f_dwmDefWindowProc dwmDefWindowProc = 0; - - typedef HRESULT (FAR STDAPICALLTYPE *f_dwmSetWindowAttribute)(HWND hWnd, DWORD dwAttribute, _In_ LPCVOID pvAttribute, DWORD cbAttribute); - f_dwmSetWindowAttribute dwmSetWindowAttribute = 0; - - typedef HRESULT (FAR STDAPICALLTYPE *f_dwmExtendFrameIntoClientArea)(HWND hWnd, const MARGINS *pMarInset); - f_dwmExtendFrameIntoClientArea dwmExtendFrameIntoClientArea = 0; - typedef HRESULT (FAR STDAPICALLTYPE *f_setWindowTheme)(HWND hWnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList); f_setWindowTheme setWindowTheme = 0; @@ -657,22 +645,12 @@ namespace { class _PsInitializer { public: _PsInitializer() { - setupDWM(); - useDWM = true; - frameless = !useDWM; + frameless = false; setupUx(); setupShell(); setupWtsapi(); } - void setupDWM() { - HINSTANCE procId = LoadLibrary(L"DWMAPI.DLL"); - - if (!loadFunction(procId, "DwmDefWindowProc", dwmDefWindowProc)) return; - if (!loadFunction(procId, "DwmSetWindowAttribute", dwmSetWindowAttribute)) return; - if (!loadFunction(procId, "DwmExtendFrameIntoClientArea", dwmExtendFrameIntoClientArea)) return; - useDWM = true; - } void setupUx() { HINSTANCE procId = LoadLibrary(L"UXTHEME.DLL"); @@ -755,7 +733,6 @@ namespace { if (LOWORD(wParam) == WA_CLICKACTIVE) { App::wnd()->inactivePress(true); } - Application::wnd()->psUpdateMargins(); if (LOWORD(wParam) != WA_INACTIVE) { _psShadowWindows.setColor(_shActive); _psShadowWindows.update(_PsShadowActivate); @@ -768,33 +745,38 @@ namespace { case WM_NCPAINT: if (QSysInfo::WindowsVersion >= QSysInfo::WV_WINDOWS8) return false; *result = 0; return true; - case WM_NCCALCSIZE: if (!useDWM) return false; { - if (wParam == TRUE) { + case WM_NCCALCSIZE: { + WINDOWPLACEMENT wp; + wp.length = sizeof(WINDOWPLACEMENT); + if (GetWindowPlacement(hWnd, &wp) && wp.showCmd == SW_SHOWMAXIMIZED) { LPNCCALCSIZE_PARAMS params = (LPNCCALCSIZE_PARAMS)lParam; - params->rgrc[0].left += margins.left() - simpleMargins.left(); - params->rgrc[0].top += margins.top() - simpleMargins.top(); - params->rgrc[0].right -= margins.right() - simpleMargins.right(); - params->rgrc[0].bottom -= margins.bottom() - simpleMargins.bottom(); - } else if (wParam == FALSE) { - LPRECT rect = (LPRECT)lParam; - - rect->left += margins.left() - simpleMargins.left(); - rect->top += margins.top() - simpleMargins.top(); - rect->right += margins.right() - simpleMargins.right(); - rect->bottom += margins.bottom() - simpleMargins.bottom(); + LPRECT r = (wParam == TRUE) ? ¶ms->rgrc[0] : (LPRECT)lParam; + HMONITOR hMonitor = MonitorFromPoint({ (r->left + r->right) / 2, (r->top + r->bottom) / 2 }, MONITOR_DEFAULTTONEAREST); + if (hMonitor) { + MONITORINFO mi; + mi.cbSize = sizeof(mi); + if (GetMonitorInfo(hMonitor, &mi)) { + *r = mi.rcWork; + } + } } *result = 0; - } return true; + return true; + } case WM_NCACTIVATE: { - //Application::wnd()->psUpdateMargins(); *result = DefWindowProc(hWnd, msg, wParam, -1); - //Application::wnd()->repaint(); } return true; case WM_WINDOWPOSCHANGING: case WM_WINDOWPOSCHANGED: { - _psShadowWindows.update(_PsShadowMoved | _PsShadowResized, (WINDOWPOS*)lParam); + WINDOWPLACEMENT wp; + wp.length = sizeof(WINDOWPLACEMENT); + if (GetWindowPlacement(hWnd, &wp) && (wp.showCmd == SW_SHOWMAXIMIZED || wp.showCmd == SW_SHOWMINIMIZED)) { + _psShadowWindows.update(_PsShadowHidden); + } else { + _psShadowWindows.update(_PsShadowMoved | _PsShadowResized, (WINDOWPOS*)lParam); + } } return false; case WM_SIZE: { @@ -811,6 +793,7 @@ namespace { } else { App::wnd()->psUpdatedPosition(); } + App::wnd()->psUpdateMargins(); int changes = (wParam == SIZE_MINIMIZED || wParam == SIZE_MAXIMIZED) ? _PsShadowHidden : (_PsShadowResized | _PsShadowShown); _psShadowWindows.update(changes); } @@ -1075,7 +1058,7 @@ void PsMainWindow::psInitSize() { pos.w = st::wndDefWidth; pos.h = st::wndDefHeight; } - QRect avail(QDesktopWidget().availableGeometry()); + QRect avail(App::app() ? App::app()->desktop()->availableGeometry() : QDesktopWidget().availableGeometry()); bool maximized = false; QRect geom(avail.x() + (avail.width() - st::wndDefWidth) / 2, avail.y() + (avail.height() - st::wndDefHeight) / 2, st::wndDefWidth, st::wndDefHeight); if (pos.w && pos.h) { @@ -1195,7 +1178,7 @@ void PsMainWindow::psFirstShow() { } bool PsMainWindow::psHandleTitle() { - return useDWM; + return true; } void PsMainWindow::psInitSysMenu() { @@ -1248,7 +1231,7 @@ void PsMainWindow::psUpdateSysMenu(Qt::WindowState state) { } void PsMainWindow::psUpdateMargins() { - if (!useDWM) return; + if (!ps_hWnd) return; RECT r, a; @@ -1257,7 +1240,7 @@ void PsMainWindow::psUpdateMargins() { LONG style = GetWindowLong(ps_hWnd, GWL_STYLE), styleEx = GetWindowLong(ps_hWnd, GWL_EXSTYLE); AdjustWindowRectEx(&a, style, false, styleEx); - simpleMargins = QMargins(a.left - r.left, a.top - r.top, r.right - a.right, r.bottom - a.bottom); + QMargins margins = QMargins(a.left - r.left, a.top - r.top, r.right - a.right, r.bottom - a.bottom); if (style & WS_MAXIMIZE) { RECT w, m; GetWindowRect(ps_hWnd, &w); @@ -1274,23 +1257,22 @@ void PsMainWindow::psUpdateMargins() { dleft = w.left - m.left; dtop = w.top - m.top; - margins.setLeft(simpleMargins.left() - w.left + m.left); - margins.setRight(simpleMargins.right() - m.right + w.right); - margins.setBottom(simpleMargins.bottom() - m.bottom + w.bottom); - margins.setTop(simpleMargins.top() - w.top + m.top); + margins.setLeft(margins.left() - w.left + m.left); + margins.setRight(margins.right() - m.right + w.right); + margins.setBottom(margins.bottom() - m.bottom + w.bottom); + margins.setTop(margins.top() - w.top + m.top); } else { - margins = simpleMargins; dleft = dtop = 0; } QPlatformNativeInterface *i = QGuiApplication::platformNativeInterface(); - i->setWindowProperty(windowHandle()->handle(), "WindowsCustomMargins", QVariant::fromValue(margins)); + i->setWindowProperty(windowHandle()->handle(), qsl("WindowsCustomMargins"), QVariant::fromValue(margins)); if (!themeInited) { themeInited = true; if (useTheme) { if (QSysInfo::WindowsVersion < QSysInfo::WV_WINDOWS8) { setWindowTheme(ps_hWnd, L" ", L" "); - QApplication::setStyle(QStyleFactory::create("Windows")); + QApplication::setStyle(QStyleFactory::create(qsl("Windows"))); } } } diff --git a/Telegram/SourceFiles/window.cpp b/Telegram/SourceFiles/window.cpp index df84bd33e..862e7efb8 100644 --- a/Telegram/SourceFiles/window.cpp +++ b/Telegram/SourceFiles/window.cpp @@ -384,7 +384,6 @@ void Window::stateChanged(Qt::WindowState state) { updateIsActive((state == Qt::WindowMinimized) ? cOfflineBlurTimeout() : cOnlineFocusTimeout()); psUpdateSysMenu(state); - psUpdateMargins(); if (state == Qt::WindowMinimized && cWorkMode() == dbiwmTrayOnly) { App::wnd()->minimizeToTray(); }