widget/windows/TaskbarPreview.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* vim: se cin sw=2 ts=2 et : */
michael@0 2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 3 *
michael@0 4 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "TaskbarPreview.h"
michael@0 9 #include <nsITaskbarPreviewController.h>
michael@0 10 #include <windows.h>
michael@0 11
michael@0 12 #include <nsError.h>
michael@0 13 #include <nsCOMPtr.h>
michael@0 14 #include <nsIWidget.h>
michael@0 15 #include <nsIBaseWindow.h>
michael@0 16 #include <nsIObserverService.h>
michael@0 17 #include <nsServiceManagerUtils.h>
michael@0 18
michael@0 19 #include "nsUXThemeData.h"
michael@0 20 #include "nsWindow.h"
michael@0 21 #include "nsAppShell.h"
michael@0 22 #include "TaskbarPreviewButton.h"
michael@0 23 #include "WinUtils.h"
michael@0 24 #include "gfxWindowsPlatform.h"
michael@0 25
michael@0 26 #include <nsIBaseWindow.h>
michael@0 27 #include <nsICanvasRenderingContextInternal.h>
michael@0 28 #include "mozilla/dom/CanvasRenderingContext2D.h"
michael@0 29 #include <imgIContainer.h>
michael@0 30 #include <nsIDocShell.h>
michael@0 31
michael@0 32 #include "mozilla/Telemetry.h"
michael@0 33
michael@0 34 // Defined in dwmapi in a header that needs a higher numbered _WINNT #define
michael@0 35 #define DWM_SIT_DISPLAYFRAME 0x1
michael@0 36
michael@0 37 namespace mozilla {
michael@0 38 namespace widget {
michael@0 39
michael@0 40 namespace {
michael@0 41
michael@0 42 // Shared by all TaskbarPreviews to avoid the expensive creation process.
michael@0 43 // Manually refcounted (see gInstCount) by the ctor and dtor of TaskbarPreview.
michael@0 44 // This is done because static constructors aren't allowed for perf reasons.
michael@0 45 dom::CanvasRenderingContext2D* gCtx = nullptr;
michael@0 46 // Used in tracking the number of previews. Used in freeing
michael@0 47 // the static 2d rendering context on shutdown.
michael@0 48 uint32_t gInstCount = 0;
michael@0 49
michael@0 50 /* Helper method to lazily create a canvas rendering context and associate a given
michael@0 51 * surface with it.
michael@0 52 *
michael@0 53 * @param shell The docShell used by the canvas context for text settings and other
michael@0 54 * misc things.
michael@0 55 * @param surface The gfxSurface backing the context
michael@0 56 * @param width The width of the given surface
michael@0 57 * @param height The height of the given surface
michael@0 58 */
michael@0 59 nsresult
michael@0 60 GetRenderingContext(nsIDocShell *shell, gfxASurface *surface,
michael@0 61 uint32_t width, uint32_t height) {
michael@0 62 if (!gCtx) {
michael@0 63 // create the canvas rendering context
michael@0 64 Telemetry::Accumulate(Telemetry::CANVAS_2D_USED, 1);
michael@0 65 gCtx = new mozilla::dom::CanvasRenderingContext2D();
michael@0 66 NS_ADDREF(gCtx);
michael@0 67 }
michael@0 68
michael@0 69 // Set the surface we'll use to render.
michael@0 70 return gCtx->InitializeWithSurface(shell, surface, width, height);
michael@0 71 }
michael@0 72
michael@0 73 /* Helper method for freeing surface resources associated with the rendering context.
michael@0 74 */
michael@0 75 void
michael@0 76 ResetRenderingContext() {
michael@0 77 if (!gCtx)
michael@0 78 return;
michael@0 79
michael@0 80 if (NS_FAILED(gCtx->Reset())) {
michael@0 81 NS_RELEASE(gCtx);
michael@0 82 gCtx = nullptr;
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 }
michael@0 87
michael@0 88 TaskbarPreview::TaskbarPreview(ITaskbarList4 *aTaskbar, nsITaskbarPreviewController *aController, HWND aHWND, nsIDocShell *aShell)
michael@0 89 : mTaskbar(aTaskbar),
michael@0 90 mController(aController),
michael@0 91 mWnd(aHWND),
michael@0 92 mVisible(false),
michael@0 93 mDocShell(do_GetWeakReference(aShell))
michael@0 94 {
michael@0 95 // TaskbarPreview may outlive the WinTaskbar that created it
michael@0 96 ::CoInitialize(nullptr);
michael@0 97
michael@0 98 gInstCount++;
michael@0 99
michael@0 100 WindowHook &hook = GetWindowHook();
michael@0 101 hook.AddMonitor(WM_DESTROY, MainWindowHook, this);
michael@0 102 }
michael@0 103
michael@0 104 TaskbarPreview::~TaskbarPreview() {
michael@0 105 // Avoid dangling pointer
michael@0 106 if (sActivePreview == this)
michael@0 107 sActivePreview = nullptr;
michael@0 108
michael@0 109 // Our subclass should have invoked DetachFromNSWindow already.
michael@0 110 NS_ASSERTION(!mWnd, "TaskbarPreview::DetachFromNSWindow was not called before destruction");
michael@0 111
michael@0 112 // Make sure to release before potentially uninitializing COM
michael@0 113 mTaskbar = nullptr;
michael@0 114
michael@0 115 if (--gInstCount == 0)
michael@0 116 NS_IF_RELEASE(gCtx);
michael@0 117
michael@0 118 ::CoUninitialize();
michael@0 119 }
michael@0 120
michael@0 121 NS_IMETHODIMP
michael@0 122 TaskbarPreview::SetController(nsITaskbarPreviewController *aController) {
michael@0 123 NS_ENSURE_ARG(aController);
michael@0 124
michael@0 125 mController = aController;
michael@0 126 return NS_OK;
michael@0 127 }
michael@0 128
michael@0 129 NS_IMETHODIMP
michael@0 130 TaskbarPreview::GetController(nsITaskbarPreviewController **aController) {
michael@0 131 NS_ADDREF(*aController = mController);
michael@0 132 return NS_OK;
michael@0 133 }
michael@0 134
michael@0 135 NS_IMETHODIMP
michael@0 136 TaskbarPreview::GetTooltip(nsAString &aTooltip) {
michael@0 137 aTooltip = mTooltip;
michael@0 138 return NS_OK;
michael@0 139 }
michael@0 140
michael@0 141 NS_IMETHODIMP
michael@0 142 TaskbarPreview::SetTooltip(const nsAString &aTooltip) {
michael@0 143 mTooltip = aTooltip;
michael@0 144 return CanMakeTaskbarCalls() ? UpdateTooltip() : NS_OK;
michael@0 145 }
michael@0 146
michael@0 147 NS_IMETHODIMP
michael@0 148 TaskbarPreview::SetVisible(bool visible) {
michael@0 149 if (mVisible == visible) return NS_OK;
michael@0 150 mVisible = visible;
michael@0 151
michael@0 152 // If the nsWindow has already been destroyed but the caller is still trying
michael@0 153 // to use it then just pretend that everything succeeded. The caller doesn't
michael@0 154 // actually have a way to detect this since it's the same case as when we
michael@0 155 // CanMakeTaskbarCalls returns false.
michael@0 156 if (!mWnd)
michael@0 157 return NS_OK;
michael@0 158
michael@0 159 return visible ? Enable() : Disable();
michael@0 160 }
michael@0 161
michael@0 162 NS_IMETHODIMP
michael@0 163 TaskbarPreview::GetVisible(bool *visible) {
michael@0 164 *visible = mVisible;
michael@0 165 return NS_OK;
michael@0 166 }
michael@0 167
michael@0 168 NS_IMETHODIMP
michael@0 169 TaskbarPreview::SetActive(bool active) {
michael@0 170 if (active)
michael@0 171 sActivePreview = this;
michael@0 172 else if (sActivePreview == this)
michael@0 173 sActivePreview = nullptr;
michael@0 174
michael@0 175 return CanMakeTaskbarCalls() ? ShowActive(active) : NS_OK;
michael@0 176 }
michael@0 177
michael@0 178 NS_IMETHODIMP
michael@0 179 TaskbarPreview::GetActive(bool *active) {
michael@0 180 *active = sActivePreview == this;
michael@0 181 return NS_OK;
michael@0 182 }
michael@0 183
michael@0 184 NS_IMETHODIMP
michael@0 185 TaskbarPreview::Invalidate() {
michael@0 186 if (!mVisible)
michael@0 187 return NS_ERROR_FAILURE;
michael@0 188
michael@0 189 // DWM Composition is required for previews
michael@0 190 if (!nsUXThemeData::CheckForCompositor())
michael@0 191 return NS_OK;
michael@0 192
michael@0 193 HWND previewWindow = PreviewWindow();
michael@0 194 return FAILED(WinUtils::dwmInvalidateIconicBitmapsPtr(previewWindow))
michael@0 195 ? NS_ERROR_FAILURE
michael@0 196 : NS_OK;
michael@0 197 }
michael@0 198
michael@0 199 nsresult
michael@0 200 TaskbarPreview::UpdateTaskbarProperties() {
michael@0 201 nsresult rv = UpdateTooltip();
michael@0 202
michael@0 203 // If we are the active preview and our window is the active window, restore
michael@0 204 // our active state - otherwise some other non-preview window is now active
michael@0 205 // and should be displayed as so.
michael@0 206 if (sActivePreview == this) {
michael@0 207 if (mWnd == ::GetActiveWindow()) {
michael@0 208 nsresult rvActive = ShowActive(true);
michael@0 209 if (NS_FAILED(rvActive))
michael@0 210 rv = rvActive;
michael@0 211 } else {
michael@0 212 sActivePreview = nullptr;
michael@0 213 }
michael@0 214 }
michael@0 215 return rv;
michael@0 216 }
michael@0 217
michael@0 218 nsresult
michael@0 219 TaskbarPreview::Enable() {
michael@0 220 nsresult rv = NS_OK;
michael@0 221 if (CanMakeTaskbarCalls()) {
michael@0 222 rv = UpdateTaskbarProperties();
michael@0 223 } else {
michael@0 224 WindowHook &hook = GetWindowHook();
michael@0 225 hook.AddMonitor(nsAppShell::GetTaskbarButtonCreatedMessage(), MainWindowHook, this);
michael@0 226 }
michael@0 227 return rv;
michael@0 228 }
michael@0 229
michael@0 230 nsresult
michael@0 231 TaskbarPreview::Disable() {
michael@0 232 WindowHook &hook = GetWindowHook();
michael@0 233 (void) hook.RemoveMonitor(nsAppShell::GetTaskbarButtonCreatedMessage(), MainWindowHook, this);
michael@0 234
michael@0 235 return NS_OK;
michael@0 236 }
michael@0 237
michael@0 238 bool
michael@0 239 TaskbarPreview::IsWindowAvailable() const {
michael@0 240 if (mWnd) {
michael@0 241 nsWindow* win = WinUtils::GetNSWindowPtr(mWnd);
michael@0 242 if(win && !win->Destroyed()) {
michael@0 243 return true;
michael@0 244 }
michael@0 245 }
michael@0 246 return false;
michael@0 247 }
michael@0 248
michael@0 249 void
michael@0 250 TaskbarPreview::DetachFromNSWindow() {
michael@0 251 WindowHook &hook = GetWindowHook();
michael@0 252 hook.RemoveMonitor(WM_DESTROY, MainWindowHook, this);
michael@0 253 mWnd = nullptr;
michael@0 254 }
michael@0 255
michael@0 256 LRESULT
michael@0 257 TaskbarPreview::WndProc(UINT nMsg, WPARAM wParam, LPARAM lParam) {
michael@0 258 switch (nMsg) {
michael@0 259 case WM_DWMSENDICONICTHUMBNAIL:
michael@0 260 {
michael@0 261 uint32_t width = HIWORD(lParam);
michael@0 262 uint32_t height = LOWORD(lParam);
michael@0 263 float aspectRatio = width/float(height);
michael@0 264
michael@0 265 nsresult rv;
michael@0 266 float preferredAspectRatio;
michael@0 267 rv = mController->GetThumbnailAspectRatio(&preferredAspectRatio);
michael@0 268 if (NS_FAILED(rv))
michael@0 269 break;
michael@0 270
michael@0 271 uint32_t thumbnailWidth = width;
michael@0 272 uint32_t thumbnailHeight = height;
michael@0 273
michael@0 274 if (aspectRatio > preferredAspectRatio) {
michael@0 275 thumbnailWidth = uint32_t(thumbnailHeight * preferredAspectRatio);
michael@0 276 } else {
michael@0 277 thumbnailHeight = uint32_t(thumbnailWidth / preferredAspectRatio);
michael@0 278 }
michael@0 279
michael@0 280 DrawBitmap(thumbnailWidth, thumbnailHeight, false);
michael@0 281 }
michael@0 282 break;
michael@0 283 case WM_DWMSENDICONICLIVEPREVIEWBITMAP:
michael@0 284 {
michael@0 285 uint32_t width, height;
michael@0 286 nsresult rv;
michael@0 287 rv = mController->GetWidth(&width);
michael@0 288 if (NS_FAILED(rv))
michael@0 289 break;
michael@0 290 rv = mController->GetHeight(&height);
michael@0 291 if (NS_FAILED(rv))
michael@0 292 break;
michael@0 293
michael@0 294 double scale = nsIWidget::DefaultScaleOverride();
michael@0 295 if (scale <= 0.0)
michael@0 296 scale = gfxWindowsPlatform::GetPlatform()->GetDPIScale();
michael@0 297
michael@0 298 DrawBitmap(NSToIntRound(scale * width), NSToIntRound(scale * height), true);
michael@0 299 }
michael@0 300 break;
michael@0 301 }
michael@0 302 return ::DefWindowProcW(PreviewWindow(), nMsg, wParam, lParam);
michael@0 303 }
michael@0 304
michael@0 305 bool
michael@0 306 TaskbarPreview::CanMakeTaskbarCalls() {
michael@0 307 // If the nsWindow has already been destroyed and we know it but our caller
michael@0 308 // clearly doesn't so we can't make any calls.
michael@0 309 if (!mWnd)
michael@0 310 return false;
michael@0 311 // Certain functions like SetTabOrder seem to require a visible window. During
michael@0 312 // window close, the window seems to be hidden before being destroyed.
michael@0 313 if (!::IsWindowVisible(mWnd))
michael@0 314 return false;
michael@0 315 if (mVisible) {
michael@0 316 nsWindow *window = WinUtils::GetNSWindowPtr(mWnd);
michael@0 317 NS_ASSERTION(window, "Could not get nsWindow from HWND");
michael@0 318 return window->HasTaskbarIconBeenCreated();
michael@0 319 }
michael@0 320 return false;
michael@0 321 }
michael@0 322
michael@0 323 WindowHook&
michael@0 324 TaskbarPreview::GetWindowHook() {
michael@0 325 nsWindow *window = WinUtils::GetNSWindowPtr(mWnd);
michael@0 326 NS_ASSERTION(window, "Cannot use taskbar previews in an embedded context!");
michael@0 327
michael@0 328 return window->GetWindowHook();
michael@0 329 }
michael@0 330
michael@0 331 void
michael@0 332 TaskbarPreview::EnableCustomDrawing(HWND aHWND, bool aEnable) {
michael@0 333 BOOL enabled = aEnable;
michael@0 334 WinUtils::dwmSetWindowAttributePtr(
michael@0 335 aHWND,
michael@0 336 DWMWA_FORCE_ICONIC_REPRESENTATION,
michael@0 337 &enabled,
michael@0 338 sizeof(enabled));
michael@0 339
michael@0 340 WinUtils::dwmSetWindowAttributePtr(
michael@0 341 aHWND,
michael@0 342 DWMWA_HAS_ICONIC_BITMAP,
michael@0 343 &enabled,
michael@0 344 sizeof(enabled));
michael@0 345 }
michael@0 346
michael@0 347
michael@0 348 nsresult
michael@0 349 TaskbarPreview::UpdateTooltip() {
michael@0 350 NS_ASSERTION(CanMakeTaskbarCalls() && mVisible, "UpdateTooltip called on invisible tab preview");
michael@0 351
michael@0 352 if (FAILED(mTaskbar->SetThumbnailTooltip(PreviewWindow(), mTooltip.get())))
michael@0 353 return NS_ERROR_FAILURE;
michael@0 354 return NS_OK;
michael@0 355 }
michael@0 356
michael@0 357 void
michael@0 358 TaskbarPreview::DrawBitmap(uint32_t width, uint32_t height, bool isPreview) {
michael@0 359 nsresult rv;
michael@0 360 nsRefPtr<gfxWindowsSurface> surface = new gfxWindowsSurface(gfxIntSize(width, height), gfxImageFormat::ARGB32);
michael@0 361
michael@0 362 nsCOMPtr<nsIDocShell> shell = do_QueryReferent(mDocShell);
michael@0 363
michael@0 364 if (!shell)
michael@0 365 return;
michael@0 366
michael@0 367 rv = GetRenderingContext(shell, surface, width, height);
michael@0 368 if (NS_FAILED(rv))
michael@0 369 return;
michael@0 370
michael@0 371 bool drawFrame = false;
michael@0 372 if (isPreview)
michael@0 373 rv = mController->DrawPreview(gCtx, &drawFrame);
michael@0 374 else
michael@0 375 rv = mController->DrawThumbnail(gCtx, width, height, &drawFrame);
michael@0 376
michael@0 377 if (NS_FAILED(rv))
michael@0 378 return;
michael@0 379
michael@0 380 HDC hDC = surface->GetDC();
michael@0 381 HBITMAP hBitmap = (HBITMAP)GetCurrentObject(hDC, OBJ_BITMAP);
michael@0 382
michael@0 383 DWORD flags = drawFrame ? DWM_SIT_DISPLAYFRAME : 0;
michael@0 384 POINT pptClient = { 0, 0 };
michael@0 385 if (isPreview)
michael@0 386 WinUtils::dwmSetIconicLivePreviewBitmapPtr(PreviewWindow(), hBitmap, &pptClient, flags);
michael@0 387 else
michael@0 388 WinUtils::dwmSetIconicThumbnailPtr(PreviewWindow(), hBitmap, flags);
michael@0 389
michael@0 390 ResetRenderingContext();
michael@0 391 }
michael@0 392
michael@0 393 /* static */
michael@0 394 bool
michael@0 395 TaskbarPreview::MainWindowHook(void *aContext,
michael@0 396 HWND hWnd, UINT nMsg,
michael@0 397 WPARAM wParam, LPARAM lParam,
michael@0 398 LRESULT *aResult)
michael@0 399 {
michael@0 400 NS_ASSERTION(nMsg == nsAppShell::GetTaskbarButtonCreatedMessage() ||
michael@0 401 nMsg == WM_DESTROY,
michael@0 402 "Window hook proc called with wrong message");
michael@0 403 NS_ASSERTION(aContext, "Null context in MainWindowHook");
michael@0 404 if (!aContext)
michael@0 405 return false;
michael@0 406 TaskbarPreview *preview = reinterpret_cast<TaskbarPreview*>(aContext);
michael@0 407 if (nMsg == WM_DESTROY) {
michael@0 408 // nsWindow is being destroyed
michael@0 409 // We can't really do anything at this point including removing hooks
michael@0 410 preview->mWnd = nullptr;
michael@0 411 } else {
michael@0 412 nsWindow *window = WinUtils::GetNSWindowPtr(preview->mWnd);
michael@0 413 if (window) {
michael@0 414 window->SetHasTaskbarIconBeenCreated();
michael@0 415
michael@0 416 if (preview->mVisible)
michael@0 417 preview->UpdateTaskbarProperties();
michael@0 418 }
michael@0 419 }
michael@0 420 return false;
michael@0 421 }
michael@0 422
michael@0 423 TaskbarPreview *
michael@0 424 TaskbarPreview::sActivePreview = nullptr;
michael@0 425
michael@0 426 } // namespace widget
michael@0 427 } // namespace mozilla
michael@0 428

mercurial