1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsViewManager.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
#include "mozilla/Preferences.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/StartupTimeline.h"
#include "mozilla/dom/Document.h"
#include "nsGfxCIID.h"
#include "nsView.h"
#include "nsCOMPtr.h"
#include "nsRegion.h"
#include "nsCOMArray.h"
#include "nsXULPopupManager.h"
#include "nsPresContext.h"
#include "nsRefreshDriver.h"
#include "nsContentUtils.h" // for nsAutoScriptBlocker
#include "nsLayoutUtils.h"
#include "gfxPlatform.h"
#include "WindowRenderer.h"
/**
XXX TODO XXX
DeCOMify newly private methods
Optimize view storage
*/
/**
A note about platform assumptions:
We assume that a widget is z-ordered on top of its parent.
We do NOT assume anything about the relative z-ordering of sibling widgets.
Even though we ask for a specific z-order, we don't assume that widget
z-ordering actually works.
*/
using namespace mozilla;
using namespace mozilla::layers;
#define NSCOORD_NONE INT32_MIN
#undef DEBUG_MOUSE_LOCATION
uint32_t nsViewManager::gLastUserEventTime = 0;
nsViewManager::nsViewManager(nsDeviceContext* aContext)
: mContext(aContext),
mPresShell(nullptr),
mDelayedResize(NSCOORD_NONE, NSCOORD_NONE),
mRootView(nullptr),
mPainting(false),
mRecursiveRefreshPending(false),
mHasPendingWidgetGeometryChanges(false) {
MOZ_ASSERT(aContext);
}
nsViewManager::~nsViewManager() {
if (mRootView) {
// Destroy any remaining views
mRootView->Destroy();
mRootView = nullptr;
}
mRootViewManager = nullptr;
MOZ_RELEASE_ASSERT(!mPresShell,
"Releasing nsViewManager without having called Destroy on "
"the PresShell!");
}
nsView* nsViewManager::CreateView(const nsRect& aBounds, nsView* aParent,
ViewVisibility aVisibilityFlag) {
auto* v = new nsView(this, aVisibilityFlag);
v->SetParent(aParent);
v->SetPosition(aBounds.X(), aBounds.Y());
nsRect dim(0, 0, aBounds.Width(), aBounds.Height());
v->SetDimensions(dim);
return v;
}
void nsViewManager::SetRootView(nsView* aView) {
MOZ_ASSERT(!aView || aView->GetViewManager() == this,
"Unexpected viewmanager on root view");
// Do NOT destroy the current root view. It's the caller's responsibility
// to destroy it
mRootView = aView;
if (mRootView) {
nsView* parent = mRootView->GetParent();
if (parent) {
// Calling InsertChild on |parent| will InvalidateHierarchy() on us, so
// no need to set mRootViewManager ourselves here.
parent->InsertChild(mRootView, nullptr);
} else {
InvalidateHierarchy();
}
}
// Else don't touch mRootViewManager
}
void nsViewManager::GetWindowDimensions(nscoord* aWidth, nscoord* aHeight) {
if (nullptr != mRootView) {
if (mDelayedResize == nsSize(NSCOORD_NONE, NSCOORD_NONE)) {
nsRect dim = mRootView->GetDimensions();
*aWidth = dim.Width();
*aHeight = dim.Height();
} else {
*aWidth = mDelayedResize.width;
*aHeight = mDelayedResize.height;
}
} else {
*aWidth = 0;
*aHeight = 0;
}
}
void nsViewManager::DoSetWindowDimensions(nscoord aWidth, nscoord aHeight) {
nsRect oldDim = mRootView->GetDimensions();
nsRect newDim(0, 0, aWidth, aHeight);
// We care about resizes even when one dimension is already zero.
if (oldDim.IsEqualEdges(newDim)) {
return;
}
// Don't resize the widget. It is already being set elsewhere.
mRootView->SetDimensions(newDim);
if (RefPtr<PresShell> presShell = mPresShell) {
presShell->ResizeReflow(aWidth, aHeight);
}
}
bool nsViewManager::ShouldDelayResize() const {
MOZ_ASSERT(mRootView);
if (!mRootView->IsEffectivelyVisible() || !mPresShell ||
!mPresShell->IsVisible()) {
return true;
}
if (nsRefreshDriver* rd = mPresShell->GetRefreshDriver()) {
if (rd->IsResizeSuppressed()) {
return true;
}
}
return false;
}
void nsViewManager::SetWindowDimensions(nscoord aWidth, nscoord aHeight,
bool aDelayResize) {
if (mRootView) {
if (!ShouldDelayResize() && !aDelayResize) {
if (mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE) &&
mDelayedResize != nsSize(aWidth, aHeight)) {
// We have a delayed resize; that now obsolete size may already have
// been flushed to the PresContext so we need to update the PresContext
// with the new size because if the new size is exactly the same as the
// root view's current size then DoSetWindowDimensions will not
// request a resize reflow (which would correct it). See bug 617076.
mDelayedResize = nsSize(aWidth, aHeight);
FlushDelayedResize();
}
mDelayedResize.SizeTo(NSCOORD_NONE, NSCOORD_NONE);
DoSetWindowDimensions(aWidth, aHeight);
} else {
mDelayedResize.SizeTo(aWidth, aHeight);
if (mPresShell) {
mPresShell->SetNeedStyleFlush();
mPresShell->SetNeedLayoutFlush();
}
}
}
}
void nsViewManager::FlushDelayedResize() {
if (mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE)) {
DoSetWindowDimensions(mDelayedResize.width, mDelayedResize.height);
mDelayedResize.SizeTo(NSCOORD_NONE, NSCOORD_NONE);
}
}
// Convert aIn from being relative to and in appunits of aFromView, to being
// relative to and in appunits of aToView.
static nsRect ConvertRectBetweenViews(const nsRect& aIn, nsView* aFromView,
nsView* aToView) {
nsRect out = aIn;
out.MoveBy(aFromView->GetOffsetTo(aToView));
out = out.ScaleToOtherAppUnitsRoundOut(
aFromView->GetViewManager()->AppUnitsPerDevPixel(),
aToView->GetViewManager()->AppUnitsPerDevPixel());
return out;
}
nsView* nsViewManager::GetDisplayRootFor(nsView* aView) {
nsView* displayRoot = aView;
for (;;) {
nsView* displayParent = displayRoot->GetParent();
if (!displayParent) {
return displayRoot;
}
// Any popup view is a display root.
if (displayRoot->GetFrame() &&
displayRoot->GetFrame()->IsMenuPopupFrame()) {
return displayRoot;
}
displayRoot = displayParent;
}
}
/**
aRegion is given in device coordinates!!
aContext may be null, in which case layers should be used for
rendering.
*/
void nsViewManager::Refresh(nsView* aView,
const LayoutDeviceIntRegion& aRegion) {
NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
if (mPresShell && mPresShell->IsNeverPainting()) {
return;
}
if (aRegion.IsEmpty()) {
return;
}
nsIWidget* widget = aView->GetWidget();
if (!widget) {
return;
}
NS_ASSERTION(!IsPainting(), "recursive painting not permitted");
if (IsPainting()) {
RootViewManager()->mRecursiveRefreshPending = true;
return;
}
{
nsAutoScriptBlocker scriptBlocker;
SetPainting(true);
NS_ASSERTION(GetDisplayRootFor(aView) == aView,
"Widgets that we paint must all be display roots");
if (RefPtr<PresShell> presShell = mPresShell) {
#ifdef MOZ_DUMP_PAINTING
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
printf_stderr("--COMPOSITE-- %p\n", presShell.get());
}
#endif
RefPtr<WindowRenderer> renderer = widget->GetWindowRenderer();
if (!renderer->NeedsWidgetInvalidation()) {
renderer->FlushRendering(wr::RenderReasons::WIDGET);
} else {
presShell->SyncPaintFallback(aView->GetFrame(), renderer);
}
#ifdef MOZ_DUMP_PAINTING
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
printf_stderr("--ENDCOMPOSITE--\n");
}
#endif
mozilla::StartupTimeline::RecordOnce(
mozilla::StartupTimeline::FIRST_PAINT);
}
SetPainting(false);
}
if (RootViewManager()->mRecursiveRefreshPending) {
RootViewManager()->mRecursiveRefreshPending = false;
InvalidateAllViews();
}
}
void nsViewManager::ProcessPendingUpdatesForView(nsView* aView,
bool aFlushDirtyRegion) {
NS_ASSERTION(IsRootVM(), "Updates will be missed");
if (!aView) {
return;
}
RefPtr<PresShell> rootPresShell = mPresShell;
AutoTArray<nsCOMPtr<nsIWidget>, 1> widgets;
aView->GetViewManager()->ProcessPendingUpdatesRecurse(aView, widgets);
for (uint32_t i = 0; i < widgets.Length(); ++i) {
nsView* view = nsView::GetViewFor(widgets[i]);
if (!view) {
continue;
}
if (view->mNeedsWindowPropertiesSync) {
view->mNeedsWindowPropertiesSync = false;
if (nsViewManager* vm = view->GetViewManager()) {
if (PresShell* presShell = vm->GetPresShell()) {
presShell->SyncWindowProperties(/* aSync */ true);
}
}
}
}
if (rootPresShell->GetViewManager() != this) {
return; // presentation might have been torn down
}
if (aFlushDirtyRegion) {
nsAutoScriptBlocker scriptBlocker;
SetPainting(true);
for (uint32_t i = 0; i < widgets.Length(); ++i) {
nsIWidget* widget = widgets[i];
nsView* view = nsView::GetViewFor(widget);
if (view) {
RefPtr<nsViewManager> viewManager = view->GetViewManager();
viewManager->ProcessPendingUpdatesPaint(MOZ_KnownLive(widget));
}
}
SetPainting(false);
}
}
void nsViewManager::ProcessPendingUpdatesRecurse(
nsView* aView, AutoTArray<nsCOMPtr<nsIWidget>, 1>& aWidgets) {
if (mPresShell && mPresShell->IsNeverPainting()) {
return;
}
for (nsView* childView = aView->GetFirstChild(); childView;
childView = childView->GetNextSibling()) {
childView->GetViewManager()->ProcessPendingUpdatesRecurse(childView,
aWidgets);
}
nsIWidget* widget = aView->GetWidget();
if (widget) {
aWidgets.AppendElement(widget);
} else {
FlushDirtyRegionToWidget(aView);
}
}
void nsViewManager::ProcessPendingUpdatesPaint(nsIWidget* aWidget) {
if (aWidget->NeedsPaint()) {
// If an ancestor widget was hidden and then shown, we could
// have a delayed resize to handle.
for (RefPtr<nsViewManager> vm = this; vm;
vm = vm->mRootView->GetParent()
? vm->mRootView->GetParent()->GetViewManager()
: nullptr) {
if (vm->mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE) &&
vm->mRootView->IsEffectivelyVisible() && vm->mPresShell &&
vm->mPresShell->IsVisible()) {
vm->FlushDelayedResize();
}
}
nsView* view = nsView::GetViewFor(aWidget);
if (!view) {
NS_ERROR("FlushDelayedResize destroyed the nsView?");
return;
}
nsIWidgetListener* previousListener =
aWidget->GetPreviouslyAttachedWidgetListener();
if (previousListener && previousListener != view &&
view->IsPrimaryFramePaintSuppressed()) {
return;
}
if (RefPtr<PresShell> presShell = mPresShell) {
#ifdef MOZ_DUMP_PAINTING
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
printf_stderr(
"---- PAINT START ----PresShell(%p), nsView(%p), nsIWidget(%p)\n",
presShell.get(), view, aWidget);
}
#endif
presShell->PaintAndRequestComposite(
view->GetFrame(), aWidget->GetWindowRenderer(), PaintFlags::None);
view->SetForcedRepaint(false);
#ifdef MOZ_DUMP_PAINTING
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
printf_stderr("---- PAINT END ----\n");
}
#endif
}
}
FlushDirtyRegionToWidget(nsView::GetViewFor(aWidget));
}
void nsViewManager::FlushDirtyRegionToWidget(nsView* aView) {
NS_ASSERTION(aView->GetViewManager() == this,
"FlushDirtyRegionToWidget called on view we don't own");
if (!aView->IsDirty()) {
return;
}
const nsRect dirtyRegion = aView->GetDimensions();
nsView* nearestViewWithWidget = aView;
while (!nearestViewWithWidget->HasWidget() &&
nearestViewWithWidget->GetParent()) {
nearestViewWithWidget = nearestViewWithWidget->GetParent();
}
nsRect r = ConvertRectBetweenViews(dirtyRegion, aView, nearestViewWithWidget);
nsViewManager* widgetVM = nearestViewWithWidget->GetViewManager();
widgetVM->InvalidateWidgetArea(nearestViewWithWidget, r);
aView->SetIsDirty(false);
}
void nsViewManager::PostPendingUpdate() {
nsViewManager* rootVM = RootViewManager();
rootVM->mHasPendingWidgetGeometryChanges = true;
if (rootVM->mPresShell) {
rootVM->mPresShell->SetNeedLayoutFlush();
rootVM->mPresShell->SchedulePaint();
}
}
/**
* @param aDamagedRegion this region, relative to aWidgetView, is invalidated in
* every widget child of aWidgetView, plus aWidgetView's own widget
*/
void nsViewManager::InvalidateWidgetArea(nsView* aWidgetView,
const nsRegion& aDamagedRegion) {
NS_ASSERTION(aWidgetView->GetViewManager() == this,
"InvalidateWidgetArea called on view we don't own");
nsIWidget* widget = aWidgetView->GetWidget();
#if 0
nsRect dbgBounds = aDamagedRegion.GetBounds();
printf("InvalidateWidgetArea view:%X (%d) widget:%X region: %d, %d, %d, %d\n",
aWidgetView, aWidgetView->IsAttachedToTopLevel(),
widget, dbgBounds.x, dbgBounds.y, dbgBounds.width, dbgBounds.height);
#endif
// If the widget is hidden, it don't cover nothing
if (widget && !widget->IsVisible()) {
return;
}
if (!widget) {
// The root view or a scrolling view might not have a widget
// (for example, during printing). We get here when we scroll
// during printing to show selected options in a listbox, for example.
return;
}
if (!aDamagedRegion.IsEmpty()) {
for (auto iter = aDamagedRegion.RectIter(); !iter.Done(); iter.Next()) {
LayoutDeviceIntRect bounds = ViewToWidget(aWidgetView, iter.Get());
widget->Invalidate(bounds);
}
}
}
static bool ShouldIgnoreInvalidation(nsViewManager* aVM) {
while (aVM) {
PresShell* presShell = aVM->GetPresShell();
if (!presShell || presShell->ShouldIgnoreInvalidation()) {
return true;
}
nsView* view = aVM->GetRootView()->GetParent();
aVM = view ? view->GetViewManager() : nullptr;
}
return false;
}
void nsViewManager::InvalidateView(nsView* aView) {
// If painting is suppressed in the presshell or an ancestor drop all
// invalidates, it will invalidate everything when it unsuppresses.
if (ShouldIgnoreInvalidation(this)) {
return;
}
NS_ASSERTION(aView->GetViewManager() == this,
"InvalidateView called on view we don't own");
if (aView->GetBounds().IsEmpty()) {
return;
}
GetDisplayRootFor(aView)->SetIsDirty(true);
}
void nsViewManager::InvalidateAllViews() {
if (RootViewManager() != this) {
return RootViewManager()->InvalidateAllViews();
}
InvalidateViews(mRootView);
}
void nsViewManager::InvalidateViews(nsView* aView) {
// Invalidate this view.
InvalidateView(aView);
// Invalidate all children as well.
nsView* childView = aView->GetFirstChild();
while (nullptr != childView) {
childView->GetViewManager()->InvalidateViews(childView);
childView = childView->GetNextSibling();
}
}
void nsViewManager::WillPaintWindow(nsIWidget* aWidget) {
if (aWidget) {
nsView* view = nsView::GetViewFor(aWidget);
WindowRenderer* renderer = aWidget->GetWindowRenderer();
if (view &&
(view->ForcedRepaint() || !renderer->NeedsWidgetInvalidation())) {
ProcessPendingUpdates();
// Re-get the view pointer here since the ProcessPendingUpdates might have
// destroyed it during CallWillPaintOnObservers.
view = nsView::GetViewFor(aWidget);
if (view) {
view->SetForcedRepaint(false);
}
}
}
}
bool nsViewManager::PaintWindow(nsIWidget* aWidget,
const LayoutDeviceIntRegion& aRegion) {
if (!aWidget) {
return false;
}
// Get the view pointer here since NS_WILL_PAINT might have
// destroyed it during CallWillPaintOnObservers (bug 378273).
nsView* view = nsView::GetViewFor(aWidget);
if (view && !aRegion.IsEmpty()) {
Refresh(view, aRegion);
}
return true;
}
void nsViewManager::DidPaintWindow() {
if (RefPtr<PresShell> presShell = mPresShell) {
presShell->DidPaintWindow();
}
}
void nsViewManager::DispatchEvent(WidgetGUIEvent* aEvent, nsView* aView,
nsEventStatus* aStatus) {
AUTO_PROFILER_LABEL("nsViewManager::DispatchEvent", OTHER);
WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
if ((mouseEvent &&
// Ignore mouse events that we synthesize.
mouseEvent->mReason == WidgetMouseEvent::eReal &&
// Ignore mouse exit and enter (we'll get moves if the user
// is really moving the mouse) since we get them when we
// create and destroy widgets.
mouseEvent->mMessage != eMouseExitFromWidget &&
mouseEvent->mMessage != eMouseEnterIntoWidget) ||
aEvent->HasKeyEventMessage() || aEvent->HasIMEEventMessage()) {
gLastUserEventTime = PR_IntervalToMicroseconds(PR_IntervalNow());
}
// Find the view whose coordinates system we're in.
nsView* view = aView;
bool dispatchUsingCoordinates = aEvent->IsUsingCoordinates();
if (dispatchUsingCoordinates) {
// Will dispatch using coordinates. Pretty bogus but it's consistent
// with what presshell does.
view = GetDisplayRootFor(view);
}
// If the view has no frame, look for a view that does.
nsIFrame* frame = view->GetFrame();
if (!frame && (dispatchUsingCoordinates || aEvent->HasKeyEventMessage() ||
aEvent->IsIMERelatedEvent())) {
while (view && !view->GetFrame()) {
view = view->GetParent();
}
if (view) {
frame = view->GetFrame();
}
}
if (frame) {
// Hold a refcount to the presshell. The continued existence of the
// presshell will delay deletion of this view hierarchy should the event
// want to cause its destruction in, say, some JavaScript event handler.
if (RefPtr<PresShell> presShell = view->GetViewManager()->GetPresShell()) {
presShell->HandleEvent(frame, aEvent, false, aStatus);
return;
}
}
*aStatus = nsEventStatus_eIgnore;
}
void nsViewManager::InsertChild(nsView* aParent, nsView* aChild,
nsView* aSibling, bool aAfter) {
MOZ_ASSERT(nullptr != aParent, "null ptr");
MOZ_ASSERT(nullptr != aChild, "null ptr");
NS_ASSERTION(aSibling == nullptr || aSibling->GetParent() == aParent,
"tried to insert view with invalid sibling");
NS_ASSERTION(!IsViewInserted(aChild),
"tried to insert an already-inserted view");
if ((nullptr != aParent) && (nullptr != aChild)) {
// if aAfter is set, we will insert the child after 'prev' (i.e. after 'kid'
// in document order, otherwise after 'kid' (i.e. before 'kid' in document
// order).
if (nullptr == aSibling) {
if (aAfter) {
// insert at end of document order, i.e., before first view
// this is the common case, by far
aParent->InsertChild(aChild, nullptr);
} else {
// insert at beginning of document order, i.e., after last view
nsView* kid = aParent->GetFirstChild();
nsView* prev = nullptr;
while (kid) {
prev = kid;
kid = kid->GetNextSibling();
}
// prev is last view or null if there are no children
aParent->InsertChild(aChild, prev);
}
} else {
nsView* kid = aParent->GetFirstChild();
nsView* prev = nullptr;
while (kid && aSibling != kid) {
// get the next sibling view
prev = kid;
kid = kid->GetNextSibling();
}
NS_ASSERTION(kid != nullptr, "couldn't find sibling in child list");
if (aAfter) {
// insert after 'kid' in document order, i.e. before in view order
aParent->InsertChild(aChild, prev);
} else {
// insert before 'kid' in document order, i.e. after in view order
aParent->InsertChild(aChild, kid);
}
}
}
}
void nsViewManager::RemoveChild(nsView* aChild) {
NS_ASSERTION(aChild, "aChild must not be null");
nsView* parent = aChild->GetParent();
if (nullptr != parent) {
NS_ASSERTION(
aChild->GetViewManager() == this || parent->GetViewManager() == this,
"wrong view manager");
parent->RemoveChild(aChild);
}
}
void nsViewManager::MoveViewTo(nsView* aView, nscoord aX, nscoord aY) {
NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
aView->SetPosition(aX, aY);
}
void nsViewManager::ResizeView(nsView* aView, const nsRect& aRect) {
NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
nsRect oldDimensions = aView->GetDimensions();
if (!oldDimensions.IsEqualEdges(aRect)) {
aView->SetDimensions(aRect);
}
// Note that if layout resizes the view and the view has a custom clip
// region set, then we expect layout to update the clip region too. Thus
// in the case where mClipRect has been optimized away to just be a null
// pointer, and this resize is implicitly changing the clip rect, it's OK
// because layout will change it back again if necessary.
}
void nsViewManager::SetViewVisibility(nsView* aView, ViewVisibility aVisible) {
NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
if (aVisible != aView->GetVisibility()) {
aView->SetVisibility(aVisible);
}
}
bool nsViewManager::IsViewInserted(nsView* aView) {
if (mRootView == aView) {
return true;
}
if (aView->GetParent() == nullptr) {
return false;
}
nsView* view = aView->GetParent()->GetFirstChild();
while (view != nullptr) {
if (view == aView) {
return true;
}
view = view->GetNextSibling();
}
return false;
}
nsIWidget* nsViewManager::GetRootWidget() const {
if (!mRootView) {
return nullptr;
}
if (mRootView->HasWidget()) {
return mRootView->GetWidget();
}
if (mRootView->GetParent()) {
return mRootView->GetParent()->GetViewManager()->GetRootWidget();
}
return nullptr;
}
LayoutDeviceIntRect nsViewManager::ViewToWidget(nsView* aView,
const nsRect& aRect) const {
NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
// account for the view's origin not lining up with the widget's
nsRect rect = aRect + aView->ViewToWidgetOffset();
// finally, convert to device coordinates.
return LayoutDeviceIntRect::FromUnknownRect(
rect.ToOutsidePixels(AppUnitsPerDevPixel()));
}
void nsViewManager::IsPainting(bool& aIsPainting) {
aIsPainting = IsPainting();
}
void nsViewManager::ProcessPendingUpdates() {
if (!IsRootVM()) {
RefPtr<nsViewManager> rootViewManager = RootViewManager();
rootViewManager->ProcessPendingUpdates();
return;
}
// Flush things like reflows by calling WillPaint on observer presShells.
if (mPresShell) {
RefPtr<nsViewManager> strongThis(this);
CallWillPaintOnObservers();
ProcessPendingUpdatesForView(mRootView, true);
}
}
void nsViewManager::UpdateWidgetGeometry() {
if (!IsRootVM()) {
RefPtr<nsViewManager> rootViewManager = RootViewManager();
rootViewManager->UpdateWidgetGeometry();
return;
}
if (mHasPendingWidgetGeometryChanges) {
mHasPendingWidgetGeometryChanges = false;
ProcessPendingUpdatesForView(mRootView, false);
}
}
/* static */ void nsViewManager::CollectVMsForWillPaint(
nsView* aView, nsViewManager* aParentVM,
nsTArray<RefPtr<nsViewManager>>& aVMs) {
nsViewManager* vm = aView->GetViewManager();
if (vm != aParentVM) {
aVMs.AppendElement(vm);
}
for (nsView* child = aView->GetFirstChild(); child;
child = child->GetNextSibling()) {
CollectVMsForWillPaint(child, vm, aVMs);
}
}
void nsViewManager::CallWillPaintOnObservers() {
MOZ_ASSERT(IsRootVM(), "Must be root VM for this to be called!");
if (!mRootView) {
return;
}
AutoTArray<RefPtr<nsViewManager>, 2> VMs;
CollectVMsForWillPaint(mRootView, nullptr, VMs);
for (const auto& vm : VMs) {
if (vm->GetRootView() && vm->GetRootView()->IsEffectivelyVisible()) {
if (RefPtr<PresShell> presShell = vm->GetPresShell()) {
presShell->WillPaint();
}
}
}
}
void nsViewManager::InvalidateHierarchy() {
if (mRootView) {
mRootViewManager = nullptr;
nsView* parent = mRootView->GetParent();
if (parent) {
mRootViewManager = parent->GetViewManager()->RootViewManager();
NS_ASSERTION(mRootViewManager != this,
"Root view had a parent, but it has the same view manager");
}
// else, we are implicitly our own root view manager.
}
}
|