Bug 1787199 [wpt PR 35620] - Add tests for `VisibilityStateEntry`, a=testonly
[gecko.git] / view / nsViewManager.cpp
blobab91a8575ee5a0288955ed02c94527e0db4444ee
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsViewManager.h"
8 #include "mozilla/MouseEvents.h"
9 #include "mozilla/PresShell.h"
10 #include "mozilla/PresShellInlines.h"
11 #include "mozilla/Preferences.h"
12 #include "mozilla/ProfilerLabels.h"
13 #include "mozilla/StartupTimeline.h"
14 #include "mozilla/dom/Document.h"
15 #include "nsGfxCIID.h"
16 #include "nsView.h"
17 #include "nsCOMPtr.h"
18 #include "nsRegion.h"
19 #include "nsCOMArray.h"
20 #include "nsXULPopupManager.h"
21 #include "nsPresContext.h"
22 #include "nsRefreshDriver.h"
23 #include "nsContentUtils.h" // for nsAutoScriptBlocker
24 #include "nsLayoutUtils.h"
25 #include "Layers.h"
26 #include "gfxPlatform.h"
27 #include "WindowRenderer.h"
29 /**
30 XXX TODO XXX
32 DeCOMify newly private methods
33 Optimize view storage
36 /**
37 A note about platform assumptions:
39 We assume that a widget is z-ordered on top of its parent.
41 We do NOT assume anything about the relative z-ordering of sibling widgets.
42 Even though we ask for a specific z-order, we don't assume that widget
43 z-ordering actually works.
46 using namespace mozilla;
47 using namespace mozilla::layers;
49 #define NSCOORD_NONE INT32_MIN
51 #undef DEBUG_MOUSE_LOCATION
53 // Weakly held references to all of the view managers
54 StaticAutoPtr<nsTArray<nsViewManager*>> nsViewManager::gViewManagers;
55 uint32_t nsViewManager::gLastUserEventTime = 0;
57 nsViewManager::nsViewManager()
58 : mPresShell(nullptr),
59 mDelayedResize(NSCOORD_NONE, NSCOORD_NONE),
60 mRootView(nullptr),
61 mRefreshDisableCount(0),
62 mPainting(false),
63 mRecursiveRefreshPending(false),
64 mHasPendingWidgetGeometryChanges(false) {
65 if (gViewManagers == nullptr) {
66 // Create an array to hold a list of view managers
67 gViewManagers = new nsTArray<nsViewManager*>;
70 gViewManagers->AppendElement(this);
73 nsViewManager::~nsViewManager() {
74 if (mRootView) {
75 // Destroy any remaining views
76 mRootView->Destroy();
77 mRootView = nullptr;
80 mRootViewManager = nullptr;
82 NS_ASSERTION(gViewManagers != nullptr, "About to use null gViewManagers");
84 #ifdef DEBUG
85 bool removed =
86 #endif
87 gViewManagers->RemoveElement(this);
88 NS_ASSERTION(
89 removed,
90 "Viewmanager instance was not in the global list of viewmanagers");
92 if (gViewManagers->IsEmpty()) {
93 // There aren't any more view managers so
94 // release the global array of view managers
95 gViewManagers = nullptr;
98 MOZ_RELEASE_ASSERT(!mPresShell,
99 "Releasing nsViewManager without having called Destroy on "
100 "the PresShell!");
103 // We don't hold a reference to the presentation context because it
104 // holds a reference to us.
105 nsresult nsViewManager::Init(nsDeviceContext* aContext) {
106 MOZ_ASSERT(nullptr != aContext, "null ptr");
108 if (nullptr == aContext) {
109 return NS_ERROR_NULL_POINTER;
111 if (nullptr != mContext) {
112 return NS_ERROR_ALREADY_INITIALIZED;
114 mContext = aContext;
116 return NS_OK;
119 nsView* nsViewManager::CreateView(const nsRect& aBounds, nsView* aParent,
120 nsViewVisibility aVisibilityFlag) {
121 auto* v = new nsView(this, aVisibilityFlag);
122 v->SetParent(aParent);
123 v->SetPosition(aBounds.X(), aBounds.Y());
124 nsRect dim(0, 0, aBounds.Width(), aBounds.Height());
125 v->SetDimensions(dim, false);
126 return v;
129 void nsViewManager::SetRootView(nsView* aView) {
130 MOZ_ASSERT(!aView || aView->GetViewManager() == this,
131 "Unexpected viewmanager on root view");
133 // Do NOT destroy the current root view. It's the caller's responsibility
134 // to destroy it
135 mRootView = aView;
137 if (mRootView) {
138 nsView* parent = mRootView->GetParent();
139 if (parent) {
140 // Calling InsertChild on |parent| will InvalidateHierarchy() on us, so
141 // no need to set mRootViewManager ourselves here.
142 parent->InsertChild(mRootView, nullptr);
143 } else {
144 InvalidateHierarchy();
147 mRootView->SetZIndex(false, 0);
149 // Else don't touch mRootViewManager
152 void nsViewManager::GetWindowDimensions(nscoord* aWidth, nscoord* aHeight) {
153 if (nullptr != mRootView) {
154 if (mDelayedResize == nsSize(NSCOORD_NONE, NSCOORD_NONE)) {
155 nsRect dim = mRootView->GetDimensions();
156 *aWidth = dim.Width();
157 *aHeight = dim.Height();
158 } else {
159 *aWidth = mDelayedResize.width;
160 *aHeight = mDelayedResize.height;
162 } else {
163 *aWidth = 0;
164 *aHeight = 0;
168 void nsViewManager::DoSetWindowDimensions(nscoord aWidth, nscoord aHeight) {
169 nsRect oldDim = mRootView->GetDimensions();
170 nsRect newDim(0, 0, aWidth, aHeight);
171 // We care about resizes even when one dimension is already zero.
172 if (oldDim.IsEqualEdges(newDim)) {
173 return;
175 // Don't resize the widget. It is already being set elsewhere.
176 mRootView->SetDimensions(newDim, true, false);
177 if (RefPtr<PresShell> presShell = mPresShell) {
178 presShell->ResizeReflow(aWidth, aHeight);
182 bool nsViewManager::ShouldDelayResize() const {
183 MOZ_ASSERT(mRootView);
184 if (!mRootView->IsEffectivelyVisible() || !mPresShell ||
185 !mPresShell->IsVisible()) {
186 return true;
188 if (nsRefreshDriver* rd = mPresShell->GetRefreshDriver()) {
189 if (rd->IsResizeSuppressed()) {
190 return true;
193 return false;
196 void nsViewManager::SetWindowDimensions(nscoord aWidth, nscoord aHeight,
197 bool aDelayResize) {
198 if (mRootView) {
199 if (!ShouldDelayResize() && !aDelayResize) {
200 if (mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE) &&
201 mDelayedResize != nsSize(aWidth, aHeight)) {
202 // We have a delayed resize; that now obsolete size may already have
203 // been flushed to the PresContext so we need to update the PresContext
204 // with the new size because if the new size is exactly the same as the
205 // root view's current size then DoSetWindowDimensions will not
206 // request a resize reflow (which would correct it). See bug 617076.
207 mDelayedResize = nsSize(aWidth, aHeight);
208 FlushDelayedResize();
210 mDelayedResize.SizeTo(NSCOORD_NONE, NSCOORD_NONE);
211 DoSetWindowDimensions(aWidth, aHeight);
212 } else {
213 mDelayedResize.SizeTo(aWidth, aHeight);
214 if (mPresShell) {
215 mPresShell->SetNeedStyleFlush();
216 mPresShell->SetNeedLayoutFlush();
222 void nsViewManager::FlushDelayedResize() {
223 if (mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE)) {
224 DoSetWindowDimensions(mDelayedResize.width, mDelayedResize.height);
225 mDelayedResize.SizeTo(NSCOORD_NONE, NSCOORD_NONE);
229 // Convert aIn from being relative to and in appunits of aFromView, to being
230 // relative to and in appunits of aToView.
231 static nsRegion ConvertRegionBetweenViews(const nsRegion& aIn,
232 nsView* aFromView, nsView* aToView) {
233 nsRegion out = aIn;
234 out.MoveBy(aFromView->GetOffsetTo(aToView));
235 out = out.ScaleToOtherAppUnitsRoundOut(
236 aFromView->GetViewManager()->AppUnitsPerDevPixel(),
237 aToView->GetViewManager()->AppUnitsPerDevPixel());
238 return out;
241 nsView* nsViewManager::GetDisplayRootFor(nsView* aView) {
242 nsView* displayRoot = aView;
243 for (;;) {
244 nsView* displayParent = displayRoot->GetParent();
245 if (!displayParent) return displayRoot;
247 if (displayRoot->GetFloating() && !displayParent->GetFloating())
248 return displayRoot;
250 // If we have a combobox dropdown popup within a panel popup, both the view
251 // for the dropdown popup and its parent will be floating, so we need to
252 // distinguish this situation. We do this by looking for a widget. Any view
253 // with a widget is a display root.
254 nsIWidget* widget = displayRoot->GetWidget();
255 if (widget && widget->WindowType() == eWindowType_popup) {
256 NS_ASSERTION(displayRoot->GetFloating() && displayParent->GetFloating(),
257 "this should only happen with floating views that have "
258 "floating parents");
259 return displayRoot;
262 displayRoot = displayParent;
267 aRegion is given in device coordinates!!
268 aContext may be null, in which case layers should be used for
269 rendering.
271 void nsViewManager::Refresh(nsView* aView,
272 const LayoutDeviceIntRegion& aRegion) {
273 NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
275 if (mPresShell && mPresShell->IsNeverPainting()) {
276 return;
279 if (aRegion.IsEmpty()) {
280 return;
283 nsIWidget* widget = aView->GetWidget();
284 if (!widget) {
285 return;
288 NS_ASSERTION(!IsPainting(), "recursive painting not permitted");
289 if (IsPainting()) {
290 RootViewManager()->mRecursiveRefreshPending = true;
291 return;
295 nsAutoScriptBlocker scriptBlocker;
296 SetPainting(true);
298 NS_ASSERTION(GetDisplayRootFor(aView) == aView,
299 "Widgets that we paint must all be display roots");
301 if (RefPtr<PresShell> presShell = mPresShell) {
302 #ifdef MOZ_DUMP_PAINTING
303 if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
304 printf_stderr("--COMPOSITE-- %p\n", presShell.get());
306 #endif
307 WindowRenderer* renderer = widget->GetWindowRenderer();
308 if (!renderer->NeedsWidgetInvalidation()) {
309 renderer->FlushRendering(wr::RenderReasons::WIDGET);
310 } else {
311 presShell->SyncPaintFallback(aView);
313 #ifdef MOZ_DUMP_PAINTING
314 if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
315 printf_stderr("--ENDCOMPOSITE--\n");
317 #endif
318 mozilla::StartupTimeline::RecordOnce(
319 mozilla::StartupTimeline::FIRST_PAINT);
322 SetPainting(false);
325 if (RootViewManager()->mRecursiveRefreshPending) {
326 RootViewManager()->mRecursiveRefreshPending = false;
327 InvalidateAllViews();
331 void nsViewManager::ProcessPendingUpdatesForView(nsView* aView,
332 bool aFlushDirtyRegion) {
333 NS_ASSERTION(IsRootVM(), "Updates will be missed");
334 if (!aView) {
335 return;
338 RefPtr<PresShell> rootPresShell = mPresShell;
339 AutoTArray<nsCOMPtr<nsIWidget>, 1> widgets;
340 aView->GetViewManager()->ProcessPendingUpdatesRecurse(aView, widgets);
341 for (uint32_t i = 0; i < widgets.Length(); ++i) {
342 nsView* view = nsView::GetViewFor(widgets[i]);
343 if (view) {
344 if (view->mNeedsWindowPropertiesSync) {
345 view->mNeedsWindowPropertiesSync = false;
346 if (nsViewManager* vm = view->GetViewManager()) {
347 if (PresShell* presShell = vm->GetPresShell()) {
348 presShell->SyncWindowProperties(view);
353 view = nsView::GetViewFor(widgets[i]);
354 if (view) {
355 view->ResetWidgetBounds(false, true);
358 if (rootPresShell->GetViewManager() != this) {
359 return; // presentation might have been torn down
361 if (aFlushDirtyRegion) {
362 nsAutoScriptBlocker scriptBlocker;
363 SetPainting(true);
364 for (uint32_t i = 0; i < widgets.Length(); ++i) {
365 nsIWidget* widget = widgets[i];
366 nsView* view = nsView::GetViewFor(widget);
367 if (view) {
368 RefPtr<nsViewManager> viewManager = view->GetViewManager();
369 viewManager->ProcessPendingUpdatesPaint(MOZ_KnownLive(widget));
372 SetPainting(false);
376 void nsViewManager::ProcessPendingUpdatesRecurse(
377 nsView* aView, AutoTArray<nsCOMPtr<nsIWidget>, 1>& aWidgets) {
378 if (mPresShell && mPresShell->IsNeverPainting()) {
379 return;
382 for (nsView* childView = aView->GetFirstChild(); childView;
383 childView = childView->GetNextSibling()) {
384 childView->GetViewManager()->ProcessPendingUpdatesRecurse(childView,
385 aWidgets);
388 nsIWidget* widget = aView->GetWidget();
389 if (widget) {
390 aWidgets.AppendElement(widget);
391 } else {
392 FlushDirtyRegionToWidget(aView);
396 void nsViewManager::ProcessPendingUpdatesPaint(nsIWidget* aWidget) {
397 if (aWidget->NeedsPaint()) {
398 // If an ancestor widget was hidden and then shown, we could
399 // have a delayed resize to handle.
400 for (RefPtr<nsViewManager> vm = this; vm;
401 vm = vm->mRootView->GetParent()
402 ? vm->mRootView->GetParent()->GetViewManager()
403 : nullptr) {
404 if (vm->mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE) &&
405 vm->mRootView->IsEffectivelyVisible() && vm->mPresShell &&
406 vm->mPresShell->IsVisible()) {
407 vm->FlushDelayedResize();
410 nsView* view = nsView::GetViewFor(aWidget);
412 if (!view) {
413 NS_ERROR("FlushDelayedResize destroyed the nsView?");
414 return;
417 nsIWidgetListener* previousListener =
418 aWidget->GetPreviouslyAttachedWidgetListener();
420 if (previousListener && previousListener != view &&
421 view->IsPrimaryFramePaintSuppressed()) {
422 return;
425 if (RefPtr<PresShell> presShell = mPresShell) {
426 #ifdef MOZ_DUMP_PAINTING
427 if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
428 printf_stderr(
429 "---- PAINT START ----PresShell(%p), nsView(%p), nsIWidget(%p)\n",
430 presShell.get(), view, aWidget);
432 #endif
434 presShell->PaintAndRequestComposite(view, PaintFlags::None);
435 view->SetForcedRepaint(false);
437 #ifdef MOZ_DUMP_PAINTING
438 if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
439 printf_stderr("---- PAINT END ----\n");
441 #endif
444 FlushDirtyRegionToWidget(nsView::GetViewFor(aWidget));
447 void nsViewManager::FlushDirtyRegionToWidget(nsView* aView) {
448 NS_ASSERTION(aView->GetViewManager() == this,
449 "FlushDirtyRegionToWidget called on view we don't own");
451 if (!aView->HasNonEmptyDirtyRegion()) {
452 return;
455 nsRegion& dirtyRegion = aView->GetDirtyRegion();
456 nsView* nearestViewWithWidget = aView;
457 while (!nearestViewWithWidget->HasWidget() &&
458 nearestViewWithWidget->GetParent()) {
459 nearestViewWithWidget = nearestViewWithWidget->GetParent();
461 nsRegion r =
462 ConvertRegionBetweenViews(dirtyRegion, aView, nearestViewWithWidget);
464 nsViewManager* widgetVM = nearestViewWithWidget->GetViewManager();
465 widgetVM->InvalidateWidgetArea(nearestViewWithWidget, r);
466 dirtyRegion.SetEmpty();
469 void nsViewManager::InvalidateView(nsView* aView) {
470 // Mark the entire view as damaged
471 InvalidateView(aView, aView->GetDimensions());
474 static void AddDirtyRegion(nsView* aView, const nsRegion& aDamagedRegion) {
475 nsRegion& dirtyRegion = aView->GetDirtyRegion();
476 dirtyRegion.Or(dirtyRegion, aDamagedRegion);
477 dirtyRegion.SimplifyOutward(8);
480 void nsViewManager::PostPendingUpdate() {
481 nsViewManager* rootVM = RootViewManager();
482 rootVM->mHasPendingWidgetGeometryChanges = true;
483 if (rootVM->mPresShell) {
484 rootVM->mPresShell->ScheduleViewManagerFlush();
489 * @param aDamagedRegion this region, relative to aWidgetView, is invalidated in
490 * every widget child of aWidgetView, plus aWidgetView's own widget
492 void nsViewManager::InvalidateWidgetArea(nsView* aWidgetView,
493 const nsRegion& aDamagedRegion) {
494 NS_ASSERTION(aWidgetView->GetViewManager() == this,
495 "InvalidateWidgetArea called on view we don't own");
496 nsIWidget* widget = aWidgetView->GetWidget();
498 #if 0
499 nsRect dbgBounds = aDamagedRegion.GetBounds();
500 printf("InvalidateWidgetArea view:%X (%d) widget:%X region: %d, %d, %d, %d\n",
501 aWidgetView, aWidgetView->IsAttachedToTopLevel(),
502 widget, dbgBounds.x, dbgBounds.y, dbgBounds.width, dbgBounds.height);
503 #endif
505 // If the widget is hidden, it don't cover nothing
506 if (widget && !widget->IsVisible()) {
507 return;
510 if (!widget) {
511 // The root view or a scrolling view might not have a widget
512 // (for example, during printing). We get here when we scroll
513 // during printing to show selected options in a listbox, for example.
514 return;
517 if (!aDamagedRegion.IsEmpty()) {
518 for (auto iter = aDamagedRegion.RectIter(); !iter.Done(); iter.Next()) {
519 LayoutDeviceIntRect bounds = ViewToWidget(aWidgetView, iter.Get());
520 widget->Invalidate(bounds);
525 static bool ShouldIgnoreInvalidation(nsViewManager* aVM) {
526 while (aVM) {
527 PresShell* presShell = aVM->GetPresShell();
528 if (!presShell || presShell->ShouldIgnoreInvalidation()) {
529 return true;
531 nsView* view = aVM->GetRootView()->GetParent();
532 aVM = view ? view->GetViewManager() : nullptr;
534 return false;
537 void nsViewManager::InvalidateView(nsView* aView, const nsRect& aRect) {
538 // If painting is suppressed in the presshell or an ancestor drop all
539 // invalidates, it will invalidate everything when it unsuppresses.
540 if (ShouldIgnoreInvalidation(this)) {
541 return;
544 InvalidateViewNoSuppression(aView, aRect);
547 void nsViewManager::InvalidateViewNoSuppression(nsView* aView,
548 const nsRect& aRect) {
549 MOZ_ASSERT(nullptr != aView, "null view");
551 NS_ASSERTION(aView->GetViewManager() == this,
552 "InvalidateViewNoSuppression called on view we don't own");
554 nsRect damagedRect(aRect);
555 if (damagedRect.IsEmpty()) {
556 return;
559 nsView* displayRoot = GetDisplayRootFor(aView);
560 nsViewManager* displayRootVM = displayRoot->GetViewManager();
561 // Propagate the update to the displayRoot, since iframes, for example,
562 // can overlap each other and be translucent. So we have to possibly
563 // invalidate our rect in each of the widgets we have lying about.
564 damagedRect.MoveBy(aView->GetOffsetTo(displayRoot));
565 int32_t rootAPD = displayRootVM->AppUnitsPerDevPixel();
566 int32_t APD = AppUnitsPerDevPixel();
567 damagedRect = damagedRect.ScaleToOtherAppUnitsRoundOut(APD, rootAPD);
569 // accumulate this rectangle in the view's dirty region, so we can
570 // process it later.
571 AddDirtyRegion(displayRoot, nsRegion(damagedRect));
574 void nsViewManager::InvalidateAllViews() {
575 if (RootViewManager() != this) {
576 return RootViewManager()->InvalidateAllViews();
579 InvalidateViews(mRootView);
582 void nsViewManager::InvalidateViews(nsView* aView) {
583 // Invalidate this view.
584 InvalidateView(aView);
586 // Invalidate all children as well.
587 nsView* childView = aView->GetFirstChild();
588 while (nullptr != childView) {
589 childView->GetViewManager()->InvalidateViews(childView);
590 childView = childView->GetNextSibling();
594 void nsViewManager::WillPaintWindow(nsIWidget* aWidget) {
595 if (aWidget) {
596 nsView* view = nsView::GetViewFor(aWidget);
597 WindowRenderer* renderer = aWidget->GetWindowRenderer();
598 if (view &&
599 (view->ForcedRepaint() || !renderer->NeedsWidgetInvalidation())) {
600 ProcessPendingUpdates();
601 // Re-get the view pointer here since the ProcessPendingUpdates might have
602 // destroyed it during CallWillPaintOnObservers.
603 view = nsView::GetViewFor(aWidget);
604 if (view) {
605 view->SetForcedRepaint(false);
611 bool nsViewManager::PaintWindow(nsIWidget* aWidget,
612 const LayoutDeviceIntRegion& aRegion) {
613 if (!aWidget || !mContext) return false;
615 NS_ASSERTION(
616 IsPaintingAllowed(),
617 "shouldn't be receiving paint events while painting is disallowed!");
619 // Get the view pointer here since NS_WILL_PAINT might have
620 // destroyed it during CallWillPaintOnObservers (bug 378273).
621 nsView* view = nsView::GetViewFor(aWidget);
622 if (view && !aRegion.IsEmpty()) {
623 Refresh(view, aRegion);
626 return true;
629 void nsViewManager::DidPaintWindow() {
630 if (RefPtr<PresShell> presShell = mPresShell) {
631 presShell->DidPaintWindow();
635 void nsViewManager::DispatchEvent(WidgetGUIEvent* aEvent, nsView* aView,
636 nsEventStatus* aStatus) {
637 AUTO_PROFILER_LABEL("nsViewManager::DispatchEvent", OTHER);
639 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
640 if ((mouseEvent &&
641 // Ignore mouse events that we synthesize.
642 mouseEvent->mReason == WidgetMouseEvent::eReal &&
643 // Ignore mouse exit and enter (we'll get moves if the user
644 // is really moving the mouse) since we get them when we
645 // create and destroy widgets.
646 mouseEvent->mMessage != eMouseExitFromWidget &&
647 mouseEvent->mMessage != eMouseEnterIntoWidget) ||
648 aEvent->HasKeyEventMessage() || aEvent->HasIMEEventMessage()) {
649 gLastUserEventTime = PR_IntervalToMicroseconds(PR_IntervalNow());
652 // Find the view whose coordinates system we're in.
653 nsView* view = aView;
654 bool dispatchUsingCoordinates = aEvent->IsUsingCoordinates();
655 if (dispatchUsingCoordinates) {
656 // Will dispatch using coordinates. Pretty bogus but it's consistent
657 // with what presshell does.
658 view = GetDisplayRootFor(view);
661 // If the view has no frame, look for a view that does.
662 nsIFrame* frame = view->GetFrame();
663 if (!frame && (dispatchUsingCoordinates || aEvent->HasKeyEventMessage() ||
664 aEvent->IsIMERelatedEvent())) {
665 while (view && !view->GetFrame()) {
666 view = view->GetParent();
669 if (view) {
670 frame = view->GetFrame();
674 if (nullptr != frame) {
675 // Hold a refcount to the presshell. The continued existence of the
676 // presshell will delay deletion of this view hierarchy should the event
677 // want to cause its destruction in, say, some JavaScript event handler.
678 if (RefPtr<PresShell> presShell = view->GetViewManager()->GetPresShell()) {
679 presShell->HandleEvent(frame, aEvent, false, aStatus);
680 return;
684 *aStatus = nsEventStatus_eIgnore;
687 // Recursively reparent widgets if necessary
689 void nsViewManager::ReparentChildWidgets(nsView* aView, nsIWidget* aNewWidget) {
690 MOZ_ASSERT(aNewWidget, "null widget");
692 if (aView->HasWidget()) {
693 // Check to see if the parent widget is the
694 // same as the new parent. If not then reparent
695 // the widget, otherwise there is nothing more
696 // to do for the view and its descendants
697 nsIWidget* widget = aView->GetWidget();
698 nsIWidget* parentWidget = widget->GetParent();
699 if (parentWidget) {
700 // Child widget
701 if (parentWidget != aNewWidget) {
702 widget->SetParent(aNewWidget);
704 } else {
705 // Toplevel widget (popup, dialog, etc)
706 widget->ReparentNativeWidget(aNewWidget);
708 return;
711 // Need to check each of the views children to see
712 // if they have a widget and reparent it.
714 for (nsView* kid = aView->GetFirstChild(); kid; kid = kid->GetNextSibling()) {
715 ReparentChildWidgets(kid, aNewWidget);
719 // Reparent a view and its descendant views widgets if necessary
721 void nsViewManager::ReparentWidgets(nsView* aView, nsView* aParent) {
722 MOZ_ASSERT(aParent, "Must have a parent");
723 MOZ_ASSERT(aView, "Must have a view");
725 // Quickly determine whether the view has pre-existing children or a
726 // widget. In most cases the view will not have any pre-existing
727 // children when this is called. Only in the case
728 // where a view has been reparented by removing it from
729 // a reinserting it into a new location in the view hierarchy do we
730 // have to consider reparenting the existing widgets for the view and
731 // it's descendants.
732 if (aView->HasWidget() || aView->GetFirstChild()) {
733 nsIWidget* parentWidget = aParent->GetNearestWidget(nullptr);
734 if (parentWidget) {
735 ReparentChildWidgets(aView, parentWidget);
736 return;
738 NS_WARNING("Can not find a widget for the parent view");
742 void nsViewManager::InsertChild(nsView* aParent, nsView* aChild,
743 nsView* aSibling, bool aAfter) {
744 MOZ_ASSERT(nullptr != aParent, "null ptr");
745 MOZ_ASSERT(nullptr != aChild, "null ptr");
746 NS_ASSERTION(aSibling == nullptr || aSibling->GetParent() == aParent,
747 "tried to insert view with invalid sibling");
748 NS_ASSERTION(!IsViewInserted(aChild),
749 "tried to insert an already-inserted view");
751 if ((nullptr != aParent) && (nullptr != aChild)) {
752 // if aAfter is set, we will insert the child after 'prev' (i.e. after 'kid'
753 // in document order, otherwise after 'kid' (i.e. before 'kid' in document
754 // order).
756 if (nullptr == aSibling) {
757 if (aAfter) {
758 // insert at end of document order, i.e., before first view
759 // this is the common case, by far
760 aParent->InsertChild(aChild, nullptr);
761 ReparentWidgets(aChild, aParent);
762 } else {
763 // insert at beginning of document order, i.e., after last view
764 nsView* kid = aParent->GetFirstChild();
765 nsView* prev = nullptr;
766 while (kid) {
767 prev = kid;
768 kid = kid->GetNextSibling();
770 // prev is last view or null if there are no children
771 aParent->InsertChild(aChild, prev);
772 ReparentWidgets(aChild, aParent);
774 } else {
775 nsView* kid = aParent->GetFirstChild();
776 nsView* prev = nullptr;
777 while (kid && aSibling != kid) {
778 // get the next sibling view
779 prev = kid;
780 kid = kid->GetNextSibling();
782 NS_ASSERTION(kid != nullptr, "couldn't find sibling in child list");
783 if (aAfter) {
784 // insert after 'kid' in document order, i.e. before in view order
785 aParent->InsertChild(aChild, prev);
786 ReparentWidgets(aChild, aParent);
787 } else {
788 // insert before 'kid' in document order, i.e. after in view order
789 aParent->InsertChild(aChild, kid);
790 ReparentWidgets(aChild, aParent);
794 // if the parent view is marked as "floating", make the newly added view
795 // float as well.
796 if (aParent->GetFloating()) aChild->SetFloating(true);
800 void nsViewManager::RemoveChild(nsView* aChild) {
801 NS_ASSERTION(aChild, "aChild must not be null");
803 nsView* parent = aChild->GetParent();
805 if (nullptr != parent) {
806 NS_ASSERTION(
807 aChild->GetViewManager() == this || parent->GetViewManager() == this,
808 "wrong view manager");
809 parent->RemoveChild(aChild);
813 void nsViewManager::MoveViewTo(nsView* aView, nscoord aX, nscoord aY) {
814 NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
815 aView->SetPosition(aX, aY);
818 void nsViewManager::ResizeView(nsView* aView, const nsRect& aRect,
819 bool aRepaintExposedAreaOnly) {
820 NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
822 nsRect oldDimensions = aView->GetDimensions();
823 if (!oldDimensions.IsEqualEdges(aRect)) {
824 aView->SetDimensions(aRect, true);
827 // Note that if layout resizes the view and the view has a custom clip
828 // region set, then we expect layout to update the clip region too. Thus
829 // in the case where mClipRect has been optimized away to just be a null
830 // pointer, and this resize is implicitly changing the clip rect, it's OK
831 // because layout will change it back again if necessary.
834 void nsViewManager::SetViewFloating(nsView* aView, bool aFloating) {
835 NS_ASSERTION(!(nullptr == aView), "no view");
837 aView->SetFloating(aFloating);
840 void nsViewManager::SetViewVisibility(nsView* aView,
841 nsViewVisibility aVisible) {
842 NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
844 if (aVisible != aView->GetVisibility()) {
845 aView->SetVisibility(aVisible);
849 bool nsViewManager::IsViewInserted(nsView* aView) {
850 if (mRootView == aView) {
851 return true;
853 if (aView->GetParent() == nullptr) {
854 return false;
856 nsView* view = aView->GetParent()->GetFirstChild();
857 while (view != nullptr) {
858 if (view == aView) {
859 return true;
861 view = view->GetNextSibling();
863 return false;
866 void nsViewManager::SetViewZIndex(nsView* aView, bool aAutoZIndex,
867 int32_t aZIndex) {
868 NS_ASSERTION((aView != nullptr), "no view");
870 // don't allow the root view's z-index to be changed. It should always be
871 // zero. This could be removed and replaced with a style rule, or just removed
872 // altogether, with interesting consequences
873 if (aView == mRootView) {
874 return;
877 if (aAutoZIndex) {
878 aZIndex = 0;
881 aView->SetZIndex(aAutoZIndex, aZIndex);
884 nsViewManager* nsViewManager::IncrementDisableRefreshCount() {
885 if (!IsRootVM()) {
886 return RootViewManager()->IncrementDisableRefreshCount();
889 ++mRefreshDisableCount;
891 return this;
894 void nsViewManager::DecrementDisableRefreshCount() {
895 NS_ASSERTION(IsRootVM(), "Should only be called on root");
896 --mRefreshDisableCount;
897 NS_ASSERTION(mRefreshDisableCount >= 0, "Invalid refresh disable count!");
900 already_AddRefed<nsIWidget> nsViewManager::GetRootWidget() {
901 nsCOMPtr<nsIWidget> rootWidget;
902 if (mRootView) {
903 if (mRootView->HasWidget()) {
904 rootWidget = mRootView->GetWidget();
905 } else if (mRootView->GetParent()) {
906 rootWidget = mRootView->GetParent()->GetViewManager()->GetRootWidget();
909 return rootWidget.forget();
912 LayoutDeviceIntRect nsViewManager::ViewToWidget(nsView* aView,
913 const nsRect& aRect) const {
914 NS_ASSERTION(aView->GetViewManager() == this, "wrong view manager");
916 // account for the view's origin not lining up with the widget's
917 nsRect rect = aRect + aView->ViewToWidgetOffset();
919 // finally, convert to device coordinates.
920 return LayoutDeviceIntRect::FromUnknownRect(
921 rect.ToOutsidePixels(AppUnitsPerDevPixel()));
924 void nsViewManager::IsPainting(bool& aIsPainting) {
925 aIsPainting = IsPainting();
928 void nsViewManager::ProcessPendingUpdates() {
929 if (!IsRootVM()) {
930 RefPtr<nsViewManager> rootViewManager = RootViewManager();
931 rootViewManager->ProcessPendingUpdates();
932 return;
935 // Flush things like reflows by calling WillPaint on observer presShells.
936 if (mPresShell) {
937 mPresShell->GetPresContext()->RefreshDriver()->RevokeViewManagerFlush();
939 RefPtr<nsViewManager> strongThis(this);
940 CallWillPaintOnObservers();
942 ProcessPendingUpdatesForView(mRootView, true);
943 if (mPresShell) {
944 if (nsPresContext* pc = mPresShell->GetPresContext()) {
945 pc->RefreshDriver()->ClearHasScheduleFlush();
951 void nsViewManager::UpdateWidgetGeometry() {
952 if (!IsRootVM()) {
953 RefPtr<nsViewManager> rootViewManager = RootViewManager();
954 rootViewManager->UpdateWidgetGeometry();
955 return;
958 if (mHasPendingWidgetGeometryChanges) {
959 mHasPendingWidgetGeometryChanges = false;
960 ProcessPendingUpdatesForView(mRootView, false);
964 void nsViewManager::CallWillPaintOnObservers() {
965 MOZ_ASSERT(IsRootVM(), "Must be root VM for this to be called!");
967 if (NS_WARN_IF(!gViewManagers)) {
968 return;
971 uint32_t index;
972 for (index = 0; index < gViewManagers->Length(); index++) {
973 nsViewManager* vm = gViewManagers->ElementAt(index);
974 if (vm->RootViewManager() == this) {
975 // One of our kids.
976 if (vm->mRootView && vm->mRootView->IsEffectivelyVisible()) {
977 if (RefPtr<PresShell> presShell = vm->GetPresShell()) {
978 presShell->WillPaint();
985 void nsViewManager::GetLastUserEventTime(uint32_t& aTime) {
986 aTime = gLastUserEventTime;
989 void nsViewManager::InvalidateHierarchy() {
990 if (mRootView) {
991 mRootViewManager = nullptr;
992 nsView* parent = mRootView->GetParent();
993 if (parent) {
994 mRootViewManager = parent->GetViewManager()->RootViewManager();
995 NS_ASSERTION(mRootViewManager != this,
996 "Root view had a parent, but it has the same view manager");
998 // else, we are implicitly our own root view manager.