From e6a30388da571cb8f1dcaad32e7c8b1db79edbd0 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Tue, 10 Jan 2017 16:34:41 +0100 Subject: [PATCH 01/20] Make `Notification` factory function a member of `NotificationPresenter` so that we can create different types of notifications based on runtime conditions. --- brightray/browser/linux/libnotify_notification.cc | 6 ------ brightray/browser/linux/notification_presenter_linux.cc | 4 ++++ brightray/browser/linux/notification_presenter_linux.h | 2 ++ brightray/browser/mac/cocoa_notification.mm | 6 ------ brightray/browser/mac/notification_presenter_mac.h | 2 ++ brightray/browser/mac/notification_presenter_mac.mm | 4 ++++ brightray/browser/notification.h | 8 +------- brightray/browser/notification_presenter.cc | 3 ++- brightray/browser/notification_presenter.h | 1 + brightray/browser/win/notification_presenter_win.cc | 4 ++++ brightray/browser/win/notification_presenter_win.h | 2 ++ brightray/browser/win/windows_toast_notification.cc | 6 ------ 12 files changed, 22 insertions(+), 26 deletions(-) diff --git a/brightray/browser/linux/libnotify_notification.cc b/brightray/browser/linux/libnotify_notification.cc index aea7b7cc42d..dad3acb4335 100644 --- a/brightray/browser/linux/libnotify_notification.cc +++ b/brightray/browser/linux/libnotify_notification.cc @@ -55,12 +55,6 @@ void log_and_clear_error(GError* error, const char* context) { } // namespace -// static -Notification* Notification::Create(NotificationDelegate* delegate, - NotificationPresenter* presenter) { - return new LibnotifyNotification(delegate, presenter); -} - // static bool LibnotifyNotification::Initialize() { if (!libnotify_loader_.Load("libnotify.so.4") && // most common one diff --git a/brightray/browser/linux/notification_presenter_linux.cc b/brightray/browser/linux/notification_presenter_linux.cc index c846fca6659..25d1b6d982b 100644 --- a/brightray/browser/linux/notification_presenter_linux.cc +++ b/brightray/browser/linux/notification_presenter_linux.cc @@ -22,4 +22,8 @@ NotificationPresenterLinux::NotificationPresenterLinux() { NotificationPresenterLinux::~NotificationPresenterLinux() { } +Notification* NotificationPresenterLinux::CreateNotificationObject(NotificationDelegate* delegate) { + return new LibnotifyNotification(delegate, this); +} + } // namespace brightray diff --git a/brightray/browser/linux/notification_presenter_linux.h b/brightray/browser/linux/notification_presenter_linux.h index ef436799484..b8adcd1348d 100644 --- a/brightray/browser/linux/notification_presenter_linux.h +++ b/brightray/browser/linux/notification_presenter_linux.h @@ -16,6 +16,8 @@ class NotificationPresenterLinux : public NotificationPresenter { ~NotificationPresenterLinux(); private: + Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + DISALLOW_COPY_AND_ASSIGN(NotificationPresenterLinux); }; diff --git a/brightray/browser/mac/cocoa_notification.mm b/brightray/browser/mac/cocoa_notification.mm index 4e9ee3238b8..f4599487c84 100644 --- a/brightray/browser/mac/cocoa_notification.mm +++ b/brightray/browser/mac/cocoa_notification.mm @@ -12,12 +12,6 @@ namespace brightray { -// static -Notification* Notification::Create(NotificationDelegate* delegate, - NotificationPresenter* presenter) { - return new CocoaNotification(delegate, presenter); -} - CocoaNotification::CocoaNotification(NotificationDelegate* delegate, NotificationPresenter* presenter) : Notification(delegate, presenter) { diff --git a/brightray/browser/mac/notification_presenter_mac.h b/brightray/browser/mac/notification_presenter_mac.h index 825a1dada23..f6e5f7959c8 100644 --- a/brightray/browser/mac/notification_presenter_mac.h +++ b/brightray/browser/mac/notification_presenter_mac.h @@ -22,6 +22,8 @@ class NotificationPresenterMac : public NotificationPresenter { ~NotificationPresenterMac(); private: + Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + base::scoped_nsobject notification_center_delegate_; diff --git a/brightray/browser/mac/notification_presenter_mac.mm b/brightray/browser/mac/notification_presenter_mac.mm index a37e9182fc6..a1fb2e77893 100644 --- a/brightray/browser/mac/notification_presenter_mac.mm +++ b/brightray/browser/mac/notification_presenter_mac.mm @@ -35,4 +35,8 @@ NotificationPresenterMac::~NotificationPresenterMac() { NSUserNotificationCenter.defaultUserNotificationCenter.delegate = nil; } +Notification* NotificationPresenterMac::CreateNotificationObject(NotificationDelegate* delegate) { + return new CocoaNotification(delegate, this); +} + } // namespace brightray diff --git a/brightray/browser/notification.h b/brightray/browser/notification.h index 87096bbe263..76e3ba4320f 100644 --- a/brightray/browser/notification.h +++ b/brightray/browser/notification.h @@ -47,16 +47,10 @@ class Notification { protected: Notification(NotificationDelegate* delegate, NotificationPresenter* presenter); + public: virtual ~Notification(); private: - friend class NotificationPresenter; - - // Can only be called by NotificationPresenter, the caller is responsible of - // freeing the returned instance. - static Notification* Create(NotificationDelegate* delegate, - NotificationPresenter* presenter); - NotificationDelegate* delegate_; NotificationPresenter* presenter_; diff --git a/brightray/browser/notification_presenter.cc b/brightray/browser/notification_presenter.cc index ad46e292a27..39c3efd7ddf 100644 --- a/brightray/browser/notification_presenter.cc +++ b/brightray/browser/notification_presenter.cc @@ -18,7 +18,8 @@ NotificationPresenter::~NotificationPresenter() { base::WeakPtr NotificationPresenter::CreateNotification( NotificationDelegate* delegate) { - Notification* notification = Notification::Create(delegate, this); + Notification* notification = CreateNotificationObject(delegate); + if (!notification) return {}; notifications_.insert(notification); return notification->GetWeakPtr(); } diff --git a/brightray/browser/notification_presenter.h b/brightray/browser/notification_presenter.h index b3dac3005dc..f88a9a6449a 100644 --- a/brightray/browser/notification_presenter.h +++ b/brightray/browser/notification_presenter.h @@ -27,6 +27,7 @@ class NotificationPresenter { protected: NotificationPresenter(); + virtual Notification* CreateNotificationObject(NotificationDelegate* delegate) = 0; private: friend class Notification; diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index c185246809e..5fc961008c9 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -66,4 +66,8 @@ base::string16 NotificationPresenterWin::SaveIconToFilesystem( return base::UTF8ToUTF16(origin.spec()); } +Notification* NotificationPresenterWin::CreateNotificationObject(NotificationDelegate* delegate) { + return new WindowsToastNotification(delegate, this); +} + } // namespace brightray diff --git a/brightray/browser/win/notification_presenter_win.h b/brightray/browser/win/notification_presenter_win.h index c3e6a9ad43f..1e1186c500b 100644 --- a/brightray/browser/win/notification_presenter_win.h +++ b/brightray/browser/win/notification_presenter_win.h @@ -42,6 +42,8 @@ class NotificationPresenterWin : public NotificationPresenter { base::string16 SaveIconToFilesystem(const SkBitmap& icon, const GURL& origin); private: + Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + base::ScopedTempDir temp_dir_; DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin); diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index 4a3a5a6f678..0e0c9249c11 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -42,12 +42,6 @@ bool GetAppUserModelId(ScopedHString* app_id) { } // namespace -// static -Notification* Notification::Create(NotificationDelegate* delegate, - NotificationPresenter* presenter) { - return new WindowsToastNotification(delegate, presenter); -} - // static ComPtr WindowsToastNotification::toast_manager_; From fe05b66a6c599cdbdfe88d5b3977dd10f98d3488 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 15 Mar 2017 13:56:06 +0100 Subject: [PATCH 02/20] Added desktop notifications implementation for Windows 7 (and earlier) --- .../win/notification_presenter_win7.cc | 47 + .../browser/win/notification_presenter_win7.h | 28 + .../win/win32_desktop_notifications/common.h | 57 ++ .../desktop_notification_controller.cc | 426 +++++++++ .../desktop_notification_controller.h | 103 +++ .../win/win32_desktop_notifications/toast.cc | 811 ++++++++++++++++++ .../win/win32_desktop_notifications/toast.h | 99 +++ brightray/browser/win/win32_notification.cc | 60 ++ brightray/browser/win/win32_notification.h | 30 + brightray/filenames.gypi | 17 +- 10 files changed, 1674 insertions(+), 4 deletions(-) create mode 100644 brightray/browser/win/notification_presenter_win7.cc create mode 100644 brightray/browser/win/notification_presenter_win7.h create mode 100644 brightray/browser/win/win32_desktop_notifications/common.h create mode 100644 brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc create mode 100644 brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h create mode 100644 brightray/browser/win/win32_desktop_notifications/toast.cc create mode 100644 brightray/browser/win/win32_desktop_notifications/toast.h create mode 100644 brightray/browser/win/win32_notification.cc create mode 100644 brightray/browser/win/win32_notification.h diff --git a/brightray/browser/win/notification_presenter_win7.cc b/brightray/browser/win/notification_presenter_win7.cc new file mode 100644 index 00000000000..0203e2e85d3 --- /dev/null +++ b/brightray/browser/win/notification_presenter_win7.cc @@ -0,0 +1,47 @@ +#include "notification_presenter_win7.h" +#include "win32_notification.h" + +namespace brightray { + +brightray::Notification* NotificationPresenterWin7::CreateNotificationObject(NotificationDelegate* delegate) +{ + return new Win32Notification(delegate, this); +} + +Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef(const DesktopNotificationController::Notification& ref) +{ + for(auto n : this->notifications()) + { + auto w32n = static_cast(n); + if(w32n->GetRef() == ref) + return w32n; + } + + return nullptr; +} + +Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag(const std::string& tag) +{ + for(auto n : this->notifications()) + { + auto w32n = static_cast(n); + if(w32n->GetTag() == tag) + return w32n; + } + + return nullptr; +} + +void NotificationPresenterWin7::OnNotificationClicked(Notification& notification) +{ + auto n = GetNotificationObjectByRef(notification); + if(n) n->NotificationClicked(); +} + +void NotificationPresenterWin7::OnNotificationDismissed(Notification& notification) +{ + auto n = GetNotificationObjectByRef(notification); + if(n) n->NotificationDismissed(); +} + +} diff --git a/brightray/browser/win/notification_presenter_win7.h b/brightray/browser/win/notification_presenter_win7.h new file mode 100644 index 00000000000..cebef5438ff --- /dev/null +++ b/brightray/browser/win/notification_presenter_win7.h @@ -0,0 +1,28 @@ +#pragma once +#include "browser/notification_presenter.h" +#include "browser/win/win32_desktop_notifications/desktop_notification_controller.h" + +namespace brightray { + +class Win32Notification; + +class NotificationPresenterWin7 : + public NotificationPresenter, + public DesktopNotificationController +{ +public: + NotificationPresenterWin7() = default; + + Win32Notification* GetNotificationObjectByRef(const DesktopNotificationController::Notification& ref); + Win32Notification* GetNotificationObjectByTag(const std::string& tag); + +private: + DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin7); + + brightray::Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + + void OnNotificationClicked(Notification& notification) override; + void OnNotificationDismissed(Notification& notification) override; +}; + +} // namespace diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h new file mode 100644 index 00000000000..5f44debf312 --- /dev/null +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -0,0 +1,57 @@ +#pragma once +#include + +namespace brightray { + +struct NotificationData +{ + DesktopNotificationController* controller = nullptr; + + std::wstring caption; + std::wstring bodyText; + HBITMAP image = NULL; + + + NotificationData() = default; + + ~NotificationData() + { + if(image) DeleteObject(image); + } + + NotificationData(const NotificationData& other) = delete; + NotificationData& operator=(const NotificationData& other) = delete; +}; + +template +inline T ScaleForDpi(T value, unsigned dpi) +{ + return value * dpi / 96; +} + +struct ScreenMetrics +{ + UINT dpiX, dpiY; + + ScreenMetrics() + { + typedef HRESULT WINAPI GetDpiForMonitor_t(HMONITOR, int, UINT*, UINT*); + auto GetDpiForMonitor = (GetDpiForMonitor_t*)GetProcAddress(GetModuleHandle(TEXT("shcore")), "GetDpiForMonitor"); + if(GetDpiForMonitor) + { + auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY); + if(GetDpiForMonitor(monitor, 0, &dpiX, &dpiY) == S_OK) + return; + } + + HDC hdc = GetDC(NULL); + dpiX = GetDeviceCaps(hdc, LOGPIXELSX); + dpiY = GetDeviceCaps(hdc, LOGPIXELSY); + ReleaseDC(NULL, hdc); + } + + template T X(T value) const { return ScaleForDpi(value, dpiX); } + template T Y(T value) const { return ScaleForDpi(value, dpiY); } +}; + +} diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc new file mode 100644 index 00000000000..e79c6e5be56 --- /dev/null +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -0,0 +1,426 @@ +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include "desktop_notification_controller.h" +#include "common.h" +#include "toast.h" +#include +#include +#include + +using namespace std; + +namespace brightray { + +HBITMAP CopyBitmap(HBITMAP bitmap) +{ + HBITMAP ret = NULL; + + BITMAP bm; + if(bitmap && GetObject(bitmap, sizeof(bm), &bm)) + { + HDC hdcScreen = GetDC(NULL); + ret = CreateCompatibleBitmap(hdcScreen, bm.bmWidth, bm.bmHeight); + ReleaseDC(NULL, hdcScreen); + + if(ret) + { + HDC hdcSrc = CreateCompatibleDC(NULL); + HDC hdcDst = CreateCompatibleDC(NULL); + SelectBitmap(hdcSrc, bitmap); + SelectBitmap(hdcDst, ret); + BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY); + DeleteDC(hdcDst); + DeleteDC(hdcSrc); + } + } + + return ret; +} + +HINSTANCE DesktopNotificationController::RegisterWndClasses() +{ + // We keep a static `module` variable which serves a dual purpose: + // 1. Stores the HINSTANCE where the window classes are registered, which can be passed to `CreateWindow` + // 2. Indicates whether we already attempted the registration so that we don't do it twice (we don't retry + // even if registration fails, as there is no point. + static HMODULE module = NULL; + + if(!module) + { + if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast(&RegisterWndClasses), &module)) + { + Toast::Register(module); + + WNDCLASSEX wc = { sizeof(wc) }; + wc.lpfnWndProc = &WndProc; + wc.lpszClassName = className; + wc.cbWndExtra = sizeof(DesktopNotificationController*); + wc.hInstance = module; + + RegisterClassEx(&wc); + } + } + + return module; +} + +DesktopNotificationController::DesktopNotificationController(unsigned maximumToasts) +{ + instances.reserve(maximumToasts); +} + +DesktopNotificationController::~DesktopNotificationController() +{ + for(auto&& inst : instances) DestroyToast(inst); + if(hwndController) DestroyWindow(hwndController); + ClearAssets(); +} + +LRESULT CALLBACK DesktopNotificationController::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch(message) + { + case WM_CREATE: + { + auto& cs = reinterpret_cast(lParam); + SetWindowLongPtr(hWnd, 0, (LONG_PTR)cs->lpCreateParams); + } + break; + + case WM_TIMER: + if(wParam == TimerID_Animate) + { + Get(hWnd)->AnimateAll(); + } + return 0; + + case WM_DISPLAYCHANGE: + { + auto inst = Get(hWnd); + inst->ClearAssets(); + inst->AnimateAll(); + } + break; + + case WM_SETTINGCHANGE: + if(wParam == SPI_SETWORKAREA) + { + Get(hWnd)->AnimateAll(); + } + break; + } + + return DefWindowProc(hWnd, message, wParam, lParam); +} + +void DesktopNotificationController::StartAnimation() +{ + _ASSERT(hwndController); + + if(!isAnimating && hwndController) + { + // NOTE: 15ms is shorter than what we'd need for 60 fps, but since the timer + // is not accurate we must request a higher frame rate to get at least 60 + + SetTimer(hwndController, TimerID_Animate, 15, nullptr); + isAnimating = true; + } +} + +HFONT DesktopNotificationController::GetCaptionFont() +{ + InitializeFonts(); + return captionFont; +} + +HFONT DesktopNotificationController::GetBodyFont() +{ + InitializeFonts(); + return bodyFont; +} + +void DesktopNotificationController::InitializeFonts() +{ + if(!bodyFont) + { + NONCLIENTMETRICS metrics = { sizeof(metrics) }; + if(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0)) + { + auto baseHeight = metrics.lfMessageFont.lfHeight; + + HDC hdc = GetDC(NULL); + auto dpiY = GetDeviceCaps(hdc, LOGPIXELSY); + ReleaseDC(NULL, hdc); + + metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.1f, dpiY); + bodyFont = CreateFontIndirect(&metrics.lfMessageFont); + + if(captionFont) DeleteFont(captionFont); + metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.4f, dpiY); + captionFont = CreateFontIndirect(&metrics.lfMessageFont); + } + } +} + +void DesktopNotificationController::ClearAssets() +{ + if(captionFont) { DeleteFont(captionFont); captionFont = NULL; } + if(bodyFont) { DeleteFont(bodyFont); bodyFont = NULL; } +} + +void DesktopNotificationController::AnimateAll() +{ + // NOTE: This function refreshes position and size of all toasts according + // to all current conditions. Animation time is only one of the variables + // influencing them. Screen resolution is another. + + bool keepAnimating = false; + + if(!instances.empty()) + { + RECT workArea; + if(SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) + { + ScreenMetrics metrics; + POINT origin = { workArea.right, + workArea.bottom - metrics.Y(toastMargin) }; + + auto hdwp = BeginDeferWindowPos((int)instances.size()); + for(auto&& inst : instances) + { + if(!inst.hwnd) continue; + + auto notification = Toast::Get(inst.hwnd); + hdwp = notification->Animate(hdwp, origin); + if(!hdwp) break; + keepAnimating |= notification->IsAnimationActive(); + } + if(hdwp) EndDeferWindowPos(hdwp); + } + } + + if(!keepAnimating) + { + _ASSERT(hwndController); + if(hwndController) KillTimer(hwndController, TimerID_Animate); + isAnimating = false; + } + + // Purge dismissed notifications and collapse the stack between + // items which are highlighted + if(!instances.empty()) + { + auto isAlive = [](ToastInstance& inst) { + return inst.hwnd && IsWindowVisible(inst.hwnd); + }; + + auto isHighlighted = [](ToastInstance& inst) { + return inst.hwnd && Toast::Get(inst.hwnd)->IsHighlighted(); + }; + + for(auto it = instances.begin();; ++it) + { + // find next highlighted item + auto it2 = find_if(it, instances.end(), isHighlighted); + + // collapse the stack in front of the highlighted item + it = stable_partition(it, it2, isAlive); + + // purge the dead items + for_each(it, it2, [this](auto&& inst) { DestroyToast(inst); }); + + if(it2 == instances.end()) + { + instances.erase(it, it2); + break; + } + + it = move(it2); + } + } + + // Set new toast positions + if(!instances.empty()) + { + ScreenMetrics metrics; + auto margin = metrics.Y(toastMargin); + + int targetPos = 0; + for(auto&& inst : instances) + { + if(inst.hwnd) + { + auto toast = Toast::Get(inst.hwnd); + + if(toast->IsHighlighted()) + targetPos = toast->GetVerticalPosition(); + else + toast->SetVerticalPosition(targetPos); + + targetPos += toast->GetHeight() + margin; + } + } + } + + // Create new toasts from the queue + CheckQueue(); +} + +DesktopNotificationController::Notification DesktopNotificationController::AddNotification(std::wstring caption, std::wstring bodyText, HBITMAP image) +{ + NotificationLink data(this); + + data->caption = move(caption); + data->bodyText = move(bodyText); + data->image = CopyBitmap(image); + + // Enqueue new notification + Notification ret = *queue.insert(queue.end(), move(data)); + CheckQueue(); + return ret; +} + +void DesktopNotificationController::CloseNotification(Notification& notification) +{ + // Remove it from the queue + auto it = find(queue.begin(), queue.end(), notification.data); + if(it != queue.end()) + { + queue.erase(it); + this->OnNotificationClosed(notification); + return; + } + + // Dismiss active toast + auto hwnd = GetToast(notification.data.get()); + if(hwnd) + { + auto toast = Toast::Get(hwnd); + toast->Dismiss(); + } +} + +void DesktopNotificationController::CheckQueue() +{ + while(instances.size() < instances.capacity() && !queue.empty()) + { + CreateToast(move(queue.front())); + queue.pop_front(); + } +} + +void DesktopNotificationController::CreateToast(NotificationLink&& data) +{ + auto hInstance = RegisterWndClasses(); + auto hwnd = Toast::Create(hInstance, data); + if(hwnd) + { + int toastPos = 0; + if(!instances.empty()) + { + auto& item = instances.back(); + _ASSERT(item.hwnd); + + ScreenMetrics scr; + auto toast = Toast::Get(item.hwnd); + toastPos = toast->GetVerticalPosition() + toast->GetHeight() + scr.Y(toastMargin); + } + + instances.push_back({ hwnd, move(data) }); + + if(!hwndController) + { + // NOTE: We cannot use a message-only window because we need to receive system notifications + hwndController = CreateWindow(className, nullptr, 0, 0, 0, 0, 0, NULL, NULL, hInstance, this); + } + + auto toast = Toast::Get(hwnd); + toast->PopUp(toastPos); + } +} + +HWND DesktopNotificationController::GetToast(const NotificationData* data) const +{ + auto it = find_if(instances.cbegin(), instances.cend(), [data](auto&& inst) { + auto toast = Toast::Get(inst.hwnd); + return data == toast->GetNotification().get(); + }); + + return (it != instances.cend()) ? it->hwnd : NULL; +} + +void DesktopNotificationController::DestroyToast(ToastInstance& inst) +{ + if(inst.hwnd) + { + auto data = Toast::Get(inst.hwnd)->GetNotification(); + + DestroyWindow(inst.hwnd); + inst.hwnd = NULL; + + Notification notification(data); + OnNotificationClosed(notification); + } +} + + +DesktopNotificationController::Notification::Notification(const shared_ptr& data) : + data(data) +{ + _ASSERT(data != nullptr); +} + +bool DesktopNotificationController::Notification::operator==(const Notification& other) const +{ + return data == other.data; +} + +void DesktopNotificationController::Notification::Close() +{ + // No business calling this when not pointing to a valid instance + _ASSERT(data); + + if(data->controller) + data->controller->CloseNotification(*this); +} + +void DesktopNotificationController::Notification::Set(std::wstring caption, std::wstring bodyText, HBITMAP image) +{ + // No business calling this when not pointing to a valid instance + _ASSERT(data); + + // Do nothing when the notification has been closed + if(!data->controller) + return; + + if(data->image) DeleteBitmap(data->image); + + data->caption = move(caption); + data->bodyText = move(bodyText); + data->image = CopyBitmap(image); + + auto hwnd = data->controller->GetToast(data.get()); + if(hwnd) + { + auto toast = Toast::Get(hwnd); + toast->ResetContents(); + } + + // Change of contents can affect size and position of all toasts + data->controller->StartAnimation(); +} + + +DesktopNotificationController::NotificationLink::NotificationLink(DesktopNotificationController* controller) : + shared_ptr(make_shared()) +{ + get()->controller = controller; +} + +DesktopNotificationController::NotificationLink::~NotificationLink() +{ + auto p = get(); + if(p) p->controller = nullptr; +} + +} diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h new file mode 100644 index 00000000000..1d93591f60d --- /dev/null +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -0,0 +1,103 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace brightray { + +struct NotificationData; + +class DesktopNotificationController +{ +public: + DesktopNotificationController(unsigned maximumToasts = 3); + ~DesktopNotificationController(); + + class Notification; + Notification AddNotification(std::wstring caption, std::wstring bodyText, HBITMAP image); + void CloseNotification(Notification& notification); + + // Event handlers -- override to receive the events +private: + virtual void OnNotificationClosed(Notification& notification) {} + virtual void OnNotificationClicked(Notification& notification) {} + virtual void OnNotificationDismissed(Notification& notification) {} + +private: + static HINSTANCE RegisterWndClasses(); + void StartAnimation(); + HFONT GetCaptionFont(); + HFONT GetBodyFont(); + +private: + enum TimerID { + TimerID_Animate = 1 + }; + + template + static constexpr T toastMargin = 20; + + // Wrapper around `NotificationData` which makes sure that + // the `controller` member is cleared when the controller object + // stops tracking the notification + struct NotificationLink : std::shared_ptr { + NotificationLink(DesktopNotificationController* controller); + ~NotificationLink(); + + NotificationLink(NotificationLink&&) = default; + NotificationLink(const NotificationLink&) = delete; + NotificationLink& operator=(NotificationLink&&) = default; + }; + + struct ToastInstance { + HWND hwnd; + NotificationLink data; + }; + + class Toast; + + static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + static DesktopNotificationController* Get(HWND hWnd) + { + return reinterpret_cast(GetWindowLongPtr(hWnd, 0)); + } + + DesktopNotificationController(const DesktopNotificationController&) = delete; + + void InitializeFonts(); + void ClearAssets(); + void AnimateAll(); + void CheckQueue(); + void CreateToast(NotificationLink&& data); + HWND GetToast(const NotificationData* data) const; + void DestroyToast(ToastInstance& inst); + +private: + static constexpr const TCHAR className[] = TEXT("DesktopNotificationController"); + HWND hwndController = NULL; + HFONT captionFont = NULL, bodyFont = NULL; + std::vector instances; + std::deque queue; + bool isAnimating = false; +}; + +class DesktopNotificationController::Notification +{ +public: + Notification() = default; + Notification(const std::shared_ptr& data); + + bool operator==(const Notification& other) const; + + void Close(); + void Set(std::wstring caption, std::wstring bodyText, HBITMAP image); + +private: + std::shared_ptr data; + + friend class DesktopNotificationController; +}; + +} diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc new file mode 100644 index 00000000000..f6cb8d06cef --- /dev/null +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -0,0 +1,811 @@ +#define NOMINMAX +#include "toast.h" +#include "common.h" +#include +#include +#include + +#pragma comment(lib, "msimg32.lib") +#pragma comment(lib, "uxtheme.lib") + +using namespace std; + +namespace brightray { + +static COLORREF GetAccentColor() +{ + bool success = false; + if(IsAppThemed()) + { + HKEY hkey; + if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\DWM"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) + { + COLORREF color; + DWORD type, size; + if(RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, &type, (BYTE*)&color, &(size = sizeof(color))) == ERROR_SUCCESS && type == REG_DWORD) + { + // convert from RGBA + color = RGB(GetRValue(color), GetGValue(color), GetBValue(color)); + success = true; + } + else if(RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, &type, (BYTE*)&color, &(size = sizeof(color))) == ERROR_SUCCESS && type == REG_DWORD) + { + // convert from BGRA + color = RGB(GetBValue(color), GetGValue(color), GetRValue(color)); + success = true; + } + + RegCloseKey(hkey); + + if(success) return color; + } + } + + return GetSysColor(COLOR_ACTIVECAPTION); +} + +// Stretches a bitmap to the specified size, preserves alpha channel +static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) +{ + // We use StretchBlt for the scaling, but that discards the alpha channel. + // Therefore we first create a separate grayscale bitmap from the alpha channel, + // scale that separately and copy it back to the scaled color bitmap. + + BITMAP bm; + if(!GetObject(bitmap, sizeof(bm), &bm)) + return NULL; + + if(width == 0 || height == 0) + return NULL; + + HBITMAP resultBitmap = NULL; + + HDC hdcScreen = GetDC(NULL); + + HBITMAP alphaSrcBitmap; + { + BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; + bmi.biWidth = bm.bmWidth; + bmi.biHeight = bm.bmHeight; + bmi.biPlanes = bm.bmPlanes; + bmi.biBitCount = bm.bmBitsPixel; + bmi.biCompression = BI_RGB; + + void* alphaSrcBits; + alphaSrcBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, &alphaSrcBits, NULL, 0); + + if(alphaSrcBitmap) + { + if(GetDIBits(hdcScreen, bitmap, 0, 0, 0, (BITMAPINFO*)&bmi, DIB_RGB_COLORS) && + bmi.biSizeImage > 0 && + (bmi.biSizeImage % 4) == 0) + { + auto buf = (DWORD*)_aligned_malloc(bmi.biSizeImage, sizeof(DWORD)); + if(buf) + { + GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + + BYTE* dest = (BYTE*)alphaSrcBits; + for(const DWORD *src = buf, *end = (DWORD*)((BYTE*)buf + bmi.biSizeImage); + src != end; + ++src, ++dest) + { + BYTE a = *src >> 24; + *dest++ = a; + *dest++ = a; + *dest++ = a; + } + + _aligned_free(buf); + } + } + } + } + + if(alphaSrcBitmap) + { + BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; + bmi.biWidth = width; + bmi.biHeight = height; + bmi.biPlanes = 1; + bmi.biBitCount = 32; + bmi.biCompression = BI_RGB; + + void* colorBits; + auto colorBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, &colorBits, NULL, 0); + + void* alphaBits; + auto alphaBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, &alphaBits, NULL, 0); + + HDC hdc = CreateCompatibleDC(NULL); + HDC hdcSrc = CreateCompatibleDC(NULL); + + if(colorBitmap && alphaBitmap && hdc && hdcSrc) + { + SetStretchBltMode(hdc, HALFTONE); + + // resize color channels + SelectObject(hdc, colorBitmap); + SelectObject(hdcSrc, bitmap); + StretchBlt(hdc, 0, 0, width, height, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); + + // resize alpha channel + SelectObject(hdc, alphaBitmap); + SelectObject(hdcSrc, alphaSrcBitmap); + StretchBlt(hdc, 0, 0, width, height, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); + + // flush before touching the bits + GdiFlush(); + + // apply the alpha channel + auto dest = (BYTE*)colorBits; + auto src = (const BYTE*)alphaBits; + auto end = src + (width * height * 4); + while(src != end) + { + dest[3] = src[0]; + dest += 4; + src += 4; + } + + // create the resulting bitmap + resultBitmap = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, colorBits, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + } + + if(hdcSrc) DeleteDC(hdcSrc); + if(hdc) DeleteDC(hdc); + + if(alphaBitmap) DeleteObject(alphaBitmap); + if(colorBitmap) DeleteObject(colorBitmap); + + DeleteObject(alphaSrcBitmap); + } + + ReleaseDC(NULL, hdcScreen); + + return resultBitmap; +} + +DesktopNotificationController::Toast::Toast(HWND hWnd, shared_ptr* data) : + hWnd(hWnd), data(*data) +{ + HDC hdcScreen = GetDC(NULL); + hdc = CreateCompatibleDC(hdcScreen); + ReleaseDC(NULL, hdcScreen); +} + +DesktopNotificationController::Toast::~Toast() +{ + DeleteDC(hdc); + if(bitmap) DeleteBitmap(bitmap); + if(scaledImage) DeleteBitmap(scaledImage); +} + +void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) +{ + WNDCLASSEX wc = { sizeof(wc) }; + wc.lpfnWndProc = &Toast::WndProc; + wc.lpszClassName = className; + wc.cbWndExtra = sizeof(Toast*); + wc.hInstance = hInstance; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + + RegisterClassEx(&wc); +} + +LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch(message) + { + case WM_CREATE: + { + auto& cs = reinterpret_cast(lParam); + auto inst = new Toast(hWnd, static_cast*>(cs->lpCreateParams)); + SetWindowLongPtr(hWnd, 0, (LONG_PTR)inst); + } + break; + + case WM_NCDESTROY: + delete Get(hWnd); + SetWindowLongPtr(hWnd, 0, 0); + return 0; + + case WM_MOUSEACTIVATE: + return MA_NOACTIVATE; + + case WM_TIMER: + if(wParam == TimerID_AutoDismiss) + { + Get(hWnd)->AutoDismiss(); + } + return 0; + + case WM_LBUTTONDOWN: + { + auto inst = Get(hWnd); + + inst->Dismiss(); + + Notification notification(inst->data); + if(inst->isCloseHot) + inst->data->controller->OnNotificationDismissed(notification); + else + inst->data->controller->OnNotificationClicked(notification); + } + return 0; + + case WM_MOUSEMOVE: + { + auto inst = Get(hWnd); + if(!inst->isHighlighted) + { + inst->isHighlighted = true; + + TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, hWnd }; + TrackMouseEvent(&tme); + } + + POINT cursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; + inst->isCloseHot = (PtInRect(&inst->closeButtonRect, cursor) != FALSE); + + if(!inst->isNonInteractive) + inst->CancelDismiss(); + + inst->UpdateContents(); + } + return 0; + + case WM_MOUSELEAVE: + { + auto inst = Get(hWnd); + inst->isHighlighted = false; + inst->isCloseHot = false; + inst->UpdateContents(); + + if(!inst->easeOutActive && inst->easeInPos == 1.0f) + inst->ScheduleDismissal(); + + // Make sure stack collapse happens if needed + inst->data->controller->StartAnimation(); + } + return 0; + + case WM_WINDOWPOSCHANGED: + { + auto& wp = reinterpret_cast(lParam); + if(wp->flags & SWP_HIDEWINDOW) + { + if(!IsWindowVisible(hWnd)) + Get(hWnd)->isHighlighted = false; + } + } + break; + } + + return DefWindowProc(hWnd, message, wParam, lParam); +} + +HWND DesktopNotificationController::Toast::Create(HINSTANCE hInstance, shared_ptr& data) +{ + return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, className, nullptr, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, &data); +} + +void DesktopNotificationController::Toast::Draw() +{ + const COLORREF accent = GetAccentColor(); + + COLORREF backColor; + { + // base background color is 2/3 of accent + // highlighted adds a bit of intensity to every channel + + int h = isHighlighted ? (0xff / 20) : 0; + + backColor = RGB(min(0xff, (GetRValue(accent) * 2 / 3) + h), + min(0xff, (GetGValue(accent) * 2 / 3) + h), + min(0xff, (GetBValue(accent) * 2 / 3) + h)); + } + + const float backLuma = + (GetRValue(backColor) * 0.299f / 255) + + (GetGValue(backColor) * 0.587f / 255) + + (GetBValue(backColor) * 0.114f / 255); + + const struct { float r, g, b; } backF = { + GetRValue(backColor) / 255.0f, + GetGValue(backColor) / 255.0f, + GetBValue(backColor) / 255.0f, + }; + + COLORREF foreColor, dimmedColor; + { + // based on the lightness of background, we draw foreground in light or dark + // shades of gray blended onto the background with slight transparency + // to avoid sharp contrast + + constexpr float alpha = 0.9f; + constexpr float intensityLight[] = { (1.0f * alpha), (0.8f * alpha) }; + constexpr float intensityDark[] = { (0.1f * alpha), (0.3f * alpha) }; + + // select foreground intensity values (light or dark) + auto& i = (backLuma < 0.6f) ? intensityLight : intensityDark; + + float r, g, b; + + r = i[0] + backF.r * (1 - alpha); + g = i[0] + backF.g * (1 - alpha); + b = i[0] + backF.b * (1 - alpha); + foreColor = RGB(r * 0xff, g * 0xff, b * 0xff); + + r = i[1] + backF.r * (1 - alpha); + g = i[1] + backF.g * (1 - alpha); + b = i[1] + backF.b * (1 - alpha); + dimmedColor = RGB(r * 0xff, g * 0xff, b * 0xff); + } + + // Draw background + { + auto brush = CreateSolidBrush(backColor); + + RECT rc = { 0, 0, toastSize.cx, toastSize.cy }; + FillRect(hdc, &rc, brush); + + DeleteBrush(brush); + } + + SetBkMode(hdc, TRANSPARENT); + + const auto close = L'\x2715'; + auto captionFont = data->controller->GetCaptionFont(); + auto bodyFont = data->controller->GetBodyFont(); + + TEXTMETRIC tmCap; + SelectFont(hdc, captionFont); + GetTextMetrics(hdc, &tmCap); + + auto textOffsetX = margin.cx; + + BITMAP imageInfo = {}; + if(scaledImage) + { + GetObject(scaledImage, sizeof(imageInfo), &imageInfo); + + textOffsetX += margin.cx + imageInfo.bmWidth; + } + + // calculate close button rect + POINT closePos; + { + SIZE extent = {}; + GetTextExtentPoint32W(hdc, &close, 1, &extent); + + closeButtonRect.right = toastSize.cx; + closeButtonRect.top = 0; + + closePos.x = closeButtonRect.right - margin.cy - extent.cx; + closePos.y = closeButtonRect.top + margin.cy; + + closeButtonRect.left = closePos.x - margin.cy; + closeButtonRect.bottom = closePos.y + extent.cy + margin.cy; + } + + // image + if(scaledImage) + { + HDC hdcImage = CreateCompatibleDC(NULL); + SelectBitmap(hdcImage, scaledImage); + BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; + AlphaBlend(hdc, margin.cx, margin.cy, imageInfo.bmWidth, imageInfo.bmHeight, hdcImage, 0, 0, imageInfo.bmWidth, imageInfo.bmHeight, blend); + DeleteDC(hdcImage); + } + + // caption + { + RECT rc = { + textOffsetX, + margin.cy, + closeButtonRect.left, + toastSize.cy + }; + + SelectFont(hdc, captionFont); + SetTextColor(hdc, foreColor); + DrawText(hdc, data->caption.data(), (UINT)data->caption.length(), &rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX); + } + + // body text + if(!data->bodyText.empty()) + { + RECT rc = { + textOffsetX, + 2 * margin.cy + tmCap.tmAscent, + toastSize.cx - margin.cx, + toastSize.cy - margin.cy + }; + + SelectFont(hdc, bodyFont); + SetTextColor(hdc, dimmedColor); + DrawText(hdc, data->bodyText.data(), (UINT)data->bodyText.length(), &rc, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | DT_END_ELLIPSIS | DT_EDITCONTROL); + } + + // close button + { + SelectFont(hdc, captionFont); + SetTextColor(hdc, isCloseHot ? foreColor : dimmedColor); + ExtTextOut(hdc, closePos.x, closePos.y, 0, nullptr, &close, 1, nullptr); + } + + isContentUpdated = true; +} + +void DesktopNotificationController::Toast::Invalidate() +{ + isContentUpdated = false; +} + +bool DesktopNotificationController::Toast::IsRedrawNeeded() const +{ + return !isContentUpdated; +} + +void DesktopNotificationController::Toast::UpdateBufferSize() +{ + if(hdc) + { + SIZE newSize; + { + TEXTMETRIC tmCap = {}; + HFONT font = data->controller->GetCaptionFont(); + if(font) + { + SelectFont(hdc, font); + if(!GetTextMetrics(hdc, &tmCap)) return; + } + + TEXTMETRIC tmBody = {}; + font = data->controller->GetBodyFont(); + if(font) + { + SelectFont(hdc, font); + if(!GetTextMetrics(hdc, &tmBody)) return; + } + + this->margin = { tmCap.tmAveCharWidth * 2, tmCap.tmAscent / 2 }; + + newSize.cx = margin.cx + (32 * tmCap.tmAveCharWidth) + margin.cx; + newSize.cy = margin.cy + (tmCap.tmHeight) + margin.cy; + + if(!data->bodyText.empty()) + newSize.cy += margin.cy + (3 * tmBody.tmHeight); + + if(data->image) + { + BITMAP bm; + if(GetObject(data->image, sizeof(bm), &bm)) + { + // cap the image size + const int maxDimSize = 80; + + auto width = bm.bmWidth; + auto height = bm.bmHeight; + if(width < height) + { + if(height > maxDimSize) + { + width = width * maxDimSize / height; + height = maxDimSize; + } + } + else + { + if(width > maxDimSize) + { + height = height * maxDimSize / width; + width = maxDimSize; + } + } + + ScreenMetrics scr; + SIZE imageDrawSize = { scr.X(width), scr.Y(height) }; + + newSize.cx += imageDrawSize.cx + margin.cx; + + auto heightWithImage = margin.cy + (imageDrawSize.cy) + margin.cy; + if(newSize.cy < heightWithImage) newSize.cy = heightWithImage; + + UpdateScaledImage(imageDrawSize); + } + } + } + + if(newSize.cx != this->toastSize.cx || newSize.cy != this->toastSize.cy) + { + HDC hdcScreen = GetDC(NULL); + auto newBitmap = CreateCompatibleBitmap(hdcScreen, newSize.cx, newSize.cy); + ReleaseDC(NULL, hdcScreen); + + if(newBitmap) + { + if(SelectBitmap(hdc, newBitmap)) + { + RECT dirty1 = {}, dirty2 = {}; + if(toastSize.cx < newSize.cx) dirty1 = { toastSize.cx, 0, newSize.cx, toastSize.cy }; + if(toastSize.cy < newSize.cy) dirty2 = { 0, toastSize.cy, newSize.cx, newSize.cy }; + + if(this->bitmap) DeleteBitmap(this->bitmap); + this->bitmap = newBitmap; + this->toastSize = newSize; + + Invalidate(); + + // Resize also the DWM buffer to prevent flicker during window resizing. + // Make sure any existing data is not overwritten by marking the dirty region. + { + POINT origin = { 0, 0 }; + + UPDATELAYEREDWINDOWINFO ulw; + ulw.cbSize = sizeof(ulw); + ulw.hdcDst = NULL; + ulw.pptDst = nullptr; + ulw.psize = &toastSize; + ulw.hdcSrc = hdc; + ulw.pptSrc = &origin; + ulw.crKey = 0; + ulw.pblend = nullptr; + ulw.dwFlags = 0; + ulw.prcDirty = &dirty1; + auto b1 = UpdateLayeredWindowIndirect(hWnd, &ulw); + ulw.prcDirty = &dirty2; + auto b2 = UpdateLayeredWindowIndirect(hWnd, &ulw); + _ASSERT(b1 && b2); + } + + return; + } + + DeleteBitmap(newBitmap); + } + } + } +} + +void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) +{ + BITMAP bm; + if(!GetObject(scaledImage, sizeof(bm), &bm) || + bm.bmWidth != size.cx || + bm.bmHeight != size.cy) + { + if(scaledImage) DeleteBitmap(scaledImage); + scaledImage = StretchBitmap(data->image, size.cx, size.cy); + } +} + +void DesktopNotificationController::Toast::UpdateContents() +{ + Draw(); + + if(IsWindowVisible(hWnd)) + { + RECT rc; + GetWindowRect(hWnd, &rc); + POINT origin = { 0, 0 }; + SIZE size = { rc.right - rc.left, rc.bottom - rc.top }; + UpdateLayeredWindow(hWnd, NULL, nullptr, &size, hdc, &origin, 0, nullptr, 0); + } +} + +void DesktopNotificationController::Toast::Dismiss() +{ + if(!isNonInteractive) + { + // Set a flag to prevent further interaction. We don't disable the HWND because + // we still want to receive mouse move messages in order to keep the toast under + // the cursor and not collapse it while dismissing. + isNonInteractive = true; + + AutoDismiss(); + } +} + +void DesktopNotificationController::Toast::AutoDismiss() +{ + KillTimer(hWnd, TimerID_AutoDismiss); + StartEaseOut(); +} + +void DesktopNotificationController::Toast::CancelDismiss() +{ + KillTimer(hWnd, TimerID_AutoDismiss); + easeOutActive = false; + easeOutPos = 0; +} + +void DesktopNotificationController::Toast::ScheduleDismissal() +{ + SetTimer(hWnd, TimerID_AutoDismiss, 4000, nullptr); +} + +void DesktopNotificationController::Toast::ResetContents() +{ + if(scaledImage) + { + DeleteBitmap(scaledImage); + scaledImage = NULL; + } + + Invalidate(); +} + +void DesktopNotificationController::Toast::PopUp(int y) +{ + verticalPosTarget = verticalPos = y; + StartEaseIn(); +} + +void DesktopNotificationController::Toast::SetVerticalPosition(int y) +{ + // Don't restart animation if current target is the same + if(y == verticalPosTarget) + return; + + // Make sure the new animation's origin is at the current position + verticalPos += (int)((verticalPosTarget - verticalPos) * stackCollapsePos); + + // Set new target position and start the animation + verticalPosTarget = y; + stackCollapseStart = GetTickCount(); + data->controller->StartAnimation(); +} + +HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origin) +{ + UpdateBufferSize(); + + if(IsRedrawNeeded()) + Draw(); + + POINT srcOrigin = { 0, 0 }; + + UPDATELAYEREDWINDOWINFO ulw; + ulw.cbSize = sizeof(ulw); + ulw.hdcDst = NULL; + ulw.pptDst = nullptr; + ulw.psize = nullptr; + ulw.hdcSrc = hdc; + ulw.pptSrc = &srcOrigin; + ulw.crKey = 0; + ulw.pblend = nullptr; + ulw.dwFlags = 0; + ulw.prcDirty = nullptr; + + POINT pt = { 0, 0 }; + SIZE size = { 0, 0 }; + BLENDFUNCTION blend; + UINT dwpFlags = SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOREDRAW | SWP_NOCOPYBITS; + + auto easeInPos = AnimateEaseIn(); + auto easeOutPos = AnimateEaseOut(); + auto stackCollapsePos = AnimateStackCollapse(); + + auto yOffset = (verticalPosTarget - verticalPos) * stackCollapsePos; + + size.cx = (int)(toastSize.cx * easeInPos); + size.cy = toastSize.cy; + + pt.x = origin.x - size.cx; + pt.y = (int)(origin.y - verticalPos - yOffset - size.cy); + + ulw.pptDst = &pt; + ulw.psize = &size; + + if(easeInActive && easeInPos == 1.0f) + { + easeInActive = false; + ScheduleDismissal(); + } + + this->easeInPos = easeInPos; + this->stackCollapsePos = stackCollapsePos; + + if(easeOutPos != this->easeOutPos) + { + blend.BlendOp = AC_SRC_OVER; + blend.BlendFlags = 0; + blend.SourceConstantAlpha = (BYTE)(255 * (1.0f - easeOutPos)); + blend.AlphaFormat = 0; + + ulw.pblend = &blend; + ulw.dwFlags = ULW_ALPHA; + + this->easeOutPos = easeOutPos; + + if(easeOutPos == 1.0f) + { + easeOutActive = false; + + dwpFlags &= ~SWP_SHOWWINDOW; + dwpFlags |= SWP_HIDEWINDOW; + } + } + + if(stackCollapsePos == 1.0f) + { + verticalPos = verticalPosTarget; + } + + // `UpdateLayeredWindowIndirect` updates position, size, and transparency. + // `DeferWindowPos` updates z-order, and also position and size in case ULWI fails, + // which can happen when one of the dimensions is zero (e.g. at the beginning of ease-in). + + auto ulwResult = UpdateLayeredWindowIndirect(hWnd, &ulw); + hdwp = DeferWindowPos(hdwp, hWnd, HWND_TOPMOST, pt.x, pt.y, size.cx, size.cy, dwpFlags); + return hdwp; +} + +void DesktopNotificationController::Toast::StartEaseIn() +{ + _ASSERT(!easeInActive); + easeInStart = GetTickCount(); + easeInActive = true; + data->controller->StartAnimation(); +} + +void DesktopNotificationController::Toast::StartEaseOut() +{ + _ASSERT(!easeOutActive); + easeOutStart = GetTickCount(); + easeOutActive = true; + data->controller->StartAnimation(); +} + +bool DesktopNotificationController::Toast::IsStackCollapseActive() const +{ + return (verticalPos != verticalPosTarget); +} + +float DesktopNotificationController::Toast::AnimateEaseIn() +{ + if(!easeInActive) + return easeInPos; + + constexpr float duration = 500.0f; + float time = std::min(duration, (float)(GetTickCount() - easeInStart)) / duration; + + // decelerating exponential ease + const float a = -8.0f; + auto pos = (std::exp(a * time) - 1.0f) / (std::exp(a) - 1.0f); + + return pos; +} + +float DesktopNotificationController::Toast::AnimateEaseOut() +{ + if(!easeOutActive) + return easeOutPos; + + constexpr float duration = 120.0f; + float time = std::min(duration, (float)(GetTickCount() - easeOutStart)) / duration; + + // accelerating circle ease + auto pos = 1.0f - std::sqrt(1 - time * time); + + return pos; +} + +float DesktopNotificationController::Toast::AnimateStackCollapse() +{ + if(!IsStackCollapseActive()) + return stackCollapsePos; + + constexpr float duration = 500.0f; + float time = std::min(duration, (float)(GetTickCount() - stackCollapseStart)) / duration; + + // decelerating exponential ease + const float a = -8.0f; + auto pos = (std::exp(a * time) - 1.0f) / (std::exp(a) - 1.0f); + + return pos; +} + +} diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h new file mode 100644 index 00000000000..bdf3ce383b3 --- /dev/null +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -0,0 +1,99 @@ +#pragma once +#include "desktop_notification_controller.h" + +namespace brightray { + +class DesktopNotificationController::Toast +{ +public: + static void Register(HINSTANCE hInstance); + static HWND Create(HINSTANCE hInstance, std::shared_ptr& data); + static Toast* Get(HWND hWnd) + { + return reinterpret_cast(GetWindowLongPtr(hWnd, 0)); + } + + static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + + const std::shared_ptr& GetNotification() const + { + return data; + } + + void ResetContents(); + + void Dismiss(); + + void PopUp(int y); + void SetVerticalPosition(int y); + int GetVerticalPosition() const + { + return verticalPosTarget; + } + int GetHeight() const + { + return toastSize.cy; + } + HDWP Animate(HDWP hdwp, const POINT& origin); + bool IsAnimationActive() const + { + return easeInActive || easeOutActive || IsStackCollapseActive(); + } + bool IsHighlighted() const + { + _ASSERT(!(isHighlighted && !IsWindowVisible(hWnd))); + return isHighlighted; + } + +private: + enum TimerID { + TimerID_AutoDismiss = 1 + }; + + Toast(HWND hWnd, std::shared_ptr* data); + ~Toast(); + + void UpdateBufferSize(); + void UpdateScaledImage(const SIZE& size); + void Draw(); + void Invalidate(); + bool IsRedrawNeeded() const; + void UpdateContents(); + + void AutoDismiss(); + void CancelDismiss(); + void ScheduleDismissal(); + + void StartEaseIn(); + void StartEaseOut(); + bool IsStackCollapseActive() const; + + float AnimateEaseIn(); + float AnimateEaseOut(); + float AnimateStackCollapse(); + +private: + static constexpr const TCHAR className[] = TEXT("DesktopNotificationToast"); + + const HWND hWnd; + HDC hdc; + HBITMAP bitmap = NULL; + + const std::shared_ptr data; // never null + + SIZE toastSize = {}; + SIZE margin = {}; + RECT closeButtonRect = {}; + HBITMAP scaledImage = NULL; + + int verticalPos = 0; + int verticalPosTarget = 0; + bool isNonInteractive = false; + bool easeInActive = false; + bool easeOutActive = false; + bool isContentUpdated = false, isHighlighted = false, isCloseHot = false; + DWORD easeInStart, easeOutStart, stackCollapseStart; + float easeInPos = 0, easeOutPos = 0, stackCollapsePos = 0; +}; + +} diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc new file mode 100644 index 00000000000..fcff85cd30e --- /dev/null +++ b/brightray/browser/win/win32_notification.cc @@ -0,0 +1,60 @@ +#define WIN32_LEAN_AND_MEAN +#include "win32_notification.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include + +namespace brightray { + +void Win32Notification::Show(const base::string16& title, const base::string16& msg, const std::string& tag, const GURL& icon_url, const SkBitmap& icon, const bool silent) +{ + auto presenter = static_cast(this->presenter()); + if(!presenter) return; + + HBITMAP image = NULL; + + if(!icon.drawsNothing()) + { + if(icon.colorType() == kBGRA_8888_SkColorType) + { + icon.lockPixels(); + + BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; + bmi.biWidth = icon.width(); + bmi.biHeight = -icon.height(); + bmi.biPlanes = 1; + bmi.biBitCount = 32; + bmi.biCompression = BI_RGB; + + HDC hdcScreen = GetDC(NULL); + image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, icon.getPixels(), (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + ReleaseDC(NULL, hdcScreen); + + icon.unlockPixels(); + } + } + + Win32Notification* existing = nullptr; + if(!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag); + + if(existing) + { + existing->tag.clear(); + this->notificationRef = std::move(existing->notificationRef); + this->notificationRef.Set(title, msg, image); + } + else + { + this->notificationRef = presenter->AddNotification(title, msg, image); + } + + this->tag = tag; + + if(image) DeleteObject(image); +} + +void Win32Notification::Dismiss() +{ + notificationRef.Close(); +} + +} diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h new file mode 100644 index 00000000000..95089268ba4 --- /dev/null +++ b/brightray/browser/win/win32_notification.h @@ -0,0 +1,30 @@ +#pragma once +#include "browser/notification.h" +#include "browser/win/notification_presenter_win7.h" + +namespace brightray { + +class Win32Notification : public brightray::Notification +{ +public: + Win32Notification(NotificationDelegate* delegate, NotificationPresenterWin7* presenter) : Notification(delegate, presenter) {} + void Show(const base::string16& title, const base::string16& msg, const std::string& tag, const GURL& icon_url, const SkBitmap& icon, const bool silent) override; + void Dismiss() override; + + const DesktopNotificationController::Notification& GetRef() const + { + return notificationRef; + } + + const std::string& GetTag() const + { + return tag; + } + +private: + DISALLOW_COPY_AND_ASSIGN(Win32Notification); + DesktopNotificationController::Notification notificationRef; + std::string tag; +}; + +} diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index a0858ea926c..b7d44013b84 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -84,12 +84,21 @@ 'browser/linux/libnotify_notification.cc', 'browser/linux/notification_presenter_linux.h', 'browser/linux/notification_presenter_linux.cc', - 'browser/win/notification_presenter_win.h', 'browser/win/notification_presenter_win.cc', - 'browser/win/windows_toast_notification.h', - 'browser/win/windows_toast_notification.cc', - 'browser/win/scoped_hstring.h', + 'browser/win/notification_presenter_win.h', + 'browser/win/notification_presenter_win7.cc', + 'browser/win/notification_presenter_win7.h', 'browser/win/scoped_hstring.cc', + 'browser/win/scoped_hstring.h', + 'browser/win/win32_desktop_notifications/common.h', + 'browser/win/win32_desktop_notifications/desktop_notification_controller.cc', + 'browser/win/win32_desktop_notifications/desktop_notification_controller.h', + 'browser/win/win32_desktop_notifications/toast.cc', + 'browser/win/win32_desktop_notifications/toast.h', + 'browser/win/win32_notification.cc', + 'browser/win/win32_notification.h', + 'browser/win/windows_toast_notification.cc', + 'browser/win/windows_toast_notification.h', 'browser/special_storage_policy.cc', 'browser/special_storage_policy.h', 'browser/url_request_context_getter.cc', From 0fa6c82b3f60526f55c894b3184823a35967c67e Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 15 Mar 2017 13:58:53 +0100 Subject: [PATCH 03/20] Use `NotificationPresenterWin7` on Windows 7 --- brightray/browser/browser_client.cc | 10 ---------- brightray/browser/win/notification_presenter_win.cc | 6 ++++++ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/brightray/browser/browser_client.cc b/brightray/browser/browser_client.cc index e425b7a83b1..7b25c6a7858 100644 --- a/brightray/browser/browser_client.cc +++ b/brightray/browser/browser_client.cc @@ -13,10 +13,6 @@ #include "browser/platform_notification_service.h" #include "content/public/common/url_constants.h" -#if defined(OS_WIN) -#include "base/win/windows_version.h" -#endif - namespace brightray { namespace { @@ -39,12 +35,6 @@ BrowserClient::~BrowserClient() { } NotificationPresenter* BrowserClient::GetNotificationPresenter() { - #if defined(OS_WIN) - // Bail out if on Windows 7 or even lower, no operating will follow - if (base::win::GetVersion() < base::win::VERSION_WIN8) - return nullptr; - #endif - if (!notification_presenter_) { // Create a new presenter if on OS X, Linux, or Windows 8+ notification_presenter_.reset(NotificationPresenter::Create()); diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index 5fc961008c9..faa95cc7e4b 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -10,6 +10,7 @@ #include "base/md5.h" #include "base/strings/utf_string_conversions.h" #include "base/win/windows_version.h" +#include "browser/win/notification_presenter_win7.h" #include "browser/win/windows_toast_notification.h" #include "content/public/browser/desktop_notification_delegate.h" #include "content/public/common/platform_notification_data.h" @@ -36,6 +37,11 @@ bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) { // static NotificationPresenter* NotificationPresenter::Create() { + auto version = base::win::GetVersion(); + if (version < base::win::VERSION_WIN7) + return nullptr; + if (version < base::win::VERSION_WIN8) + return new NotificationPresenterWin7; if (!WindowsToastNotification::Initialize()) return nullptr; std::unique_ptr presenter( From 392d606848e00aa6a7f77bafb39499fec8d8b05f Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Fri, 17 Mar 2017 14:41:22 +0100 Subject: [PATCH 04/20] Changed member variable naming style to snake case --- .../win/win32_desktop_notifications/common.h | 14 +- .../desktop_notification_controller.cc | 122 ++++---- .../desktop_notification_controller.h | 16 +- .../win/win32_desktop_notifications/toast.cc | 284 +++++++++--------- .../win/win32_desktop_notifications/toast.h | 46 +-- brightray/browser/win/win32_notification.cc | 12 +- brightray/browser/win/win32_notification.h | 8 +- 7 files changed, 251 insertions(+), 251 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h index 5f44debf312..9df81bb1194 100644 --- a/brightray/browser/win/win32_desktop_notifications/common.h +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -8,7 +8,7 @@ struct NotificationData DesktopNotificationController* controller = nullptr; std::wstring caption; - std::wstring bodyText; + std::wstring body_text; HBITMAP image = NULL; @@ -31,7 +31,7 @@ inline T ScaleForDpi(T value, unsigned dpi) struct ScreenMetrics { - UINT dpiX, dpiY; + UINT dpi_x, dpi_y; ScreenMetrics() { @@ -40,18 +40,18 @@ struct ScreenMetrics if(GetDpiForMonitor) { auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY); - if(GetDpiForMonitor(monitor, 0, &dpiX, &dpiY) == S_OK) + if(GetDpiForMonitor(monitor, 0, &dpi_x, &dpi_y) == S_OK) return; } HDC hdc = GetDC(NULL); - dpiX = GetDeviceCaps(hdc, LOGPIXELSX); - dpiY = GetDeviceCaps(hdc, LOGPIXELSY); + dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); + dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(NULL, hdc); } - template T X(T value) const { return ScaleForDpi(value, dpiX); } - template T Y(T value) const { return ScaleForDpi(value, dpiY); } + template T X(T value) const { return ScaleForDpi(value, dpi_x); } + template T Y(T value) const { return ScaleForDpi(value, dpi_y); } }; } diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index e79c6e5be56..262d19ceb18 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -53,7 +53,7 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() WNDCLASSEX wc = { sizeof(wc) }; wc.lpfnWndProc = &WndProc; - wc.lpszClassName = className; + wc.lpszClassName = class_name_; wc.cbWndExtra = sizeof(DesktopNotificationController*); wc.hInstance = module; @@ -66,13 +66,13 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() DesktopNotificationController::DesktopNotificationController(unsigned maximumToasts) { - instances.reserve(maximumToasts); + instances_.reserve(maximumToasts); } DesktopNotificationController::~DesktopNotificationController() { - for(auto&& inst : instances) DestroyToast(inst); - if(hwndController) DestroyWindow(hwndController); + for(auto&& inst : instances_) DestroyToast(inst); + if(hwnd_controller_) DestroyWindow(hwnd_controller_); ClearAssets(); } @@ -115,33 +115,33 @@ LRESULT CALLBACK DesktopNotificationController::WndProc(HWND hWnd, UINT message, void DesktopNotificationController::StartAnimation() { - _ASSERT(hwndController); + _ASSERT(hwnd_controller_); - if(!isAnimating && hwndController) + if(!is_animating_ && hwnd_controller_) { // NOTE: 15ms is shorter than what we'd need for 60 fps, but since the timer // is not accurate we must request a higher frame rate to get at least 60 - SetTimer(hwndController, TimerID_Animate, 15, nullptr); - isAnimating = true; + SetTimer(hwnd_controller_, TimerID_Animate, 15, nullptr); + is_animating_ = true; } } HFONT DesktopNotificationController::GetCaptionFont() { InitializeFonts(); - return captionFont; + return caption_font_; } HFONT DesktopNotificationController::GetBodyFont() { InitializeFonts(); - return bodyFont; + return body_font_; } void DesktopNotificationController::InitializeFonts() { - if(!bodyFont) + if(!body_font_) { NONCLIENTMETRICS metrics = { sizeof(metrics) }; if(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0)) @@ -153,19 +153,19 @@ void DesktopNotificationController::InitializeFonts() ReleaseDC(NULL, hdc); metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.1f, dpiY); - bodyFont = CreateFontIndirect(&metrics.lfMessageFont); + body_font_ = CreateFontIndirect(&metrics.lfMessageFont); - if(captionFont) DeleteFont(captionFont); + if(caption_font_) DeleteFont(caption_font_); metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.4f, dpiY); - captionFont = CreateFontIndirect(&metrics.lfMessageFont); + caption_font_ = CreateFontIndirect(&metrics.lfMessageFont); } } } void DesktopNotificationController::ClearAssets() { - if(captionFont) { DeleteFont(captionFont); captionFont = NULL; } - if(bodyFont) { DeleteFont(bodyFont); bodyFont = NULL; } + if(caption_font_) { DeleteFont(caption_font_); caption_font_ = NULL; } + if(body_font_) { DeleteFont(body_font_); body_font_ = NULL; } } void DesktopNotificationController::AnimateAll() @@ -176,17 +176,17 @@ void DesktopNotificationController::AnimateAll() bool keepAnimating = false; - if(!instances.empty()) + if(!instances_.empty()) { RECT workArea; if(SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) { ScreenMetrics metrics; POINT origin = { workArea.right, - workArea.bottom - metrics.Y(toastMargin) }; + workArea.bottom - metrics.Y(toast_margin_) }; - auto hdwp = BeginDeferWindowPos((int)instances.size()); - for(auto&& inst : instances) + auto hdwp = BeginDeferWindowPos((int)instances_.size()); + for(auto&& inst : instances_) { if(!inst.hwnd) continue; @@ -201,14 +201,14 @@ void DesktopNotificationController::AnimateAll() if(!keepAnimating) { - _ASSERT(hwndController); - if(hwndController) KillTimer(hwndController, TimerID_Animate); - isAnimating = false; + _ASSERT(hwnd_controller_); + if(hwnd_controller_) KillTimer(hwnd_controller_, TimerID_Animate); + is_animating_ = false; } // Purge dismissed notifications and collapse the stack between // items which are highlighted - if(!instances.empty()) + if(!instances_.empty()) { auto isAlive = [](ToastInstance& inst) { return inst.hwnd && IsWindowVisible(inst.hwnd); @@ -218,10 +218,10 @@ void DesktopNotificationController::AnimateAll() return inst.hwnd && Toast::Get(inst.hwnd)->IsHighlighted(); }; - for(auto it = instances.begin();; ++it) + for(auto it = instances_.begin();; ++it) { // find next highlighted item - auto it2 = find_if(it, instances.end(), isHighlighted); + auto it2 = find_if(it, instances_.end(), isHighlighted); // collapse the stack in front of the highlighted item it = stable_partition(it, it2, isAlive); @@ -229,9 +229,9 @@ void DesktopNotificationController::AnimateAll() // purge the dead items for_each(it, it2, [this](auto&& inst) { DestroyToast(inst); }); - if(it2 == instances.end()) + if(it2 == instances_.end()) { - instances.erase(it, it2); + instances_.erase(it, it2); break; } @@ -240,13 +240,13 @@ void DesktopNotificationController::AnimateAll() } // Set new toast positions - if(!instances.empty()) + if(!instances_.empty()) { ScreenMetrics metrics; - auto margin = metrics.Y(toastMargin); + auto margin = metrics.Y(toast_margin_); int targetPos = 0; - for(auto&& inst : instances) + for(auto&& inst : instances_) { if(inst.hwnd) { @@ -271,11 +271,11 @@ DesktopNotificationController::Notification DesktopNotificationController::AddNo NotificationLink data(this); data->caption = move(caption); - data->bodyText = move(bodyText); + data->body_text = move(bodyText); data->image = CopyBitmap(image); // Enqueue new notification - Notification ret = *queue.insert(queue.end(), move(data)); + Notification ret = *queue_.insert(queue_.end(), move(data)); CheckQueue(); return ret; } @@ -283,16 +283,16 @@ DesktopNotificationController::Notification DesktopNotificationController::AddNo void DesktopNotificationController::CloseNotification(Notification& notification) { // Remove it from the queue - auto it = find(queue.begin(), queue.end(), notification.data); - if(it != queue.end()) + auto it = find(queue_.begin(), queue_.end(), notification.data_); + if(it != queue_.end()) { - queue.erase(it); + queue_.erase(it); this->OnNotificationClosed(notification); return; } // Dismiss active toast - auto hwnd = GetToast(notification.data.get()); + auto hwnd = GetToast(notification.data_.get()); if(hwnd) { auto toast = Toast::Get(hwnd); @@ -302,10 +302,10 @@ void DesktopNotificationController::CloseNotification(Notification& notification void DesktopNotificationController::CheckQueue() { - while(instances.size() < instances.capacity() && !queue.empty()) + while(instances_.size() < instances_.capacity() && !queue_.empty()) { - CreateToast(move(queue.front())); - queue.pop_front(); + CreateToast(move(queue_.front())); + queue_.pop_front(); } } @@ -316,22 +316,22 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) if(hwnd) { int toastPos = 0; - if(!instances.empty()) + if(!instances_.empty()) { - auto& item = instances.back(); + auto& item = instances_.back(); _ASSERT(item.hwnd); ScreenMetrics scr; auto toast = Toast::Get(item.hwnd); - toastPos = toast->GetVerticalPosition() + toast->GetHeight() + scr.Y(toastMargin); + toastPos = toast->GetVerticalPosition() + toast->GetHeight() + scr.Y(toast_margin_); } - instances.push_back({ hwnd, move(data) }); + instances_.push_back({ hwnd, move(data) }); - if(!hwndController) + if(!hwnd_controller_) { // NOTE: We cannot use a message-only window because we need to receive system notifications - hwndController = CreateWindow(className, nullptr, 0, 0, 0, 0, 0, NULL, NULL, hInstance, this); + hwnd_controller_ = CreateWindow(class_name_, nullptr, 0, 0, 0, 0, 0, NULL, NULL, hInstance, this); } auto toast = Toast::Get(hwnd); @@ -341,12 +341,12 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) HWND DesktopNotificationController::GetToast(const NotificationData* data) const { - auto it = find_if(instances.cbegin(), instances.cend(), [data](auto&& inst) { + auto it = find_if(instances_.cbegin(), instances_.cend(), [data](auto&& inst) { auto toast = Toast::Get(inst.hwnd); return data == toast->GetNotification().get(); }); - return (it != instances.cend()) ? it->hwnd : NULL; + return (it != instances_.cend()) ? it->hwnd : NULL; } void DesktopNotificationController::DestroyToast(ToastInstance& inst) @@ -365,41 +365,41 @@ void DesktopNotificationController::DestroyToast(ToastInstance& inst) DesktopNotificationController::Notification::Notification(const shared_ptr& data) : - data(data) + data_(data) { _ASSERT(data != nullptr); } bool DesktopNotificationController::Notification::operator==(const Notification& other) const { - return data == other.data; + return data_ == other.data_; } void DesktopNotificationController::Notification::Close() { // No business calling this when not pointing to a valid instance - _ASSERT(data); + _ASSERT(data_); - if(data->controller) - data->controller->CloseNotification(*this); + if(data_->controller) + data_->controller->CloseNotification(*this); } void DesktopNotificationController::Notification::Set(std::wstring caption, std::wstring bodyText, HBITMAP image) { // No business calling this when not pointing to a valid instance - _ASSERT(data); + _ASSERT(data_); // Do nothing when the notification has been closed - if(!data->controller) + if(!data_->controller) return; - if(data->image) DeleteBitmap(data->image); + if(data_->image) DeleteBitmap(data_->image); - data->caption = move(caption); - data->bodyText = move(bodyText); - data->image = CopyBitmap(image); + data_->caption = move(caption); + data_->body_text = move(bodyText); + data_->image = CopyBitmap(image); - auto hwnd = data->controller->GetToast(data.get()); + auto hwnd = data_->controller->GetToast(data_.get()); if(hwnd) { auto toast = Toast::Get(hwnd); @@ -407,7 +407,7 @@ void DesktopNotificationController::Notification::Set(std::wstring caption, std: } // Change of contents can affect size and position of all toasts - data->controller->StartAnimation(); + data_->controller->StartAnimation(); } diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index 1d93591f60d..8b5b160b0e8 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -37,7 +37,7 @@ private: }; template - static constexpr T toastMargin = 20; + static constexpr T toast_margin_ = 20; // Wrapper around `NotificationData` which makes sure that // the `controller` member is cleared when the controller object @@ -75,12 +75,12 @@ private: void DestroyToast(ToastInstance& inst); private: - static constexpr const TCHAR className[] = TEXT("DesktopNotificationController"); - HWND hwndController = NULL; - HFONT captionFont = NULL, bodyFont = NULL; - std::vector instances; - std::deque queue; - bool isAnimating = false; + static constexpr const TCHAR class_name_[] = TEXT("DesktopNotificationController"); + HWND hwnd_controller_ = NULL; + HFONT caption_font_ = NULL, body_font_ = NULL; + std::vector instances_; + std::deque queue_; + bool is_animating_ = false; }; class DesktopNotificationController::Notification @@ -95,7 +95,7 @@ public: void Set(std::wstring caption, std::wstring bodyText, HBITMAP image); private: - std::shared_ptr data; + std::shared_ptr data_; friend class DesktopNotificationController; }; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index f6cb8d06cef..4f90c37d97f 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -167,25 +167,25 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) } DesktopNotificationController::Toast::Toast(HWND hWnd, shared_ptr* data) : - hWnd(hWnd), data(*data) + hwnd_(hWnd), data_(*data) { HDC hdcScreen = GetDC(NULL); - hdc = CreateCompatibleDC(hdcScreen); + hdc_ = CreateCompatibleDC(hdcScreen); ReleaseDC(NULL, hdcScreen); } DesktopNotificationController::Toast::~Toast() { - DeleteDC(hdc); - if(bitmap) DeleteBitmap(bitmap); - if(scaledImage) DeleteBitmap(scaledImage); + DeleteDC(hdc_); + if(bitmap_) DeleteBitmap(bitmap_); + if(scaled_image_) DeleteBitmap(scaled_image_); } void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) { WNDCLASSEX wc = { sizeof(wc) }; wc.lpfnWndProc = &Toast::WndProc; - wc.lpszClassName = className; + wc.lpszClassName = class_name_; wc.cbWndExtra = sizeof(Toast*); wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); @@ -226,29 +226,29 @@ LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, W inst->Dismiss(); - Notification notification(inst->data); - if(inst->isCloseHot) - inst->data->controller->OnNotificationDismissed(notification); + Notification notification(inst->data_); + if(inst->is_close_hot_) + inst->data_->controller->OnNotificationDismissed(notification); else - inst->data->controller->OnNotificationClicked(notification); + inst->data_->controller->OnNotificationClicked(notification); } return 0; case WM_MOUSEMOVE: { auto inst = Get(hWnd); - if(!inst->isHighlighted) + if(!inst->is_highlighted_) { - inst->isHighlighted = true; + inst->is_highlighted_ = true; TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, hWnd }; TrackMouseEvent(&tme); } POINT cursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; - inst->isCloseHot = (PtInRect(&inst->closeButtonRect, cursor) != FALSE); + inst->is_close_hot_ = (PtInRect(&inst->close_button_rect_, cursor) != FALSE); - if(!inst->isNonInteractive) + if(!inst->is_non_interactive_) inst->CancelDismiss(); inst->UpdateContents(); @@ -258,15 +258,15 @@ LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, W case WM_MOUSELEAVE: { auto inst = Get(hWnd); - inst->isHighlighted = false; - inst->isCloseHot = false; + inst->is_highlighted_ = false; + inst->is_close_hot_ = false; inst->UpdateContents(); - if(!inst->easeOutActive && inst->easeInPos == 1.0f) + if(!inst->ease_out_active_ && inst->ease_in_pos_ == 1.0f) inst->ScheduleDismissal(); // Make sure stack collapse happens if needed - inst->data->controller->StartAnimation(); + inst->data_->controller->StartAnimation(); } return 0; @@ -276,7 +276,7 @@ LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, W if(wp->flags & SWP_HIDEWINDOW) { if(!IsWindowVisible(hWnd)) - Get(hWnd)->isHighlighted = false; + Get(hWnd)->is_highlighted_ = false; } } break; @@ -287,7 +287,7 @@ LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, W HWND DesktopNotificationController::Toast::Create(HINSTANCE hInstance, shared_ptr& data) { - return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, className, nullptr, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, &data); + return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, class_name_, nullptr, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, &data); } void DesktopNotificationController::Toast::Draw() @@ -299,7 +299,7 @@ void DesktopNotificationController::Toast::Draw() // base background color is 2/3 of accent // highlighted adds a bit of intensity to every channel - int h = isHighlighted ? (0xff / 20) : 0; + int h = is_highlighted_ ? (0xff / 20) : 0; backColor = RGB(min(0xff, (GetRValue(accent) * 2 / 3) + h), min(0xff, (GetGValue(accent) * 2 / 3) + h), @@ -347,55 +347,55 @@ void DesktopNotificationController::Toast::Draw() { auto brush = CreateSolidBrush(backColor); - RECT rc = { 0, 0, toastSize.cx, toastSize.cy }; - FillRect(hdc, &rc, brush); + RECT rc = { 0, 0, toast_size_.cx, toast_size_.cy }; + FillRect(hdc_, &rc, brush); DeleteBrush(brush); } - SetBkMode(hdc, TRANSPARENT); + SetBkMode(hdc_, TRANSPARENT); const auto close = L'\x2715'; - auto captionFont = data->controller->GetCaptionFont(); - auto bodyFont = data->controller->GetBodyFont(); + auto captionFont = data_->controller->GetCaptionFont(); + auto bodyFont = data_->controller->GetBodyFont(); TEXTMETRIC tmCap; - SelectFont(hdc, captionFont); - GetTextMetrics(hdc, &tmCap); + SelectFont(hdc_, captionFont); + GetTextMetrics(hdc_, &tmCap); - auto textOffsetX = margin.cx; + auto textOffsetX = margin_.cx; BITMAP imageInfo = {}; - if(scaledImage) + if(scaled_image_) { - GetObject(scaledImage, sizeof(imageInfo), &imageInfo); + GetObject(scaled_image_, sizeof(imageInfo), &imageInfo); - textOffsetX += margin.cx + imageInfo.bmWidth; + textOffsetX += margin_.cx + imageInfo.bmWidth; } // calculate close button rect POINT closePos; { SIZE extent = {}; - GetTextExtentPoint32W(hdc, &close, 1, &extent); + GetTextExtentPoint32W(hdc_, &close, 1, &extent); - closeButtonRect.right = toastSize.cx; - closeButtonRect.top = 0; + close_button_rect_.right = toast_size_.cx; + close_button_rect_.top = 0; - closePos.x = closeButtonRect.right - margin.cy - extent.cx; - closePos.y = closeButtonRect.top + margin.cy; + closePos.x = close_button_rect_.right - margin_.cy - extent.cx; + closePos.y = close_button_rect_.top + margin_.cy; - closeButtonRect.left = closePos.x - margin.cy; - closeButtonRect.bottom = closePos.y + extent.cy + margin.cy; + close_button_rect_.left = closePos.x - margin_.cy; + close_button_rect_.bottom = closePos.y + extent.cy + margin_.cy; } // image - if(scaledImage) + if(scaled_image_) { HDC hdcImage = CreateCompatibleDC(NULL); - SelectBitmap(hdcImage, scaledImage); + SelectBitmap(hdcImage, scaled_image_); BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; - AlphaBlend(hdc, margin.cx, margin.cy, imageInfo.bmWidth, imageInfo.bmHeight, hdcImage, 0, 0, imageInfo.bmWidth, imageInfo.bmHeight, blend); + AlphaBlend(hdc_, margin_.cx, margin_.cy, imageInfo.bmWidth, imageInfo.bmHeight, hdcImage, 0, 0, imageInfo.bmWidth, imageInfo.bmHeight, blend); DeleteDC(hdcImage); } @@ -403,85 +403,85 @@ void DesktopNotificationController::Toast::Draw() { RECT rc = { textOffsetX, - margin.cy, - closeButtonRect.left, - toastSize.cy + margin_.cy, + close_button_rect_.left, + toast_size_.cy }; - SelectFont(hdc, captionFont); - SetTextColor(hdc, foreColor); - DrawText(hdc, data->caption.data(), (UINT)data->caption.length(), &rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX); + SelectFont(hdc_, captionFont); + SetTextColor(hdc_, foreColor); + DrawText(hdc_, data_->caption.data(), (UINT)data_->caption.length(), &rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX); } // body text - if(!data->bodyText.empty()) + if(!data_->body_text.empty()) { RECT rc = { textOffsetX, - 2 * margin.cy + tmCap.tmAscent, - toastSize.cx - margin.cx, - toastSize.cy - margin.cy + 2 * margin_.cy + tmCap.tmAscent, + toast_size_.cx - margin_.cx, + toast_size_.cy - margin_.cy }; - SelectFont(hdc, bodyFont); - SetTextColor(hdc, dimmedColor); - DrawText(hdc, data->bodyText.data(), (UINT)data->bodyText.length(), &rc, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | DT_END_ELLIPSIS | DT_EDITCONTROL); + SelectFont(hdc_, bodyFont); + SetTextColor(hdc_, dimmedColor); + DrawText(hdc_, data_->body_text.data(), (UINT)data_->body_text.length(), &rc, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | DT_END_ELLIPSIS | DT_EDITCONTROL); } // close button { - SelectFont(hdc, captionFont); - SetTextColor(hdc, isCloseHot ? foreColor : dimmedColor); - ExtTextOut(hdc, closePos.x, closePos.y, 0, nullptr, &close, 1, nullptr); + SelectFont(hdc_, captionFont); + SetTextColor(hdc_, is_close_hot_ ? foreColor : dimmedColor); + ExtTextOut(hdc_, closePos.x, closePos.y, 0, nullptr, &close, 1, nullptr); } - isContentUpdated = true; + is_content_updated_ = true; } void DesktopNotificationController::Toast::Invalidate() { - isContentUpdated = false; + is_content_updated_ = false; } bool DesktopNotificationController::Toast::IsRedrawNeeded() const { - return !isContentUpdated; + return !is_content_updated_; } void DesktopNotificationController::Toast::UpdateBufferSize() { - if(hdc) + if(hdc_) { SIZE newSize; { TEXTMETRIC tmCap = {}; - HFONT font = data->controller->GetCaptionFont(); + HFONT font = data_->controller->GetCaptionFont(); if(font) { - SelectFont(hdc, font); - if(!GetTextMetrics(hdc, &tmCap)) return; + SelectFont(hdc_, font); + if(!GetTextMetrics(hdc_, &tmCap)) return; } TEXTMETRIC tmBody = {}; - font = data->controller->GetBodyFont(); + font = data_->controller->GetBodyFont(); if(font) { - SelectFont(hdc, font); - if(!GetTextMetrics(hdc, &tmBody)) return; + SelectFont(hdc_, font); + if(!GetTextMetrics(hdc_, &tmBody)) return; } - this->margin = { tmCap.tmAveCharWidth * 2, tmCap.tmAscent / 2 }; + this->margin_ = { tmCap.tmAveCharWidth * 2, tmCap.tmAscent / 2 }; - newSize.cx = margin.cx + (32 * tmCap.tmAveCharWidth) + margin.cx; - newSize.cy = margin.cy + (tmCap.tmHeight) + margin.cy; + newSize.cx = margin_.cx + (32 * tmCap.tmAveCharWidth) + margin_.cx; + newSize.cy = margin_.cy + (tmCap.tmHeight) + margin_.cy; - if(!data->bodyText.empty()) - newSize.cy += margin.cy + (3 * tmBody.tmHeight); + if(!data_->body_text.empty()) + newSize.cy += margin_.cy + (3 * tmBody.tmHeight); - if(data->image) + if(data_->image) { BITMAP bm; - if(GetObject(data->image, sizeof(bm), &bm)) + if(GetObject(data_->image, sizeof(bm), &bm)) { // cap the image size const int maxDimSize = 80; @@ -508,9 +508,9 @@ void DesktopNotificationController::Toast::UpdateBufferSize() ScreenMetrics scr; SIZE imageDrawSize = { scr.X(width), scr.Y(height) }; - newSize.cx += imageDrawSize.cx + margin.cx; + newSize.cx += imageDrawSize.cx + margin_.cx; - auto heightWithImage = margin.cy + (imageDrawSize.cy) + margin.cy; + auto heightWithImage = margin_.cy + (imageDrawSize.cy) + margin_.cy; if(newSize.cy < heightWithImage) newSize.cy = heightWithImage; UpdateScaledImage(imageDrawSize); @@ -518,7 +518,7 @@ void DesktopNotificationController::Toast::UpdateBufferSize() } } - if(newSize.cx != this->toastSize.cx || newSize.cy != this->toastSize.cy) + if(newSize.cx != this->toast_size_.cx || newSize.cy != this->toast_size_.cy) { HDC hdcScreen = GetDC(NULL); auto newBitmap = CreateCompatibleBitmap(hdcScreen, newSize.cx, newSize.cy); @@ -526,15 +526,15 @@ void DesktopNotificationController::Toast::UpdateBufferSize() if(newBitmap) { - if(SelectBitmap(hdc, newBitmap)) + if(SelectBitmap(hdc_, newBitmap)) { RECT dirty1 = {}, dirty2 = {}; - if(toastSize.cx < newSize.cx) dirty1 = { toastSize.cx, 0, newSize.cx, toastSize.cy }; - if(toastSize.cy < newSize.cy) dirty2 = { 0, toastSize.cy, newSize.cx, newSize.cy }; + if(toast_size_.cx < newSize.cx) dirty1 = { toast_size_.cx, 0, newSize.cx, toast_size_.cy }; + if(toast_size_.cy < newSize.cy) dirty2 = { 0, toast_size_.cy, newSize.cx, newSize.cy }; - if(this->bitmap) DeleteBitmap(this->bitmap); - this->bitmap = newBitmap; - this->toastSize = newSize; + if(this->bitmap_) DeleteBitmap(this->bitmap_); + this->bitmap_ = newBitmap; + this->toast_size_ = newSize; Invalidate(); @@ -547,16 +547,16 @@ void DesktopNotificationController::Toast::UpdateBufferSize() ulw.cbSize = sizeof(ulw); ulw.hdcDst = NULL; ulw.pptDst = nullptr; - ulw.psize = &toastSize; - ulw.hdcSrc = hdc; + ulw.psize = &toast_size_; + ulw.hdcSrc = hdc_; ulw.pptSrc = &origin; ulw.crKey = 0; ulw.pblend = nullptr; ulw.dwFlags = 0; ulw.prcDirty = &dirty1; - auto b1 = UpdateLayeredWindowIndirect(hWnd, &ulw); + auto b1 = UpdateLayeredWindowIndirect(hwnd_, &ulw); ulw.prcDirty = &dirty2; - auto b2 = UpdateLayeredWindowIndirect(hWnd, &ulw); + auto b2 = UpdateLayeredWindowIndirect(hwnd_, &ulw); _ASSERT(b1 && b2); } @@ -572,12 +572,12 @@ void DesktopNotificationController::Toast::UpdateBufferSize() void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) { BITMAP bm; - if(!GetObject(scaledImage, sizeof(bm), &bm) || + if(!GetObject(scaled_image_, sizeof(bm), &bm) || bm.bmWidth != size.cx || bm.bmHeight != size.cy) { - if(scaledImage) DeleteBitmap(scaledImage); - scaledImage = StretchBitmap(data->image, size.cx, size.cy); + if(scaled_image_) DeleteBitmap(scaled_image_); + scaled_image_ = StretchBitmap(data_->image, size.cx, size.cy); } } @@ -585,24 +585,24 @@ void DesktopNotificationController::Toast::UpdateContents() { Draw(); - if(IsWindowVisible(hWnd)) + if(IsWindowVisible(hwnd_)) { RECT rc; - GetWindowRect(hWnd, &rc); + GetWindowRect(hwnd_, &rc); POINT origin = { 0, 0 }; SIZE size = { rc.right - rc.left, rc.bottom - rc.top }; - UpdateLayeredWindow(hWnd, NULL, nullptr, &size, hdc, &origin, 0, nullptr, 0); + UpdateLayeredWindow(hwnd_, NULL, nullptr, &size, hdc_, &origin, 0, nullptr, 0); } } void DesktopNotificationController::Toast::Dismiss() { - if(!isNonInteractive) + if(!is_non_interactive_) { // Set a flag to prevent further interaction. We don't disable the HWND because // we still want to receive mouse move messages in order to keep the toast under // the cursor and not collapse it while dismissing. - isNonInteractive = true; + is_non_interactive_ = true; AutoDismiss(); } @@ -610,28 +610,28 @@ void DesktopNotificationController::Toast::Dismiss() void DesktopNotificationController::Toast::AutoDismiss() { - KillTimer(hWnd, TimerID_AutoDismiss); + KillTimer(hwnd_, TimerID_AutoDismiss); StartEaseOut(); } void DesktopNotificationController::Toast::CancelDismiss() { - KillTimer(hWnd, TimerID_AutoDismiss); - easeOutActive = false; - easeOutPos = 0; + KillTimer(hwnd_, TimerID_AutoDismiss); + ease_out_active_ = false; + ease_out_pos_ = 0; } void DesktopNotificationController::Toast::ScheduleDismissal() { - SetTimer(hWnd, TimerID_AutoDismiss, 4000, nullptr); + SetTimer(hwnd_, TimerID_AutoDismiss, 4000, nullptr); } void DesktopNotificationController::Toast::ResetContents() { - if(scaledImage) + if(scaled_image_) { - DeleteBitmap(scaledImage); - scaledImage = NULL; + DeleteBitmap(scaled_image_); + scaled_image_ = NULL; } Invalidate(); @@ -639,23 +639,23 @@ void DesktopNotificationController::Toast::ResetContents() void DesktopNotificationController::Toast::PopUp(int y) { - verticalPosTarget = verticalPos = y; + vertical_pos_target_ = vertical_pos_ = y; StartEaseIn(); } void DesktopNotificationController::Toast::SetVerticalPosition(int y) { // Don't restart animation if current target is the same - if(y == verticalPosTarget) + if(y == vertical_pos_target_) return; // Make sure the new animation's origin is at the current position - verticalPos += (int)((verticalPosTarget - verticalPos) * stackCollapsePos); + vertical_pos_ += (int)((vertical_pos_target_ - vertical_pos_) * stack_collapse_pos_); // Set new target position and start the animation - verticalPosTarget = y; - stackCollapseStart = GetTickCount(); - data->controller->StartAnimation(); + vertical_pos_target_ = y; + stack_collapse_start_ = GetTickCount(); + data_->controller->StartAnimation(); } HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origin) @@ -672,7 +672,7 @@ HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origi ulw.hdcDst = NULL; ulw.pptDst = nullptr; ulw.psize = nullptr; - ulw.hdcSrc = hdc; + ulw.hdcSrc = hdc_; ulw.pptSrc = &srcOrigin; ulw.crKey = 0; ulw.pblend = nullptr; @@ -688,27 +688,27 @@ HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origi auto easeOutPos = AnimateEaseOut(); auto stackCollapsePos = AnimateStackCollapse(); - auto yOffset = (verticalPosTarget - verticalPos) * stackCollapsePos; + auto yOffset = (vertical_pos_target_ - vertical_pos_) * stackCollapsePos; - size.cx = (int)(toastSize.cx * easeInPos); - size.cy = toastSize.cy; + size.cx = (int)(toast_size_.cx * easeInPos); + size.cy = toast_size_.cy; pt.x = origin.x - size.cx; - pt.y = (int)(origin.y - verticalPos - yOffset - size.cy); + pt.y = (int)(origin.y - vertical_pos_ - yOffset - size.cy); ulw.pptDst = &pt; ulw.psize = &size; - if(easeInActive && easeInPos == 1.0f) + if(ease_in_active_ && easeInPos == 1.0f) { - easeInActive = false; + ease_in_active_ = false; ScheduleDismissal(); } - this->easeInPos = easeInPos; - this->stackCollapsePos = stackCollapsePos; + this->ease_in_pos_ = easeInPos; + this->stack_collapse_pos_ = stackCollapsePos; - if(easeOutPos != this->easeOutPos) + if(easeOutPos != this->ease_out_pos_) { blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; @@ -718,11 +718,11 @@ HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origi ulw.pblend = &blend; ulw.dwFlags = ULW_ALPHA; - this->easeOutPos = easeOutPos; + this->ease_out_pos_ = easeOutPos; if(easeOutPos == 1.0f) { - easeOutActive = false; + ease_out_active_ = false; dwpFlags &= ~SWP_SHOWWINDOW; dwpFlags |= SWP_HIDEWINDOW; @@ -731,46 +731,46 @@ HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origi if(stackCollapsePos == 1.0f) { - verticalPos = verticalPosTarget; + vertical_pos_ = vertical_pos_target_; } // `UpdateLayeredWindowIndirect` updates position, size, and transparency. // `DeferWindowPos` updates z-order, and also position and size in case ULWI fails, // which can happen when one of the dimensions is zero (e.g. at the beginning of ease-in). - auto ulwResult = UpdateLayeredWindowIndirect(hWnd, &ulw); - hdwp = DeferWindowPos(hdwp, hWnd, HWND_TOPMOST, pt.x, pt.y, size.cx, size.cy, dwpFlags); + auto ulwResult = UpdateLayeredWindowIndirect(hwnd_, &ulw); + hdwp = DeferWindowPos(hdwp, hwnd_, HWND_TOPMOST, pt.x, pt.y, size.cx, size.cy, dwpFlags); return hdwp; } void DesktopNotificationController::Toast::StartEaseIn() { - _ASSERT(!easeInActive); - easeInStart = GetTickCount(); - easeInActive = true; - data->controller->StartAnimation(); + _ASSERT(!ease_in_active_); + ease_in_start_ = GetTickCount(); + ease_in_active_ = true; + data_->controller->StartAnimation(); } void DesktopNotificationController::Toast::StartEaseOut() { - _ASSERT(!easeOutActive); - easeOutStart = GetTickCount(); - easeOutActive = true; - data->controller->StartAnimation(); + _ASSERT(!ease_out_active_); + ease_out_start_ = GetTickCount(); + ease_out_active_ = true; + data_->controller->StartAnimation(); } bool DesktopNotificationController::Toast::IsStackCollapseActive() const { - return (verticalPos != verticalPosTarget); + return (vertical_pos_ != vertical_pos_target_); } float DesktopNotificationController::Toast::AnimateEaseIn() { - if(!easeInActive) - return easeInPos; + if(!ease_in_active_) + return ease_in_pos_; constexpr float duration = 500.0f; - float time = std::min(duration, (float)(GetTickCount() - easeInStart)) / duration; + float time = std::min(duration, (float)(GetTickCount() - ease_in_start_)) / duration; // decelerating exponential ease const float a = -8.0f; @@ -781,11 +781,11 @@ float DesktopNotificationController::Toast::AnimateEaseIn() float DesktopNotificationController::Toast::AnimateEaseOut() { - if(!easeOutActive) - return easeOutPos; + if(!ease_out_active_) + return ease_out_pos_; constexpr float duration = 120.0f; - float time = std::min(duration, (float)(GetTickCount() - easeOutStart)) / duration; + float time = std::min(duration, (float)(GetTickCount() - ease_out_start_)) / duration; // accelerating circle ease auto pos = 1.0f - std::sqrt(1 - time * time); @@ -796,10 +796,10 @@ float DesktopNotificationController::Toast::AnimateEaseOut() float DesktopNotificationController::Toast::AnimateStackCollapse() { if(!IsStackCollapseActive()) - return stackCollapsePos; + return stack_collapse_pos_; constexpr float duration = 500.0f; - float time = std::min(duration, (float)(GetTickCount() - stackCollapseStart)) / duration; + float time = std::min(duration, (float)(GetTickCount() - stack_collapse_start_)) / duration; // decelerating exponential ease const float a = -8.0f; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index bdf3ce383b3..64ea7d1e99b 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -17,7 +17,7 @@ public: const std::shared_ptr& GetNotification() const { - return data; + return data_; } void ResetContents(); @@ -28,21 +28,21 @@ public: void SetVerticalPosition(int y); int GetVerticalPosition() const { - return verticalPosTarget; + return vertical_pos_target_; } int GetHeight() const { - return toastSize.cy; + return toast_size_.cy; } HDWP Animate(HDWP hdwp, const POINT& origin); bool IsAnimationActive() const { - return easeInActive || easeOutActive || IsStackCollapseActive(); + return ease_in_active_ || ease_out_active_ || IsStackCollapseActive(); } bool IsHighlighted() const { - _ASSERT(!(isHighlighted && !IsWindowVisible(hWnd))); - return isHighlighted; + _ASSERT(!(is_highlighted_ && !IsWindowVisible(hwnd_))); + return is_highlighted_; } private: @@ -73,27 +73,27 @@ private: float AnimateStackCollapse(); private: - static constexpr const TCHAR className[] = TEXT("DesktopNotificationToast"); + static constexpr const TCHAR class_name_[] = TEXT("DesktopNotificationToast"); - const HWND hWnd; - HDC hdc; - HBITMAP bitmap = NULL; + const HWND hwnd_; + HDC hdc_; + HBITMAP bitmap_ = NULL; - const std::shared_ptr data; // never null + const std::shared_ptr data_; // never null - SIZE toastSize = {}; - SIZE margin = {}; - RECT closeButtonRect = {}; - HBITMAP scaledImage = NULL; + SIZE toast_size_ = {}; + SIZE margin_ = {}; + RECT close_button_rect_ = {}; + HBITMAP scaled_image_ = NULL; - int verticalPos = 0; - int verticalPosTarget = 0; - bool isNonInteractive = false; - bool easeInActive = false; - bool easeOutActive = false; - bool isContentUpdated = false, isHighlighted = false, isCloseHot = false; - DWORD easeInStart, easeOutStart, stackCollapseStart; - float easeInPos = 0, easeOutPos = 0, stackCollapsePos = 0; + int vertical_pos_ = 0; + int vertical_pos_target_ = 0; + bool is_non_interactive_ = false; + bool ease_in_active_ = false; + bool ease_out_active_ = false; + bool is_content_updated_ = false, is_highlighted_ = false, is_close_hot_ = false; + DWORD ease_in_start_, ease_out_start_, stack_collapse_start_; + float ease_in_pos_ = 0, ease_out_pos_ = 0, stack_collapse_pos_ = 0; }; } diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index fcff85cd30e..86b42cf768f 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -38,23 +38,23 @@ void Win32Notification::Show(const base::string16& title, const base::string16& if(existing) { - existing->tag.clear(); - this->notificationRef = std::move(existing->notificationRef); - this->notificationRef.Set(title, msg, image); + existing->tag_.clear(); + this->notification_ref_ = std::move(existing->notification_ref_); + this->notification_ref_.Set(title, msg, image); } else { - this->notificationRef = presenter->AddNotification(title, msg, image); + this->notification_ref_ = presenter->AddNotification(title, msg, image); } - this->tag = tag; + this->tag_ = tag; if(image) DeleteObject(image); } void Win32Notification::Dismiss() { - notificationRef.Close(); + notification_ref_.Close(); } } diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index 95089268ba4..79919354332 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -13,18 +13,18 @@ public: const DesktopNotificationController::Notification& GetRef() const { - return notificationRef; + return notification_ref_; } const std::string& GetTag() const { - return tag; + return tag_; } private: DISALLOW_COPY_AND_ASSIGN(Win32Notification); - DesktopNotificationController::Notification notificationRef; - std::string tag; + DesktopNotificationController::Notification notification_ref_; + std::string tag_; }; } From 0bba5b9b4199842646ba2b94cca05774705cc543 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Mon, 3 Apr 2017 10:38:21 +0200 Subject: [PATCH 05/20] Address cpplint issue "Lines should be <= 80 characters long [whitespace/line_length] [2]" --- .../linux/notification_presenter_linux.cc | 3 +- .../linux/notification_presenter_linux.h | 3 +- .../browser/mac/notification_presenter_mac.h | 3 +- .../browser/mac/notification_presenter_mac.mm | 3 +- brightray/browser/notification_presenter.h | 3 +- .../browser/win/notification_presenter_win.cc | 3 +- .../browser/win/notification_presenter_win.h | 3 +- .../win/notification_presenter_win7.cc | 15 +- .../browser/win/notification_presenter_win7.h | 7 +- .../win/win32_desktop_notifications/common.h | 8 +- .../desktop_notification_controller.cc | 75 +++++--- .../desktop_notification_controller.h | 16 +- .../win/win32_desktop_notifications/toast.cc | 171 ++++++++++++------ .../win/win32_desktop_notifications/toast.h | 13 +- brightray/browser/win/win32_notification.cc | 8 +- brightray/browser/win/win32_notification.h | 10 +- 16 files changed, 238 insertions(+), 106 deletions(-) diff --git a/brightray/browser/linux/notification_presenter_linux.cc b/brightray/browser/linux/notification_presenter_linux.cc index 25d1b6d982b..6df5ebaaec9 100644 --- a/brightray/browser/linux/notification_presenter_linux.cc +++ b/brightray/browser/linux/notification_presenter_linux.cc @@ -22,7 +22,8 @@ NotificationPresenterLinux::NotificationPresenterLinux() { NotificationPresenterLinux::~NotificationPresenterLinux() { } -Notification* NotificationPresenterLinux::CreateNotificationObject(NotificationDelegate* delegate) { +Notification* NotificationPresenterLinux::CreateNotificationObject( + NotificationDelegate* delegate) { return new LibnotifyNotification(delegate, this); } diff --git a/brightray/browser/linux/notification_presenter_linux.h b/brightray/browser/linux/notification_presenter_linux.h index b8adcd1348d..a90f31ea60e 100644 --- a/brightray/browser/linux/notification_presenter_linux.h +++ b/brightray/browser/linux/notification_presenter_linux.h @@ -16,7 +16,8 @@ class NotificationPresenterLinux : public NotificationPresenter { ~NotificationPresenterLinux(); private: - Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + Notification* CreateNotificationObject( + NotificationDelegate* delegate) override; DISALLOW_COPY_AND_ASSIGN(NotificationPresenterLinux); }; diff --git a/brightray/browser/mac/notification_presenter_mac.h b/brightray/browser/mac/notification_presenter_mac.h index f6e5f7959c8..514a27625ea 100644 --- a/brightray/browser/mac/notification_presenter_mac.h +++ b/brightray/browser/mac/notification_presenter_mac.h @@ -22,7 +22,8 @@ class NotificationPresenterMac : public NotificationPresenter { ~NotificationPresenterMac(); private: - Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + Notification* CreateNotificationObject( + NotificationDelegate* delegate) override; base::scoped_nsobject notification_center_delegate_; diff --git a/brightray/browser/mac/notification_presenter_mac.mm b/brightray/browser/mac/notification_presenter_mac.mm index a1fb2e77893..fec4c1a0539 100644 --- a/brightray/browser/mac/notification_presenter_mac.mm +++ b/brightray/browser/mac/notification_presenter_mac.mm @@ -35,7 +35,8 @@ NotificationPresenterMac::~NotificationPresenterMac() { NSUserNotificationCenter.defaultUserNotificationCenter.delegate = nil; } -Notification* NotificationPresenterMac::CreateNotificationObject(NotificationDelegate* delegate) { +Notification* NotificationPresenterMac::CreateNotificationObject( + NotificationDelegate* delegate) { return new CocoaNotification(delegate, this); } diff --git a/brightray/browser/notification_presenter.h b/brightray/browser/notification_presenter.h index f88a9a6449a..721ba92ace4 100644 --- a/brightray/browser/notification_presenter.h +++ b/brightray/browser/notification_presenter.h @@ -27,7 +27,8 @@ class NotificationPresenter { protected: NotificationPresenter(); - virtual Notification* CreateNotificationObject(NotificationDelegate* delegate) = 0; + virtual Notification* CreateNotificationObject( + NotificationDelegate* delegate) = 0; private: friend class Notification; diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index faa95cc7e4b..42819eada71 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -72,7 +72,8 @@ base::string16 NotificationPresenterWin::SaveIconToFilesystem( return base::UTF8ToUTF16(origin.spec()); } -Notification* NotificationPresenterWin::CreateNotificationObject(NotificationDelegate* delegate) { +Notification* NotificationPresenterWin::CreateNotificationObject( + NotificationDelegate* delegate) { return new WindowsToastNotification(delegate, this); } diff --git a/brightray/browser/win/notification_presenter_win.h b/brightray/browser/win/notification_presenter_win.h index 1e1186c500b..679490c1852 100644 --- a/brightray/browser/win/notification_presenter_win.h +++ b/brightray/browser/win/notification_presenter_win.h @@ -42,7 +42,8 @@ class NotificationPresenterWin : public NotificationPresenter { base::string16 SaveIconToFilesystem(const SkBitmap& icon, const GURL& origin); private: - Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + Notification* CreateNotificationObject( + NotificationDelegate* delegate) override; base::ScopedTempDir temp_dir_; diff --git a/brightray/browser/win/notification_presenter_win7.cc b/brightray/browser/win/notification_presenter_win7.cc index 0203e2e85d3..9e80b5c1c1e 100644 --- a/brightray/browser/win/notification_presenter_win7.cc +++ b/brightray/browser/win/notification_presenter_win7.cc @@ -3,12 +3,14 @@ namespace brightray { -brightray::Notification* NotificationPresenterWin7::CreateNotificationObject(NotificationDelegate* delegate) +brightray::Notification* NotificationPresenterWin7::CreateNotificationObject( + NotificationDelegate* delegate) { return new Win32Notification(delegate, this); } -Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef(const DesktopNotificationController::Notification& ref) +Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef( + const DesktopNotificationController::Notification& ref) { for(auto n : this->notifications()) { @@ -20,7 +22,8 @@ Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef(const D return nullptr; } -Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag(const std::string& tag) +Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag( + const std::string& tag) { for(auto n : this->notifications()) { @@ -32,13 +35,15 @@ Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag(const s return nullptr; } -void NotificationPresenterWin7::OnNotificationClicked(Notification& notification) +void NotificationPresenterWin7::OnNotificationClicked( + Notification& notification) { auto n = GetNotificationObjectByRef(notification); if(n) n->NotificationClicked(); } -void NotificationPresenterWin7::OnNotificationDismissed(Notification& notification) +void NotificationPresenterWin7::OnNotificationDismissed( + Notification& notification) { auto n = GetNotificationObjectByRef(notification); if(n) n->NotificationDismissed(); diff --git a/brightray/browser/win/notification_presenter_win7.h b/brightray/browser/win/notification_presenter_win7.h index cebef5438ff..bdc5afdb2af 100644 --- a/brightray/browser/win/notification_presenter_win7.h +++ b/brightray/browser/win/notification_presenter_win7.h @@ -13,13 +13,16 @@ class NotificationPresenterWin7 : public: NotificationPresenterWin7() = default; - Win32Notification* GetNotificationObjectByRef(const DesktopNotificationController::Notification& ref); + Win32Notification* GetNotificationObjectByRef( + const DesktopNotificationController::Notification& ref); + Win32Notification* GetNotificationObjectByTag(const std::string& tag); private: DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin7); - brightray::Notification* CreateNotificationObject(NotificationDelegate* delegate) override; + brightray::Notification* CreateNotificationObject( + NotificationDelegate* delegate) override; void OnNotificationClicked(Notification& notification) override; void OnNotificationDismissed(Notification& notification) override; diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h index 9df81bb1194..d6611bf4205 100644 --- a/brightray/browser/win/win32_desktop_notifications/common.h +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -36,7 +36,9 @@ struct ScreenMetrics ScreenMetrics() { typedef HRESULT WINAPI GetDpiForMonitor_t(HMONITOR, int, UINT*, UINT*); - auto GetDpiForMonitor = (GetDpiForMonitor_t*)GetProcAddress(GetModuleHandle(TEXT("shcore")), "GetDpiForMonitor"); + auto GetDpiForMonitor = + (GetDpiForMonitor_t*)GetProcAddress(GetModuleHandle(TEXT("shcore")), + "GetDpiForMonitor"); if(GetDpiForMonitor) { auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY); @@ -50,8 +52,8 @@ struct ScreenMetrics ReleaseDC(NULL, hdc); } - template T X(T value) const { return ScaleForDpi(value, dpi_x); } - template T Y(T value) const { return ScaleForDpi(value, dpi_y); } + template T X(T value) const { return ScaleForDpi(value, dpi_x); } + template T Y(T value) const { return ScaleForDpi(value, dpi_y); } }; } diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index 262d19ceb18..f6ea2e8071e 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -28,7 +28,8 @@ HBITMAP CopyBitmap(HBITMAP bitmap) HDC hdcDst = CreateCompatibleDC(NULL); SelectBitmap(hdcSrc, bitmap); SelectBitmap(hdcDst, ret); - BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY); + BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, + hdcSrc, 0, 0, SRCCOPY); DeleteDC(hdcDst); DeleteDC(hdcSrc); } @@ -40,14 +41,19 @@ HBITMAP CopyBitmap(HBITMAP bitmap) HINSTANCE DesktopNotificationController::RegisterWndClasses() { // We keep a static `module` variable which serves a dual purpose: - // 1. Stores the HINSTANCE where the window classes are registered, which can be passed to `CreateWindow` - // 2. Indicates whether we already attempted the registration so that we don't do it twice (we don't retry - // even if registration fails, as there is no point. + // 1. Stores the HINSTANCE where the window classes are registered, + // which can be passed to `CreateWindow` + // 2. Indicates whether we already attempted the registration so that + // we don't do it twice (we don't retry even if registration fails, + // as there is no point). static HMODULE module = NULL; if(!module) { - if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast(&RegisterWndClasses), &module)) + if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(&RegisterWndClasses), + &module)) { Toast::Register(module); @@ -64,7 +70,8 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() return module; } -DesktopNotificationController::DesktopNotificationController(unsigned maximumToasts) +DesktopNotificationController::DesktopNotificationController( + unsigned maximumToasts) { instances_.reserve(maximumToasts); } @@ -76,7 +83,8 @@ DesktopNotificationController::~DesktopNotificationController() ClearAssets(); } -LRESULT CALLBACK DesktopNotificationController::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT CALLBACK DesktopNotificationController::WndProc( + HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { @@ -119,8 +127,9 @@ void DesktopNotificationController::StartAnimation() if(!is_animating_ && hwnd_controller_) { - // NOTE: 15ms is shorter than what we'd need for 60 fps, but since the timer - // is not accurate we must request a higher frame rate to get at least 60 + // NOTE: 15ms is shorter than what we'd need for 60 fps, but since + // the timer is not accurate we must request a higher frame rate + // to get at least 60 SetTimer(hwnd_controller_, TimerID_Animate, 15, nullptr); is_animating_ = true; @@ -152,11 +161,13 @@ void DesktopNotificationController::InitializeFonts() auto dpiY = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(NULL, hdc); - metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.1f, dpiY); + metrics.lfMessageFont.lfHeight = + (LONG)ScaleForDpi(baseHeight * 1.1f, dpiY); body_font_ = CreateFontIndirect(&metrics.lfMessageFont); if(caption_font_) DeleteFont(caption_font_); - metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.4f, dpiY); + metrics.lfMessageFont.lfHeight = + (LONG)ScaleForDpi(baseHeight * 1.4f, dpiY); caption_font_ = CreateFontIndirect(&metrics.lfMessageFont); } } @@ -266,7 +277,9 @@ void DesktopNotificationController::AnimateAll() CheckQueue(); } -DesktopNotificationController::Notification DesktopNotificationController::AddNotification(std::wstring caption, std::wstring bodyText, HBITMAP image) +DesktopNotificationController::Notification + DesktopNotificationController::AddNotification( + std::wstring caption, std::wstring bodyText, HBITMAP image) { NotificationLink data(this); @@ -280,7 +293,8 @@ DesktopNotificationController::Notification DesktopNotificationController::AddNo return ret; } -void DesktopNotificationController::CloseNotification(Notification& notification) +void DesktopNotificationController::CloseNotification( + Notification& notification) { // Remove it from the queue auto it = find(queue_.begin(), queue_.end(), notification.data_); @@ -323,15 +337,20 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) ScreenMetrics scr; auto toast = Toast::Get(item.hwnd); - toastPos = toast->GetVerticalPosition() + toast->GetHeight() + scr.Y(toast_margin_); + toastPos = toast->GetVerticalPosition() + + toast->GetHeight() + + scr.Y(toast_margin_); } instances_.push_back({ hwnd, move(data) }); if(!hwnd_controller_) { - // NOTE: We cannot use a message-only window because we need to receive system notifications - hwnd_controller_ = CreateWindow(class_name_, nullptr, 0, 0, 0, 0, 0, NULL, NULL, hInstance, this); + // NOTE: We cannot use a message-only window because we need to + // receive system notifications + hwnd_controller_ = CreateWindow(class_name_, nullptr, 0, + 0, 0, 0, 0, + NULL, NULL, hInstance, this); } auto toast = Toast::Get(hwnd); @@ -339,12 +358,14 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) } } -HWND DesktopNotificationController::GetToast(const NotificationData* data) const +HWND DesktopNotificationController::GetToast( + const NotificationData* data) const { - auto it = find_if(instances_.cbegin(), instances_.cend(), [data](auto&& inst) { - auto toast = Toast::Get(inst.hwnd); - return data == toast->GetNotification().get(); - }); + auto it = find_if(instances_.cbegin(), instances_.cend(), + [data](auto&& inst) { + auto toast = Toast::Get(inst.hwnd); + return data == toast->GetNotification().get(); + }); return (it != instances_.cend()) ? it->hwnd : NULL; } @@ -364,13 +385,15 @@ void DesktopNotificationController::DestroyToast(ToastInstance& inst) } -DesktopNotificationController::Notification::Notification(const shared_ptr& data) : +DesktopNotificationController::Notification::Notification( + const shared_ptr& data) : data_(data) { _ASSERT(data != nullptr); } -bool DesktopNotificationController::Notification::operator==(const Notification& other) const +bool DesktopNotificationController::Notification::operator==( + const Notification& other) const { return data_ == other.data_; } @@ -384,7 +407,8 @@ void DesktopNotificationController::Notification::Close() data_->controller->CloseNotification(*this); } -void DesktopNotificationController::Notification::Set(std::wstring caption, std::wstring bodyText, HBITMAP image) +void DesktopNotificationController::Notification::Set( + std::wstring caption, std::wstring bodyText, HBITMAP image) { // No business calling this when not pointing to a valid instance _ASSERT(data_); @@ -411,7 +435,8 @@ void DesktopNotificationController::Notification::Set(std::wstring caption, std: } -DesktopNotificationController::NotificationLink::NotificationLink(DesktopNotificationController* controller) : +DesktopNotificationController::NotificationLink::NotificationLink( + DesktopNotificationController* controller) : shared_ptr(make_shared()) { get()->controller = controller; diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index 8b5b160b0e8..62a85c9c9dd 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -16,7 +16,8 @@ public: ~DesktopNotificationController(); class Notification; - Notification AddNotification(std::wstring caption, std::wstring bodyText, HBITMAP image); + Notification AddNotification(std::wstring caption, std::wstring bodyText, + HBITMAP image); void CloseNotification(Notification& notification); // Event handlers -- override to receive the events @@ -58,13 +59,16 @@ private: class Toast; - static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, + WPARAM wParam, LPARAM lParam); static DesktopNotificationController* Get(HWND hWnd) { - return reinterpret_cast(GetWindowLongPtr(hWnd, 0)); + return reinterpret_cast( + GetWindowLongPtr(hWnd, 0)); } - DesktopNotificationController(const DesktopNotificationController&) = delete; + DesktopNotificationController( + const DesktopNotificationController&) = delete; void InitializeFonts(); void ClearAssets(); @@ -75,7 +79,9 @@ private: void DestroyToast(ToastInstance& inst); private: - static constexpr const TCHAR class_name_[] = TEXT("DesktopNotificationController"); + static constexpr const TCHAR class_name_[] = + TEXT("DesktopNotificationController"); + HWND hwnd_controller_ = NULL; HFONT caption_font_ = NULL, body_font_ = NULL; std::vector instances_; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 4f90c37d97f..be2b04502bd 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -18,20 +18,34 @@ static COLORREF GetAccentColor() if(IsAppThemed()) { HKEY hkey; - if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\DWM"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) + if(RegOpenKeyEx(HKEY_CURRENT_USER, + TEXT("SOFTWARE\\Microsoft\\Windows\\DWM"), 0, + KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { COLORREF color; DWORD type, size; - if(RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, &type, (BYTE*)&color, &(size = sizeof(color))) == ERROR_SUCCESS && type == REG_DWORD) + if(RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, + &type, + (BYTE*)&color, + &(size = sizeof(color))) == ERROR_SUCCESS && + type == REG_DWORD) { // convert from RGBA - color = RGB(GetRValue(color), GetGValue(color), GetBValue(color)); + color = RGB(GetRValue(color), + GetGValue(color), + GetBValue(color)); success = true; } - else if(RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, &type, (BYTE*)&color, &(size = sizeof(color))) == ERROR_SUCCESS && type == REG_DWORD) + else if(RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, + &type, + (BYTE*)&color, + &(size = sizeof(color))) == ERROR_SUCCESS && + type == REG_DWORD) { // convert from BGRA - color = RGB(GetBValue(color), GetGValue(color), GetRValue(color)); + color = RGB(GetBValue(color), + GetGValue(color), + GetRValue(color)); success = true; } @@ -48,8 +62,8 @@ static COLORREF GetAccentColor() static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { // We use StretchBlt for the scaling, but that discards the alpha channel. - // Therefore we first create a separate grayscale bitmap from the alpha channel, - // scale that separately and copy it back to the scaled color bitmap. + // So we first create a separate grayscale bitmap from the alpha channel, + // scale that separately, and copy it back to the scaled color bitmap. BITMAP bm; if(!GetObject(bitmap, sizeof(bm), &bm)) @@ -72,21 +86,27 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) bmi.biCompression = BI_RGB; void* alphaSrcBits; - alphaSrcBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, &alphaSrcBits, NULL, 0); + alphaSrcBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, + DIB_RGB_COLORS, &alphaSrcBits, + NULL, 0); if(alphaSrcBitmap) { - if(GetDIBits(hdcScreen, bitmap, 0, 0, 0, (BITMAPINFO*)&bmi, DIB_RGB_COLORS) && + if(GetDIBits(hdcScreen, bitmap, 0, 0, 0, + (BITMAPINFO*)&bmi, DIB_RGB_COLORS) && bmi.biSizeImage > 0 && (bmi.biSizeImage % 4) == 0) { - auto buf = (DWORD*)_aligned_malloc(bmi.biSizeImage, sizeof(DWORD)); + auto buf = (BYTE*)_aligned_malloc(bmi.biSizeImage, + sizeof(DWORD)); if(buf) { - GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, + (BITMAPINFO*)&bmi, DIB_RGB_COLORS); BYTE* dest = (BYTE*)alphaSrcBits; - for(const DWORD *src = buf, *end = (DWORD*)((BYTE*)buf + bmi.biSizeImage); + for(const DWORD *src = (DWORD*)buf, + *end = (DWORD*)(buf + bmi.biSizeImage); src != end; ++src, ++dest) { @@ -112,10 +132,14 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) bmi.biCompression = BI_RGB; void* colorBits; - auto colorBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, &colorBits, NULL, 0); + auto colorBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, + DIB_RGB_COLORS, &colorBits, + NULL, 0); void* alphaBits; - auto alphaBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, &alphaBits, NULL, 0); + auto alphaBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, + DIB_RGB_COLORS, &alphaBits, + NULL, 0); HDC hdc = CreateCompatibleDC(NULL); HDC hdcSrc = CreateCompatibleDC(NULL); @@ -127,12 +151,16 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) // resize color channels SelectObject(hdc, colorBitmap); SelectObject(hdcSrc, bitmap); - StretchBlt(hdc, 0, 0, width, height, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); + StretchBlt(hdc, 0, 0, width, height, + hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, + SRCCOPY); // resize alpha channel SelectObject(hdc, alphaBitmap); SelectObject(hdcSrc, alphaSrcBitmap); - StretchBlt(hdc, 0, 0, width, height, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); + StretchBlt(hdc, 0, 0, width, height, + hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, + SRCCOPY); // flush before touching the bits GdiFlush(); @@ -149,7 +177,9 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) } // create the resulting bitmap - resultBitmap = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, colorBits, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + resultBitmap = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, + colorBits, (BITMAPINFO*)&bmi, + DIB_RGB_COLORS); } if(hdcSrc) DeleteDC(hdcSrc); @@ -166,7 +196,8 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) return resultBitmap; } -DesktopNotificationController::Toast::Toast(HWND hWnd, shared_ptr* data) : +DesktopNotificationController::Toast::Toast( + HWND hWnd, shared_ptr* data) : hwnd_(hWnd), data_(*data) { HDC hdcScreen = GetDC(NULL); @@ -193,14 +224,17 @@ void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) RegisterClassEx(&wc); } -LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +LRESULT DesktopNotificationController::Toast::WndProc( + HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: { auto& cs = reinterpret_cast(lParam); - auto inst = new Toast(hWnd, static_cast*>(cs->lpCreateParams)); + auto data = + static_cast*>(cs->lpCreateParams); + auto inst = new Toast(hWnd, data); SetWindowLongPtr(hWnd, 0, (LONG_PTR)inst); } break; @@ -246,7 +280,8 @@ LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, W } POINT cursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; - inst->is_close_hot_ = (PtInRect(&inst->close_button_rect_, cursor) != FALSE); + inst->is_close_hot_ = + (PtInRect(&inst->close_button_rect_, cursor) != FALSE); if(!inst->is_non_interactive_) inst->CancelDismiss(); @@ -285,9 +320,12 @@ LRESULT DesktopNotificationController::Toast::WndProc(HWND hWnd, UINT message, W return DefWindowProc(hWnd, message, wParam, lParam); } -HWND DesktopNotificationController::Toast::Create(HINSTANCE hInstance, shared_ptr& data) +HWND DesktopNotificationController::Toast::Create( + HINSTANCE hInstance, shared_ptr& data) { - return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, class_name_, nullptr, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, &data); + return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, + class_name_, nullptr, WS_POPUP, 0, 0, 0, 0, + NULL, NULL, hInstance, &data); } void DesktopNotificationController::Toast::Draw() @@ -319,9 +357,9 @@ void DesktopNotificationController::Toast::Draw() COLORREF foreColor, dimmedColor; { - // based on the lightness of background, we draw foreground in light or dark - // shades of gray blended onto the background with slight transparency - // to avoid sharp contrast + // based on the lightness of background, we draw foreground in light + // or dark shades of gray blended onto the background with slight + // transparency to avoid sharp contrast constexpr float alpha = 0.9f; constexpr float intensityLight[] = { (1.0f * alpha), (0.8f * alpha) }; @@ -395,7 +433,11 @@ void DesktopNotificationController::Toast::Draw() HDC hdcImage = CreateCompatibleDC(NULL); SelectBitmap(hdcImage, scaled_image_); BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; - AlphaBlend(hdc_, margin_.cx, margin_.cy, imageInfo.bmWidth, imageInfo.bmHeight, hdcImage, 0, 0, imageInfo.bmWidth, imageInfo.bmHeight, blend); + AlphaBlend(hdc_, margin_.cx, margin_.cy, + imageInfo.bmWidth, imageInfo.bmHeight, + hdcImage, 0, 0, + imageInfo.bmWidth, imageInfo.bmHeight, + blend); DeleteDC(hdcImage); } @@ -410,7 +452,8 @@ void DesktopNotificationController::Toast::Draw() SelectFont(hdc_, captionFont); SetTextColor(hdc_, foreColor); - DrawText(hdc_, data_->caption.data(), (UINT)data_->caption.length(), &rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX); + DrawText(hdc_, data_->caption.data(), (UINT)data_->caption.length(), + &rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX); } // body text @@ -425,14 +468,18 @@ void DesktopNotificationController::Toast::Draw() SelectFont(hdc_, bodyFont); SetTextColor(hdc_, dimmedColor); - DrawText(hdc_, data_->body_text.data(), (UINT)data_->body_text.length(), &rc, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | DT_END_ELLIPSIS | DT_EDITCONTROL); + DrawText(hdc_, data_->body_text.data(), (UINT)data_->body_text.length(), + &rc, + DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | + DT_END_ELLIPSIS | DT_EDITCONTROL); } // close button { SelectFont(hdc_, captionFont); SetTextColor(hdc_, is_close_hot_ ? foreColor : dimmedColor); - ExtTextOut(hdc_, closePos.x, closePos.y, 0, nullptr, &close, 1, nullptr); + ExtTextOut(hdc_, closePos.x, closePos.y, 0, nullptr, + &close, 1, nullptr); } is_content_updated_ = true; @@ -510,18 +557,22 @@ void DesktopNotificationController::Toast::UpdateBufferSize() newSize.cx += imageDrawSize.cx + margin_.cx; - auto heightWithImage = margin_.cy + (imageDrawSize.cy) + margin_.cy; - if(newSize.cy < heightWithImage) newSize.cy = heightWithImage; + auto heightWithImage = + margin_.cy + (imageDrawSize.cy) + margin_.cy; + if(newSize.cy < heightWithImage) + newSize.cy = heightWithImage; UpdateScaledImage(imageDrawSize); } } } - if(newSize.cx != this->toast_size_.cx || newSize.cy != this->toast_size_.cy) + if(newSize.cx != this->toast_size_.cx || + newSize.cy != this->toast_size_.cy) { HDC hdcScreen = GetDC(NULL); - auto newBitmap = CreateCompatibleBitmap(hdcScreen, newSize.cx, newSize.cy); + auto newBitmap = CreateCompatibleBitmap(hdcScreen, + newSize.cx, newSize.cy); ReleaseDC(NULL, hdcScreen); if(newBitmap) @@ -529,8 +580,16 @@ void DesktopNotificationController::Toast::UpdateBufferSize() if(SelectBitmap(hdc_, newBitmap)) { RECT dirty1 = {}, dirty2 = {}; - if(toast_size_.cx < newSize.cx) dirty1 = { toast_size_.cx, 0, newSize.cx, toast_size_.cy }; - if(toast_size_.cy < newSize.cy) dirty2 = { 0, toast_size_.cy, newSize.cx, newSize.cy }; + if(toast_size_.cx < newSize.cx) + { + dirty1 = { toast_size_.cx, 0, + newSize.cx, toast_size_.cy }; + } + if(toast_size_.cy < newSize.cy) + { + dirty2 = { 0, toast_size_.cy, + newSize.cx, newSize.cy }; + } if(this->bitmap_) DeleteBitmap(this->bitmap_); this->bitmap_ = newBitmap; @@ -538,8 +597,9 @@ void DesktopNotificationController::Toast::UpdateBufferSize() Invalidate(); - // Resize also the DWM buffer to prevent flicker during window resizing. - // Make sure any existing data is not overwritten by marking the dirty region. + // Resize also the DWM buffer to prevent flicker during + // window resizing. Make sure any existing data is not + // overwritten by marking the dirty region. { POINT origin = { 0, 0 }; @@ -591,7 +651,8 @@ void DesktopNotificationController::Toast::UpdateContents() GetWindowRect(hwnd_, &rc); POINT origin = { 0, 0 }; SIZE size = { rc.right - rc.left, rc.bottom - rc.top }; - UpdateLayeredWindow(hwnd_, NULL, nullptr, &size, hdc_, &origin, 0, nullptr, 0); + UpdateLayeredWindow(hwnd_, NULL, nullptr, &size, + hdc_, &origin, 0, nullptr, 0); } } @@ -599,9 +660,9 @@ void DesktopNotificationController::Toast::Dismiss() { if(!is_non_interactive_) { - // Set a flag to prevent further interaction. We don't disable the HWND because - // we still want to receive mouse move messages in order to keep the toast under - // the cursor and not collapse it while dismissing. + // Set a flag to prevent further interaction. We don't disable the HWND + // because we still want to receive mouse move messages in order to keep + // the toast under the cursor and not collapse it while dismissing. is_non_interactive_ = true; AutoDismiss(); @@ -650,7 +711,8 @@ void DesktopNotificationController::Toast::SetVerticalPosition(int y) return; // Make sure the new animation's origin is at the current position - vertical_pos_ += (int)((vertical_pos_target_ - vertical_pos_) * stack_collapse_pos_); + vertical_pos_ += + (int)((vertical_pos_target_ - vertical_pos_) * stack_collapse_pos_); // Set new target position and start the animation vertical_pos_target_ = y; @@ -658,7 +720,8 @@ void DesktopNotificationController::Toast::SetVerticalPosition(int y) data_->controller->StartAnimation(); } -HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origin) +HDWP DesktopNotificationController::Toast::Animate( + HDWP hdwp, const POINT& origin) { UpdateBufferSize(); @@ -682,7 +745,8 @@ HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origi POINT pt = { 0, 0 }; SIZE size = { 0, 0 }; BLENDFUNCTION blend; - UINT dwpFlags = SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOREDRAW | SWP_NOCOPYBITS; + UINT dwpFlags = SWP_NOACTIVATE | SWP_SHOWWINDOW | + SWP_NOREDRAW | SWP_NOCOPYBITS; auto easeInPos = AnimateEaseIn(); auto easeOutPos = AnimateEaseOut(); @@ -735,11 +799,13 @@ HDWP DesktopNotificationController::Toast::Animate(HDWP hdwp, const POINT& origi } // `UpdateLayeredWindowIndirect` updates position, size, and transparency. - // `DeferWindowPos` updates z-order, and also position and size in case ULWI fails, - // which can happen when one of the dimensions is zero (e.g. at the beginning of ease-in). + // `DeferWindowPos` updates z-order, and also position and size in case + // ULWI fails, which can happen when one of the dimensions is zero (e.g. + // at the beginning of ease-in). auto ulwResult = UpdateLayeredWindowIndirect(hwnd_, &ulw); - hdwp = DeferWindowPos(hdwp, hwnd_, HWND_TOPMOST, pt.x, pt.y, size.cx, size.cy, dwpFlags); + hdwp = DeferWindowPos(hdwp, hwnd_, HWND_TOPMOST, + pt.x, pt.y, size.cx, size.cy, dwpFlags); return hdwp; } @@ -770,7 +836,8 @@ float DesktopNotificationController::Toast::AnimateEaseIn() return ease_in_pos_; constexpr float duration = 500.0f; - float time = std::min(duration, (float)(GetTickCount() - ease_in_start_)) / duration; + float elapsed = GetTickCount() - ease_in_start_; + float time = std::min(duration, elapsed) / duration; // decelerating exponential ease const float a = -8.0f; @@ -785,7 +852,8 @@ float DesktopNotificationController::Toast::AnimateEaseOut() return ease_out_pos_; constexpr float duration = 120.0f; - float time = std::min(duration, (float)(GetTickCount() - ease_out_start_)) / duration; + float elapsed = GetTickCount() - ease_out_start_; + float time = std::min(duration, elapsed) / duration; // accelerating circle ease auto pos = 1.0f - std::sqrt(1 - time * time); @@ -799,7 +867,8 @@ float DesktopNotificationController::Toast::AnimateStackCollapse() return stack_collapse_pos_; constexpr float duration = 500.0f; - float time = std::min(duration, (float)(GetTickCount() - stack_collapse_start_)) / duration; + float elapsed = GetTickCount() - stack_collapse_start_; + float time = std::min(duration, elapsed) / duration; // decelerating exponential ease const float a = -8.0f; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index 64ea7d1e99b..6505b5e01af 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -7,13 +7,15 @@ class DesktopNotificationController::Toast { public: static void Register(HINSTANCE hInstance); - static HWND Create(HINSTANCE hInstance, std::shared_ptr& data); + static HWND Create(HINSTANCE hInstance, + std::shared_ptr& data); static Toast* Get(HWND hWnd) { return reinterpret_cast(GetWindowLongPtr(hWnd, 0)); } - static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, + WPARAM wParam, LPARAM lParam); const std::shared_ptr& GetNotification() const { @@ -73,7 +75,8 @@ private: float AnimateStackCollapse(); private: - static constexpr const TCHAR class_name_[] = TEXT("DesktopNotificationToast"); + static constexpr const TCHAR class_name_[] = + TEXT("DesktopNotificationToast"); const HWND hwnd_; HDC hdc_; @@ -91,7 +94,9 @@ private: bool is_non_interactive_ = false; bool ease_in_active_ = false; bool ease_out_active_ = false; - bool is_content_updated_ = false, is_highlighted_ = false, is_close_hot_ = false; + bool is_content_updated_ = false; + bool is_highlighted_ = false; + bool is_close_hot_ = false; DWORD ease_in_start_, ease_out_start_, stack_collapse_start_; float ease_in_pos_ = 0, ease_out_pos_ = 0, stack_collapse_pos_ = 0; }; diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index 86b42cf768f..c9e29b127b2 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -5,7 +5,10 @@ namespace brightray { -void Win32Notification::Show(const base::string16& title, const base::string16& msg, const std::string& tag, const GURL& icon_url, const SkBitmap& icon, const bool silent) +void Win32Notification::Show( + const base::string16& title, const base::string16& msg, + const std::string& tag, const GURL& icon_url, + const SkBitmap& icon, const bool silent) { auto presenter = static_cast(this->presenter()); if(!presenter) return; @@ -26,7 +29,8 @@ void Win32Notification::Show(const base::string16& title, const base::string16& bmi.biCompression = BI_RGB; HDC hdcScreen = GetDC(NULL); - image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, icon.getPixels(), (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, icon.getPixels(), + (BITMAPINFO*)&bmi, DIB_RGB_COLORS); ReleaseDC(NULL, hdcScreen); icon.unlockPixels(); diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index 79919354332..b1fe7e764b1 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -7,8 +7,14 @@ namespace brightray { class Win32Notification : public brightray::Notification { public: - Win32Notification(NotificationDelegate* delegate, NotificationPresenterWin7* presenter) : Notification(delegate, presenter) {} - void Show(const base::string16& title, const base::string16& msg, const std::string& tag, const GURL& icon_url, const SkBitmap& icon, const bool silent) override; + Win32Notification(NotificationDelegate* delegate, + NotificationPresenterWin7* presenter) : + Notification(delegate, presenter) + { + } + void Show(const base::string16& title, const base::string16& msg, + const std::string& tag, const GURL& icon_url, + const SkBitmap& icon, const bool silent) override; void Dismiss() override; const DesktopNotificationController::Notification& GetRef() const From 33b4936a680ae9c92d45fb0a51da76e003c7c4d8 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Mon, 3 Apr 2017 10:42:26 +0200 Subject: [PATCH 06/20] Address cpplint issue "Include the directory when naming .h files [build/include] [4]" --- brightray/browser/win/notification_presenter_win7.cc | 4 ++-- .../desktop_notification_controller.cc | 6 +++--- brightray/browser/win/win32_desktop_notifications/toast.cc | 4 ++-- brightray/browser/win/win32_desktop_notifications/toast.h | 2 +- brightray/browser/win/win32_notification.cc | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win7.cc b/brightray/browser/win/notification_presenter_win7.cc index 9e80b5c1c1e..3ce59901069 100644 --- a/brightray/browser/win/notification_presenter_win7.cc +++ b/brightray/browser/win/notification_presenter_win7.cc @@ -1,5 +1,5 @@ -#include "notification_presenter_win7.h" -#include "win32_notification.h" +#include "browser/win/notification_presenter_win7.h" +#include "browser/win/win32_notification.h" namespace brightray { diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index f6ea2e8071e..1559e73ea47 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -1,8 +1,8 @@ #define NOMINMAX #define WIN32_LEAN_AND_MEAN -#include "desktop_notification_controller.h" -#include "common.h" -#include "toast.h" +#include "browser/win/win32_desktop_notifications/desktop_notification_controller.h" +#include "browser/win/win32_desktop_notifications/common.h" +#include "browser/win/win32_desktop_notifications/toast.h" #include #include #include diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index be2b04502bd..6c4614de7c6 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -1,6 +1,6 @@ #define NOMINMAX -#include "toast.h" -#include "common.h" +#include "browser/win/win32_desktop_notifications/toast.h" +#include "browser/win/win32_desktop_notifications/common.h" #include #include #include diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index 6505b5e01af..2e0a1032114 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -1,5 +1,5 @@ #pragma once -#include "desktop_notification_controller.h" +#include "browser/win/win32_desktop_notifications/desktop_notification_controller.h" namespace brightray { diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index c9e29b127b2..17382c449be 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -1,5 +1,5 @@ #define WIN32_LEAN_AND_MEAN -#include "win32_notification.h" +#include "browser/win/win32_notification.h" #include "third_party/skia/include/core/SkBitmap.h" #include From 20d0bda538f848261a23fc42fd39ac7464d1c881 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Mon, 3 Apr 2017 10:48:18 +0200 Subject: [PATCH 07/20] Address cpplint issue "Found C++ system header after other header. Should be: toast.h, c system, c++ system, other. [build/include_order] [4]" --- .../desktop_notification_controller.cc | 6 +++--- brightray/browser/win/win32_desktop_notifications/toast.cc | 4 ++-- brightray/browser/win/win32_notification.cc | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index 1559e73ea47..a56f6000e4a 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -1,11 +1,11 @@ #define NOMINMAX #define WIN32_LEAN_AND_MEAN #include "browser/win/win32_desktop_notifications/desktop_notification_controller.h" -#include "browser/win/win32_desktop_notifications/common.h" -#include "browser/win/win32_desktop_notifications/toast.h" +#include #include #include -#include +#include "browser/win/win32_desktop_notifications/common.h" +#include "browser/win/win32_desktop_notifications/toast.h" using namespace std; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 6c4614de7c6..62aacdb1d00 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -1,9 +1,9 @@ #define NOMINMAX #include "browser/win/win32_desktop_notifications/toast.h" -#include "browser/win/win32_desktop_notifications/common.h" -#include #include #include +#include +#include "browser/win/win32_desktop_notifications/common.h" #pragma comment(lib, "msimg32.lib") #pragma comment(lib, "uxtheme.lib") diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index 17382c449be..beb13754486 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -1,7 +1,7 @@ #define WIN32_LEAN_AND_MEAN #include "browser/win/win32_notification.h" -#include "third_party/skia/include/core/SkBitmap.h" #include +#include "third_party/skia/include/core/SkBitmap.h" namespace brightray { From 63eeb96e710d9786c6704b243097ee7ddb97afff Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Mon, 3 Apr 2017 11:23:36 +0200 Subject: [PATCH 08/20] Address cpplint issue "Do not use namespace using-directives. Use using-declarations instead. [build/namespaces] [5]" --- .../desktop_notification_controller.cc | 3 ++- brightray/browser/win/win32_desktop_notifications/toast.cc | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index a56f6000e4a..7213f4382c9 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -7,7 +7,8 @@ #include "browser/win/win32_desktop_notifications/common.h" #include "browser/win/win32_desktop_notifications/toast.h" -using namespace std; +using std::make_shared; +using std::shared_ptr; namespace brightray { diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 62aacdb1d00..e11eea5f4d6 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -8,7 +8,8 @@ #pragma comment(lib, "msimg32.lib") #pragma comment(lib, "uxtheme.lib") -using namespace std; +using std::min; +using std::shared_ptr; namespace brightray { From 29f333227638c36485faa2e891dabbac2f64e56c Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Mon, 3 Apr 2017 12:11:39 +0200 Subject: [PATCH 09/20] Address cpplint issue "{ should almost always be at the end of the previous line [whitespace/braces] [4]" --- .../win/notification_presenter_win7.cc | 21 +- .../browser/win/notification_presenter_win7.h | 3 +- .../win/win32_desktop_notifications/common.h | 18 +- .../desktop_notification_controller.cc | 153 +++++-------- .../desktop_notification_controller.h | 9 +- .../win/win32_desktop_notifications/toast.cc | 201 ++++++------------ .../win/win32_desktop_notifications/toast.h | 21 +- brightray/browser/win/win32_notification.cc | 18 +- brightray/browser/win/win32_notification.h | 12 +- 9 files changed, 152 insertions(+), 304 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win7.cc b/brightray/browser/win/notification_presenter_win7.cc index 3ce59901069..cdbdfc803d4 100644 --- a/brightray/browser/win/notification_presenter_win7.cc +++ b/brightray/browser/win/notification_presenter_win7.cc @@ -4,16 +4,13 @@ namespace brightray { brightray::Notification* NotificationPresenterWin7::CreateNotificationObject( - NotificationDelegate* delegate) -{ + NotificationDelegate* delegate) { return new Win32Notification(delegate, this); } Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef( - const DesktopNotificationController::Notification& ref) -{ - for(auto n : this->notifications()) - { + const DesktopNotificationController::Notification& ref) { + for(auto n : this->notifications()) { auto w32n = static_cast(n); if(w32n->GetRef() == ref) return w32n; @@ -23,10 +20,8 @@ Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef( } Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag( - const std::string& tag) -{ - for(auto n : this->notifications()) - { + const std::string& tag) { + for(auto n : this->notifications()) { auto w32n = static_cast(n); if(w32n->GetTag() == tag) return w32n; @@ -36,15 +31,13 @@ Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag( } void NotificationPresenterWin7::OnNotificationClicked( - Notification& notification) -{ + Notification& notification) { auto n = GetNotificationObjectByRef(notification); if(n) n->NotificationClicked(); } void NotificationPresenterWin7::OnNotificationDismissed( - Notification& notification) -{ + Notification& notification) { auto n = GetNotificationObjectByRef(notification); if(n) n->NotificationDismissed(); } diff --git a/brightray/browser/win/notification_presenter_win7.h b/brightray/browser/win/notification_presenter_win7.h index bdc5afdb2af..2b352aad8af 100644 --- a/brightray/browser/win/notification_presenter_win7.h +++ b/brightray/browser/win/notification_presenter_win7.h @@ -8,8 +8,7 @@ class Win32Notification; class NotificationPresenterWin7 : public NotificationPresenter, - public DesktopNotificationController -{ + public DesktopNotificationController { public: NotificationPresenterWin7() = default; diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h index d6611bf4205..f68ca396086 100644 --- a/brightray/browser/win/win32_desktop_notifications/common.h +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -3,8 +3,7 @@ namespace brightray { -struct NotificationData -{ +struct NotificationData { DesktopNotificationController* controller = nullptr; std::wstring caption; @@ -14,8 +13,7 @@ struct NotificationData NotificationData() = default; - ~NotificationData() - { + ~NotificationData() { if(image) DeleteObject(image); } @@ -24,23 +22,19 @@ struct NotificationData }; template -inline T ScaleForDpi(T value, unsigned dpi) -{ +inline T ScaleForDpi(T value, unsigned dpi) { return value * dpi / 96; } -struct ScreenMetrics -{ +struct ScreenMetrics { UINT dpi_x, dpi_y; - ScreenMetrics() - { + ScreenMetrics() { typedef HRESULT WINAPI GetDpiForMonitor_t(HMONITOR, int, UINT*, UINT*); auto GetDpiForMonitor = (GetDpiForMonitor_t*)GetProcAddress(GetModuleHandle(TEXT("shcore")), "GetDpiForMonitor"); - if(GetDpiForMonitor) - { + if(GetDpiForMonitor) { auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY); if(GetDpiForMonitor(monitor, 0, &dpi_x, &dpi_y) == S_OK) return; diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index 7213f4382c9..072be506534 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -12,19 +12,16 @@ using std::shared_ptr; namespace brightray { -HBITMAP CopyBitmap(HBITMAP bitmap) -{ +HBITMAP CopyBitmap(HBITMAP bitmap) { HBITMAP ret = NULL; BITMAP bm; - if(bitmap && GetObject(bitmap, sizeof(bm), &bm)) - { + if(bitmap && GetObject(bitmap, sizeof(bm), &bm)) { HDC hdcScreen = GetDC(NULL); ret = CreateCompatibleBitmap(hdcScreen, bm.bmWidth, bm.bmHeight); ReleaseDC(NULL, hdcScreen); - if(ret) - { + if(ret) { HDC hdcSrc = CreateCompatibleDC(NULL); HDC hdcDst = CreateCompatibleDC(NULL); SelectBitmap(hdcSrc, bitmap); @@ -39,8 +36,7 @@ HBITMAP CopyBitmap(HBITMAP bitmap) return ret; } -HINSTANCE DesktopNotificationController::RegisterWndClasses() -{ +HINSTANCE DesktopNotificationController::RegisterWndClasses() { // We keep a static `module` variable which serves a dual purpose: // 1. Stores the HINSTANCE where the window classes are registered, // which can be passed to `CreateWindow` @@ -49,13 +45,11 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() // as there is no point). static HMODULE module = NULL; - if(!module) - { + if(!module) { if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast(&RegisterWndClasses), - &module)) - { + &module)) { Toast::Register(module); WNDCLASSEX wc = { sizeof(wc) }; @@ -72,23 +66,19 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() } DesktopNotificationController::DesktopNotificationController( - unsigned maximumToasts) -{ + unsigned maximumToasts) { instances_.reserve(maximumToasts); } -DesktopNotificationController::~DesktopNotificationController() -{ +DesktopNotificationController::~DesktopNotificationController() { for(auto&& inst : instances_) DestroyToast(inst); if(hwnd_controller_) DestroyWindow(hwnd_controller_); ClearAssets(); } LRESULT CALLBACK DesktopNotificationController::WndProc( - HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - switch(message) - { + HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + switch(message) { case WM_CREATE: { auto& cs = reinterpret_cast(lParam); @@ -97,8 +87,7 @@ LRESULT CALLBACK DesktopNotificationController::WndProc( break; case WM_TIMER: - if(wParam == TimerID_Animate) - { + if(wParam == TimerID_Animate) { Get(hWnd)->AnimateAll(); } return 0; @@ -112,8 +101,7 @@ LRESULT CALLBACK DesktopNotificationController::WndProc( break; case WM_SETTINGCHANGE: - if(wParam == SPI_SETWORKAREA) - { + if(wParam == SPI_SETWORKAREA) { Get(hWnd)->AnimateAll(); } break; @@ -122,12 +110,10 @@ LRESULT CALLBACK DesktopNotificationController::WndProc( return DefWindowProc(hWnd, message, wParam, lParam); } -void DesktopNotificationController::StartAnimation() -{ +void DesktopNotificationController::StartAnimation() { _ASSERT(hwnd_controller_); - if(!is_animating_ && hwnd_controller_) - { + if(!is_animating_ && hwnd_controller_) { // NOTE: 15ms is shorter than what we'd need for 60 fps, but since // the timer is not accurate we must request a higher frame rate // to get at least 60 @@ -137,25 +123,20 @@ void DesktopNotificationController::StartAnimation() } } -HFONT DesktopNotificationController::GetCaptionFont() -{ +HFONT DesktopNotificationController::GetCaptionFont() { InitializeFonts(); return caption_font_; } -HFONT DesktopNotificationController::GetBodyFont() -{ +HFONT DesktopNotificationController::GetBodyFont() { InitializeFonts(); return body_font_; } -void DesktopNotificationController::InitializeFonts() -{ - if(!body_font_) - { +void DesktopNotificationController::InitializeFonts() { + if(!body_font_) { NONCLIENTMETRICS metrics = { sizeof(metrics) }; - if(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0)) - { + if(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0)) { auto baseHeight = metrics.lfMessageFont.lfHeight; HDC hdc = GetDC(NULL); @@ -174,32 +155,27 @@ void DesktopNotificationController::InitializeFonts() } } -void DesktopNotificationController::ClearAssets() -{ +void DesktopNotificationController::ClearAssets() { if(caption_font_) { DeleteFont(caption_font_); caption_font_ = NULL; } if(body_font_) { DeleteFont(body_font_); body_font_ = NULL; } } -void DesktopNotificationController::AnimateAll() -{ +void DesktopNotificationController::AnimateAll() { // NOTE: This function refreshes position and size of all toasts according // to all current conditions. Animation time is only one of the variables // influencing them. Screen resolution is another. bool keepAnimating = false; - if(!instances_.empty()) - { + if(!instances_.empty()) { RECT workArea; - if(SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) - { + if(SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) { ScreenMetrics metrics; POINT origin = { workArea.right, workArea.bottom - metrics.Y(toast_margin_) }; auto hdwp = BeginDeferWindowPos((int)instances_.size()); - for(auto&& inst : instances_) - { + for(auto&& inst : instances_) { if(!inst.hwnd) continue; auto notification = Toast::Get(inst.hwnd); @@ -211,8 +187,7 @@ void DesktopNotificationController::AnimateAll() } } - if(!keepAnimating) - { + if(!keepAnimating) { _ASSERT(hwnd_controller_); if(hwnd_controller_) KillTimer(hwnd_controller_, TimerID_Animate); is_animating_ = false; @@ -220,8 +195,7 @@ void DesktopNotificationController::AnimateAll() // Purge dismissed notifications and collapse the stack between // items which are highlighted - if(!instances_.empty()) - { + if(!instances_.empty()) { auto isAlive = [](ToastInstance& inst) { return inst.hwnd && IsWindowVisible(inst.hwnd); }; @@ -230,8 +204,7 @@ void DesktopNotificationController::AnimateAll() return inst.hwnd && Toast::Get(inst.hwnd)->IsHighlighted(); }; - for(auto it = instances_.begin();; ++it) - { + for(auto it = instances_.begin();; ++it) { // find next highlighted item auto it2 = find_if(it, instances_.end(), isHighlighted); @@ -241,8 +214,7 @@ void DesktopNotificationController::AnimateAll() // purge the dead items for_each(it, it2, [this](auto&& inst) { DestroyToast(inst); }); - if(it2 == instances_.end()) - { + if(it2 == instances_.end()) { instances_.erase(it, it2); break; } @@ -252,16 +224,13 @@ void DesktopNotificationController::AnimateAll() } // Set new toast positions - if(!instances_.empty()) - { + if(!instances_.empty()) { ScreenMetrics metrics; auto margin = metrics.Y(toast_margin_); int targetPos = 0; - for(auto&& inst : instances_) - { - if(inst.hwnd) - { + for(auto&& inst : instances_) { + if(inst.hwnd) { auto toast = Toast::Get(inst.hwnd); if(toast->IsHighlighted()) @@ -280,8 +249,7 @@ void DesktopNotificationController::AnimateAll() DesktopNotificationController::Notification DesktopNotificationController::AddNotification( - std::wstring caption, std::wstring bodyText, HBITMAP image) -{ + std::wstring caption, std::wstring bodyText, HBITMAP image) { NotificationLink data(this); data->caption = move(caption); @@ -295,12 +263,10 @@ DesktopNotificationController::Notification } void DesktopNotificationController::CloseNotification( - Notification& notification) -{ + Notification& notification) { // Remove it from the queue auto it = find(queue_.begin(), queue_.end(), notification.data_); - if(it != queue_.end()) - { + if(it != queue_.end()) { queue_.erase(it); this->OnNotificationClosed(notification); return; @@ -308,31 +274,25 @@ void DesktopNotificationController::CloseNotification( // Dismiss active toast auto hwnd = GetToast(notification.data_.get()); - if(hwnd) - { + if(hwnd) { auto toast = Toast::Get(hwnd); toast->Dismiss(); } } -void DesktopNotificationController::CheckQueue() -{ - while(instances_.size() < instances_.capacity() && !queue_.empty()) - { +void DesktopNotificationController::CheckQueue() { + while(instances_.size() < instances_.capacity() && !queue_.empty()) { CreateToast(move(queue_.front())); queue_.pop_front(); } } -void DesktopNotificationController::CreateToast(NotificationLink&& data) -{ +void DesktopNotificationController::CreateToast(NotificationLink&& data) { auto hInstance = RegisterWndClasses(); auto hwnd = Toast::Create(hInstance, data); - if(hwnd) - { + if(hwnd) { int toastPos = 0; - if(!instances_.empty()) - { + if(!instances_.empty()) { auto& item = instances_.back(); _ASSERT(item.hwnd); @@ -345,8 +305,7 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) instances_.push_back({ hwnd, move(data) }); - if(!hwnd_controller_) - { + if(!hwnd_controller_) { // NOTE: We cannot use a message-only window because we need to // receive system notifications hwnd_controller_ = CreateWindow(class_name_, nullptr, 0, @@ -360,8 +319,7 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) } HWND DesktopNotificationController::GetToast( - const NotificationData* data) const -{ + const NotificationData* data) const { auto it = find_if(instances_.cbegin(), instances_.cend(), [data](auto&& inst) { auto toast = Toast::Get(inst.hwnd); @@ -371,10 +329,8 @@ HWND DesktopNotificationController::GetToast( return (it != instances_.cend()) ? it->hwnd : NULL; } -void DesktopNotificationController::DestroyToast(ToastInstance& inst) -{ - if(inst.hwnd) - { +void DesktopNotificationController::DestroyToast(ToastInstance& inst) { + if(inst.hwnd) { auto data = Toast::Get(inst.hwnd)->GetNotification(); DestroyWindow(inst.hwnd); @@ -388,19 +344,16 @@ void DesktopNotificationController::DestroyToast(ToastInstance& inst) DesktopNotificationController::Notification::Notification( const shared_ptr& data) : - data_(data) -{ + data_(data) { _ASSERT(data != nullptr); } bool DesktopNotificationController::Notification::operator==( - const Notification& other) const -{ + const Notification& other) const { return data_ == other.data_; } -void DesktopNotificationController::Notification::Close() -{ +void DesktopNotificationController::Notification::Close() { // No business calling this when not pointing to a valid instance _ASSERT(data_); @@ -409,8 +362,7 @@ void DesktopNotificationController::Notification::Close() } void DesktopNotificationController::Notification::Set( - std::wstring caption, std::wstring bodyText, HBITMAP image) -{ + std::wstring caption, std::wstring bodyText, HBITMAP image) { // No business calling this when not pointing to a valid instance _ASSERT(data_); @@ -425,8 +377,7 @@ void DesktopNotificationController::Notification::Set( data_->image = CopyBitmap(image); auto hwnd = data_->controller->GetToast(data_.get()); - if(hwnd) - { + if(hwnd) { auto toast = Toast::Get(hwnd); toast->ResetContents(); } @@ -438,13 +389,11 @@ void DesktopNotificationController::Notification::Set( DesktopNotificationController::NotificationLink::NotificationLink( DesktopNotificationController* controller) : - shared_ptr(make_shared()) -{ + shared_ptr(make_shared()) { get()->controller = controller; } -DesktopNotificationController::NotificationLink::~NotificationLink() -{ +DesktopNotificationController::NotificationLink::~NotificationLink() { auto p = get(); if(p) p->controller = nullptr; } diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index 62a85c9c9dd..a15d6b1b354 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -9,8 +9,7 @@ namespace brightray { struct NotificationData; -class DesktopNotificationController -{ +class DesktopNotificationController { public: DesktopNotificationController(unsigned maximumToasts = 3); ~DesktopNotificationController(); @@ -61,8 +60,7 @@ private: static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); - static DesktopNotificationController* Get(HWND hWnd) - { + static DesktopNotificationController* Get(HWND hWnd) { return reinterpret_cast( GetWindowLongPtr(hWnd, 0)); } @@ -89,8 +87,7 @@ private: bool is_animating_ = false; }; -class DesktopNotificationController::Notification -{ +class DesktopNotificationController::Notification { public: Notification() = default; Notification(const std::shared_ptr& data); diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index e11eea5f4d6..4f71dad9370 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -13,24 +13,20 @@ using std::shared_ptr; namespace brightray { -static COLORREF GetAccentColor() -{ +static COLORREF GetAccentColor() { bool success = false; - if(IsAppThemed()) - { + if(IsAppThemed()) { HKEY hkey; if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\DWM"), 0, - KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) - { + KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { COLORREF color; DWORD type, size; if(RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, &type, (BYTE*)&color, &(size = sizeof(color))) == ERROR_SUCCESS && - type == REG_DWORD) - { + type == REG_DWORD) { // convert from RGBA color = RGB(GetRValue(color), GetGValue(color), @@ -41,8 +37,7 @@ static COLORREF GetAccentColor() &type, (BYTE*)&color, &(size = sizeof(color))) == ERROR_SUCCESS && - type == REG_DWORD) - { + type == REG_DWORD) { // convert from BGRA color = RGB(GetBValue(color), GetGValue(color), @@ -60,8 +55,7 @@ static COLORREF GetAccentColor() } // Stretches a bitmap to the specified size, preserves alpha channel -static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) -{ +static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { // We use StretchBlt for the scaling, but that discards the alpha channel. // So we first create a separate grayscale bitmap from the alpha channel, // scale that separately, and copy it back to the scaled color bitmap. @@ -91,17 +85,14 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) DIB_RGB_COLORS, &alphaSrcBits, NULL, 0); - if(alphaSrcBitmap) - { + if(alphaSrcBitmap) { if(GetDIBits(hdcScreen, bitmap, 0, 0, 0, (BITMAPINFO*)&bmi, DIB_RGB_COLORS) && bmi.biSizeImage > 0 && - (bmi.biSizeImage % 4) == 0) - { + (bmi.biSizeImage % 4) == 0) { auto buf = (BYTE*)_aligned_malloc(bmi.biSizeImage, sizeof(DWORD)); - if(buf) - { + if(buf) { GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); @@ -109,8 +100,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) for(const DWORD *src = (DWORD*)buf, *end = (DWORD*)(buf + bmi.biSizeImage); src != end; - ++src, ++dest) - { + ++src, ++dest) { BYTE a = *src >> 24; *dest++ = a; *dest++ = a; @@ -123,8 +113,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) } } - if(alphaSrcBitmap) - { + if(alphaSrcBitmap) { BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; bmi.biWidth = width; bmi.biHeight = height; @@ -145,8 +134,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) HDC hdc = CreateCompatibleDC(NULL); HDC hdcSrc = CreateCompatibleDC(NULL); - if(colorBitmap && alphaBitmap && hdc && hdcSrc) - { + if(colorBitmap && alphaBitmap && hdc && hdcSrc) { SetStretchBltMode(hdc, HALFTONE); // resize color channels @@ -170,8 +158,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) auto dest = (BYTE*)colorBits; auto src = (const BYTE*)alphaBits; auto end = src + (width * height * 4); - while(src != end) - { + while(src != end) { dest[3] = src[0]; dest += 4; src += 4; @@ -199,22 +186,19 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) DesktopNotificationController::Toast::Toast( HWND hWnd, shared_ptr* data) : - hwnd_(hWnd), data_(*data) -{ + hwnd_(hWnd), data_(*data) { HDC hdcScreen = GetDC(NULL); hdc_ = CreateCompatibleDC(hdcScreen); ReleaseDC(NULL, hdcScreen); } -DesktopNotificationController::Toast::~Toast() -{ +DesktopNotificationController::Toast::~Toast() { DeleteDC(hdc_); if(bitmap_) DeleteBitmap(bitmap_); if(scaled_image_) DeleteBitmap(scaled_image_); } -void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) -{ +void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) { WNDCLASSEX wc = { sizeof(wc) }; wc.lpfnWndProc = &Toast::WndProc; wc.lpszClassName = class_name_; @@ -226,10 +210,8 @@ void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) } LRESULT DesktopNotificationController::Toast::WndProc( - HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - switch(message) - { + HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + switch(message) { case WM_CREATE: { auto& cs = reinterpret_cast(lParam); @@ -249,8 +231,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( return MA_NOACTIVATE; case WM_TIMER: - if(wParam == TimerID_AutoDismiss) - { + if(wParam == TimerID_AutoDismiss) { Get(hWnd)->AutoDismiss(); } return 0; @@ -272,8 +253,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_MOUSEMOVE: { auto inst = Get(hWnd); - if(!inst->is_highlighted_) - { + if(!inst->is_highlighted_) { inst->is_highlighted_ = true; TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, hWnd }; @@ -309,8 +289,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_WINDOWPOSCHANGED: { auto& wp = reinterpret_cast(lParam); - if(wp->flags & SWP_HIDEWINDOW) - { + if(wp->flags & SWP_HIDEWINDOW) { if(!IsWindowVisible(hWnd)) Get(hWnd)->is_highlighted_ = false; } @@ -322,15 +301,13 @@ LRESULT DesktopNotificationController::Toast::WndProc( } HWND DesktopNotificationController::Toast::Create( - HINSTANCE hInstance, shared_ptr& data) -{ + HINSTANCE hInstance, shared_ptr& data) { return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, class_name_, nullptr, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInstance, &data); } -void DesktopNotificationController::Toast::Draw() -{ +void DesktopNotificationController::Toast::Draw() { const COLORREF accent = GetAccentColor(); COLORREF backColor; @@ -405,8 +382,7 @@ void DesktopNotificationController::Toast::Draw() auto textOffsetX = margin_.cx; BITMAP imageInfo = {}; - if(scaled_image_) - { + if(scaled_image_) { GetObject(scaled_image_, sizeof(imageInfo), &imageInfo); textOffsetX += margin_.cx + imageInfo.bmWidth; @@ -429,8 +405,7 @@ void DesktopNotificationController::Toast::Draw() } // image - if(scaled_image_) - { + if(scaled_image_) { HDC hdcImage = CreateCompatibleDC(NULL); SelectBitmap(hdcImage, scaled_image_); BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; @@ -458,8 +433,7 @@ void DesktopNotificationController::Toast::Draw() } // body text - if(!data_->body_text.empty()) - { + if(!data_->body_text.empty()) { RECT rc = { textOffsetX, 2 * margin_.cy + tmCap.tmAscent, @@ -486,34 +460,28 @@ void DesktopNotificationController::Toast::Draw() is_content_updated_ = true; } -void DesktopNotificationController::Toast::Invalidate() -{ +void DesktopNotificationController::Toast::Invalidate() { is_content_updated_ = false; } -bool DesktopNotificationController::Toast::IsRedrawNeeded() const -{ +bool DesktopNotificationController::Toast::IsRedrawNeeded() const { return !is_content_updated_; } -void DesktopNotificationController::Toast::UpdateBufferSize() -{ - if(hdc_) - { +void DesktopNotificationController::Toast::UpdateBufferSize() { + if(hdc_) { SIZE newSize; { TEXTMETRIC tmCap = {}; HFONT font = data_->controller->GetCaptionFont(); - if(font) - { + if(font) { SelectFont(hdc_, font); if(!GetTextMetrics(hdc_, &tmCap)) return; } TEXTMETRIC tmBody = {}; font = data_->controller->GetBodyFont(); - if(font) - { + if(font) { SelectFont(hdc_, font); if(!GetTextMetrics(hdc_, &tmBody)) return; } @@ -526,28 +494,22 @@ void DesktopNotificationController::Toast::UpdateBufferSize() if(!data_->body_text.empty()) newSize.cy += margin_.cy + (3 * tmBody.tmHeight); - if(data_->image) - { + if(data_->image) { BITMAP bm; - if(GetObject(data_->image, sizeof(bm), &bm)) - { + if(GetObject(data_->image, sizeof(bm), &bm)) { // cap the image size const int maxDimSize = 80; auto width = bm.bmWidth; auto height = bm.bmHeight; - if(width < height) - { - if(height > maxDimSize) - { + if(width < height) { + if(height > maxDimSize) { width = width * maxDimSize / height; height = maxDimSize; } } - else - { - if(width > maxDimSize) - { + else { + if(width > maxDimSize) { height = height * maxDimSize / width; width = maxDimSize; } @@ -569,25 +531,20 @@ void DesktopNotificationController::Toast::UpdateBufferSize() } if(newSize.cx != this->toast_size_.cx || - newSize.cy != this->toast_size_.cy) - { + newSize.cy != this->toast_size_.cy) { HDC hdcScreen = GetDC(NULL); auto newBitmap = CreateCompatibleBitmap(hdcScreen, newSize.cx, newSize.cy); ReleaseDC(NULL, hdcScreen); - if(newBitmap) - { - if(SelectBitmap(hdc_, newBitmap)) - { + if(newBitmap) { + if(SelectBitmap(hdc_, newBitmap)) { RECT dirty1 = {}, dirty2 = {}; - if(toast_size_.cx < newSize.cx) - { + if(toast_size_.cx < newSize.cx) { dirty1 = { toast_size_.cx, 0, newSize.cx, toast_size_.cy }; } - if(toast_size_.cy < newSize.cy) - { + if(toast_size_.cy < newSize.cy) { dirty2 = { 0, toast_size_.cy, newSize.cx, newSize.cy }; } @@ -630,24 +587,20 @@ void DesktopNotificationController::Toast::UpdateBufferSize() } } -void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) -{ +void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) { BITMAP bm; if(!GetObject(scaled_image_, sizeof(bm), &bm) || bm.bmWidth != size.cx || - bm.bmHeight != size.cy) - { + bm.bmHeight != size.cy) { if(scaled_image_) DeleteBitmap(scaled_image_); scaled_image_ = StretchBitmap(data_->image, size.cx, size.cy); } } -void DesktopNotificationController::Toast::UpdateContents() -{ +void DesktopNotificationController::Toast::UpdateContents() { Draw(); - if(IsWindowVisible(hwnd_)) - { + if(IsWindowVisible(hwnd_)) { RECT rc; GetWindowRect(hwnd_, &rc); POINT origin = { 0, 0 }; @@ -657,10 +610,8 @@ void DesktopNotificationController::Toast::UpdateContents() } } -void DesktopNotificationController::Toast::Dismiss() -{ - if(!is_non_interactive_) - { +void DesktopNotificationController::Toast::Dismiss() { + if(!is_non_interactive_) { // Set a flag to prevent further interaction. We don't disable the HWND // because we still want to receive mouse move messages in order to keep // the toast under the cursor and not collapse it while dismissing. @@ -670,28 +621,23 @@ void DesktopNotificationController::Toast::Dismiss() } } -void DesktopNotificationController::Toast::AutoDismiss() -{ +void DesktopNotificationController::Toast::AutoDismiss() { KillTimer(hwnd_, TimerID_AutoDismiss); StartEaseOut(); } -void DesktopNotificationController::Toast::CancelDismiss() -{ +void DesktopNotificationController::Toast::CancelDismiss() { KillTimer(hwnd_, TimerID_AutoDismiss); ease_out_active_ = false; ease_out_pos_ = 0; } -void DesktopNotificationController::Toast::ScheduleDismissal() -{ +void DesktopNotificationController::Toast::ScheduleDismissal() { SetTimer(hwnd_, TimerID_AutoDismiss, 4000, nullptr); } -void DesktopNotificationController::Toast::ResetContents() -{ - if(scaled_image_) - { +void DesktopNotificationController::Toast::ResetContents() { + if(scaled_image_) { DeleteBitmap(scaled_image_); scaled_image_ = NULL; } @@ -699,14 +645,12 @@ void DesktopNotificationController::Toast::ResetContents() Invalidate(); } -void DesktopNotificationController::Toast::PopUp(int y) -{ +void DesktopNotificationController::Toast::PopUp(int y) { vertical_pos_target_ = vertical_pos_ = y; StartEaseIn(); } -void DesktopNotificationController::Toast::SetVerticalPosition(int y) -{ +void DesktopNotificationController::Toast::SetVerticalPosition(int y) { // Don't restart animation if current target is the same if(y == vertical_pos_target_) return; @@ -722,8 +666,7 @@ void DesktopNotificationController::Toast::SetVerticalPosition(int y) } HDWP DesktopNotificationController::Toast::Animate( - HDWP hdwp, const POINT& origin) -{ + HDWP hdwp, const POINT& origin) { UpdateBufferSize(); if(IsRedrawNeeded()) @@ -764,8 +707,7 @@ HDWP DesktopNotificationController::Toast::Animate( ulw.pptDst = &pt; ulw.psize = &size; - if(ease_in_active_ && easeInPos == 1.0f) - { + if(ease_in_active_ && easeInPos == 1.0f) { ease_in_active_ = false; ScheduleDismissal(); } @@ -773,8 +715,7 @@ HDWP DesktopNotificationController::Toast::Animate( this->ease_in_pos_ = easeInPos; this->stack_collapse_pos_ = stackCollapsePos; - if(easeOutPos != this->ease_out_pos_) - { + if(easeOutPos != this->ease_out_pos_) { blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = (BYTE)(255 * (1.0f - easeOutPos)); @@ -785,8 +726,7 @@ HDWP DesktopNotificationController::Toast::Animate( this->ease_out_pos_ = easeOutPos; - if(easeOutPos == 1.0f) - { + if(easeOutPos == 1.0f) { ease_out_active_ = false; dwpFlags &= ~SWP_SHOWWINDOW; @@ -794,8 +734,7 @@ HDWP DesktopNotificationController::Toast::Animate( } } - if(stackCollapsePos == 1.0f) - { + if(stackCollapsePos == 1.0f) { vertical_pos_ = vertical_pos_target_; } @@ -810,29 +749,25 @@ HDWP DesktopNotificationController::Toast::Animate( return hdwp; } -void DesktopNotificationController::Toast::StartEaseIn() -{ +void DesktopNotificationController::Toast::StartEaseIn() { _ASSERT(!ease_in_active_); ease_in_start_ = GetTickCount(); ease_in_active_ = true; data_->controller->StartAnimation(); } -void DesktopNotificationController::Toast::StartEaseOut() -{ +void DesktopNotificationController::Toast::StartEaseOut() { _ASSERT(!ease_out_active_); ease_out_start_ = GetTickCount(); ease_out_active_ = true; data_->controller->StartAnimation(); } -bool DesktopNotificationController::Toast::IsStackCollapseActive() const -{ +bool DesktopNotificationController::Toast::IsStackCollapseActive() const { return (vertical_pos_ != vertical_pos_target_); } -float DesktopNotificationController::Toast::AnimateEaseIn() -{ +float DesktopNotificationController::Toast::AnimateEaseIn() { if(!ease_in_active_) return ease_in_pos_; @@ -847,8 +782,7 @@ float DesktopNotificationController::Toast::AnimateEaseIn() return pos; } -float DesktopNotificationController::Toast::AnimateEaseOut() -{ +float DesktopNotificationController::Toast::AnimateEaseOut() { if(!ease_out_active_) return ease_out_pos_; @@ -862,8 +796,7 @@ float DesktopNotificationController::Toast::AnimateEaseOut() return pos; } -float DesktopNotificationController::Toast::AnimateStackCollapse() -{ +float DesktopNotificationController::Toast::AnimateStackCollapse() { if(!IsStackCollapseActive()) return stack_collapse_pos_; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index 2e0a1032114..e77535f86c7 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -3,22 +3,19 @@ namespace brightray { -class DesktopNotificationController::Toast -{ +class DesktopNotificationController::Toast { public: static void Register(HINSTANCE hInstance); static HWND Create(HINSTANCE hInstance, std::shared_ptr& data); - static Toast* Get(HWND hWnd) - { + static Toast* Get(HWND hWnd) { return reinterpret_cast(GetWindowLongPtr(hWnd, 0)); } static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); - const std::shared_ptr& GetNotification() const - { + const std::shared_ptr& GetNotification() const { return data_; } @@ -28,21 +25,17 @@ public: void PopUp(int y); void SetVerticalPosition(int y); - int GetVerticalPosition() const - { + int GetVerticalPosition() const { return vertical_pos_target_; } - int GetHeight() const - { + int GetHeight() const { return toast_size_.cy; } HDWP Animate(HDWP hdwp, const POINT& origin); - bool IsAnimationActive() const - { + bool IsAnimationActive() const { return ease_in_active_ || ease_out_active_ || IsStackCollapseActive(); } - bool IsHighlighted() const - { + bool IsHighlighted() const { _ASSERT(!(is_highlighted_ && !IsWindowVisible(hwnd_))); return is_highlighted_; } diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index beb13754486..f8830e3ea2d 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -8,17 +8,14 @@ namespace brightray { void Win32Notification::Show( const base::string16& title, const base::string16& msg, const std::string& tag, const GURL& icon_url, - const SkBitmap& icon, const bool silent) -{ + const SkBitmap& icon, const bool silent) { auto presenter = static_cast(this->presenter()); if(!presenter) return; HBITMAP image = NULL; - if(!icon.drawsNothing()) - { - if(icon.colorType() == kBGRA_8888_SkColorType) - { + if(!icon.drawsNothing()) { + if(icon.colorType() == kBGRA_8888_SkColorType) { icon.lockPixels(); BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; @@ -40,14 +37,12 @@ void Win32Notification::Show( Win32Notification* existing = nullptr; if(!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag); - if(existing) - { + if(existing) { existing->tag_.clear(); this->notification_ref_ = std::move(existing->notification_ref_); this->notification_ref_.Set(title, msg, image); } - else - { + else { this->notification_ref_ = presenter->AddNotification(title, msg, image); } @@ -56,8 +51,7 @@ void Win32Notification::Show( if(image) DeleteObject(image); } -void Win32Notification::Dismiss() -{ +void Win32Notification::Dismiss() { notification_ref_.Close(); } diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index b1fe7e764b1..41f9583bd1f 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -4,26 +4,22 @@ namespace brightray { -class Win32Notification : public brightray::Notification -{ +class Win32Notification : public brightray::Notification { public: Win32Notification(NotificationDelegate* delegate, NotificationPresenterWin7* presenter) : - Notification(delegate, presenter) - { + Notification(delegate, presenter) { } void Show(const base::string16& title, const base::string16& msg, const std::string& tag, const GURL& icon_url, const SkBitmap& icon, const bool silent) override; void Dismiss() override; - const DesktopNotificationController::Notification& GetRef() const - { + const DesktopNotificationController::Notification& GetRef() const { return notification_ref_; } - const std::string& GetTag() const - { + const std::string& GetTag() const { return tag_; } From 91cff2e6c794179e3c5485bb1430b172f3b6bff8 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Mon, 3 Apr 2017 13:34:01 +0200 Subject: [PATCH 10/20] Address cpplint issue "Missing space before ( in if( [whitespace/parens] [5]" --- .../win/notification_presenter_win7.cc | 12 +- .../win/win32_desktop_notifications/common.h | 6 +- .../desktop_notification_controller.cc | 84 +++++----- .../win/win32_desktop_notifications/toast.cc | 157 +++++++++--------- brightray/browser/win/win32_notification.cc | 12 +- 5 files changed, 136 insertions(+), 135 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win7.cc b/brightray/browser/win/notification_presenter_win7.cc index cdbdfc803d4..e74e212a575 100644 --- a/brightray/browser/win/notification_presenter_win7.cc +++ b/brightray/browser/win/notification_presenter_win7.cc @@ -10,9 +10,9 @@ brightray::Notification* NotificationPresenterWin7::CreateNotificationObject( Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef( const DesktopNotificationController::Notification& ref) { - for(auto n : this->notifications()) { + for (auto n : this->notifications()) { auto w32n = static_cast(n); - if(w32n->GetRef() == ref) + if (w32n->GetRef() == ref) return w32n; } @@ -21,9 +21,9 @@ Win32Notification* NotificationPresenterWin7::GetNotificationObjectByRef( Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag( const std::string& tag) { - for(auto n : this->notifications()) { + for (auto n : this->notifications()) { auto w32n = static_cast(n); - if(w32n->GetTag() == tag) + if (w32n->GetTag() == tag) return w32n; } @@ -33,13 +33,13 @@ Win32Notification* NotificationPresenterWin7::GetNotificationObjectByTag( void NotificationPresenterWin7::OnNotificationClicked( Notification& notification) { auto n = GetNotificationObjectByRef(notification); - if(n) n->NotificationClicked(); + if (n) n->NotificationClicked(); } void NotificationPresenterWin7::OnNotificationDismissed( Notification& notification) { auto n = GetNotificationObjectByRef(notification); - if(n) n->NotificationDismissed(); + if (n) n->NotificationDismissed(); } } diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h index f68ca396086..5c7d5ae3764 100644 --- a/brightray/browser/win/win32_desktop_notifications/common.h +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -14,7 +14,7 @@ struct NotificationData { NotificationData() = default; ~NotificationData() { - if(image) DeleteObject(image); + if (image) DeleteObject(image); } NotificationData(const NotificationData& other) = delete; @@ -34,9 +34,9 @@ struct ScreenMetrics { auto GetDpiForMonitor = (GetDpiForMonitor_t*)GetProcAddress(GetModuleHandle(TEXT("shcore")), "GetDpiForMonitor"); - if(GetDpiForMonitor) { + if (GetDpiForMonitor) { auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY); - if(GetDpiForMonitor(monitor, 0, &dpi_x, &dpi_y) == S_OK) + if (GetDpiForMonitor(monitor, 0, &dpi_x, &dpi_y) == S_OK) return; } diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index 072be506534..b371df608a2 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -16,12 +16,12 @@ HBITMAP CopyBitmap(HBITMAP bitmap) { HBITMAP ret = NULL; BITMAP bm; - if(bitmap && GetObject(bitmap, sizeof(bm), &bm)) { + if (bitmap && GetObject(bitmap, sizeof(bm), &bm)) { HDC hdcScreen = GetDC(NULL); ret = CreateCompatibleBitmap(hdcScreen, bm.bmWidth, bm.bmHeight); ReleaseDC(NULL, hdcScreen); - if(ret) { + if (ret) { HDC hdcSrc = CreateCompatibleDC(NULL); HDC hdcDst = CreateCompatibleDC(NULL); SelectBitmap(hdcSrc, bitmap); @@ -45,8 +45,8 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() { // as there is no point). static HMODULE module = NULL; - if(!module) { - if(GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + if (!module) { + if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast(&RegisterWndClasses), &module)) { @@ -71,14 +71,14 @@ DesktopNotificationController::DesktopNotificationController( } DesktopNotificationController::~DesktopNotificationController() { - for(auto&& inst : instances_) DestroyToast(inst); - if(hwnd_controller_) DestroyWindow(hwnd_controller_); + for (auto&& inst : instances_) DestroyToast(inst); + if (hwnd_controller_) DestroyWindow(hwnd_controller_); ClearAssets(); } LRESULT CALLBACK DesktopNotificationController::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { - switch(message) { + switch (message) { case WM_CREATE: { auto& cs = reinterpret_cast(lParam); @@ -87,7 +87,7 @@ LRESULT CALLBACK DesktopNotificationController::WndProc( break; case WM_TIMER: - if(wParam == TimerID_Animate) { + if (wParam == TimerID_Animate) { Get(hWnd)->AnimateAll(); } return 0; @@ -101,7 +101,7 @@ LRESULT CALLBACK DesktopNotificationController::WndProc( break; case WM_SETTINGCHANGE: - if(wParam == SPI_SETWORKAREA) { + if (wParam == SPI_SETWORKAREA) { Get(hWnd)->AnimateAll(); } break; @@ -113,7 +113,7 @@ LRESULT CALLBACK DesktopNotificationController::WndProc( void DesktopNotificationController::StartAnimation() { _ASSERT(hwnd_controller_); - if(!is_animating_ && hwnd_controller_) { + if (!is_animating_ && hwnd_controller_) { // NOTE: 15ms is shorter than what we'd need for 60 fps, but since // the timer is not accurate we must request a higher frame rate // to get at least 60 @@ -134,9 +134,9 @@ HFONT DesktopNotificationController::GetBodyFont() { } void DesktopNotificationController::InitializeFonts() { - if(!body_font_) { + if (!body_font_) { NONCLIENTMETRICS metrics = { sizeof(metrics) }; - if(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0)) { + if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0)) { auto baseHeight = metrics.lfMessageFont.lfHeight; HDC hdc = GetDC(NULL); @@ -147,7 +147,7 @@ void DesktopNotificationController::InitializeFonts() { (LONG)ScaleForDpi(baseHeight * 1.1f, dpiY); body_font_ = CreateFontIndirect(&metrics.lfMessageFont); - if(caption_font_) DeleteFont(caption_font_); + if (caption_font_) DeleteFont(caption_font_); metrics.lfMessageFont.lfHeight = (LONG)ScaleForDpi(baseHeight * 1.4f, dpiY); caption_font_ = CreateFontIndirect(&metrics.lfMessageFont); @@ -156,8 +156,8 @@ void DesktopNotificationController::InitializeFonts() { } void DesktopNotificationController::ClearAssets() { - if(caption_font_) { DeleteFont(caption_font_); caption_font_ = NULL; } - if(body_font_) { DeleteFont(body_font_); body_font_ = NULL; } + if (caption_font_) { DeleteFont(caption_font_); caption_font_ = NULL; } + if (body_font_) { DeleteFont(body_font_); body_font_ = NULL; } } void DesktopNotificationController::AnimateAll() { @@ -167,35 +167,35 @@ void DesktopNotificationController::AnimateAll() { bool keepAnimating = false; - if(!instances_.empty()) { + if (!instances_.empty()) { RECT workArea; - if(SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) { + if (SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) { ScreenMetrics metrics; POINT origin = { workArea.right, workArea.bottom - metrics.Y(toast_margin_) }; auto hdwp = BeginDeferWindowPos((int)instances_.size()); - for(auto&& inst : instances_) { - if(!inst.hwnd) continue; + for (auto&& inst : instances_) { + if (!inst.hwnd) continue; auto notification = Toast::Get(inst.hwnd); hdwp = notification->Animate(hdwp, origin); - if(!hdwp) break; + if (!hdwp) break; keepAnimating |= notification->IsAnimationActive(); } - if(hdwp) EndDeferWindowPos(hdwp); + if (hdwp) EndDeferWindowPos(hdwp); } } - if(!keepAnimating) { + if (!keepAnimating) { _ASSERT(hwnd_controller_); - if(hwnd_controller_) KillTimer(hwnd_controller_, TimerID_Animate); + if (hwnd_controller_) KillTimer(hwnd_controller_, TimerID_Animate); is_animating_ = false; } // Purge dismissed notifications and collapse the stack between // items which are highlighted - if(!instances_.empty()) { + if (!instances_.empty()) { auto isAlive = [](ToastInstance& inst) { return inst.hwnd && IsWindowVisible(inst.hwnd); }; @@ -204,7 +204,7 @@ void DesktopNotificationController::AnimateAll() { return inst.hwnd && Toast::Get(inst.hwnd)->IsHighlighted(); }; - for(auto it = instances_.begin();; ++it) { + for (auto it = instances_.begin();; ++it) { // find next highlighted item auto it2 = find_if(it, instances_.end(), isHighlighted); @@ -214,7 +214,7 @@ void DesktopNotificationController::AnimateAll() { // purge the dead items for_each(it, it2, [this](auto&& inst) { DestroyToast(inst); }); - if(it2 == instances_.end()) { + if (it2 == instances_.end()) { instances_.erase(it, it2); break; } @@ -224,16 +224,16 @@ void DesktopNotificationController::AnimateAll() { } // Set new toast positions - if(!instances_.empty()) { + if (!instances_.empty()) { ScreenMetrics metrics; auto margin = metrics.Y(toast_margin_); int targetPos = 0; - for(auto&& inst : instances_) { - if(inst.hwnd) { + for (auto&& inst : instances_) { + if (inst.hwnd) { auto toast = Toast::Get(inst.hwnd); - if(toast->IsHighlighted()) + if (toast->IsHighlighted()) targetPos = toast->GetVerticalPosition(); else toast->SetVerticalPosition(targetPos); @@ -266,7 +266,7 @@ void DesktopNotificationController::CloseNotification( Notification& notification) { // Remove it from the queue auto it = find(queue_.begin(), queue_.end(), notification.data_); - if(it != queue_.end()) { + if (it != queue_.end()) { queue_.erase(it); this->OnNotificationClosed(notification); return; @@ -274,14 +274,14 @@ void DesktopNotificationController::CloseNotification( // Dismiss active toast auto hwnd = GetToast(notification.data_.get()); - if(hwnd) { + if (hwnd) { auto toast = Toast::Get(hwnd); toast->Dismiss(); } } void DesktopNotificationController::CheckQueue() { - while(instances_.size() < instances_.capacity() && !queue_.empty()) { + while (instances_.size() < instances_.capacity() && !queue_.empty()) { CreateToast(move(queue_.front())); queue_.pop_front(); } @@ -290,9 +290,9 @@ void DesktopNotificationController::CheckQueue() { void DesktopNotificationController::CreateToast(NotificationLink&& data) { auto hInstance = RegisterWndClasses(); auto hwnd = Toast::Create(hInstance, data); - if(hwnd) { + if (hwnd) { int toastPos = 0; - if(!instances_.empty()) { + if (!instances_.empty()) { auto& item = instances_.back(); _ASSERT(item.hwnd); @@ -305,7 +305,7 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) { instances_.push_back({ hwnd, move(data) }); - if(!hwnd_controller_) { + if (!hwnd_controller_) { // NOTE: We cannot use a message-only window because we need to // receive system notifications hwnd_controller_ = CreateWindow(class_name_, nullptr, 0, @@ -330,7 +330,7 @@ HWND DesktopNotificationController::GetToast( } void DesktopNotificationController::DestroyToast(ToastInstance& inst) { - if(inst.hwnd) { + if (inst.hwnd) { auto data = Toast::Get(inst.hwnd)->GetNotification(); DestroyWindow(inst.hwnd); @@ -357,7 +357,7 @@ void DesktopNotificationController::Notification::Close() { // No business calling this when not pointing to a valid instance _ASSERT(data_); - if(data_->controller) + if (data_->controller) data_->controller->CloseNotification(*this); } @@ -367,17 +367,17 @@ void DesktopNotificationController::Notification::Set( _ASSERT(data_); // Do nothing when the notification has been closed - if(!data_->controller) + if (!data_->controller) return; - if(data_->image) DeleteBitmap(data_->image); + if (data_->image) DeleteBitmap(data_->image); data_->caption = move(caption); data_->body_text = move(bodyText); data_->image = CopyBitmap(image); auto hwnd = data_->controller->GetToast(data_.get()); - if(hwnd) { + if (hwnd) { auto toast = Toast::Get(hwnd); toast->ResetContents(); } @@ -395,7 +395,7 @@ DesktopNotificationController::NotificationLink::NotificationLink( DesktopNotificationController::NotificationLink::~NotificationLink() { auto p = get(); - if(p) p->controller = nullptr; + if (p) p->controller = nullptr; } } diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 4f71dad9370..798dd76fba4 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -15,29 +15,30 @@ namespace brightray { static COLORREF GetAccentColor() { bool success = false; - if(IsAppThemed()) { + if (IsAppThemed()) { HKEY hkey; - if(RegOpenKeyEx(HKEY_CURRENT_USER, - TEXT("SOFTWARE\\Microsoft\\Windows\\DWM"), 0, - KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { + if (RegOpenKeyEx(HKEY_CURRENT_USER, + TEXT("SOFTWARE\\Microsoft\\Windows\\DWM"), 0, + KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { COLORREF color; DWORD type, size; - if(RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, - &type, - (BYTE*)&color, - &(size = sizeof(color))) == ERROR_SUCCESS && - type == REG_DWORD) { + if (RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, + &type, + (BYTE*)&color, + &(size = sizeof(color))) == ERROR_SUCCESS && + type == REG_DWORD) { // convert from RGBA color = RGB(GetRValue(color), GetGValue(color), GetBValue(color)); success = true; } - else if(RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, - &type, - (BYTE*)&color, - &(size = sizeof(color))) == ERROR_SUCCESS && - type == REG_DWORD) { + else if ( + RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, + &type, + (BYTE*)&color, + &(size = sizeof(color))) == ERROR_SUCCESS && + type == REG_DWORD) { // convert from BGRA color = RGB(GetBValue(color), GetGValue(color), @@ -47,7 +48,7 @@ static COLORREF GetAccentColor() { RegCloseKey(hkey); - if(success) return color; + if (success) return color; } } @@ -61,10 +62,10 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { // scale that separately, and copy it back to the scaled color bitmap. BITMAP bm; - if(!GetObject(bitmap, sizeof(bm), &bm)) + if (!GetObject(bitmap, sizeof(bm), &bm)) return NULL; - if(width == 0 || height == 0) + if (width == 0 || height == 0) return NULL; HBITMAP resultBitmap = NULL; @@ -85,22 +86,22 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { DIB_RGB_COLORS, &alphaSrcBits, NULL, 0); - if(alphaSrcBitmap) { - if(GetDIBits(hdcScreen, bitmap, 0, 0, 0, + if (alphaSrcBitmap) { + if (GetDIBits(hdcScreen, bitmap, 0, 0, 0, (BITMAPINFO*)&bmi, DIB_RGB_COLORS) && - bmi.biSizeImage > 0 && - (bmi.biSizeImage % 4) == 0) { + bmi.biSizeImage > 0 && + (bmi.biSizeImage % 4) == 0) { auto buf = (BYTE*)_aligned_malloc(bmi.biSizeImage, sizeof(DWORD)); - if(buf) { + if (buf) { GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); BYTE* dest = (BYTE*)alphaSrcBits; - for(const DWORD *src = (DWORD*)buf, - *end = (DWORD*)(buf + bmi.biSizeImage); - src != end; - ++src, ++dest) { + for (const DWORD *src = (DWORD*)buf, + *end = (DWORD*)(buf + bmi.biSizeImage); + src != end; + ++src, ++dest) { BYTE a = *src >> 24; *dest++ = a; *dest++ = a; @@ -113,7 +114,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { } } - if(alphaSrcBitmap) { + if (alphaSrcBitmap) { BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; bmi.biWidth = width; bmi.biHeight = height; @@ -134,7 +135,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { HDC hdc = CreateCompatibleDC(NULL); HDC hdcSrc = CreateCompatibleDC(NULL); - if(colorBitmap && alphaBitmap && hdc && hdcSrc) { + if (colorBitmap && alphaBitmap && hdc && hdcSrc) { SetStretchBltMode(hdc, HALFTONE); // resize color channels @@ -158,7 +159,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { auto dest = (BYTE*)colorBits; auto src = (const BYTE*)alphaBits; auto end = src + (width * height * 4); - while(src != end) { + while (src != end) { dest[3] = src[0]; dest += 4; src += 4; @@ -170,11 +171,11 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { DIB_RGB_COLORS); } - if(hdcSrc) DeleteDC(hdcSrc); - if(hdc) DeleteDC(hdc); + if (hdcSrc) DeleteDC(hdcSrc); + if (hdc) DeleteDC(hdc); - if(alphaBitmap) DeleteObject(alphaBitmap); - if(colorBitmap) DeleteObject(colorBitmap); + if (alphaBitmap) DeleteObject(alphaBitmap); + if (colorBitmap) DeleteObject(colorBitmap); DeleteObject(alphaSrcBitmap); } @@ -194,8 +195,8 @@ DesktopNotificationController::Toast::Toast( DesktopNotificationController::Toast::~Toast() { DeleteDC(hdc_); - if(bitmap_) DeleteBitmap(bitmap_); - if(scaled_image_) DeleteBitmap(scaled_image_); + if (bitmap_) DeleteBitmap(bitmap_); + if (scaled_image_) DeleteBitmap(scaled_image_); } void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) { @@ -211,7 +212,7 @@ void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) { LRESULT DesktopNotificationController::Toast::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { - switch(message) { + switch (message) { case WM_CREATE: { auto& cs = reinterpret_cast(lParam); @@ -231,7 +232,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( return MA_NOACTIVATE; case WM_TIMER: - if(wParam == TimerID_AutoDismiss) { + if (wParam == TimerID_AutoDismiss) { Get(hWnd)->AutoDismiss(); } return 0; @@ -243,7 +244,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( inst->Dismiss(); Notification notification(inst->data_); - if(inst->is_close_hot_) + if (inst->is_close_hot_) inst->data_->controller->OnNotificationDismissed(notification); else inst->data_->controller->OnNotificationClicked(notification); @@ -253,7 +254,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_MOUSEMOVE: { auto inst = Get(hWnd); - if(!inst->is_highlighted_) { + if (!inst->is_highlighted_) { inst->is_highlighted_ = true; TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, hWnd }; @@ -264,7 +265,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( inst->is_close_hot_ = (PtInRect(&inst->close_button_rect_, cursor) != FALSE); - if(!inst->is_non_interactive_) + if (!inst->is_non_interactive_) inst->CancelDismiss(); inst->UpdateContents(); @@ -278,7 +279,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( inst->is_close_hot_ = false; inst->UpdateContents(); - if(!inst->ease_out_active_ && inst->ease_in_pos_ == 1.0f) + if (!inst->ease_out_active_ && inst->ease_in_pos_ == 1.0f) inst->ScheduleDismissal(); // Make sure stack collapse happens if needed @@ -289,8 +290,8 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_WINDOWPOSCHANGED: { auto& wp = reinterpret_cast(lParam); - if(wp->flags & SWP_HIDEWINDOW) { - if(!IsWindowVisible(hWnd)) + if (wp->flags & SWP_HIDEWINDOW) { + if (!IsWindowVisible(hWnd)) Get(hWnd)->is_highlighted_ = false; } } @@ -382,7 +383,7 @@ void DesktopNotificationController::Toast::Draw() { auto textOffsetX = margin_.cx; BITMAP imageInfo = {}; - if(scaled_image_) { + if (scaled_image_) { GetObject(scaled_image_, sizeof(imageInfo), &imageInfo); textOffsetX += margin_.cx + imageInfo.bmWidth; @@ -405,7 +406,7 @@ void DesktopNotificationController::Toast::Draw() { } // image - if(scaled_image_) { + if (scaled_image_) { HDC hdcImage = CreateCompatibleDC(NULL); SelectBitmap(hdcImage, scaled_image_); BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; @@ -433,7 +434,7 @@ void DesktopNotificationController::Toast::Draw() { } // body text - if(!data_->body_text.empty()) { + if (!data_->body_text.empty()) { RECT rc = { textOffsetX, 2 * margin_.cy + tmCap.tmAscent, @@ -469,21 +470,21 @@ bool DesktopNotificationController::Toast::IsRedrawNeeded() const { } void DesktopNotificationController::Toast::UpdateBufferSize() { - if(hdc_) { + if (hdc_) { SIZE newSize; { TEXTMETRIC tmCap = {}; HFONT font = data_->controller->GetCaptionFont(); - if(font) { + if (font) { SelectFont(hdc_, font); - if(!GetTextMetrics(hdc_, &tmCap)) return; + if (!GetTextMetrics(hdc_, &tmCap)) return; } TEXTMETRIC tmBody = {}; font = data_->controller->GetBodyFont(); - if(font) { + if (font) { SelectFont(hdc_, font); - if(!GetTextMetrics(hdc_, &tmBody)) return; + if (!GetTextMetrics(hdc_, &tmBody)) return; } this->margin_ = { tmCap.tmAveCharWidth * 2, tmCap.tmAscent / 2 }; @@ -491,25 +492,25 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { newSize.cx = margin_.cx + (32 * tmCap.tmAveCharWidth) + margin_.cx; newSize.cy = margin_.cy + (tmCap.tmHeight) + margin_.cy; - if(!data_->body_text.empty()) + if (!data_->body_text.empty()) newSize.cy += margin_.cy + (3 * tmBody.tmHeight); - if(data_->image) { + if (data_->image) { BITMAP bm; - if(GetObject(data_->image, sizeof(bm), &bm)) { + if (GetObject(data_->image, sizeof(bm), &bm)) { // cap the image size const int maxDimSize = 80; auto width = bm.bmWidth; auto height = bm.bmHeight; - if(width < height) { - if(height > maxDimSize) { + if (width < height) { + if (height > maxDimSize) { width = width * maxDimSize / height; height = maxDimSize; } } else { - if(width > maxDimSize) { + if (width > maxDimSize) { height = height * maxDimSize / width; width = maxDimSize; } @@ -522,7 +523,7 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { auto heightWithImage = margin_.cy + (imageDrawSize.cy) + margin_.cy; - if(newSize.cy < heightWithImage) + if (newSize.cy < heightWithImage) newSize.cy = heightWithImage; UpdateScaledImage(imageDrawSize); @@ -530,26 +531,26 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { } } - if(newSize.cx != this->toast_size_.cx || + if (newSize.cx != this->toast_size_.cx || newSize.cy != this->toast_size_.cy) { HDC hdcScreen = GetDC(NULL); auto newBitmap = CreateCompatibleBitmap(hdcScreen, newSize.cx, newSize.cy); ReleaseDC(NULL, hdcScreen); - if(newBitmap) { - if(SelectBitmap(hdc_, newBitmap)) { + if (newBitmap) { + if (SelectBitmap(hdc_, newBitmap)) { RECT dirty1 = {}, dirty2 = {}; - if(toast_size_.cx < newSize.cx) { + if (toast_size_.cx < newSize.cx) { dirty1 = { toast_size_.cx, 0, newSize.cx, toast_size_.cy }; } - if(toast_size_.cy < newSize.cy) { + if (toast_size_.cy < newSize.cy) { dirty2 = { 0, toast_size_.cy, newSize.cx, newSize.cy }; } - if(this->bitmap_) DeleteBitmap(this->bitmap_); + if (this->bitmap_) DeleteBitmap(this->bitmap_); this->bitmap_ = newBitmap; this->toast_size_ = newSize; @@ -589,10 +590,10 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) { BITMAP bm; - if(!GetObject(scaled_image_, sizeof(bm), &bm) || + if (!GetObject(scaled_image_, sizeof(bm), &bm) || bm.bmWidth != size.cx || bm.bmHeight != size.cy) { - if(scaled_image_) DeleteBitmap(scaled_image_); + if (scaled_image_) DeleteBitmap(scaled_image_); scaled_image_ = StretchBitmap(data_->image, size.cx, size.cy); } } @@ -600,7 +601,7 @@ void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) { void DesktopNotificationController::Toast::UpdateContents() { Draw(); - if(IsWindowVisible(hwnd_)) { + if (IsWindowVisible(hwnd_)) { RECT rc; GetWindowRect(hwnd_, &rc); POINT origin = { 0, 0 }; @@ -611,7 +612,7 @@ void DesktopNotificationController::Toast::UpdateContents() { } void DesktopNotificationController::Toast::Dismiss() { - if(!is_non_interactive_) { + if (!is_non_interactive_) { // Set a flag to prevent further interaction. We don't disable the HWND // because we still want to receive mouse move messages in order to keep // the toast under the cursor and not collapse it while dismissing. @@ -637,7 +638,7 @@ void DesktopNotificationController::Toast::ScheduleDismissal() { } void DesktopNotificationController::Toast::ResetContents() { - if(scaled_image_) { + if (scaled_image_) { DeleteBitmap(scaled_image_); scaled_image_ = NULL; } @@ -652,7 +653,7 @@ void DesktopNotificationController::Toast::PopUp(int y) { void DesktopNotificationController::Toast::SetVerticalPosition(int y) { // Don't restart animation if current target is the same - if(y == vertical_pos_target_) + if (y == vertical_pos_target_) return; // Make sure the new animation's origin is at the current position @@ -669,7 +670,7 @@ HDWP DesktopNotificationController::Toast::Animate( HDWP hdwp, const POINT& origin) { UpdateBufferSize(); - if(IsRedrawNeeded()) + if (IsRedrawNeeded()) Draw(); POINT srcOrigin = { 0, 0 }; @@ -707,7 +708,7 @@ HDWP DesktopNotificationController::Toast::Animate( ulw.pptDst = &pt; ulw.psize = &size; - if(ease_in_active_ && easeInPos == 1.0f) { + if (ease_in_active_ && easeInPos == 1.0f) { ease_in_active_ = false; ScheduleDismissal(); } @@ -715,7 +716,7 @@ HDWP DesktopNotificationController::Toast::Animate( this->ease_in_pos_ = easeInPos; this->stack_collapse_pos_ = stackCollapsePos; - if(easeOutPos != this->ease_out_pos_) { + if (easeOutPos != this->ease_out_pos_) { blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = (BYTE)(255 * (1.0f - easeOutPos)); @@ -726,7 +727,7 @@ HDWP DesktopNotificationController::Toast::Animate( this->ease_out_pos_ = easeOutPos; - if(easeOutPos == 1.0f) { + if (easeOutPos == 1.0f) { ease_out_active_ = false; dwpFlags &= ~SWP_SHOWWINDOW; @@ -734,7 +735,7 @@ HDWP DesktopNotificationController::Toast::Animate( } } - if(stackCollapsePos == 1.0f) { + if (stackCollapsePos == 1.0f) { vertical_pos_ = vertical_pos_target_; } @@ -768,7 +769,7 @@ bool DesktopNotificationController::Toast::IsStackCollapseActive() const { } float DesktopNotificationController::Toast::AnimateEaseIn() { - if(!ease_in_active_) + if (!ease_in_active_) return ease_in_pos_; constexpr float duration = 500.0f; @@ -783,7 +784,7 @@ float DesktopNotificationController::Toast::AnimateEaseIn() { } float DesktopNotificationController::Toast::AnimateEaseOut() { - if(!ease_out_active_) + if (!ease_out_active_) return ease_out_pos_; constexpr float duration = 120.0f; @@ -797,7 +798,7 @@ float DesktopNotificationController::Toast::AnimateEaseOut() { } float DesktopNotificationController::Toast::AnimateStackCollapse() { - if(!IsStackCollapseActive()) + if (!IsStackCollapseActive()) return stack_collapse_pos_; constexpr float duration = 500.0f; diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index f8830e3ea2d..36242c06a40 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -10,12 +10,12 @@ void Win32Notification::Show( const std::string& tag, const GURL& icon_url, const SkBitmap& icon, const bool silent) { auto presenter = static_cast(this->presenter()); - if(!presenter) return; + if (!presenter) return; HBITMAP image = NULL; - if(!icon.drawsNothing()) { - if(icon.colorType() == kBGRA_8888_SkColorType) { + if (!icon.drawsNothing()) { + if (icon.colorType() == kBGRA_8888_SkColorType) { icon.lockPixels(); BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; @@ -35,9 +35,9 @@ void Win32Notification::Show( } Win32Notification* existing = nullptr; - if(!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag); + if (!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag); - if(existing) { + if (existing) { existing->tag_.clear(); this->notification_ref_ = std::move(existing->notification_ref_); this->notification_ref_.Set(title, msg, image); @@ -48,7 +48,7 @@ void Win32Notification::Show( this->tag_ = tag; - if(image) DeleteObject(image); + if (image) DeleteObject(image); } void Win32Notification::Dismiss() { From 9b7fbd4d223a9302a108763b50c7f820f070979c Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 11:53:37 +0200 Subject: [PATCH 11/20] Address cpplint issue "Using C-style cast. Use reinterpret_cast(...) instead [readability/casting] [4]" --- .../win/win32_desktop_notifications/common.h | 8 +-- .../desktop_notification_controller.cc | 5 +- .../win/win32_desktop_notifications/toast.cc | 51 +++++++++++-------- brightray/browser/win/win32_notification.cc | 3 +- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h index 5c7d5ae3764..2785f187fc7 100644 --- a/brightray/browser/win/win32_desktop_notifications/common.h +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -31,9 +31,11 @@ struct ScreenMetrics { ScreenMetrics() { typedef HRESULT WINAPI GetDpiForMonitor_t(HMONITOR, int, UINT*, UINT*); - auto GetDpiForMonitor = - (GetDpiForMonitor_t*)GetProcAddress(GetModuleHandle(TEXT("shcore")), - "GetDpiForMonitor"); + + auto GetDpiForMonitor = reinterpret_cast( + GetProcAddress(GetModuleHandle(TEXT("shcore")), + "GetDpiForMonitor")); + if (GetDpiForMonitor) { auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY); if (GetDpiForMonitor(monitor, 0, &dpi_x, &dpi_y) == S_OK) diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index b371df608a2..f0d65469b9d 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -174,7 +174,9 @@ void DesktopNotificationController::AnimateAll() { POINT origin = { workArea.right, workArea.bottom - metrics.Y(toast_margin_) }; - auto hdwp = BeginDeferWindowPos((int)instances_.size()); + auto hdwp = + BeginDeferWindowPos(static_cast(instances_.size())); + for (auto&& inst : instances_) { if (!inst.hwnd) continue; @@ -183,6 +185,7 @@ void DesktopNotificationController::AnimateAll() { if (!hdwp) break; keepAnimating |= notification->IsAnimationActive(); } + if (hdwp) EndDeferWindowPos(hdwp); } } diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 798dd76fba4..2727bc9ed12 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -24,7 +24,7 @@ static COLORREF GetAccentColor() { DWORD type, size; if (RegQueryValueEx(hkey, TEXT("AccentColor"), nullptr, &type, - (BYTE*)&color, + reinterpret_cast(&color), &(size = sizeof(color))) == ERROR_SUCCESS && type == REG_DWORD) { // convert from RGBA @@ -36,7 +36,7 @@ static COLORREF GetAccentColor() { else if ( RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, &type, - (BYTE*)&color, + reinterpret_cast(&color), &(size = sizeof(color))) == ERROR_SUCCESS && type == REG_DWORD) { // convert from BGRA @@ -82,26 +82,32 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { bmi.biCompression = BI_RGB; void* alphaSrcBits; - alphaSrcBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, + alphaSrcBitmap = CreateDIBSection(NULL, + reinterpret_cast(&bmi), DIB_RGB_COLORS, &alphaSrcBits, NULL, 0); if (alphaSrcBitmap) { if (GetDIBits(hdcScreen, bitmap, 0, 0, 0, - (BITMAPINFO*)&bmi, DIB_RGB_COLORS) && + reinterpret_cast(&bmi), + DIB_RGB_COLORS) && bmi.biSizeImage > 0 && (bmi.biSizeImage % 4) == 0) { - auto buf = (BYTE*)_aligned_malloc(bmi.biSizeImage, - sizeof(DWORD)); + auto buf = reinterpret_cast( + _aligned_malloc(bmi.biSizeImage, sizeof(DWORD))); + if (buf) { GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, - (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + reinterpret_cast(&bmi), + DIB_RGB_COLORS); - BYTE* dest = (BYTE*)alphaSrcBits; - for (const DWORD *src = (DWORD*)buf, - *end = (DWORD*)(buf + bmi.biSizeImage); - src != end; - ++src, ++dest) { + const DWORD *src = reinterpret_cast(buf); + const DWORD *end = + reinterpret_cast(buf + bmi.biSizeImage); + + BYTE* dest = reinterpret_cast(alphaSrcBits); + + for (; src != end; ++src, ++dest) { BYTE a = *src >> 24; *dest++ = a; *dest++ = a; @@ -123,12 +129,14 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { bmi.biCompression = BI_RGB; void* colorBits; - auto colorBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, + auto colorBitmap = CreateDIBSection(NULL, + reinterpret_cast(&bmi), DIB_RGB_COLORS, &colorBits, NULL, 0); void* alphaBits; - auto alphaBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmi, + auto alphaBitmap = CreateDIBSection(NULL, + reinterpret_cast(&bmi), DIB_RGB_COLORS, &alphaBits, NULL, 0); @@ -156,8 +164,8 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { GdiFlush(); // apply the alpha channel - auto dest = (BYTE*)colorBits; - auto src = (const BYTE*)alphaBits; + auto dest = reinterpret_cast(colorBits); + auto src = reinterpret_cast(alphaBits); auto end = src + (width * height * 4); while (src != end) { dest[3] = src[0]; @@ -167,7 +175,8 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { // create the resulting bitmap resultBitmap = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, - colorBits, (BITMAPINFO*)&bmi, + colorBits, + reinterpret_cast(&bmi), DIB_RGB_COLORS); } @@ -657,8 +666,8 @@ void DesktopNotificationController::Toast::SetVerticalPosition(int y) { return; // Make sure the new animation's origin is at the current position - vertical_pos_ += - (int)((vertical_pos_target_ - vertical_pos_) * stack_collapse_pos_); + vertical_pos_ += static_cast( + (vertical_pos_target_ - vertical_pos_) * stack_collapse_pos_); // Set new target position and start the animation vertical_pos_target_ = y; @@ -699,11 +708,11 @@ HDWP DesktopNotificationController::Toast::Animate( auto yOffset = (vertical_pos_target_ - vertical_pos_) * stackCollapsePos; - size.cx = (int)(toast_size_.cx * easeInPos); + size.cx = static_cast(toast_size_.cx * easeInPos); size.cy = toast_size_.cy; pt.x = origin.x - size.cx; - pt.y = (int)(origin.y - vertical_pos_ - yOffset - size.cy); + pt.y = static_cast(origin.y - vertical_pos_ - yOffset - size.cy); ulw.pptDst = &pt; ulw.psize = &size; diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index 36242c06a40..f1ac9598a7f 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -27,7 +27,8 @@ void Win32Notification::Show( HDC hdcScreen = GetDC(NULL); image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, icon.getPixels(), - (BITMAPINFO*)&bmi, DIB_RGB_COLORS); + reinterpret_cast(&bmi), + DIB_RGB_COLORS); ReleaseDC(NULL, hdcScreen); icon.unlockPixels(); From 64bf0d210e51d0984e2eb0bbeff5fe1da3cbddc1 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 12:00:35 +0200 Subject: [PATCH 12/20] Address cpplint issue "An else should appear on the same line as the preceding } [whitespace/newline] [4]" --- brightray/browser/win/win32_desktop_notifications/toast.cc | 6 ++---- brightray/browser/win/win32_notification.cc | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 2727bc9ed12..879163a2629 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -32,8 +32,7 @@ static COLORREF GetAccentColor() { GetGValue(color), GetBValue(color)); success = true; - } - else if ( + } else if ( RegQueryValueEx(hkey, TEXT("ColorizationColor"), nullptr, &type, reinterpret_cast(&color), @@ -517,8 +516,7 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { width = width * maxDimSize / height; height = maxDimSize; } - } - else { + } else { if (width > maxDimSize) { height = height * maxDimSize / width; width = maxDimSize; diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index f1ac9598a7f..495569b036a 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -42,8 +42,7 @@ void Win32Notification::Show( existing->tag_.clear(); this->notification_ref_ = std::move(existing->notification_ref_); this->notification_ref_.Set(title, msg, image); - } - else { + } else { this->notification_ref_ = presenter->AddNotification(title, msg, image); } From 9d75e9f54c4aad16a5b09169a82e61ea83a93397 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 13:10:28 +0200 Subject: [PATCH 13/20] Address cpplint issue "Namespace should be terminated with "// namespace brightray" [readability/namespace] [5]" --- brightray/browser/win/notification_presenter_win7.cc | 2 +- brightray/browser/win/notification_presenter_win7.h | 2 +- brightray/browser/win/win32_desktop_notifications/common.h | 2 +- .../desktop_notification_controller.cc | 2 +- .../desktop_notification_controller.h | 2 +- brightray/browser/win/win32_desktop_notifications/toast.cc | 2 +- brightray/browser/win/win32_desktop_notifications/toast.h | 2 +- brightray/browser/win/win32_notification.cc | 2 +- brightray/browser/win/win32_notification.h | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win7.cc b/brightray/browser/win/notification_presenter_win7.cc index e74e212a575..f002c476d5a 100644 --- a/brightray/browser/win/notification_presenter_win7.cc +++ b/brightray/browser/win/notification_presenter_win7.cc @@ -42,4 +42,4 @@ void NotificationPresenterWin7::OnNotificationDismissed( if (n) n->NotificationDismissed(); } -} +} // namespace brightray diff --git a/brightray/browser/win/notification_presenter_win7.h b/brightray/browser/win/notification_presenter_win7.h index 2b352aad8af..8c7bc9aecaa 100644 --- a/brightray/browser/win/notification_presenter_win7.h +++ b/brightray/browser/win/notification_presenter_win7.h @@ -27,4 +27,4 @@ private: void OnNotificationDismissed(Notification& notification) override; }; -} // namespace +} // namespace brightray diff --git a/brightray/browser/win/win32_desktop_notifications/common.h b/brightray/browser/win/win32_desktop_notifications/common.h index 2785f187fc7..ef364ceb4cc 100644 --- a/brightray/browser/win/win32_desktop_notifications/common.h +++ b/brightray/browser/win/win32_desktop_notifications/common.h @@ -52,4 +52,4 @@ struct ScreenMetrics { template T Y(T value) const { return ScaleForDpi(value, dpi_y); } }; -} +} // namespace brightray diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index f0d65469b9d..e0261054be4 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -401,4 +401,4 @@ DesktopNotificationController::NotificationLink::~NotificationLink() { if (p) p->controller = nullptr; } -} +} // namespace brightray diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index a15d6b1b354..9320283ea4a 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -103,4 +103,4 @@ private: friend class DesktopNotificationController; }; -} +} // namespace brightray diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 879163a2629..5a31cfd9819 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -819,4 +819,4 @@ float DesktopNotificationController::Toast::AnimateStackCollapse() { return pos; } -} +} // namespace brightray diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index e77535f86c7..926427c734b 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -94,4 +94,4 @@ private: float ease_in_pos_ = 0, ease_out_pos_ = 0, stack_collapse_pos_ = 0; }; -} +} // namespace brightray diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index 495569b036a..5b6093658aa 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -55,4 +55,4 @@ void Win32Notification::Dismiss() { notification_ref_.Close(); } -} +} // namespace brightray diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index 41f9583bd1f..02f15c915f5 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -29,4 +29,4 @@ private: std::string tag_; }; -} +} // namespace brightray From 161b4e752b4c5f9344f1fc67fa3c52ec6b51c58e Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 13:15:06 +0200 Subject: [PATCH 14/20] Address cpplint issue "public: should be indented +1 space inside class NotificationPresenterWin7 [whitespace/indent] [3]" --- .../browser/win/notification_presenter_win7.h | 4 ++-- .../desktop_notification_controller.h | 14 +++++++------- .../win/win32_desktop_notifications/toast.h | 6 +++--- brightray/browser/win/win32_notification.h | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win7.h b/brightray/browser/win/notification_presenter_win7.h index 8c7bc9aecaa..5021d2c41ca 100644 --- a/brightray/browser/win/notification_presenter_win7.h +++ b/brightray/browser/win/notification_presenter_win7.h @@ -9,7 +9,7 @@ class Win32Notification; class NotificationPresenterWin7 : public NotificationPresenter, public DesktopNotificationController { -public: + public: NotificationPresenterWin7() = default; Win32Notification* GetNotificationObjectByRef( @@ -17,7 +17,7 @@ public: Win32Notification* GetNotificationObjectByTag(const std::string& tag); -private: + private: DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin7); brightray::Notification* CreateNotificationObject( diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index 9320283ea4a..88daf5d2ed7 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -10,7 +10,7 @@ namespace brightray { struct NotificationData; class DesktopNotificationController { -public: + public: DesktopNotificationController(unsigned maximumToasts = 3); ~DesktopNotificationController(); @@ -20,18 +20,18 @@ public: void CloseNotification(Notification& notification); // Event handlers -- override to receive the events -private: + private: virtual void OnNotificationClosed(Notification& notification) {} virtual void OnNotificationClicked(Notification& notification) {} virtual void OnNotificationDismissed(Notification& notification) {} -private: + private: static HINSTANCE RegisterWndClasses(); void StartAnimation(); HFONT GetCaptionFont(); HFONT GetBodyFont(); -private: + private: enum TimerID { TimerID_Animate = 1 }; @@ -76,7 +76,7 @@ private: HWND GetToast(const NotificationData* data) const; void DestroyToast(ToastInstance& inst); -private: + private: static constexpr const TCHAR class_name_[] = TEXT("DesktopNotificationController"); @@ -88,7 +88,7 @@ private: }; class DesktopNotificationController::Notification { -public: + public: Notification() = default; Notification(const std::shared_ptr& data); @@ -97,7 +97,7 @@ public: void Close(); void Set(std::wstring caption, std::wstring bodyText, HBITMAP image); -private: + private: std::shared_ptr data_; friend class DesktopNotificationController; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index 926427c734b..00e5733b6d5 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -4,7 +4,7 @@ namespace brightray { class DesktopNotificationController::Toast { -public: + public: static void Register(HINSTANCE hInstance); static HWND Create(HINSTANCE hInstance, std::shared_ptr& data); @@ -40,7 +40,7 @@ public: return is_highlighted_; } -private: + private: enum TimerID { TimerID_AutoDismiss = 1 }; @@ -67,7 +67,7 @@ private: float AnimateEaseOut(); float AnimateStackCollapse(); -private: + private: static constexpr const TCHAR class_name_[] = TEXT("DesktopNotificationToast"); diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index 02f15c915f5..c45352d145e 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -5,7 +5,7 @@ namespace brightray { class Win32Notification : public brightray::Notification { -public: + public: Win32Notification(NotificationDelegate* delegate, NotificationPresenterWin7* presenter) : Notification(delegate, presenter) { @@ -23,7 +23,7 @@ public: return tag_; } -private: + private: DISALLOW_COPY_AND_ASSIGN(Win32Notification); DesktopNotificationController::Notification notification_ref_; std::string tag_; From bf86718d610d4df6f7ce96f52484197538a2cd83 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 13:16:41 +0200 Subject: [PATCH 15/20] Address cpplint issue "DISALLOW_COPY_AND_ASSIGN should be the last thing in the class [readability/constructors] [3]" --- brightray/browser/win/notification_presenter_win7.h | 4 ++-- brightray/browser/win/win32_notification.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win7.h b/brightray/browser/win/notification_presenter_win7.h index 5021d2c41ca..c191439befc 100644 --- a/brightray/browser/win/notification_presenter_win7.h +++ b/brightray/browser/win/notification_presenter_win7.h @@ -18,13 +18,13 @@ class NotificationPresenterWin7 : Win32Notification* GetNotificationObjectByTag(const std::string& tag); private: - DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin7); - brightray::Notification* CreateNotificationObject( NotificationDelegate* delegate) override; void OnNotificationClicked(Notification& notification) override; void OnNotificationDismissed(Notification& notification) override; + + DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin7); }; } // namespace brightray diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index c45352d145e..9a418cd5037 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -24,9 +24,10 @@ class Win32Notification : public brightray::Notification { } private: - DISALLOW_COPY_AND_ASSIGN(Win32Notification); DesktopNotificationController::Notification notification_ref_; std::string tag_; + + DISALLOW_COPY_AND_ASSIGN(Win32Notification); }; } // namespace brightray From 1a7d9a5d204e0c6df8bf464562a9fd6e4993b47c Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 13:19:19 +0200 Subject: [PATCH 16/20] Address cpplint issue "At least two spaces is best between code and comments [whitespace/comments] [2]" --- brightray/browser/win/win32_desktop_notifications/toast.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index 00e5733b6d5..5700341f7e4 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -75,7 +75,7 @@ class DesktopNotificationController::Toast { HDC hdc_; HBITMAP bitmap_ = NULL; - const std::shared_ptr data_; // never null + const std::shared_ptr data_; // never null SIZE toast_size_ = {}; SIZE margin_ = {}; From 3336e0276903be530c10b0989219c05c83c04a49 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 13:20:16 +0200 Subject: [PATCH 17/20] Address cpplint issue "Constructors callable with one argument should be marked explicit. [runtime/explicit] [5]" --- .../desktop_notification_controller.cc | 2 +- .../desktop_notification_controller.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index e0261054be4..947def20df8 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -260,7 +260,7 @@ DesktopNotificationController::Notification data->image = CopyBitmap(image); // Enqueue new notification - Notification ret = *queue_.insert(queue_.end(), move(data)); + Notification ret { *queue_.insert(queue_.end(), move(data)) }; CheckQueue(); return ret; } diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index 88daf5d2ed7..db3001028b8 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -11,7 +11,7 @@ struct NotificationData; class DesktopNotificationController { public: - DesktopNotificationController(unsigned maximumToasts = 3); + explicit DesktopNotificationController(unsigned maximumToasts = 3); ~DesktopNotificationController(); class Notification; @@ -43,7 +43,7 @@ class DesktopNotificationController { // the `controller` member is cleared when the controller object // stops tracking the notification struct NotificationLink : std::shared_ptr { - NotificationLink(DesktopNotificationController* controller); + explicit NotificationLink(DesktopNotificationController* controller); ~NotificationLink(); NotificationLink(NotificationLink&&) = default; @@ -90,7 +90,7 @@ class DesktopNotificationController { class DesktopNotificationController::Notification { public: Notification() = default; - Notification(const std::shared_ptr& data); + explicit Notification(const std::shared_ptr& data); bool operator==(const Notification& other) const; From ba03c544f3beab54a5d804b8e2d82047aa3ac786 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 13:27:46 +0200 Subject: [PATCH 18/20] Address cpplint issue ""public:" should be preceded by a blank line [whitespace/blank_line] [3]" --- brightray/browser/notification.h | 1 + 1 file changed, 1 insertion(+) diff --git a/brightray/browser/notification.h b/brightray/browser/notification.h index 76e3ba4320f..a330bc25d1b 100644 --- a/brightray/browser/notification.h +++ b/brightray/browser/notification.h @@ -47,6 +47,7 @@ class Notification { protected: Notification(NotificationDelegate* delegate, NotificationPresenter* presenter); + public: virtual ~Notification(); From f73233ee55bb610b7dd302bb59fd0fccba42d9c6 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 14:14:23 +0200 Subject: [PATCH 19/20] Changed local variable and parameter names to snake case style --- .../desktop_notification_controller.cc | 104 ++--- .../desktop_notification_controller.h | 14 +- .../win/win32_desktop_notifications/toast.cc | 362 +++++++++--------- .../win/win32_desktop_notifications/toast.h | 14 +- 4 files changed, 247 insertions(+), 247 deletions(-) diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc index 947def20df8..68068a6de26 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.cc @@ -17,19 +17,19 @@ HBITMAP CopyBitmap(HBITMAP bitmap) { BITMAP bm; if (bitmap && GetObject(bitmap, sizeof(bm), &bm)) { - HDC hdcScreen = GetDC(NULL); - ret = CreateCompatibleBitmap(hdcScreen, bm.bmWidth, bm.bmHeight); - ReleaseDC(NULL, hdcScreen); + HDC hdc_screen = GetDC(NULL); + ret = CreateCompatibleBitmap(hdc_screen, bm.bmWidth, bm.bmHeight); + ReleaseDC(NULL, hdc_screen); if (ret) { - HDC hdcSrc = CreateCompatibleDC(NULL); - HDC hdcDst = CreateCompatibleDC(NULL); - SelectBitmap(hdcSrc, bitmap); - SelectBitmap(hdcDst, ret); - BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, - hdcSrc, 0, 0, SRCCOPY); - DeleteDC(hdcDst); - DeleteDC(hdcSrc); + HDC hdc_src = CreateCompatibleDC(NULL); + HDC hdc_dst = CreateCompatibleDC(NULL); + SelectBitmap(hdc_src, bitmap); + SelectBitmap(hdc_dst, ret); + BitBlt(hdc_dst, 0, 0, bm.bmWidth, bm.bmHeight, + hdc_src, 0, 0, SRCCOPY); + DeleteDC(hdc_dst); + DeleteDC(hdc_src); } } @@ -66,8 +66,8 @@ HINSTANCE DesktopNotificationController::RegisterWndClasses() { } DesktopNotificationController::DesktopNotificationController( - unsigned maximumToasts) { - instances_.reserve(maximumToasts); + unsigned maximum_toasts) { + instances_.reserve(maximum_toasts); } DesktopNotificationController::~DesktopNotificationController() { @@ -77,37 +77,37 @@ DesktopNotificationController::~DesktopNotificationController() { } LRESULT CALLBACK DesktopNotificationController::WndProc( - HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { switch (message) { case WM_CREATE: { - auto& cs = reinterpret_cast(lParam); - SetWindowLongPtr(hWnd, 0, (LONG_PTR)cs->lpCreateParams); + auto& cs = reinterpret_cast(lparam); + SetWindowLongPtr(hwnd, 0, (LONG_PTR)cs->lpCreateParams); } break; case WM_TIMER: - if (wParam == TimerID_Animate) { - Get(hWnd)->AnimateAll(); + if (wparam == TimerID_Animate) { + Get(hwnd)->AnimateAll(); } return 0; case WM_DISPLAYCHANGE: { - auto inst = Get(hWnd); + auto inst = Get(hwnd); inst->ClearAssets(); inst->AnimateAll(); } break; case WM_SETTINGCHANGE: - if (wParam == SPI_SETWORKAREA) { - Get(hWnd)->AnimateAll(); + if (wparam == SPI_SETWORKAREA) { + Get(hwnd)->AnimateAll(); } break; } - return DefWindowProc(hWnd, message, wParam, lParam); + return DefWindowProc(hwnd, message, wparam, lparam); } void DesktopNotificationController::StartAnimation() { @@ -140,16 +140,16 @@ void DesktopNotificationController::InitializeFonts() { auto baseHeight = metrics.lfMessageFont.lfHeight; HDC hdc = GetDC(NULL); - auto dpiY = GetDeviceCaps(hdc, LOGPIXELSY); + auto dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(NULL, hdc); metrics.lfMessageFont.lfHeight = - (LONG)ScaleForDpi(baseHeight * 1.1f, dpiY); + (LONG)ScaleForDpi(baseHeight * 1.1f, dpi_y); body_font_ = CreateFontIndirect(&metrics.lfMessageFont); if (caption_font_) DeleteFont(caption_font_); metrics.lfMessageFont.lfHeight = - (LONG)ScaleForDpi(baseHeight * 1.4f, dpiY); + (LONG)ScaleForDpi(baseHeight * 1.4f, dpi_y); caption_font_ = CreateFontIndirect(&metrics.lfMessageFont); } } @@ -165,14 +165,14 @@ void DesktopNotificationController::AnimateAll() { // to all current conditions. Animation time is only one of the variables // influencing them. Screen resolution is another. - bool keepAnimating = false; + bool keep_animating = false; if (!instances_.empty()) { - RECT workArea; - if (SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0)) { + RECT work_area; + if (SystemParametersInfo(SPI_GETWORKAREA, 0, &work_area, 0)) { ScreenMetrics metrics; - POINT origin = { workArea.right, - workArea.bottom - metrics.Y(toast_margin_) }; + POINT origin = { work_area.right, + work_area.bottom - metrics.Y(toast_margin_) }; auto hdwp = BeginDeferWindowPos(static_cast(instances_.size())); @@ -183,14 +183,14 @@ void DesktopNotificationController::AnimateAll() { auto notification = Toast::Get(inst.hwnd); hdwp = notification->Animate(hdwp, origin); if (!hdwp) break; - keepAnimating |= notification->IsAnimationActive(); + keep_animating |= notification->IsAnimationActive(); } if (hdwp) EndDeferWindowPos(hdwp); } } - if (!keepAnimating) { + if (!keep_animating) { _ASSERT(hwnd_controller_); if (hwnd_controller_) KillTimer(hwnd_controller_, TimerID_Animate); is_animating_ = false; @@ -199,20 +199,20 @@ void DesktopNotificationController::AnimateAll() { // Purge dismissed notifications and collapse the stack between // items which are highlighted if (!instances_.empty()) { - auto isAlive = [](ToastInstance& inst) { + auto is_alive = [](ToastInstance& inst) { return inst.hwnd && IsWindowVisible(inst.hwnd); }; - auto isHighlighted = [](ToastInstance& inst) { + auto is_highlighted = [](ToastInstance& inst) { return inst.hwnd && Toast::Get(inst.hwnd)->IsHighlighted(); }; for (auto it = instances_.begin();; ++it) { // find next highlighted item - auto it2 = find_if(it, instances_.end(), isHighlighted); + auto it2 = find_if(it, instances_.end(), is_highlighted); // collapse the stack in front of the highlighted item - it = stable_partition(it, it2, isAlive); + it = stable_partition(it, it2, is_alive); // purge the dead items for_each(it, it2, [this](auto&& inst) { DestroyToast(inst); }); @@ -231,17 +231,17 @@ void DesktopNotificationController::AnimateAll() { ScreenMetrics metrics; auto margin = metrics.Y(toast_margin_); - int targetPos = 0; + int target_pos = 0; for (auto&& inst : instances_) { if (inst.hwnd) { auto toast = Toast::Get(inst.hwnd); if (toast->IsHighlighted()) - targetPos = toast->GetVerticalPosition(); + target_pos = toast->GetVerticalPosition(); else - toast->SetVerticalPosition(targetPos); + toast->SetVerticalPosition(target_pos); - targetPos += toast->GetHeight() + margin; + target_pos += toast->GetHeight() + margin; } } } @@ -252,11 +252,11 @@ void DesktopNotificationController::AnimateAll() { DesktopNotificationController::Notification DesktopNotificationController::AddNotification( - std::wstring caption, std::wstring bodyText, HBITMAP image) { + std::wstring caption, std::wstring body_text, HBITMAP image) { NotificationLink data(this); data->caption = move(caption); - data->body_text = move(bodyText); + data->body_text = move(body_text); data->image = CopyBitmap(image); // Enqueue new notification @@ -291,19 +291,19 @@ void DesktopNotificationController::CheckQueue() { } void DesktopNotificationController::CreateToast(NotificationLink&& data) { - auto hInstance = RegisterWndClasses(); - auto hwnd = Toast::Create(hInstance, data); + auto hinstance = RegisterWndClasses(); + auto hwnd = Toast::Create(hinstance, data); if (hwnd) { - int toastPos = 0; + int toast_pos = 0; if (!instances_.empty()) { auto& item = instances_.back(); _ASSERT(item.hwnd); ScreenMetrics scr; auto toast = Toast::Get(item.hwnd); - toastPos = toast->GetVerticalPosition() + - toast->GetHeight() + - scr.Y(toast_margin_); + toast_pos = toast->GetVerticalPosition() + + toast->GetHeight() + + scr.Y(toast_margin_); } instances_.push_back({ hwnd, move(data) }); @@ -313,11 +313,11 @@ void DesktopNotificationController::CreateToast(NotificationLink&& data) { // receive system notifications hwnd_controller_ = CreateWindow(class_name_, nullptr, 0, 0, 0, 0, 0, - NULL, NULL, hInstance, this); + NULL, NULL, hinstance, this); } auto toast = Toast::Get(hwnd); - toast->PopUp(toastPos); + toast->PopUp(toast_pos); } } @@ -365,7 +365,7 @@ void DesktopNotificationController::Notification::Close() { } void DesktopNotificationController::Notification::Set( - std::wstring caption, std::wstring bodyText, HBITMAP image) { + std::wstring caption, std::wstring body_text, HBITMAP image) { // No business calling this when not pointing to a valid instance _ASSERT(data_); @@ -376,7 +376,7 @@ void DesktopNotificationController::Notification::Set( if (data_->image) DeleteBitmap(data_->image); data_->caption = move(caption); - data_->body_text = move(bodyText); + data_->body_text = move(body_text); data_->image = CopyBitmap(image); auto hwnd = data_->controller->GetToast(data_.get()); diff --git a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h index db3001028b8..643a61f5331 100644 --- a/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h +++ b/brightray/browser/win/win32_desktop_notifications/desktop_notification_controller.h @@ -11,11 +11,11 @@ struct NotificationData; class DesktopNotificationController { public: - explicit DesktopNotificationController(unsigned maximumToasts = 3); + explicit DesktopNotificationController(unsigned maximum_toasts = 3); ~DesktopNotificationController(); class Notification; - Notification AddNotification(std::wstring caption, std::wstring bodyText, + Notification AddNotification(std::wstring caption, std::wstring body_text, HBITMAP image); void CloseNotification(Notification& notification); @@ -58,11 +58,11 @@ class DesktopNotificationController { class Toast; - static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, - WPARAM wParam, LPARAM lParam); - static DesktopNotificationController* Get(HWND hWnd) { + static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, + WPARAM wparam, LPARAM lparam); + static DesktopNotificationController* Get(HWND hwnd) { return reinterpret_cast( - GetWindowLongPtr(hWnd, 0)); + GetWindowLongPtr(hwnd, 0)); } DesktopNotificationController( @@ -95,7 +95,7 @@ class DesktopNotificationController::Notification { bool operator==(const Notification& other) const; void Close(); - void Set(std::wstring caption, std::wstring bodyText, HBITMAP image); + void Set(std::wstring caption, std::wstring body_text, HBITMAP image); private: std::shared_ptr data_; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.cc b/brightray/browser/win/win32_desktop_notifications/toast.cc index 5a31cfd9819..1ab968c3ef0 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.cc +++ b/brightray/browser/win/win32_desktop_notifications/toast.cc @@ -67,11 +67,11 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { if (width == 0 || height == 0) return NULL; - HBITMAP resultBitmap = NULL; + HBITMAP result_bitmap = NULL; - HDC hdcScreen = GetDC(NULL); + HDC hdc_screen = GetDC(NULL); - HBITMAP alphaSrcBitmap; + HBITMAP alpha_src_bitmap; { BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; bmi.biWidth = bm.bmWidth; @@ -80,14 +80,13 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { bmi.biBitCount = bm.bmBitsPixel; bmi.biCompression = BI_RGB; - void* alphaSrcBits; - alphaSrcBitmap = CreateDIBSection(NULL, - reinterpret_cast(&bmi), - DIB_RGB_COLORS, &alphaSrcBits, - NULL, 0); + void* alpha_src_bits; + alpha_src_bitmap = + CreateDIBSection(NULL, reinterpret_cast(&bmi), + DIB_RGB_COLORS, &alpha_src_bits, NULL, 0); - if (alphaSrcBitmap) { - if (GetDIBits(hdcScreen, bitmap, 0, 0, 0, + if (alpha_src_bitmap) { + if (GetDIBits(hdc_screen, bitmap, 0, 0, 0, reinterpret_cast(&bmi), DIB_RGB_COLORS) && bmi.biSizeImage > 0 && @@ -96,7 +95,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { _aligned_malloc(bmi.biSizeImage, sizeof(DWORD))); if (buf) { - GetDIBits(hdcScreen, bitmap, 0, bm.bmHeight, buf, + GetDIBits(hdc_screen, bitmap, 0, bm.bmHeight, buf, reinterpret_cast(&bmi), DIB_RGB_COLORS); @@ -104,7 +103,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { const DWORD *end = reinterpret_cast(buf + bmi.biSizeImage); - BYTE* dest = reinterpret_cast(alphaSrcBits); + BYTE* dest = reinterpret_cast(alpha_src_bits); for (; src != end; ++src, ++dest) { BYTE a = *src >> 24; @@ -119,7 +118,7 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { } } - if (alphaSrcBitmap) { + if (alpha_src_bitmap) { BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; bmi.biWidth = width; bmi.biHeight = height; @@ -127,44 +126,42 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { bmi.biBitCount = 32; bmi.biCompression = BI_RGB; - void* colorBits; - auto colorBitmap = CreateDIBSection(NULL, - reinterpret_cast(&bmi), - DIB_RGB_COLORS, &colorBits, - NULL, 0); + void* color_bits; + auto color_bitmap = + CreateDIBSection(NULL, reinterpret_cast(&bmi), + DIB_RGB_COLORS, &color_bits, NULL, 0); - void* alphaBits; - auto alphaBitmap = CreateDIBSection(NULL, - reinterpret_cast(&bmi), - DIB_RGB_COLORS, &alphaBits, - NULL, 0); + void* alpha_bits; + auto alpha_bitmap = + CreateDIBSection(NULL, reinterpret_cast(&bmi), + DIB_RGB_COLORS, &alpha_bits, NULL, 0); HDC hdc = CreateCompatibleDC(NULL); - HDC hdcSrc = CreateCompatibleDC(NULL); + HDC hdc_src = CreateCompatibleDC(NULL); - if (colorBitmap && alphaBitmap && hdc && hdcSrc) { + if (color_bitmap && alpha_bitmap && hdc && hdc_src) { SetStretchBltMode(hdc, HALFTONE); // resize color channels - SelectObject(hdc, colorBitmap); - SelectObject(hdcSrc, bitmap); + SelectObject(hdc, color_bitmap); + SelectObject(hdc_src, bitmap); StretchBlt(hdc, 0, 0, width, height, - hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, + hdc_src, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); // resize alpha channel - SelectObject(hdc, alphaBitmap); - SelectObject(hdcSrc, alphaSrcBitmap); + SelectObject(hdc, alpha_bitmap); + SelectObject(hdc_src, alpha_src_bitmap); StretchBlt(hdc, 0, 0, width, height, - hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, + hdc_src, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); // flush before touching the bits GdiFlush(); // apply the alpha channel - auto dest = reinterpret_cast(colorBits); - auto src = reinterpret_cast(alphaBits); + auto dest = reinterpret_cast(color_bits); + auto src = reinterpret_cast(alpha_bits); auto end = src + (width * height * 4); while (src != end) { dest[3] = src[0]; @@ -173,32 +170,32 @@ static HBITMAP StretchBitmap(HBITMAP bitmap, unsigned width, unsigned height) { } // create the resulting bitmap - resultBitmap = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, - colorBits, - reinterpret_cast(&bmi), - DIB_RGB_COLORS); + result_bitmap = CreateDIBitmap(hdc_screen, &bmi, CBM_INIT, + color_bits, + reinterpret_cast(&bmi), + DIB_RGB_COLORS); } - if (hdcSrc) DeleteDC(hdcSrc); + if (hdc_src) DeleteDC(hdc_src); if (hdc) DeleteDC(hdc); - if (alphaBitmap) DeleteObject(alphaBitmap); - if (colorBitmap) DeleteObject(colorBitmap); + if (alpha_bitmap) DeleteObject(alpha_bitmap); + if (color_bitmap) DeleteObject(color_bitmap); - DeleteObject(alphaSrcBitmap); + DeleteObject(alpha_src_bitmap); } - ReleaseDC(NULL, hdcScreen); + ReleaseDC(NULL, hdc_screen); - return resultBitmap; + return result_bitmap; } DesktopNotificationController::Toast::Toast( - HWND hWnd, shared_ptr* data) : - hwnd_(hWnd), data_(*data) { - HDC hdcScreen = GetDC(NULL); - hdc_ = CreateCompatibleDC(hdcScreen); - ReleaseDC(NULL, hdcScreen); + HWND hwnd, shared_ptr* data) : + hwnd_(hwnd), data_(*data) { + HDC hdc_screen = GetDC(NULL); + hdc_ = CreateCompatibleDC(hdc_screen); + ReleaseDC(NULL, hdc_screen); } DesktopNotificationController::Toast::~Toast() { @@ -207,47 +204,47 @@ DesktopNotificationController::Toast::~Toast() { if (scaled_image_) DeleteBitmap(scaled_image_); } -void DesktopNotificationController::Toast::Register(HINSTANCE hInstance) { +void DesktopNotificationController::Toast::Register(HINSTANCE hinstance) { WNDCLASSEX wc = { sizeof(wc) }; wc.lpfnWndProc = &Toast::WndProc; wc.lpszClassName = class_name_; wc.cbWndExtra = sizeof(Toast*); - wc.hInstance = hInstance; + wc.hInstance = hinstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClassEx(&wc); } LRESULT DesktopNotificationController::Toast::WndProc( - HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { switch (message) { case WM_CREATE: { - auto& cs = reinterpret_cast(lParam); + auto& cs = reinterpret_cast(lparam); auto data = static_cast*>(cs->lpCreateParams); - auto inst = new Toast(hWnd, data); - SetWindowLongPtr(hWnd, 0, (LONG_PTR)inst); + auto inst = new Toast(hwnd, data); + SetWindowLongPtr(hwnd, 0, (LONG_PTR)inst); } break; case WM_NCDESTROY: - delete Get(hWnd); - SetWindowLongPtr(hWnd, 0, 0); + delete Get(hwnd); + SetWindowLongPtr(hwnd, 0, 0); return 0; case WM_MOUSEACTIVATE: return MA_NOACTIVATE; case WM_TIMER: - if (wParam == TimerID_AutoDismiss) { - Get(hWnd)->AutoDismiss(); + if (wparam == TimerID_AutoDismiss) { + Get(hwnd)->AutoDismiss(); } return 0; case WM_LBUTTONDOWN: { - auto inst = Get(hWnd); + auto inst = Get(hwnd); inst->Dismiss(); @@ -261,15 +258,15 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_MOUSEMOVE: { - auto inst = Get(hWnd); + auto inst = Get(hwnd); if (!inst->is_highlighted_) { inst->is_highlighted_ = true; - TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, hWnd }; + TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, hwnd }; TrackMouseEvent(&tme); } - POINT cursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; + POINT cursor = { GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) }; inst->is_close_hot_ = (PtInRect(&inst->close_button_rect_, cursor) != FALSE); @@ -282,7 +279,7 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_MOUSELEAVE: { - auto inst = Get(hWnd); + auto inst = Get(hwnd); inst->is_highlighted_ = false; inst->is_close_hot_ = false; inst->UpdateContents(); @@ -297,80 +294,80 @@ LRESULT DesktopNotificationController::Toast::WndProc( case WM_WINDOWPOSCHANGED: { - auto& wp = reinterpret_cast(lParam); + auto& wp = reinterpret_cast(lparam); if (wp->flags & SWP_HIDEWINDOW) { - if (!IsWindowVisible(hWnd)) - Get(hWnd)->is_highlighted_ = false; + if (!IsWindowVisible(hwnd)) + Get(hwnd)->is_highlighted_ = false; } } break; } - return DefWindowProc(hWnd, message, wParam, lParam); + return DefWindowProc(hwnd, message, wparam, lparam); } HWND DesktopNotificationController::Toast::Create( - HINSTANCE hInstance, shared_ptr& data) { + HINSTANCE hinstance, shared_ptr& data) { return CreateWindowEx(WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST, class_name_, nullptr, WS_POPUP, 0, 0, 0, 0, - NULL, NULL, hInstance, &data); + NULL, NULL, hinstance, &data); } void DesktopNotificationController::Toast::Draw() { const COLORREF accent = GetAccentColor(); - COLORREF backColor; + COLORREF back_color; { // base background color is 2/3 of accent // highlighted adds a bit of intensity to every channel int h = is_highlighted_ ? (0xff / 20) : 0; - backColor = RGB(min(0xff, (GetRValue(accent) * 2 / 3) + h), - min(0xff, (GetGValue(accent) * 2 / 3) + h), - min(0xff, (GetBValue(accent) * 2 / 3) + h)); + back_color = RGB(min(0xff, (GetRValue(accent) * 2 / 3) + h), + min(0xff, (GetGValue(accent) * 2 / 3) + h), + min(0xff, (GetBValue(accent) * 2 / 3) + h)); } - const float backLuma = - (GetRValue(backColor) * 0.299f / 255) + - (GetGValue(backColor) * 0.587f / 255) + - (GetBValue(backColor) * 0.114f / 255); + const float back_luma = + (GetRValue(back_color) * 0.299f / 255) + + (GetGValue(back_color) * 0.587f / 255) + + (GetBValue(back_color) * 0.114f / 255); - const struct { float r, g, b; } backF = { - GetRValue(backColor) / 255.0f, - GetGValue(backColor) / 255.0f, - GetBValue(backColor) / 255.0f, + const struct { float r, g, b; } back_f = { + GetRValue(back_color) / 255.0f, + GetGValue(back_color) / 255.0f, + GetBValue(back_color) / 255.0f, }; - COLORREF foreColor, dimmedColor; + COLORREF fore_color, dimmed_color; { // based on the lightness of background, we draw foreground in light // or dark shades of gray blended onto the background with slight // transparency to avoid sharp contrast constexpr float alpha = 0.9f; - constexpr float intensityLight[] = { (1.0f * alpha), (0.8f * alpha) }; - constexpr float intensityDark[] = { (0.1f * alpha), (0.3f * alpha) }; + constexpr float intensity_light[] = { (1.0f * alpha), (0.8f * alpha) }; + constexpr float intensity_dark[] = { (0.1f * alpha), (0.3f * alpha) }; // select foreground intensity values (light or dark) - auto& i = (backLuma < 0.6f) ? intensityLight : intensityDark; + auto& i = (back_luma < 0.6f) ? intensity_light : intensity_dark; float r, g, b; - r = i[0] + backF.r * (1 - alpha); - g = i[0] + backF.g * (1 - alpha); - b = i[0] + backF.b * (1 - alpha); - foreColor = RGB(r * 0xff, g * 0xff, b * 0xff); + r = i[0] + back_f.r * (1 - alpha); + g = i[0] + back_f.g * (1 - alpha); + b = i[0] + back_f.b * (1 - alpha); + fore_color = RGB(r * 0xff, g * 0xff, b * 0xff); - r = i[1] + backF.r * (1 - alpha); - g = i[1] + backF.g * (1 - alpha); - b = i[1] + backF.b * (1 - alpha); - dimmedColor = RGB(r * 0xff, g * 0xff, b * 0xff); + r = i[1] + back_f.r * (1 - alpha); + g = i[1] + back_f.g * (1 - alpha); + b = i[1] + back_f.b * (1 - alpha); + dimmed_color = RGB(r * 0xff, g * 0xff, b * 0xff); } // Draw background { - auto brush = CreateSolidBrush(backColor); + auto brush = CreateSolidBrush(back_color); RECT rc = { 0, 0, toast_size_.cx, toast_size_.cy }; FillRect(hdc_, &rc, brush); @@ -381,24 +378,24 @@ void DesktopNotificationController::Toast::Draw() { SetBkMode(hdc_, TRANSPARENT); const auto close = L'\x2715'; - auto captionFont = data_->controller->GetCaptionFont(); - auto bodyFont = data_->controller->GetBodyFont(); + auto caption_font = data_->controller->GetCaptionFont(); + auto body_font = data_->controller->GetBodyFont(); - TEXTMETRIC tmCap; - SelectFont(hdc_, captionFont); - GetTextMetrics(hdc_, &tmCap); + TEXTMETRIC tm_cap; + SelectFont(hdc_, caption_font); + GetTextMetrics(hdc_, &tm_cap); - auto textOffsetX = margin_.cx; + auto text_offset_x = margin_.cx; - BITMAP imageInfo = {}; + BITMAP image_info = {}; if (scaled_image_) { - GetObject(scaled_image_, sizeof(imageInfo), &imageInfo); + GetObject(scaled_image_, sizeof(image_info), &image_info); - textOffsetX += margin_.cx + imageInfo.bmWidth; + text_offset_x += margin_.cx + image_info.bmWidth; } // calculate close button rect - POINT closePos; + POINT close_pos; { SIZE extent = {}; GetTextExtentPoint32W(hdc_, &close, 1, &extent); @@ -406,37 +403,37 @@ void DesktopNotificationController::Toast::Draw() { close_button_rect_.right = toast_size_.cx; close_button_rect_.top = 0; - closePos.x = close_button_rect_.right - margin_.cy - extent.cx; - closePos.y = close_button_rect_.top + margin_.cy; + close_pos.x = close_button_rect_.right - margin_.cy - extent.cx; + close_pos.y = close_button_rect_.top + margin_.cy; - close_button_rect_.left = closePos.x - margin_.cy; - close_button_rect_.bottom = closePos.y + extent.cy + margin_.cy; + close_button_rect_.left = close_pos.x - margin_.cy; + close_button_rect_.bottom = close_pos.y + extent.cy + margin_.cy; } // image if (scaled_image_) { - HDC hdcImage = CreateCompatibleDC(NULL); - SelectBitmap(hdcImage, scaled_image_); + HDC hdc_image = CreateCompatibleDC(NULL); + SelectBitmap(hdc_image, scaled_image_); BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; AlphaBlend(hdc_, margin_.cx, margin_.cy, - imageInfo.bmWidth, imageInfo.bmHeight, - hdcImage, 0, 0, - imageInfo.bmWidth, imageInfo.bmHeight, + image_info.bmWidth, image_info.bmHeight, + hdc_image, 0, 0, + image_info.bmWidth, image_info.bmHeight, blend); - DeleteDC(hdcImage); + DeleteDC(hdc_image); } // caption { RECT rc = { - textOffsetX, + text_offset_x, margin_.cy, close_button_rect_.left, toast_size_.cy }; - SelectFont(hdc_, captionFont); - SetTextColor(hdc_, foreColor); + SelectFont(hdc_, caption_font); + SetTextColor(hdc_, fore_color); DrawText(hdc_, data_->caption.data(), (UINT)data_->caption.length(), &rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX); } @@ -444,14 +441,14 @@ void DesktopNotificationController::Toast::Draw() { // body text if (!data_->body_text.empty()) { RECT rc = { - textOffsetX, - 2 * margin_.cy + tmCap.tmAscent, + text_offset_x, + 2 * margin_.cy + tm_cap.tmAscent, toast_size_.cx - margin_.cx, toast_size_.cy - margin_.cy }; - SelectFont(hdc_, bodyFont); - SetTextColor(hdc_, dimmedColor); + SelectFont(hdc_, body_font); + SetTextColor(hdc_, dimmed_color); DrawText(hdc_, data_->body_text.data(), (UINT)data_->body_text.length(), &rc, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | @@ -460,9 +457,9 @@ void DesktopNotificationController::Toast::Draw() { // close button { - SelectFont(hdc_, captionFont); - SetTextColor(hdc_, is_close_hot_ ? foreColor : dimmedColor); - ExtTextOut(hdc_, closePos.x, closePos.y, 0, nullptr, + SelectFont(hdc_, caption_font); + SetTextColor(hdc_, is_close_hot_ ? fore_color : dimmed_color); + ExtTextOut(hdc_, close_pos.x, close_pos.y, 0, nullptr, &close, 1, nullptr); } @@ -479,87 +476,90 @@ bool DesktopNotificationController::Toast::IsRedrawNeeded() const { void DesktopNotificationController::Toast::UpdateBufferSize() { if (hdc_) { - SIZE newSize; + SIZE new_size; { - TEXTMETRIC tmCap = {}; + TEXTMETRIC tm_cap = {}; HFONT font = data_->controller->GetCaptionFont(); if (font) { SelectFont(hdc_, font); - if (!GetTextMetrics(hdc_, &tmCap)) return; + if (!GetTextMetrics(hdc_, &tm_cap)) return; } - TEXTMETRIC tmBody = {}; + TEXTMETRIC tm_body = {}; font = data_->controller->GetBodyFont(); if (font) { SelectFont(hdc_, font); - if (!GetTextMetrics(hdc_, &tmBody)) return; + if (!GetTextMetrics(hdc_, &tm_body)) return; } - this->margin_ = { tmCap.tmAveCharWidth * 2, tmCap.tmAscent / 2 }; + this->margin_ = { tm_cap.tmAveCharWidth * 2, tm_cap.tmAscent / 2 }; - newSize.cx = margin_.cx + (32 * tmCap.tmAveCharWidth) + margin_.cx; - newSize.cy = margin_.cy + (tmCap.tmHeight) + margin_.cy; + new_size.cx = + margin_.cx + (32 * tm_cap.tmAveCharWidth) + margin_.cx; + new_size.cy = + margin_.cy + (tm_cap.tmHeight) + margin_.cy; if (!data_->body_text.empty()) - newSize.cy += margin_.cy + (3 * tmBody.tmHeight); + new_size.cy += margin_.cy + (3 * tm_body.tmHeight); if (data_->image) { BITMAP bm; if (GetObject(data_->image, sizeof(bm), &bm)) { // cap the image size - const int maxDimSize = 80; + const int max_dim_size = 80; auto width = bm.bmWidth; auto height = bm.bmHeight; if (width < height) { - if (height > maxDimSize) { - width = width * maxDimSize / height; - height = maxDimSize; + if (height > max_dim_size) { + width = width * max_dim_size / height; + height = max_dim_size; } } else { - if (width > maxDimSize) { - height = height * maxDimSize / width; - width = maxDimSize; + if (width > max_dim_size) { + height = height * max_dim_size / width; + width = max_dim_size; } } ScreenMetrics scr; - SIZE imageDrawSize = { scr.X(width), scr.Y(height) }; + SIZE image_draw_size = { scr.X(width), scr.Y(height) }; - newSize.cx += imageDrawSize.cx + margin_.cx; + new_size.cx += image_draw_size.cx + margin_.cx; - auto heightWithImage = - margin_.cy + (imageDrawSize.cy) + margin_.cy; - if (newSize.cy < heightWithImage) - newSize.cy = heightWithImage; + auto height_with_image = + margin_.cy + (image_draw_size.cy) + margin_.cy; - UpdateScaledImage(imageDrawSize); + if (new_size.cy < height_with_image) + new_size.cy = height_with_image; + + UpdateScaledImage(image_draw_size); } } } - if (newSize.cx != this->toast_size_.cx || - newSize.cy != this->toast_size_.cy) { - HDC hdcScreen = GetDC(NULL); - auto newBitmap = CreateCompatibleBitmap(hdcScreen, - newSize.cx, newSize.cy); - ReleaseDC(NULL, hdcScreen); + if (new_size.cx != this->toast_size_.cx || + new_size.cy != this->toast_size_.cy) { + HDC hdc_screen = GetDC(NULL); + auto new_bitmap = CreateCompatibleBitmap(hdc_screen, + new_size.cx, new_size.cy); + ReleaseDC(NULL, hdc_screen); - if (newBitmap) { - if (SelectBitmap(hdc_, newBitmap)) { + if (new_bitmap) { + if (SelectBitmap(hdc_, new_bitmap)) { RECT dirty1 = {}, dirty2 = {}; - if (toast_size_.cx < newSize.cx) { + if (toast_size_.cx < new_size.cx) { dirty1 = { toast_size_.cx, 0, - newSize.cx, toast_size_.cy }; + new_size.cx, toast_size_.cy }; } - if (toast_size_.cy < newSize.cy) { + if (toast_size_.cy < new_size.cy) { dirty2 = { 0, toast_size_.cy, - newSize.cx, newSize.cy }; + new_size.cx, new_size.cy }; } if (this->bitmap_) DeleteBitmap(this->bitmap_); - this->bitmap_ = newBitmap; - this->toast_size_ = newSize; + this->bitmap_ = new_bitmap; + this->toast_size_ = new_size; Invalidate(); @@ -589,7 +589,7 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { return; } - DeleteBitmap(newBitmap); + DeleteBitmap(new_bitmap); } } } @@ -598,8 +598,8 @@ void DesktopNotificationController::Toast::UpdateBufferSize() { void DesktopNotificationController::Toast::UpdateScaledImage(const SIZE& size) { BITMAP bm; if (!GetObject(scaled_image_, sizeof(bm), &bm) || - bm.bmWidth != size.cx || - bm.bmHeight != size.cy) { + bm.bmWidth != size.cx || + bm.bmHeight != size.cy) { if (scaled_image_) DeleteBitmap(scaled_image_); scaled_image_ = StretchBitmap(data_->image, size.cx, size.cy); } @@ -680,7 +680,7 @@ HDWP DesktopNotificationController::Toast::Animate( if (IsRedrawNeeded()) Draw(); - POINT srcOrigin = { 0, 0 }; + POINT src_origin = { 0, 0 }; UPDATELAYEREDWINDOWINFO ulw; ulw.cbSize = sizeof(ulw); @@ -688,7 +688,7 @@ HDWP DesktopNotificationController::Toast::Animate( ulw.pptDst = nullptr; ulw.psize = nullptr; ulw.hdcSrc = hdc_; - ulw.pptSrc = &srcOrigin; + ulw.pptSrc = &src_origin; ulw.crKey = 0; ulw.pblend = nullptr; ulw.dwFlags = 0; @@ -700,41 +700,41 @@ HDWP DesktopNotificationController::Toast::Animate( UINT dwpFlags = SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOREDRAW | SWP_NOCOPYBITS; - auto easeInPos = AnimateEaseIn(); - auto easeOutPos = AnimateEaseOut(); - auto stackCollapsePos = AnimateStackCollapse(); + auto ease_in_pos = AnimateEaseIn(); + auto ease_out_pos = AnimateEaseOut(); + auto stack_collapse_pos = AnimateStackCollapse(); - auto yOffset = (vertical_pos_target_ - vertical_pos_) * stackCollapsePos; + auto y_offset = (vertical_pos_target_ - vertical_pos_) * stack_collapse_pos; - size.cx = static_cast(toast_size_.cx * easeInPos); + size.cx = static_cast(toast_size_.cx * ease_in_pos); size.cy = toast_size_.cy; pt.x = origin.x - size.cx; - pt.y = static_cast(origin.y - vertical_pos_ - yOffset - size.cy); + pt.y = static_cast(origin.y - vertical_pos_ - y_offset - size.cy); ulw.pptDst = &pt; ulw.psize = &size; - if (ease_in_active_ && easeInPos == 1.0f) { + if (ease_in_active_ && ease_in_pos == 1.0f) { ease_in_active_ = false; ScheduleDismissal(); } - this->ease_in_pos_ = easeInPos; - this->stack_collapse_pos_ = stackCollapsePos; + this->ease_in_pos_ = ease_in_pos; + this->stack_collapse_pos_ = stack_collapse_pos; - if (easeOutPos != this->ease_out_pos_) { + if (ease_out_pos != this->ease_out_pos_) { blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; - blend.SourceConstantAlpha = (BYTE)(255 * (1.0f - easeOutPos)); + blend.SourceConstantAlpha = (BYTE)(255 * (1.0f - ease_out_pos)); blend.AlphaFormat = 0; ulw.pblend = &blend; ulw.dwFlags = ULW_ALPHA; - this->ease_out_pos_ = easeOutPos; + this->ease_out_pos_ = ease_out_pos; - if (easeOutPos == 1.0f) { + if (ease_out_pos == 1.0f) { ease_out_active_ = false; dwpFlags &= ~SWP_SHOWWINDOW; @@ -742,7 +742,7 @@ HDWP DesktopNotificationController::Toast::Animate( } } - if (stackCollapsePos == 1.0f) { + if (stack_collapse_pos == 1.0f) { vertical_pos_ = vertical_pos_target_; } @@ -751,7 +751,7 @@ HDWP DesktopNotificationController::Toast::Animate( // ULWI fails, which can happen when one of the dimensions is zero (e.g. // at the beginning of ease-in). - auto ulwResult = UpdateLayeredWindowIndirect(hwnd_, &ulw); + auto ulw_result = UpdateLayeredWindowIndirect(hwnd_, &ulw); hdwp = DeferWindowPos(hdwp, hwnd_, HWND_TOPMOST, pt.x, pt.y, size.cx, size.cy, dwpFlags); return hdwp; diff --git a/brightray/browser/win/win32_desktop_notifications/toast.h b/brightray/browser/win/win32_desktop_notifications/toast.h index 5700341f7e4..bb9bd9fa4ae 100644 --- a/brightray/browser/win/win32_desktop_notifications/toast.h +++ b/brightray/browser/win/win32_desktop_notifications/toast.h @@ -5,15 +5,15 @@ namespace brightray { class DesktopNotificationController::Toast { public: - static void Register(HINSTANCE hInstance); - static HWND Create(HINSTANCE hInstance, + static void Register(HINSTANCE hinstance); + static HWND Create(HINSTANCE hinstance, std::shared_ptr& data); - static Toast* Get(HWND hWnd) { - return reinterpret_cast(GetWindowLongPtr(hWnd, 0)); + static Toast* Get(HWND hwnd) { + return reinterpret_cast(GetWindowLongPtr(hwnd, 0)); } - static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, - WPARAM wParam, LPARAM lParam); + static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, + WPARAM wparam, LPARAM lparam); const std::shared_ptr& GetNotification() const { return data_; @@ -45,7 +45,7 @@ class DesktopNotificationController::Toast { TimerID_AutoDismiss = 1 }; - Toast(HWND hWnd, std::shared_ptr* data); + Toast(HWND hwnd, std::shared_ptr* data); ~Toast(); void UpdateBufferSize(); From fbe3b27409fb855bdd19986c45ebfed7562abc07 Mon Sep 17 00:00:00 2001 From: Ales Pergl Date: Wed, 5 Apr 2017 14:27:52 +0200 Subject: [PATCH 20/20] Addressed review feedback --- brightray/browser/notification_presenter.cc | 1 - brightray/browser/win/notification_presenter_win.cc | 2 -- 2 files changed, 3 deletions(-) diff --git a/brightray/browser/notification_presenter.cc b/brightray/browser/notification_presenter.cc index 39c3efd7ddf..30ef3b16b4b 100644 --- a/brightray/browser/notification_presenter.cc +++ b/brightray/browser/notification_presenter.cc @@ -19,7 +19,6 @@ NotificationPresenter::~NotificationPresenter() { base::WeakPtr NotificationPresenter::CreateNotification( NotificationDelegate* delegate) { Notification* notification = CreateNotificationObject(delegate); - if (!notification) return {}; notifications_.insert(notification); return notification->GetWeakPtr(); } diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index 42819eada71..930775c792b 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -38,8 +38,6 @@ bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) { // static NotificationPresenter* NotificationPresenter::Create() { auto version = base::win::GetVersion(); - if (version < base::win::VERSION_WIN7) - return nullptr; if (version < base::win::VERSION_WIN8) return new NotificationPresenterWin7; if (!WindowsToastNotification::Initialize())