1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMenuPopupFrame.h"
8 #include "LayoutConstants.h"
9 #include "XULButtonElement.h"
10 #include "XULPopupElement.h"
11 #include "mozilla/dom/XULPopupElement.h"
12 #include "nsGkAtoms.h"
13 #include "nsIContent.h"
14 #include "nsIFrameInlines.h"
16 #include "nsPresContext.h"
17 #include "mozilla/ComputedStyle.h"
18 #include "nsCSSRendering.h"
19 #include "nsNameSpaceManager.h"
20 #include "nsIFrameInlines.h"
21 #include "nsViewManager.h"
22 #include "nsWidgetsCID.h"
23 #include "nsPIDOMWindow.h"
24 #include "nsFrameManager.h"
25 #include "mozilla/dom/Document.h"
27 #include "nsIScrollableFrame.h"
28 #include "nsIPopupContainer.h"
29 #include "nsIDocShell.h"
30 #include "nsReadableUtils.h"
31 #include "nsUnicharUtils.h"
32 #include "nsLayoutUtils.h"
33 #include "nsContentUtils.h"
34 #include "nsCSSFrameConstructor.h"
35 #include "nsPIWindowRoot.h"
36 #include "nsIReflowCallback.h"
37 #include "nsIDocShellTreeOwner.h"
38 #include "nsIBaseWindow.h"
40 #include "nsIScreenManager.h"
41 #include "nsServiceManagerUtils.h"
42 #include "nsStyleConsts.h"
43 #include "nsStyleStructInlines.h"
44 #include "nsTransitionManager.h"
45 #include "nsDisplayList.h"
46 #include "nsIDOMXULSelectCntrlEl.h"
47 #include "mozilla/widget/ScreenManager.h"
48 #include "mozilla/AnimationUtils.h"
49 #include "mozilla/BasePrincipal.h"
50 #include "mozilla/EventDispatcher.h"
51 #include "mozilla/EventStateManager.h"
52 #include "mozilla/Preferences.h"
53 #include "mozilla/LookAndFeel.h"
54 #include "mozilla/MouseEvents.h"
55 #include "mozilla/PresShell.h"
56 #include "mozilla/Services.h"
57 #include "mozilla/dom/BrowserParent.h"
58 #include "mozilla/dom/Element.h"
59 #include "mozilla/dom/Event.h"
60 #include "mozilla/dom/KeyboardEvent.h"
61 #include "mozilla/dom/KeyboardEventBinding.h"
64 #include "X11UndefineNone.h"
65 #include "nsXULPopupManager.h"
67 using namespace mozilla
;
68 using mozilla::dom::Document
;
69 using mozilla::dom::Element
;
70 using mozilla::dom::Event
;
71 using mozilla::dom::XULButtonElement
;
73 int8_t nsMenuPopupFrame::sDefaultLevelIsTop
= -1;
75 TimeStamp
nsMenuPopupFrame::sLastKeyTime
;
78 # include "mozilla/WidgetUtilsGtk.h"
79 # define IS_WAYLAND_DISPLAY() mozilla::widget::GdkIsWaylandDisplay()
80 extern mozilla::LazyLogModule gWidgetPopupLog
;
81 # define LOG_WAYLAND(...) \
82 MOZ_LOG(gWidgetPopupLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
84 # define IS_WAYLAND_DISPLAY() false
85 # define LOG_WAYLAND(...)
88 // NS_NewMenuPopupFrame
90 // Wrapper for creating a new menu popup container
92 nsIFrame
* NS_NewMenuPopupFrame(PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
93 return new (aPresShell
)
94 nsMenuPopupFrame(aStyle
, aPresShell
->GetPresContext());
97 NS_IMPL_FRAMEARENA_HELPERS(nsMenuPopupFrame
)
99 NS_QUERYFRAME_HEAD(nsMenuPopupFrame
)
100 NS_QUERYFRAME_ENTRY(nsMenuPopupFrame
)
101 NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame
)
104 // nsMenuPopupFrame ctor
106 nsMenuPopupFrame::nsMenuPopupFrame(ComputedStyle
* aStyle
,
107 nsPresContext
* aPresContext
)
108 : nsBlockFrame(aStyle
, aPresContext
, kClassID
) {
109 // the preference name is backwards here. True means that the 'top' level is
110 // the default, and false means that the 'parent' level is the default.
111 if (sDefaultLevelIsTop
>= 0) return;
113 Preferences::GetBool("ui.panel.default_level_parent", false);
116 nsMenuPopupFrame::~nsMenuPopupFrame() = default;
118 static bool IsMouseTransparent(const ComputedStyle
& aStyle
) {
119 // If pointer-events: none; is set on the popup, then the widget should
120 // ignore mouse events, passing them through to the content behind.
121 return aStyle
.PointerEvents() == StylePointerEvents::None
;
124 static nsIWidget::InputRegion
ComputeInputRegion(const ComputedStyle
& aStyle
,
125 const nsPresContext
& aPc
) {
126 return {IsMouseTransparent(aStyle
),
127 (aStyle
.StyleUIReset()->mMozWindowInputRegionMargin
.ToCSSPixels() *
128 aPc
.CSSToDevPixelScale())
132 bool nsMenuPopupFrame::ShouldCreateWidgetUpfront() const {
133 if (mPopupType
!= PopupType::Menu
) {
134 // Any panel with a type attribute, such as the autocomplete popup, is
135 // always generated right away.
136 return mContent
->AsElement()->HasAttr(nsGkAtoms::type
);
139 // Generate the widget up-front if the parent menu is a <menulist> unless its
140 // sizetopopup is set to "none".
141 return ShouldExpandToInflowParentOrAnchor();
144 void nsMenuPopupFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
145 nsIFrame
* aPrevInFlow
) {
146 nsBlockFrame::Init(aContent
, aParent
, aPrevInFlow
);
150 // XXX Hack. The popup's view should float above all other views,
151 // so we use the nsView::SetFloating() to tell the view manager
152 // about that constraint.
153 nsView
* ourView
= GetView();
154 nsViewManager
* viewManager
= ourView
->GetViewManager();
155 viewManager
->SetViewFloating(ourView
, true);
157 const auto& el
= PopupElement();
158 mPopupType
= PopupType::Panel
;
160 mPopupType
= PopupType::Menu
;
161 } else if (el
.IsXULElement(nsGkAtoms::tooltip
)) {
162 mPopupType
= PopupType::Tooltip
;
165 if (PresContext()->IsChrome()) {
166 mInContentShell
= false;
169 // Support incontentshell=false attribute to allow popups to be displayed
170 // outside of the content shell. Chrome only.
171 if (el
.NodePrincipal()->IsSystemPrincipal()) {
172 if (el
.GetXULBoolAttr(nsGkAtoms::incontentshell
)) {
173 mInContentShell
= true;
174 } else if (el
.AttrValueIs(kNameSpaceID_None
, nsGkAtoms::incontentshell
,
175 nsGkAtoms::_false
, eCaseMatters
)) {
176 mInContentShell
= false;
180 // To improve performance, create the widget for the popup if needed. Popups
181 // such as menus will create their widgets later when the popup opens.
183 // FIXME(emilio): Doing this up-front for all menupopups causes a bunch of
184 // assertions, while it's supposed to be just an optimization.
185 if (!ourView
->HasWidget() && ShouldCreateWidgetUpfront()) {
186 CreateWidgetForView(ourView
);
189 AddStateBits(NS_FRAME_IN_POPUP
);
192 bool nsMenuPopupFrame::HasRemoteContent() const {
193 return !mInContentShell
&& mPopupType
== PopupType::Panel
&&
194 mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
195 nsGkAtoms::remote
, nsGkAtoms::_true
,
199 bool nsMenuPopupFrame::IsNoAutoHide() const {
200 // Panels with noautohide="true" don't hide when the mouse is clicked
201 // outside of them, or when another application is made active. Non-autohide
202 // panels cannot be used in content windows.
203 return !mInContentShell
&& mPopupType
== PopupType::Panel
&&
204 mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
205 nsGkAtoms::noautohide
,
206 nsGkAtoms::_true
, eIgnoreCase
);
209 widget::PopupLevel
nsMenuPopupFrame::GetPopupLevel(bool aIsNoAutoHide
) const {
210 // The popup level is determined as follows, in this order:
211 // 1. non-panels (menus and tooltips) are always topmost
212 // 2. any specified level attribute
213 // 3. if a titlebar attribute is set, use the 'floating' level
214 // 4. if this is a noautohide panel, use the 'parent' level
215 // 5. use the platform-specific default level
217 // If this is not a panel, this is always a top-most popup.
218 if (mPopupType
!= PopupType::Panel
) {
219 return PopupLevel::Top
;
222 // If the level attribute has been set, use that.
223 static Element::AttrValuesArray strings
[] = {
224 nsGkAtoms::top
, nsGkAtoms::parent
, nsGkAtoms::floating
, nullptr};
225 switch (mContent
->AsElement()->FindAttrValueIn(
226 kNameSpaceID_None
, nsGkAtoms::level
, strings
, eCaseMatters
)) {
228 return PopupLevel::Top
;
230 return PopupLevel::Parent
;
232 return PopupLevel::Floating
;
235 // Panels with titlebars most likely want to be floating popups.
236 if (mContent
->AsElement()->HasAttr(nsGkAtoms::titlebar
)) {
237 return PopupLevel::Floating
;
240 // If this panel is a noautohide panel, the default is the parent level.
242 return PopupLevel::Parent
;
245 // Otherwise, the result depends on the platform.
246 return sDefaultLevelIsTop
? PopupLevel::Top
: PopupLevel::Parent
;
249 void nsMenuPopupFrame::PrepareWidget(bool aRecreate
) {
250 nsView
* ourView
= GetView();
252 if (auto* widget
= GetWidget()) {
253 // Widget's WebRender resources needs to be cleared before creating new
255 widget
->ClearCachedWebrenderResources();
257 ourView
->DestroyWidget();
259 if (!ourView
->HasWidget()) {
260 CreateWidgetForView(ourView
);
262 PropagateStyleToWidget();
266 nsresult
nsMenuPopupFrame::CreateWidgetForView(nsView
* aView
) {
267 // Create a widget for ourselves.
268 widget::InitData widgetData
;
269 widgetData
.mWindowType
= widget::WindowType::Popup
;
270 widgetData
.mBorderStyle
= widget::BorderStyle::Default
;
271 widgetData
.mForMenupopupFrame
= true;
272 widgetData
.mClipSiblings
= true;
273 widgetData
.mPopupHint
= mPopupType
;
274 widgetData
.mNoAutoHide
= IsNoAutoHide();
276 if (!mInContentShell
) {
277 // A drag popup may be used for non-static translucent drag feedback
278 if (mPopupType
== PopupType::Panel
&&
279 mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
280 nsGkAtoms::drag
, eIgnoreCase
)) {
281 widgetData
.mIsDragPopup
= true;
286 if (widgetData
.mNoAutoHide
&&
287 mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::titlebar
,
288 nsGkAtoms::normal
, eCaseMatters
)) {
289 widgetData
.mBorderStyle
= widget::BorderStyle::Title
;
291 mContent
->AsElement()->GetAttr(nsGkAtoms::label
, title
);
292 if (mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::close
,
293 nsGkAtoms::_true
, eCaseMatters
)) {
294 widgetData
.mBorderStyle
=
295 widgetData
.mBorderStyle
| widget::BorderStyle::Close
;
299 bool remote
= HasRemoteContent();
301 const auto mode
= nsLayoutUtils::GetFrameTransparency(this, this);
302 widgetData
.mHasRemoteContent
= remote
;
303 widgetData
.mTransparencyMode
= mode
;
304 widgetData
.mPopupLevel
= GetPopupLevel(widgetData
.mNoAutoHide
);
306 // Panels which have a parent level need a parent widget. This allows them to
307 // always appear in front of the parent window but behind other windows that
308 // should be in front of it.
309 nsCOMPtr
<nsIWidget
> parentWidget
;
310 if (widgetData
.mPopupLevel
!= PopupLevel::Top
) {
311 nsCOMPtr
<nsIDocShellTreeItem
> dsti
= PresContext()->GetDocShell();
312 if (!dsti
) return NS_ERROR_FAILURE
;
314 nsCOMPtr
<nsIDocShellTreeOwner
> treeOwner
;
315 dsti
->GetTreeOwner(getter_AddRefs(treeOwner
));
316 if (!treeOwner
) return NS_ERROR_FAILURE
;
318 nsCOMPtr
<nsIBaseWindow
> baseWindow(do_QueryInterface(treeOwner
));
319 if (baseWindow
) baseWindow
->GetMainWidget(getter_AddRefs(parentWidget
));
322 nsresult rv
= aView
->CreateWidgetForPopup(&widgetData
, parentWidget
);
327 nsIWidget
* widget
= aView
->GetWidget();
328 widget
->SetTransparencyMode(mode
);
330 PropagateStyleToWidget();
332 // most popups don't have a title so avoid setting the title if there isn't
334 if (!title
.IsEmpty()) {
335 widget
->SetTitle(title
);
341 void nsMenuPopupFrame::PropagateStyleToWidget(WidgetStyleFlags aFlags
) const {
342 if (aFlags
.isEmpty()) {
346 nsIWidget
* widget
= GetWidget();
351 if (aFlags
.contains(WidgetStyle::ColorScheme
)) {
352 widget
->SetColorScheme(Some(LookAndFeel::ColorSchemeForFrame(this)));
354 if (aFlags
.contains(WidgetStyle::InputRegion
)) {
355 widget
->SetInputRegion(ComputeInputRegion(*Style(), *PresContext()));
357 if (aFlags
.contains(WidgetStyle::Opacity
)) {
358 widget
->SetWindowOpacity(StyleUIReset()->mWindowOpacity
);
360 if (aFlags
.contains(WidgetStyle::Shadow
)) {
361 widget
->SetWindowShadowStyle(GetShadowStyle());
363 if (aFlags
.contains(WidgetStyle::Transform
)) {
364 widget
->SetWindowTransform(ComputeWidgetTransform());
368 bool nsMenuPopupFrame::IsMouseTransparent() const {
369 return ::IsMouseTransparent(*Style());
372 WindowShadow
nsMenuPopupFrame::GetShadowStyle() const {
373 StyleWindowShadow shadow
= StyleUIReset()->mWindowShadow
;
374 if (shadow
!= StyleWindowShadow::Auto
) {
375 MOZ_ASSERT(shadow
== StyleWindowShadow::None
);
376 return WindowShadow::None
;
379 switch (StyleDisplay()->EffectiveAppearance()) {
380 case StyleAppearance::Tooltip
:
381 return WindowShadow::Tooltip
;
382 case StyleAppearance::Menupopup
:
383 return WindowShadow::Menu
;
385 return WindowShadow::Panel
;
389 void nsMenuPopupFrame::SetPopupState(nsPopupState aState
) {
390 mPopupState
= aState
;
392 // Work around https://gitlab.gnome.org/GNOME/gtk/-/issues/4166
393 if (aState
== ePopupShown
&& IS_WAYLAND_DISPLAY()) {
394 if (nsIWidget
* widget
= GetWidget()) {
395 widget
->SetInputRegion(ComputeInputRegion(*Style(), *PresContext()));
400 // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230, bug 1535398)
401 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
nsXULPopupShownEvent::Run() {
402 nsMenuPopupFrame
* popup
= do_QueryFrame(mPopup
->GetPrimaryFrame());
403 // Set the state to visible if the popup is still open.
404 if (popup
&& popup
->IsOpen()) {
405 popup
->SetPopupState(ePopupShown
);
408 if (!mPopup
->IsXULElement(nsGkAtoms::tooltip
)) {
409 nsCOMPtr
<nsIObserverService
> obsService
=
410 mozilla::services::GetObserverService();
412 obsService
->NotifyObservers(mPopup
, "popup-shown", nullptr);
415 WidgetMouseEvent
event(true, eXULPopupShown
, nullptr,
416 WidgetMouseEvent::eReal
);
417 return EventDispatcher::Dispatch(mPopup
, mPresContext
, &event
);
420 NS_IMETHODIMP
nsXULPopupShownEvent::HandleEvent(Event
* aEvent
) {
421 nsMenuPopupFrame
* popup
= do_QueryFrame(mPopup
->GetPrimaryFrame());
422 // Ignore events not targeted at the popup itself (ie targeted at
424 if (mPopup
!= aEvent
->GetTarget()) {
428 // ResetPopupShownDispatcher will delete the reference to this, so keep
429 // another one until Run is finished.
430 RefPtr
<nsXULPopupShownEvent
> event
= this;
431 // Only call Run if it the dispatcher was assigned. This avoids calling the
432 // Run method if the transitionend event fires multiple times.
433 if (popup
->ClearPopupShownDispatcher()) {
442 void nsXULPopupShownEvent::CancelListener() {
443 mPopup
->RemoveSystemEventListener(u
"transitionend"_ns
, this, false);
446 NS_IMPL_ISUPPORTS_INHERITED(nsXULPopupShownEvent
, Runnable
,
447 nsIDOMEventListener
);
449 void nsMenuPopupFrame::DidSetComputedStyle(ComputedStyle
* aOldStyle
) {
450 nsBlockFrame::DidSetComputedStyle(aOldStyle
);
456 WidgetStyleFlags flags
;
458 if (aOldStyle
->StyleUI()->mColorScheme
!= StyleUI()->mColorScheme
) {
459 flags
+= WidgetStyle::ColorScheme
;
462 auto& newUI
= *StyleUIReset();
463 auto& oldUI
= *aOldStyle
->StyleUIReset();
464 if (newUI
.mWindowOpacity
!= oldUI
.mWindowOpacity
) {
465 flags
+= WidgetStyle::Opacity
;
468 if (newUI
.mMozWindowTransform
!= oldUI
.mMozWindowTransform
) {
469 flags
+= WidgetStyle::Transform
;
472 if (newUI
.mWindowShadow
!= oldUI
.mWindowShadow
) {
473 flags
+= WidgetStyle::Shadow
;
476 const auto& pc
= *PresContext();
477 auto oldRegion
= ComputeInputRegion(*aOldStyle
, pc
);
478 auto newRegion
= ComputeInputRegion(*Style(), pc
);
479 if (oldRegion
.mFullyTransparent
!= newRegion
.mFullyTransparent
||
480 oldRegion
.mMargin
!= newRegion
.mMargin
) {
481 flags
+= WidgetStyle::InputRegion
;
484 PropagateStyleToWidget(flags
);
487 void nsMenuPopupFrame::TweakMinPrefISize(nscoord
& aSize
) {
488 if (!ShouldExpandToInflowParentOrAnchor()) {
491 // Make sure to accommodate for our scrollbar if needed. Do it only for
492 // menulists to match previous behavior.
494 // NOTE(emilio): This is somewhat hacky. The "right" fix (which would be
495 // using scrollbar-gutter: stable on the scroller) isn't great, because even
496 // though we want a stable gutter, we want to draw on top of the gutter when
497 // there's no scrollbar, otherwise it looks rather weird.
499 // Automatically accommodating for the scrollbar otherwise would be bug
500 // 764076, but that has its own set of problems.
501 if (nsIScrollableFrame
* sf
= GetScrollFrame()) {
502 aSize
+= sf
->GetDesiredScrollbarSizes().LeftRight();
505 nscoord menuListOrAnchorWidth
= 0;
506 if (nsIFrame
* menuList
= GetInFlowParent()) {
507 menuListOrAnchorWidth
= menuList
->GetRect().width
;
509 if (mAnchorType
== MenuPopupAnchorType_Rect
) {
510 menuListOrAnchorWidth
= std::max(menuListOrAnchorWidth
, mScreenRect
.width
);
512 // Input margin doesn't have contents, so account for it for popup sizing
514 menuListOrAnchorWidth
+=
515 2 * StyleUIReset()->mMozWindowInputRegionMargin
.ToAppUnits();
516 aSize
= std::max(aSize
, menuListOrAnchorWidth
);
519 nscoord
nsMenuPopupFrame::GetMinISize(gfxContext
* aRC
) {
521 DISPLAY_PREF_INLINE_SIZE(this, result
);
523 result
= nsBlockFrame::GetMinISize(aRC
);
524 TweakMinPrefISize(result
);
528 nscoord
nsMenuPopupFrame::GetPrefISize(gfxContext
* aRC
) {
530 DISPLAY_PREF_INLINE_SIZE(this, result
);
532 result
= nsBlockFrame::GetPrefISize(aRC
);
533 TweakMinPrefISize(result
);
537 void nsMenuPopupFrame::Reflow(nsPresContext
* aPresContext
,
538 ReflowOutput
& aDesiredSize
,
539 const ReflowInput
& aReflowInput
,
540 nsReflowStatus
& aStatus
) {
542 DO_GLOBAL_REFLOW_COUNT("nsMenuPopupFrame");
543 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aDesiredSize
, aStatus
);
544 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
546 const auto wm
= GetWritingMode();
547 // Default to preserving our bounds.
548 aDesiredSize
.SetSize(wm
, GetLogicalSize(wm
));
550 LayoutPopup(aPresContext
, aDesiredSize
, aReflowInput
, aStatus
);
552 aDesiredSize
.SetBlockStartAscent(aDesiredSize
.BSize(wm
));
553 aDesiredSize
.SetOverflowAreasToDesiredBounds();
554 FinishAndStoreOverflow(&aDesiredSize
, aReflowInput
.mStyleDisplay
);
557 void nsMenuPopupFrame::EnsureActiveMenuListItemIsVisible() {
558 if (!IsMenuList() || !IsOpen()) {
561 nsIFrame
* frame
= GetCurrentMenuItemFrame();
565 RefPtr
<mozilla::PresShell
> presShell
= PresShell();
566 presShell
->ScrollFrameIntoView(
567 frame
, Nothing(), ScrollAxis(), ScrollAxis(),
568 ScrollFlags::ScrollOverflowHidden
| ScrollFlags::ScrollFirstAncestorOnly
);
571 void nsMenuPopupFrame::LayoutPopup(nsPresContext
* aPresContext
,
572 ReflowOutput
& aDesiredSize
,
573 const ReflowInput
& aReflowInput
,
574 nsReflowStatus
& aStatus
) {
575 if (IsNativeMenu()) {
581 const bool isOpen
= IsOpen();
583 // If the popup is not open, only do layout while showing or if we're a
586 // This is needed because the SelectParent code wants to limit the height of
587 // the popup before opening it.
589 // TODO(emilio): We should consider adding a way to do that more reliably
590 // instead, but this preserves existing behavior.
591 const bool needsLayout
= mPopupState
== ePopupShowing
||
592 mPopupState
== ePopupPositioning
|| IsMenuList();
594 RemoveStateBits(NS_FRAME_FIRST_REFLOW
);
599 // Do a first reflow, with all our content, in order to find our preferred
600 // size. Then, we do a second reflow with the updated dimensions.
601 const bool needsPrefSize
= mPrefSize
== nsSize(-1, -1) || IsSubtreeDirty();
603 // Get the preferred, minimum and maximum size. If the menu is sized to the
604 // popup, then the popup's width is the menu's width.
605 ReflowOutput
preferredSize(aReflowInput
);
606 nsBlockFrame::Reflow(aPresContext
, preferredSize
, aReflowInput
, aStatus
);
607 mPrefSize
= preferredSize
.PhysicalSize();
610 // Get our desired position and final size, now that we have a preferred size.
611 auto constraints
= GetRects(mPrefSize
);
612 const auto finalSize
= constraints
.mUsedRect
.Size();
614 // We need to do an extra reflow if we haven't reflowed, our size doesn't
615 // match with our final intended size, or our bsize is unconstrained (in which
616 // case we need to specify the final size so that percentages work).
617 const bool needDefiniteReflow
=
618 aReflowInput
.ComputedBSize() == NS_UNCONSTRAINEDSIZE
|| !needsPrefSize
||
619 finalSize
!= mPrefSize
;
621 if (needDefiniteReflow
) {
622 ReflowInput
constrainedReflowInput(aReflowInput
);
623 const auto& bp
= aReflowInput
.ComputedPhysicalBorderPadding();
624 // TODO: writing-mode handling not terribly correct, but it doesn't matter.
625 const nsSize
finalContentSize(finalSize
.width
- bp
.LeftRight(),
626 finalSize
.height
- bp
.TopBottom());
627 constrainedReflowInput
.SetComputedISize(finalContentSize
.width
);
628 constrainedReflowInput
.SetComputedBSize(finalContentSize
.height
);
629 constrainedReflowInput
.SetIResize(finalSize
.width
!= mPrefSize
.width
);
630 constrainedReflowInput
.SetBResize([&] {
631 if (finalSize
.height
!= mPrefSize
.height
) {
635 aReflowInput
.ComputedBSize() == NS_UNCONSTRAINEDSIZE
&&
636 aReflowInput
.ComputedMaxBSize() == finalContentSize
.height
) {
637 // If we have measured, and maybe clamped our children via max-height,
638 // they might need to get percentages in the block axis re-resolved.
645 nsBlockFrame::Reflow(aPresContext
, aDesiredSize
, constrainedReflowInput
,
649 // Set our size, since nsAbsoluteContainingBlock won't.
650 SetRect(constraints
.mUsedRect
);
652 nsView
* view
= GetView();
654 nsViewManager
* viewManager
= view
->GetViewManager();
655 viewManager
->ResizeView(view
,
656 nsRect(nsPoint(), constraints
.mUsedRect
.Size()));
657 if (mPopupState
== ePopupOpening
) {
658 mPopupState
= ePopupVisible
;
661 viewManager
->SetViewVisibility(view
, ViewVisibility::Show
);
662 SyncFrameViewProperties(view
);
665 // Perform our move now. That will position the view and so on.
666 PerformMove(constraints
);
668 // finally, if the popup just opened, send a popupshown event
669 bool openChanged
= mIsOpenChanged
;
671 mIsOpenChanged
= false;
673 // Make sure the current selection in a menulist is visible.
674 EnsureActiveMenuListItemIsVisible();
676 // If the animate attribute is set to open, check for a transition and wait
677 // for it to finish before firing the popupshown event.
678 if (LookAndFeel::GetInt(LookAndFeel::IntID::PanelAnimations
) &&
679 mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
680 nsGkAtoms::animate
, nsGkAtoms::open
,
682 AnimationUtils::HasCurrentTransitions(mContent
->AsElement(),
683 PseudoStyleType::NotPseudo
)) {
684 mPopupShownDispatcher
= new nsXULPopupShownEvent(mContent
, aPresContext
);
685 mContent
->AddSystemEventListener(u
"transitionend"_ns
,
686 mPopupShownDispatcher
, false, false);
690 // If there are no transitions, fire the popupshown event right away.
691 nsCOMPtr
<nsIRunnable
> event
=
692 new nsXULPopupShownEvent(GetContent(), aPresContext
);
693 mContent
->OwnerDoc()->Dispatch(event
.forget());
697 bool nsMenuPopupFrame::IsMenuList() const {
698 return PopupElement().IsInMenuList();
701 bool nsMenuPopupFrame::ShouldExpandToInflowParentOrAnchor() const {
702 return IsMenuList() && !mContent
->GetParent()->AsElement()->AttrValueIs(
703 kNameSpaceID_None
, nsGkAtoms::sizetopopup
,
704 nsGkAtoms::none
, eCaseMatters
);
707 nsIContent
* nsMenuPopupFrame::GetTriggerContent(
708 nsMenuPopupFrame
* aMenuPopupFrame
) {
709 while (aMenuPopupFrame
) {
710 if (aMenuPopupFrame
->mTriggerContent
) {
711 return aMenuPopupFrame
->mTriggerContent
;
714 auto* button
= XULButtonElement::FromNodeOrNull(
715 aMenuPopupFrame
->GetContent()->GetParent());
716 if (!button
|| !button
->IsMenu()) {
720 auto* popup
= button
->GetContainingPopupElement();
725 // check up the menu hierarchy until a popup with a trigger node is found
726 aMenuPopupFrame
= do_QueryFrame(popup
->GetPrimaryFrame());
732 void nsMenuPopupFrame::InitPositionFromAnchorAlign(const nsAString
& aAnchor
,
733 const nsAString
& aAlign
) {
734 mTriggerContent
= nullptr;
736 if (aAnchor
.EqualsLiteral("topleft"))
737 mPopupAnchor
= POPUPALIGNMENT_TOPLEFT
;
738 else if (aAnchor
.EqualsLiteral("topright"))
739 mPopupAnchor
= POPUPALIGNMENT_TOPRIGHT
;
740 else if (aAnchor
.EqualsLiteral("bottomleft"))
741 mPopupAnchor
= POPUPALIGNMENT_BOTTOMLEFT
;
742 else if (aAnchor
.EqualsLiteral("bottomright"))
743 mPopupAnchor
= POPUPALIGNMENT_BOTTOMRIGHT
;
744 else if (aAnchor
.EqualsLiteral("leftcenter"))
745 mPopupAnchor
= POPUPALIGNMENT_LEFTCENTER
;
746 else if (aAnchor
.EqualsLiteral("rightcenter"))
747 mPopupAnchor
= POPUPALIGNMENT_RIGHTCENTER
;
748 else if (aAnchor
.EqualsLiteral("topcenter"))
749 mPopupAnchor
= POPUPALIGNMENT_TOPCENTER
;
750 else if (aAnchor
.EqualsLiteral("bottomcenter"))
751 mPopupAnchor
= POPUPALIGNMENT_BOTTOMCENTER
;
753 mPopupAnchor
= POPUPALIGNMENT_NONE
;
755 if (aAlign
.EqualsLiteral("topleft"))
756 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
757 else if (aAlign
.EqualsLiteral("topright"))
758 mPopupAlignment
= POPUPALIGNMENT_TOPRIGHT
;
759 else if (aAlign
.EqualsLiteral("bottomleft"))
760 mPopupAlignment
= POPUPALIGNMENT_BOTTOMLEFT
;
761 else if (aAlign
.EqualsLiteral("bottomright"))
762 mPopupAlignment
= POPUPALIGNMENT_BOTTOMRIGHT
;
763 else if (aAlign
.EqualsLiteral("leftcenter"))
764 mPopupAlignment
= POPUPALIGNMENT_LEFTCENTER
;
765 else if (aAlign
.EqualsLiteral("rightcenter"))
766 mPopupAlignment
= POPUPALIGNMENT_RIGHTCENTER
;
767 else if (aAlign
.EqualsLiteral("topcenter"))
768 mPopupAlignment
= POPUPALIGNMENT_TOPCENTER
;
769 else if (aAlign
.EqualsLiteral("bottomcenter"))
770 mPopupAlignment
= POPUPALIGNMENT_BOTTOMCENTER
;
772 mPopupAlignment
= POPUPALIGNMENT_NONE
;
774 mPosition
= POPUPPOSITION_UNKNOWN
;
777 static FlipType
FlipFromAttribute(nsMenuPopupFrame
* aFrame
) {
779 aFrame
->PopupElement().GetAttr(nsGkAtoms::flip
, flip
);
780 if (flip
.EqualsLiteral("none")) {
781 return FlipType_None
;
783 if (flip
.EqualsLiteral("both")) {
784 return FlipType_Both
;
786 if (flip
.EqualsLiteral("slide")) {
787 return FlipType_Slide
;
789 return FlipType_Default
;
792 void nsMenuPopupFrame::InitializePopup(nsIContent
* aAnchorContent
,
793 nsIContent
* aTriggerContent
,
794 const nsAString
& aPosition
,
795 int32_t aXPos
, int32_t aYPos
,
796 MenuPopupAnchorType aAnchorType
,
797 bool aAttributesOverride
) {
798 auto* widget
= GetWidget();
799 bool recreateWidget
= widget
&& widget
->NeedsRecreateToReshow();
800 PrepareWidget(recreateWidget
);
802 mPopupState
= ePopupShowing
;
803 mAnchorContent
= aAnchorContent
;
804 mTriggerContent
= aTriggerContent
;
807 mIsNativeMenu
= false;
808 mIsTopLevelContextMenu
= false;
811 mConstrainedByLayout
= false;
812 mAlignmentOffset
= 0;
813 mPositionedOffset
= 0;
814 mPositionedByMoveToRect
= false;
816 mAnchorType
= aAnchorType
;
818 // if aAttributesOverride is true, then the popupanchor, popupalign and
819 // position attributes on the <menupopup> override those values passed in.
820 // If false, those attributes are only used if the values passed in are empty
821 if (aAnchorContent
|| aAnchorType
== MenuPopupAnchorType_Rect
) {
822 nsAutoString anchor
, align
, position
;
823 mContent
->AsElement()->GetAttr(nsGkAtoms::popupanchor
, anchor
);
824 mContent
->AsElement()->GetAttr(nsGkAtoms::popupalign
, align
);
825 mContent
->AsElement()->GetAttr(nsGkAtoms::position
, position
);
827 if (aAttributesOverride
) {
828 // if the attributes are set, clear the offset position. Otherwise,
829 // the offset is used to adjust the position from the anchor point
830 if (anchor
.IsEmpty() && align
.IsEmpty() && position
.IsEmpty())
831 position
.Assign(aPosition
);
834 } else if (!aPosition
.IsEmpty()) {
835 position
.Assign(aPosition
);
838 mFlip
= FlipFromAttribute(this);
840 position
.CompressWhitespace();
841 int32_t spaceIdx
= position
.FindChar(' ');
842 // if there is a space in the position, assume it is the anchor and
843 // alignment as two separate tokens.
845 InitPositionFromAnchorAlign(Substring(position
, 0, spaceIdx
),
846 Substring(position
, spaceIdx
+ 1));
847 } else if (position
.EqualsLiteral("before_start")) {
848 mPopupAnchor
= POPUPALIGNMENT_TOPLEFT
;
849 mPopupAlignment
= POPUPALIGNMENT_BOTTOMLEFT
;
850 mPosition
= POPUPPOSITION_BEFORESTART
;
851 } else if (position
.EqualsLiteral("before_end")) {
852 mPopupAnchor
= POPUPALIGNMENT_TOPRIGHT
;
853 mPopupAlignment
= POPUPALIGNMENT_BOTTOMRIGHT
;
854 mPosition
= POPUPPOSITION_BEFOREEND
;
855 } else if (position
.EqualsLiteral("after_start")) {
856 mPopupAnchor
= POPUPALIGNMENT_BOTTOMLEFT
;
857 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
858 mPosition
= POPUPPOSITION_AFTERSTART
;
859 } else if (position
.EqualsLiteral("after_end")) {
860 mPopupAnchor
= POPUPALIGNMENT_BOTTOMRIGHT
;
861 mPopupAlignment
= POPUPALIGNMENT_TOPRIGHT
;
862 mPosition
= POPUPPOSITION_AFTEREND
;
863 } else if (position
.EqualsLiteral("start_before")) {
864 mPopupAnchor
= POPUPALIGNMENT_TOPLEFT
;
865 mPopupAlignment
= POPUPALIGNMENT_TOPRIGHT
;
866 mPosition
= POPUPPOSITION_STARTBEFORE
;
867 } else if (position
.EqualsLiteral("start_after")) {
868 mPopupAnchor
= POPUPALIGNMENT_BOTTOMLEFT
;
869 mPopupAlignment
= POPUPALIGNMENT_BOTTOMRIGHT
;
870 mPosition
= POPUPPOSITION_STARTAFTER
;
871 } else if (position
.EqualsLiteral("end_before")) {
872 mPopupAnchor
= POPUPALIGNMENT_TOPRIGHT
;
873 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
874 mPosition
= POPUPPOSITION_ENDBEFORE
;
875 } else if (position
.EqualsLiteral("end_after")) {
876 mPopupAnchor
= POPUPALIGNMENT_BOTTOMRIGHT
;
877 mPopupAlignment
= POPUPALIGNMENT_BOTTOMLEFT
;
878 mPosition
= POPUPPOSITION_ENDAFTER
;
879 } else if (position
.EqualsLiteral("overlap")) {
880 mPopupAnchor
= POPUPALIGNMENT_TOPLEFT
;
881 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
882 mPosition
= POPUPPOSITION_OVERLAP
;
883 } else if (position
.EqualsLiteral("after_pointer")) {
884 mPopupAnchor
= POPUPALIGNMENT_TOPLEFT
;
885 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
886 mPosition
= POPUPPOSITION_AFTERPOINTER
;
887 // XXXndeakin this is supposed to anchor vertically after, but with the
888 // horizontal position as the mouse pointer.
890 } else if (position
.EqualsLiteral("selection")) {
891 mPopupAnchor
= POPUPALIGNMENT_BOTTOMLEFT
;
892 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
893 mPosition
= POPUPPOSITION_SELECTION
;
895 InitPositionFromAnchorAlign(anchor
, align
);
898 // When converted back to CSSIntRect it is (-1, -1, 0, 0) - as expected in
899 // nsXULPopupManager::Rollup
900 mScreenRect
= nsRect(-AppUnitsPerCSSPixel(), -AppUnitsPerCSSPixel(), 0, 0);
902 if (aAttributesOverride
) {
903 // Use |left| and |top| dimension attributes to position the popup if
904 // present, as they may have been persisted.
905 nsAutoString left
, top
;
906 mContent
->AsElement()->GetAttr(nsGkAtoms::left
, left
);
907 mContent
->AsElement()->GetAttr(nsGkAtoms::top
, top
);
910 if (!left
.IsEmpty()) {
911 int32_t x
= left
.ToInteger(&err
);
912 if (NS_SUCCEEDED(err
)) {
913 mScreenRect
.x
= CSSPixel::ToAppUnits(x
);
916 if (!top
.IsEmpty()) {
917 int32_t y
= top
.ToInteger(&err
);
918 if (NS_SUCCEEDED(err
)) {
919 mScreenRect
.y
= CSSPixel::ToAppUnits(y
);
925 void nsMenuPopupFrame::InitializePopupAtScreen(nsIContent
* aTriggerContent
,
926 int32_t aXPos
, int32_t aYPos
,
927 bool aIsContextMenu
) {
928 auto* widget
= GetWidget();
929 bool recreateWidget
= widget
&& widget
->NeedsRecreateToReshow();
930 PrepareWidget(recreateWidget
);
932 mPopupState
= ePopupShowing
;
933 mAnchorContent
= nullptr;
934 mTriggerContent
= aTriggerContent
;
936 nsRect(CSSPixel::ToAppUnits(aXPos
), CSSPixel::ToAppUnits(aYPos
), 0, 0);
939 mFlip
= FlipFromAttribute(this);
940 mPopupAnchor
= POPUPALIGNMENT_NONE
;
941 mPopupAlignment
= POPUPALIGNMENT_NONE
;
942 mPosition
= POPUPPOSITION_UNKNOWN
;
943 mIsContextMenu
= aIsContextMenu
;
944 mIsTopLevelContextMenu
= aIsContextMenu
;
945 mIsNativeMenu
= false;
946 mAnchorType
= MenuPopupAnchorType_Point
;
947 mPositionedOffset
= 0;
948 mPositionedByMoveToRect
= false;
951 void nsMenuPopupFrame::InitializePopupAsNativeContextMenu(
952 nsIContent
* aTriggerContent
, int32_t aXPos
, int32_t aYPos
) {
953 mTriggerContent
= aTriggerContent
;
954 mPopupState
= ePopupShowing
;
955 mAnchorContent
= nullptr;
957 nsRect(CSSPixel::ToAppUnits(aXPos
), CSSPixel::ToAppUnits(aYPos
), 0, 0);
960 mFlip
= FlipType_Default
;
961 mPopupAnchor
= POPUPALIGNMENT_NONE
;
962 mPopupAlignment
= POPUPALIGNMENT_NONE
;
963 mPosition
= POPUPPOSITION_UNKNOWN
;
964 mIsContextMenu
= true;
965 mIsTopLevelContextMenu
= true;
966 mIsNativeMenu
= true;
967 mAnchorType
= MenuPopupAnchorType_Point
;
968 mPositionedOffset
= 0;
969 mPositionedByMoveToRect
= false;
972 void nsMenuPopupFrame::InitializePopupAtRect(nsIContent
* aTriggerContent
,
973 const nsAString
& aPosition
,
974 const nsIntRect
& aRect
,
975 bool aAttributesOverride
) {
976 InitializePopup(nullptr, aTriggerContent
, aPosition
, 0, 0,
977 MenuPopupAnchorType_Rect
, aAttributesOverride
);
978 mScreenRect
= ToAppUnits(aRect
, AppUnitsPerCSSPixel());
981 void nsMenuPopupFrame::ShowPopup(bool aIsContextMenu
) {
982 mIsContextMenu
= aIsContextMenu
;
984 InvalidateFrameSubtree();
986 if (mPopupState
== ePopupShowing
|| mPopupState
== ePopupPositioning
) {
987 mPopupState
= ePopupOpening
;
988 mIsOpenChanged
= true;
990 // Clear mouse capture when a popup is opened.
991 if (mPopupType
== PopupType::Menu
) {
992 if (auto* activeESM
= EventStateManager::GetActiveEventStateManager()) {
993 EventStateManager::ClearGlobalActiveContent(activeESM
);
996 PresShell::ReleaseCapturingContent();
999 if (RefPtr menu
= PopupElement().GetContainingMenu()) {
1000 menu
->PopupOpened();
1003 // We skip laying out children if we're closed, so make sure that we do a
1004 // full dirty reflow when opening to pick up any potential change.
1005 PresShell()->FrameNeedsReflow(
1006 this, IntrinsicDirty::FrameAncestorsAndDescendants
, NS_FRAME_IS_DIRTY
);
1008 if (mPopupType
== PopupType::Menu
) {
1009 nsCOMPtr
<nsISound
> sound(do_GetService("@mozilla.org/sound;1"));
1010 if (sound
) sound
->PlayEventSound(nsISound::EVENT_MENU_POPUP
);
1015 void nsMenuPopupFrame::ClearTriggerContentIncludingDocument() {
1016 // clear the trigger content if the popup is being closed. But don't clear
1017 // it if the popup is just being made invisible as a popuphiding or command
1018 if (mTriggerContent
) {
1019 // if the popup had a trigger node set, clear the global window popup node
1021 Document
* doc
= mContent
->GetUncomposedDoc();
1023 if (nsPIDOMWindowOuter
* win
= doc
->GetWindow()) {
1024 nsCOMPtr
<nsPIWindowRoot
> root
= win
->GetTopWindowRoot();
1026 root
->SetPopupNode(nullptr);
1031 mTriggerContent
= nullptr;
1034 void nsMenuPopupFrame::HidePopup(bool aDeselectMenu
, nsPopupState aNewState
,
1035 bool aFromFrameDestruction
) {
1036 NS_ASSERTION(aNewState
== ePopupClosed
|| aNewState
== ePopupInvisible
,
1037 "popup being set to unexpected state");
1039 ClearPopupShownDispatcher();
1041 // don't hide the popup when it isn't open
1042 if (mPopupState
== ePopupClosed
|| mPopupState
== ePopupShowing
||
1043 mPopupState
== ePopupPositioning
) {
1047 if (aNewState
== ePopupClosed
) {
1048 // clear the trigger content if the popup is being closed. But don't clear
1049 // it if the popup is just being made invisible as a popuphiding or command
1050 // event may want to retrieve it.
1051 ClearTriggerContentIncludingDocument();
1052 mAnchorContent
= nullptr;
1055 // when invisible and about to be closed, HidePopup has already been called,
1056 // so just set the new state to closed and return
1057 if (mPopupState
== ePopupInvisible
) {
1058 if (aNewState
== ePopupClosed
) {
1059 mPopupState
= ePopupClosed
;
1064 mPopupState
= aNewState
;
1066 mIncrementalString
.Truncate();
1068 mIsOpenChanged
= false;
1069 mHFlip
= mVFlip
= false;
1070 mConstrainedByLayout
= false;
1072 if (auto* widget
= GetWidget()) {
1073 // Ideally we should call ClearCachedWebrenderResources but there are
1074 // intermittent failures (see bug 1748788), so we currently call
1075 // ClearWebrenderAnimationResources instead.
1076 widget
->ClearWebrenderAnimationResources();
1079 nsView
* view
= GetView();
1080 nsViewManager
* viewManager
= view
->GetViewManager();
1081 viewManager
->SetViewVisibility(view
, ViewVisibility::Hide
);
1083 RefPtr popup
= &PopupElement();
1084 // XXX, bug 137033, In Windows, if mouse is outside the window when the
1085 // menupopup closes, no mouse_enter/mouse_exit event will be fired to clear
1086 // current hover state, we should clear it manually. This code may not the
1087 // best solution, but we can leave it here until we find the better approach.
1088 if (!aFromFrameDestruction
&&
1089 popup
->State().HasState(dom::ElementState::HOVER
)) {
1090 EventStateManager
* esm
= PresContext()->EventStateManager();
1091 esm
->SetContentState(nullptr, dom::ElementState::HOVER
);
1093 popup
->PopupClosed(aDeselectMenu
);
1096 nsPoint
nsMenuPopupFrame::AdjustPositionForAnchorAlign(
1097 nsRect
& anchorRect
, const nsSize
& aPrefSize
, FlipStyle
& aHFlip
,
1098 FlipStyle
& aVFlip
) const {
1099 // flip the anchor and alignment for right-to-left
1100 int8_t popupAnchor(mPopupAnchor
);
1101 int8_t popupAlign(mPopupAlignment
);
1102 if (IsDirectionRTL()) {
1103 // no need to flip the centered anchor types vertically
1104 if (popupAnchor
<= POPUPALIGNMENT_LEFTCENTER
) {
1105 popupAnchor
= -popupAnchor
;
1107 popupAlign
= -popupAlign
;
1110 nsRect
originalAnchorRect(anchorRect
);
1112 // first, determine at which corner of the anchor the popup should appear
1114 switch (popupAnchor
) {
1115 case POPUPALIGNMENT_LEFTCENTER
:
1116 pnt
= nsPoint(anchorRect
.x
, anchorRect
.y
+ anchorRect
.height
/ 2);
1117 anchorRect
.y
= pnt
.y
;
1118 anchorRect
.height
= 0;
1120 case POPUPALIGNMENT_RIGHTCENTER
:
1121 pnt
= nsPoint(anchorRect
.XMost(), anchorRect
.y
+ anchorRect
.height
/ 2);
1122 anchorRect
.y
= pnt
.y
;
1123 anchorRect
.height
= 0;
1125 case POPUPALIGNMENT_TOPCENTER
:
1126 pnt
= nsPoint(anchorRect
.x
+ anchorRect
.width
/ 2, anchorRect
.y
);
1127 anchorRect
.x
= pnt
.x
;
1128 anchorRect
.width
= 0;
1130 case POPUPALIGNMENT_BOTTOMCENTER
:
1131 pnt
= nsPoint(anchorRect
.x
+ anchorRect
.width
/ 2, anchorRect
.YMost());
1132 anchorRect
.x
= pnt
.x
;
1133 anchorRect
.width
= 0;
1135 case POPUPALIGNMENT_TOPRIGHT
:
1136 pnt
= anchorRect
.TopRight();
1138 case POPUPALIGNMENT_BOTTOMLEFT
:
1139 pnt
= anchorRect
.BottomLeft();
1141 case POPUPALIGNMENT_BOTTOMRIGHT
:
1142 pnt
= anchorRect
.BottomRight();
1144 case POPUPALIGNMENT_TOPLEFT
:
1146 pnt
= anchorRect
.TopLeft();
1150 // If the alignment is on the right edge of the popup, move the popup left
1151 // by the width. Similarly, if the alignment is on the bottom edge of the
1152 // popup, move the popup up by the height. In addition, account for the
1153 // margins of the popup on the edge on which it is aligned.
1154 nsMargin margin
= GetMargin();
1155 switch (popupAlign
) {
1156 case POPUPALIGNMENT_LEFTCENTER
:
1157 pnt
.MoveBy(margin
.left
, -aPrefSize
.height
/ 2);
1159 case POPUPALIGNMENT_RIGHTCENTER
:
1160 pnt
.MoveBy(-aPrefSize
.width
- margin
.right
, -aPrefSize
.height
/ 2);
1162 case POPUPALIGNMENT_TOPCENTER
:
1163 pnt
.MoveBy(-aPrefSize
.width
/ 2, margin
.top
);
1165 case POPUPALIGNMENT_BOTTOMCENTER
:
1166 pnt
.MoveBy(-aPrefSize
.width
/ 2, -aPrefSize
.height
- margin
.bottom
);
1168 case POPUPALIGNMENT_TOPRIGHT
:
1169 pnt
.MoveBy(-aPrefSize
.width
- margin
.right
, margin
.top
);
1171 case POPUPALIGNMENT_BOTTOMLEFT
:
1172 pnt
.MoveBy(margin
.left
, -aPrefSize
.height
- margin
.bottom
);
1174 case POPUPALIGNMENT_BOTTOMRIGHT
:
1175 pnt
.MoveBy(-aPrefSize
.width
- margin
.right
,
1176 -aPrefSize
.height
- margin
.bottom
);
1178 case POPUPALIGNMENT_TOPLEFT
:
1180 pnt
.MoveBy(margin
.left
, margin
.top
);
1184 // If we aligning to the selected item in the popup, adjust the vertical
1185 // position by the height of the menulist label and the selected item's
1187 if (mPosition
== POPUPPOSITION_SELECTION
) {
1188 MOZ_ASSERT(popupAnchor
== POPUPALIGNMENT_BOTTOMLEFT
||
1189 popupAnchor
== POPUPALIGNMENT_BOTTOMRIGHT
);
1190 MOZ_ASSERT(popupAlign
== POPUPALIGNMENT_TOPLEFT
||
1191 popupAlign
== POPUPALIGNMENT_TOPRIGHT
);
1193 // Only adjust the popup if it just opened, otherwise the popup will move
1194 // around if its gets resized or the selection changed. Cache the value in
1195 // mPositionedOffset and use that instead for any future calculations.
1196 if (mIsOpenChanged
) {
1197 if (nsIFrame
* selectedItemFrame
= GetSelectedItemForAlignment()) {
1198 const nscoord itemHeight
= selectedItemFrame
->GetRect().height
;
1199 const nscoord itemOffset
=
1200 selectedItemFrame
->GetOffsetToIgnoringScrolling(this).y
;
1201 // We want to line-up the anchor rect with the selected item, but if the
1202 // selected item is outside of our bounds, we don't want to shift the
1203 // popup up in a way that our box would no longer intersect with the
1205 nscoord maxOffset
= aPrefSize
.height
- itemHeight
;
1206 if (const nsIScrollableFrame
* sf
= GetScrollFrame()) {
1207 // HACK: We ideally would want to use the offset from the bottom
1208 // bottom of our scroll-frame to the bottom of our frame (so as to
1209 // ensure that the bottom of the scrollport is inside the anchor
1212 // But at this point of the code, the scroll frame may not be laid out
1213 // with a definite size (might be overflowing us).
1215 // So, we assume the offset from the bottom is symmetric to the offset
1216 // from the top. This holds for all the popups where this matters
1217 // (menulists on macOS, effectively), and seems better than somehow
1218 // moving the popup after the fact as we used to do.
1219 const nsIFrame
* f
= do_QueryFrame(sf
);
1220 maxOffset
-= f
->GetOffsetTo(this).y
;
1223 originalAnchorRect
.height
+ std::min(itemOffset
, maxOffset
);
1227 pnt
.y
-= mPositionedOffset
;
1230 // Flipping horizontally is allowed as long as the popup is above or below
1231 // the anchor. This will happen if both the anchor and alignment are top or
1232 // both are bottom, but different values. Similarly, flipping vertically is
1233 // allowed if the popup is to the left or right of the anchor. In this case,
1234 // the values of the constants are such that both must be positive or both
1235 // must be negative. A special case, used for overlap, allows flipping
1236 // vertically as well.
1237 // If we are flipping in both directions, we want to set a flip style both
1238 // horizontally and vertically. However, we want to flip on the inside edge
1239 // of the anchor. Consider the example of a typical dropdown menu.
1240 // Vertically, we flip the popup on the outside edges of the anchor menu,
1241 // however horizontally, we want to to use the inside edges so the popup
1242 // still appears underneath the anchor menu instead of floating off the
1243 // side of the menu.
1244 switch (popupAnchor
) {
1245 case POPUPALIGNMENT_LEFTCENTER
:
1246 case POPUPALIGNMENT_RIGHTCENTER
:
1247 aHFlip
= FlipStyle_Outside
;
1248 aVFlip
= FlipStyle_Inside
;
1250 case POPUPALIGNMENT_TOPCENTER
:
1251 case POPUPALIGNMENT_BOTTOMCENTER
:
1252 aHFlip
= FlipStyle_Inside
;
1253 aVFlip
= FlipStyle_Outside
;
1256 FlipStyle anchorEdge
=
1257 mFlip
== FlipType_Both
? FlipStyle_Inside
: FlipStyle_None
;
1258 aHFlip
= (popupAnchor
== -popupAlign
) ? FlipStyle_Outside
: anchorEdge
;
1259 if (((popupAnchor
> 0) == (popupAlign
> 0)) ||
1260 (popupAnchor
== POPUPALIGNMENT_TOPLEFT
&&
1261 popupAlign
== POPUPALIGNMENT_TOPLEFT
))
1262 aVFlip
= FlipStyle_Outside
;
1264 aVFlip
= anchorEdge
;
1272 nsIFrame
* nsMenuPopupFrame::GetSelectedItemForAlignment() const {
1273 // This method adjusts a menulist's popup such that the selected item is under
1274 // the cursor, aligned with the menulist label.
1275 nsCOMPtr
<nsIDOMXULSelectControlElement
> select
;
1276 if (mAnchorContent
) {
1277 select
= mAnchorContent
->AsElement()->AsXULSelectControl();
1281 // If there isn't an anchor, then try just getting the parent of the popup.
1282 select
= mContent
->GetParent()->AsElement()->AsXULSelectControl();
1288 nsCOMPtr
<Element
> selectedElement
;
1289 select
->GetSelectedItem(getter_AddRefs(selectedElement
));
1290 return selectedElement
? selectedElement
->GetPrimaryFrame() : nullptr;
1293 nscoord
nsMenuPopupFrame::SlideOrResize(nscoord
& aScreenPoint
, nscoord aSize
,
1294 nscoord aScreenBegin
,
1296 nscoord
* aOffset
) const {
1297 // The popup may be positioned such that either the left/top or bottom/right
1298 // is outside the screen - but never both.
1300 std::max(aScreenBegin
, std::min(aScreenEnd
- aSize
, aScreenPoint
));
1301 *aOffset
= newPos
- aScreenPoint
;
1302 aScreenPoint
= newPos
;
1303 return std::min(aSize
, aScreenEnd
- aScreenPoint
);
1306 nscoord
nsMenuPopupFrame::FlipOrResize(nscoord
& aScreenPoint
, nscoord aSize
,
1307 nscoord aScreenBegin
, nscoord aScreenEnd
,
1308 nscoord aAnchorBegin
, nscoord aAnchorEnd
,
1309 nscoord aMarginBegin
, nscoord aMarginEnd
,
1310 FlipStyle aFlip
, bool aEndAligned
,
1311 bool* aFlipSide
) const {
1312 // The flip side argument will be set to true if there wasn't room and we
1313 // flipped to the opposite side.
1316 // all of the coordinates used here are in app units relative to the screen
1317 nscoord popupSize
= aSize
;
1318 if (aScreenPoint
< aScreenBegin
) {
1319 // at its current position, the popup would extend past the left or top
1320 // edge of the screen, so it will have to be moved or resized.
1322 // for inside flips, we flip on the opposite side of the anchor
1323 nscoord startpos
= aFlip
== FlipStyle_Outside
? aAnchorBegin
: aAnchorEnd
;
1324 nscoord endpos
= aFlip
== FlipStyle_Outside
? aAnchorEnd
: aAnchorBegin
;
1326 // check whether there is more room to the left and right (or top and
1327 // bottom) of the anchor and put the popup on the side with more room.
1328 if (startpos
- aScreenBegin
>= aScreenEnd
- endpos
) {
1329 aScreenPoint
= aScreenBegin
;
1330 popupSize
= startpos
- aScreenPoint
- aMarginEnd
;
1331 *aFlipSide
= !aEndAligned
;
1333 // If the newly calculated position is different than the existing
1334 // position, flip such that the popup is to the right or bottom of the
1335 // anchor point instead . However, when flipping use the same margin
1337 nscoord newScreenPoint
= endpos
+ aMarginEnd
;
1338 if (newScreenPoint
!= aScreenPoint
) {
1339 *aFlipSide
= aEndAligned
;
1340 aScreenPoint
= newScreenPoint
;
1341 // check if the new position is still off the right or bottom edge of
1342 // the screen. If so, resize the popup.
1343 if (aScreenPoint
+ aSize
> aScreenEnd
) {
1344 popupSize
= aScreenEnd
- aScreenPoint
;
1349 aScreenPoint
= aScreenBegin
;
1351 } else if (aScreenPoint
+ aSize
> aScreenEnd
) {
1352 // at its current position, the popup would extend past the right or
1353 // bottom edge of the screen, so it will have to be moved or resized.
1355 // for inside flips, we flip on the opposite side of the anchor
1356 nscoord startpos
= aFlip
== FlipStyle_Outside
? aAnchorBegin
: aAnchorEnd
;
1357 nscoord endpos
= aFlip
== FlipStyle_Outside
? aAnchorEnd
: aAnchorBegin
;
1359 // check whether there is more room to the left and right (or top and
1360 // bottom) of the anchor and put the popup on the side with more room.
1361 if (aScreenEnd
- endpos
>= startpos
- aScreenBegin
) {
1362 *aFlipSide
= aEndAligned
;
1363 if (mIsContextMenu
) {
1364 aScreenPoint
= aScreenEnd
- aSize
;
1366 aScreenPoint
= endpos
+ aMarginBegin
;
1367 popupSize
= aScreenEnd
- aScreenPoint
;
1370 // if the newly calculated position is different than the existing
1371 // position, we flip such that the popup is to the left or top of the
1372 // anchor point instead.
1373 nscoord newScreenPoint
= startpos
- aSize
- aMarginBegin
;
1374 if (newScreenPoint
!= aScreenPoint
) {
1375 *aFlipSide
= !aEndAligned
;
1376 aScreenPoint
= newScreenPoint
;
1378 // check if the new position is still off the left or top edge of the
1379 // screen. If so, resize the popup.
1380 if (aScreenPoint
< aScreenBegin
) {
1381 aScreenPoint
= aScreenBegin
;
1382 if (!mIsContextMenu
) {
1383 popupSize
= startpos
- aScreenPoint
- aMarginBegin
;
1389 aScreenPoint
= aScreenEnd
- aSize
;
1393 // Make sure that the point is within the screen boundaries and that the
1394 // size isn't off the edge of the screen. This can happen when a large
1395 // positive or negative margin is used.
1396 if (aScreenPoint
< aScreenBegin
) {
1397 aScreenPoint
= aScreenBegin
;
1399 if (aScreenPoint
> aScreenEnd
) {
1400 aScreenPoint
= aScreenEnd
- aSize
;
1403 // If popupSize ended up being negative, or the original size was actually
1404 // smaller than the calculated popup size, just use the original size instead.
1405 if (popupSize
<= 0 || aSize
< popupSize
) {
1409 return std::min(popupSize
, aScreenEnd
- aScreenPoint
);
1412 nsRect
nsMenuPopupFrame::ComputeAnchorRect(nsPresContext
* aRootPresContext
,
1413 nsIFrame
* aAnchorFrame
) const {
1414 // Get the root frame for a reference
1415 nsIFrame
* rootFrame
= aRootPresContext
->PresShell()->GetRootFrame();
1417 // The dimensions of the anchor
1418 nsRect anchorRect
= aAnchorFrame
->GetRectRelativeToSelf();
1420 // Relative to the root
1421 anchorRect
= nsLayoutUtils::TransformFrameRectToAncestor(
1422 aAnchorFrame
, anchorRect
, rootFrame
);
1423 // Relative to the screen
1424 anchorRect
.MoveBy(rootFrame
->GetScreenRectInAppUnits().TopLeft());
1426 // In its own app units
1427 return anchorRect
.ScaleToOtherAppUnitsRoundOut(
1428 aRootPresContext
->AppUnitsPerDevPixel(),
1429 PresContext()->AppUnitsPerDevPixel());
1432 static nsIFrame
* MaybeDelegatedAnchorFrame(nsIFrame
* aFrame
) {
1436 if (auto* element
= Element::FromNodeOrNull(aFrame
->GetContent())) {
1437 if (element
->HasAttr(nsGkAtoms::delegatesanchor
)) {
1438 for (nsIFrame
* f
: aFrame
->PrincipalChildList()) {
1439 if (!f
->IsPlaceholderFrame()) {
1448 auto nsMenuPopupFrame::GetRects(const nsSize
& aPrefSize
) const -> Rects
{
1449 if (NS_WARN_IF(aPrefSize
== nsSize(-1, -1))) {
1450 // Return early if the popup hasn't been laid out yet. On Windows, this can
1451 // happen when using a drag popup before it opens.
1455 nsPresContext
* pc
= PresContext();
1456 nsIFrame
* rootFrame
= pc
->PresShell()->GetRootFrame();
1457 NS_ASSERTION(rootFrame
->GetView() && GetView() &&
1458 rootFrame
->GetView() == GetView()->GetParent(),
1459 "rootFrame's view is not our view's parent???");
1461 // Indicators of whether the popup should be flipped or resized.
1462 FlipStyle hFlip
= FlipStyle_None
, vFlip
= FlipStyle_None
;
1464 const nsMargin margin
= GetMargin();
1466 // the screen rectangle of the root frame, in dev pixels.
1467 const nsRect rootScreenRect
= rootFrame
->GetScreenRectInAppUnits();
1469 const bool isNoAutoHide
= IsNoAutoHide();
1470 const PopupLevel popupLevel
= GetPopupLevel(isNoAutoHide
);
1474 // Set the popup's size to the preferred size. Below, this size will be
1475 // adjusted to fit on the screen or within the content area. If the anchor is
1476 // sized to the popup, use the anchor's width instead of the preferred width.
1477 result
.mUsedRect
= nsRect(nsPoint(), aPrefSize
);
1479 const bool anchored
= IsAnchored();
1481 // In order to deal with transforms, we need the root prescontext:
1482 nsPresContext
* rootPc
= pc
->GetRootPresContext();
1483 if (NS_WARN_IF(!rootPc
)) {
1484 // If we can't reach a root pres context, don't bother continuing.
1488 result
.mAnchorRect
= result
.mUntransformedAnchorRect
= [&] {
1489 // If anchored to a rectangle, use that rectangle. Otherwise, determine
1490 // the rectangle from the anchor.
1491 if (mAnchorType
== MenuPopupAnchorType_Rect
) {
1494 // if the frame is not specified, use the anchor node passed to OpenPopup.
1495 // If that wasn't specified either, use the root frame. Note that
1496 // mAnchorContent might be a different document so its presshell must be
1498 nsIFrame
* anchorFrame
= GetAnchorFrame();
1500 return rootScreenRect
;
1502 return ComputeAnchorRect(rootPc
, anchorFrame
);
1505 // if we are anchored, there are certain things we don't want to do when
1506 // repositioning the popup to fit on the screen, such as end up positioned
1507 // over the anchor, for instance a popup appearing over the menu label.
1508 // When doing this reposition, we want to move the popup to the side with
1509 // the most room. The combination of anchor and alignment dictate if we
1510 // readjust above/below or to the left/right.
1511 if (mAnchorContent
|| mAnchorType
== MenuPopupAnchorType_Rect
) {
1512 // move the popup according to the anchor and alignment. This will also
1513 // tell us which axis the popup is flush against in case we have to move
1514 // it around later. The AdjustPositionForAnchorAlign method accounts for
1515 // the popup's margin.
1516 result
.mUsedRect
.MoveTo(AdjustPositionForAnchorAlign(
1517 result
.mAnchorRect
, aPrefSize
, hFlip
, vFlip
));
1519 // With no anchor, the popup is positioned relative to the root frame.
1520 result
.mUsedRect
.MoveTo(result
.mAnchorRect
.TopLeft() +
1521 nsPoint(margin
.left
, margin
.top
));
1524 // mXPos and mYPos specify an additional offset passed to OpenPopup that
1525 // should be added to the position. We also add the offset to the anchor
1526 // pos so a later flip/resize takes the offset into account.
1527 // FIXME(emilio): Wayland doesn't seem to be accounting for this offset
1528 // anywhere, and it probably should.
1530 nsPoint
offset(CSSPixel::ToAppUnits(mXPos
), CSSPixel::ToAppUnits(mYPos
));
1531 if (IsDirectionRTL()) {
1532 offset
.x
= -offset
.x
;
1534 result
.mUsedRect
.MoveBy(offset
);
1535 result
.mAnchorRect
.MoveBy(offset
);
1538 // Not anchored, use mScreenRect
1539 result
.mUsedRect
.MoveTo(mScreenRect
.TopLeft());
1540 result
.mAnchorRect
= result
.mUntransformedAnchorRect
=
1541 nsRect(mScreenRect
.TopLeft(), nsSize());
1543 // Right-align RTL context menus, and apply margin and offsets as per the
1544 // platform conventions.
1545 if (mIsContextMenu
&& IsDirectionRTL()) {
1546 result
.mUsedRect
.x
-= aPrefSize
.Width();
1547 result
.mUsedRect
.MoveBy(-margin
.right
, margin
.top
);
1549 result
.mUsedRect
.MoveBy(margin
.left
, margin
.top
);
1552 // OSX tooltips follow standard flip rule but other popups flip horizontally
1554 if (mPopupType
== PopupType::Tooltip
) {
1555 vFlip
= FlipStyle_Outside
;
1557 hFlip
= FlipStyle_Outside
;
1560 // Other OS screen positioned popups can be flipped vertically but never
1562 vFlip
= FlipStyle_Outside
;
1563 #endif // #ifdef XP_MACOSX
1566 const int32_t a2d
= pc
->AppUnitsPerDevPixel();
1568 nsView
* view
= GetView();
1569 NS_ASSERTION(view
, "popup with no view");
1571 nsIWidget
* widget
= view
->GetWidget();
1573 // If a panel has flip="none", don't constrain or flip it.
1574 // Also, always do this for content shells, so that the popup doesn't extend
1575 // outside the containing frame.
1576 if (mInContentShell
|| mFlip
!= FlipType_None
) {
1577 const Maybe
<nsRect
> constraintRect
=
1578 GetConstraintRect(result
.mAnchorRect
, rootScreenRect
, popupLevel
);
1580 if (constraintRect
) {
1581 // Ensure that anchorRect is on the constraint rect.
1582 result
.mAnchorRect
= result
.mAnchorRect
.Intersect(*constraintRect
);
1583 // Shrink the popup down if it is larger than the constraint size
1584 if (result
.mUsedRect
.width
> constraintRect
->width
) {
1585 result
.mUsedRect
.width
= constraintRect
->width
;
1587 if (result
.mUsedRect
.height
> constraintRect
->height
) {
1588 result
.mUsedRect
.height
= constraintRect
->height
;
1590 result
.mConstrainedByLayout
= true;
1593 if (IS_WAYLAND_DISPLAY() && widget
) {
1594 // Shrink the popup down if it's larger than popup size received from
1595 // Wayland compositor. We don't know screen size on Wayland so this is the
1596 // only info we have there.
1597 const nsSize waylandSize
= LayoutDeviceIntRect::ToAppUnits(
1598 widget
->GetMoveToRectPopupSize(), a2d
);
1599 if (waylandSize
.width
> 0 && result
.mUsedRect
.width
> waylandSize
.width
) {
1600 LOG_WAYLAND("Wayland constraint width [%p]: %d to %d", widget
,
1601 result
.mUsedRect
.width
, waylandSize
.width
);
1602 result
.mUsedRect
.width
= waylandSize
.width
;
1604 if (waylandSize
.height
> 0 &&
1605 result
.mUsedRect
.height
> waylandSize
.height
) {
1606 LOG_WAYLAND("Wayland constraint height [%p]: %d to %d", widget
,
1607 result
.mUsedRect
.height
, waylandSize
.height
);
1608 result
.mUsedRect
.height
= waylandSize
.height
;
1610 if (RefPtr
<widget::Screen
> s
= widget
->GetWidgetScreen()) {
1611 const nsSize screenSize
=
1612 LayoutDeviceIntSize::ToAppUnits(s
->GetAvailRect().Size(), a2d
);
1613 if (result
.mUsedRect
.height
> screenSize
.height
) {
1614 LOG_WAYLAND("Wayland constraint height to screen [%p]: %d to %d",
1615 widget
, result
.mUsedRect
.height
, screenSize
.height
);
1616 result
.mUsedRect
.height
= screenSize
.height
;
1618 if (result
.mUsedRect
.width
> screenSize
.width
) {
1619 LOG_WAYLAND("Wayland constraint widthto screen [%p]: %d to %d",
1620 widget
, result
.mUsedRect
.width
, screenSize
.width
);
1621 result
.mUsedRect
.width
= screenSize
.width
;
1626 // At this point the anchor (anchorRect) is within the available screen
1627 // area (constraintRect) and the popup is known to be no larger than the
1629 if (constraintRect
) {
1630 // We might want to "slide" an arrow if the panel is of the correct type -
1631 // but we can only slide on one axis - the other axis must be "flipped or
1632 // resized" as normal.
1633 bool slideHorizontal
= false, slideVertical
= false;
1634 if (mFlip
== FlipType_Slide
) {
1635 int8_t position
= GetAlignmentPosition();
1636 slideHorizontal
= position
>= POPUPPOSITION_BEFORESTART
&&
1637 position
<= POPUPPOSITION_AFTEREND
;
1638 slideVertical
= position
>= POPUPPOSITION_STARTBEFORE
&&
1639 position
<= POPUPPOSITION_ENDAFTER
;
1642 // Next, check if there is enough space to show the popup at full size
1643 // when positioned at screenPoint. If not, flip the popups to the opposite
1644 // side of their anchor point, or resize them as necessary.
1645 if (slideHorizontal
) {
1646 result
.mUsedRect
.width
= SlideOrResize(
1647 result
.mUsedRect
.x
, result
.mUsedRect
.width
, constraintRect
->x
,
1648 constraintRect
->XMost(), &result
.mAlignmentOffset
);
1650 const bool endAligned
=
1652 ? mPopupAlignment
== POPUPALIGNMENT_TOPLEFT
||
1653 mPopupAlignment
== POPUPALIGNMENT_BOTTOMLEFT
||
1654 mPopupAlignment
== POPUPALIGNMENT_LEFTCENTER
1655 : mPopupAlignment
== POPUPALIGNMENT_TOPRIGHT
||
1656 mPopupAlignment
== POPUPALIGNMENT_BOTTOMRIGHT
||
1657 mPopupAlignment
== POPUPALIGNMENT_RIGHTCENTER
;
1658 result
.mUsedRect
.width
= FlipOrResize(
1659 result
.mUsedRect
.x
, result
.mUsedRect
.width
, constraintRect
->x
,
1660 constraintRect
->XMost(), result
.mAnchorRect
.x
,
1661 result
.mAnchorRect
.XMost(), margin
.left
, margin
.right
, hFlip
,
1662 endAligned
, &result
.mHFlip
);
1664 if (slideVertical
) {
1665 result
.mUsedRect
.height
= SlideOrResize(
1666 result
.mUsedRect
.y
, result
.mUsedRect
.height
, constraintRect
->y
,
1667 constraintRect
->YMost(), &result
.mAlignmentOffset
);
1669 bool endAligned
= mPopupAlignment
== POPUPALIGNMENT_BOTTOMLEFT
||
1670 mPopupAlignment
== POPUPALIGNMENT_BOTTOMRIGHT
||
1671 mPopupAlignment
== POPUPALIGNMENT_BOTTOMCENTER
;
1672 result
.mUsedRect
.height
= FlipOrResize(
1673 result
.mUsedRect
.y
, result
.mUsedRect
.height
, constraintRect
->y
,
1674 constraintRect
->YMost(), result
.mAnchorRect
.y
,
1675 result
.mAnchorRect
.YMost(), margin
.top
, margin
.bottom
, vFlip
,
1676 endAligned
, &result
.mVFlip
);
1680 NS_ASSERTION(constraintRect
->Contains(result
.mUsedRect
),
1681 "Popup is offscreen");
1682 if (!constraintRect
->Contains(result
.mUsedRect
)) {
1683 NS_WARNING(nsPrintfCString("Popup is offscreen (%s vs. %s)",
1684 ToString(constraintRect
).c_str(),
1685 ToString(result
.mUsedRect
).c_str())
1691 // snap the popup's position in screen coordinates to device pixels, see
1692 // bug 622507, bug 961431
1693 result
.mUsedRect
.x
= pc
->RoundAppUnitsToNearestDevPixels(result
.mUsedRect
.x
);
1694 result
.mUsedRect
.y
= pc
->RoundAppUnitsToNearestDevPixels(result
.mUsedRect
.y
);
1696 // determine the x and y position of the view by subtracting the desired
1697 // screen position from the screen position of the root frame.
1698 result
.mViewPoint
= result
.mUsedRect
.TopLeft() - rootScreenRect
.TopLeft();
1700 // Offset the position by the width and height of the borders and titlebar.
1701 // Even though GetClientOffset should return (0, 0) when there is no titlebar
1702 // or borders, we skip these calculations anyway for non-panels to save time
1703 // since they will never have a titlebar.
1704 if (mPopupType
== PopupType::Panel
&& widget
) {
1705 result
.mClientOffset
= widget
->GetClientOffset();
1706 result
.mViewPoint
+=
1707 LayoutDeviceIntPoint::ToAppUnits(result
.mClientOffset
, a2d
);
1713 void nsMenuPopupFrame::SetPopupPosition(bool aIsMove
) {
1714 if (aIsMove
&& (mPrefSize
.width
== -1 || mPrefSize
.height
== -1)) {
1718 auto rects
= GetRects(mPrefSize
);
1719 if (rects
.mUsedRect
.Size() != mRect
.Size()) {
1720 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IN_REFLOW
));
1721 // We need to resize on top of moving, trigger an actual reflow.
1722 PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors
,
1729 void nsMenuPopupFrame::PerformMove(const Rects
& aRects
) {
1730 auto* ps
= PresShell();
1732 // We're just moving, sync frame position and offset as needed.
1733 ps
->GetViewManager()->MoveViewTo(GetView(), aRects
.mViewPoint
.x
,
1734 aRects
.mViewPoint
.y
);
1736 // Now that we've positioned the view, sync up the frame's origin.
1737 nsBlockFrame::SetPosition(aRects
.mViewPoint
-
1738 GetParent()->GetOffsetTo(ps
->GetRootFrame()));
1740 // If the popup is in the positioned state or if it is shown and the position
1741 // or size changed, dispatch a popuppositioned event if the popup wants it.
1742 if (mPopupState
== ePopupPositioning
||
1743 (mPopupState
== ePopupShown
&&
1744 !aRects
.mUsedRect
.IsEqualEdges(mUsedScreenRect
)) ||
1745 (mPopupState
== ePopupShown
&&
1746 aRects
.mAlignmentOffset
!= mAlignmentOffset
)) {
1747 mUsedScreenRect
= aRects
.mUsedRect
;
1748 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW
) && !mPendingPositionedEvent
) {
1749 mPendingPositionedEvent
=
1750 nsXULPopupPositionedEvent::DispatchIfNeeded(mContent
->AsElement());
1754 if (!mPositionedByMoveToRect
) {
1755 mUntransformedAnchorRect
= aRects
.mUntransformedAnchorRect
;
1758 mAlignmentOffset
= aRects
.mAlignmentOffset
;
1759 mLastClientOffset
= aRects
.mClientOffset
;
1760 mHFlip
= aRects
.mHFlip
;
1761 mVFlip
= aRects
.mVFlip
;
1762 mConstrainedByLayout
= aRects
.mConstrainedByLayout
;
1764 // If this is a noautohide popup, set the screen coordinates of the popup.
1765 // This way, the popup stays at the location where it was opened even when the
1766 // window is moved. Popups at the parent level follow the parent window as it
1767 // is moved and remained anchored, so we want to maintain the anchoring
1770 // FIXME: This suffers from issues like bug 1823552, where constraints imposed
1771 // by the anchor are lost, but this is super-old behavior.
1772 const bool fixPositionToPoint
=
1773 IsNoAutoHide() && (GetPopupLevel() != PopupLevel::Parent
||
1774 mAnchorType
== MenuPopupAnchorType_Rect
);
1775 if (fixPositionToPoint
) {
1776 // Account for the margin that will end up being added to the screen
1777 // coordinate the next time SetPopupPosition is called.
1778 const auto& margin
= GetMargin();
1779 mAnchorType
= MenuPopupAnchorType_Point
;
1780 mScreenRect
.x
= aRects
.mUsedRect
.x
- margin
.left
;
1781 mScreenRect
.y
= aRects
.mUsedRect
.y
- margin
.top
;
1784 // For anchored popups that shouldn't follow the anchor, fix the original
1786 if (IsAnchored() && !ShouldFollowAnchor() && !mUsedScreenRect
.IsEmpty() &&
1787 mAnchorType
!= MenuPopupAnchorType_Rect
) {
1788 mAnchorType
= MenuPopupAnchorType_Rect
;
1789 mScreenRect
= aRects
.mUntransformedAnchorRect
;
1792 // NOTE(emilio): This call below is kind of a workaround, but we need to do
1793 // this here because some position changes don't go through the
1794 // view system -> popup manager, like:
1796 // https://searchfox.org/mozilla-central/rev/477950cf9ca9c9bb5ff6f34e0d0f6ca4718ea798/widget/gtk/nsWindow.cpp#3847
1798 // So this might be the last chance we have to set the remote browser's
1801 // Ultimately this probably wants to get fixed in the widget size of things,
1802 // but given this is worst-case a redundant DOM traversal, and that popups
1803 // usually don't have all that much content, this is probably an ok
1805 WidgetPositionOrSizeDidChange();
1808 void nsMenuPopupFrame::WidgetPositionOrSizeDidChange() {
1809 // In the case this popup has remote contents having OOP iframes, it's
1810 // possible that OOP iframe's nsSubDocumentFrame has been already reflowed
1811 // thus, we will never have a chance to tell this parent browser's position
1812 // update to the OOP documents without notifying it explicitly.
1813 if (!HasRemoteContent()) {
1816 for (nsIContent
* content
= mContent
->GetFirstChild(); content
;
1817 content
= content
->GetNextNode(mContent
)) {
1818 if (content
->IsXULElement(nsGkAtoms::browser
) &&
1819 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::remote
,
1820 nsGkAtoms::_true
, eIgnoreCase
)) {
1821 if (auto* browserParent
= dom::BrowserParent::GetFrom(content
)) {
1822 browserParent
->NotifyPositionUpdatedForContentsInPopup();
1828 Maybe
<nsRect
> nsMenuPopupFrame::GetConstraintRect(
1829 const nsRect
& aAnchorRect
, const nsRect
& aRootScreenRect
,
1830 PopupLevel aPopupLevel
) const {
1831 const nsPresContext
* pc
= PresContext();
1832 const int32_t a2d
= PresContext()->AppUnitsPerDevPixel();
1833 Maybe
<nsRect
> result
;
1835 auto AddConstraint
= [&result
](const nsRect
& aConstraint
) {
1837 *result
= result
->Intersect(aConstraint
);
1839 result
.emplace(aConstraint
);
1843 // Determine the available screen space. It will be reduced by the OS chrome
1844 // such as menubars. It addition, for content shells, it will be the area of
1845 // the content rather than the screen.
1846 // In Wayland we can't use the screen rect because we can't know absolute
1848 if (!IS_WAYLAND_DISPLAY()) {
1849 const DesktopToLayoutDeviceScale scale
=
1850 pc
->DeviceContext()->GetDesktopToDeviceScale();
1851 // For content shells, get the screen where the root frame is located. This
1852 // is because we need to constrain the content to this content area, so we
1853 // should use the same screen. Otherwise, use the screen where the anchor is
1855 const nsRect
& rect
= mInContentShell
? aRootScreenRect
: aAnchorRect
;
1856 auto desktopRect
= DesktopIntRect::RoundOut(
1857 LayoutDeviceRect::FromAppUnits(rect
, a2d
) / scale
);
1858 desktopRect
.width
= std::max(1, desktopRect
.width
);
1859 desktopRect
.height
= std::max(1, desktopRect
.height
);
1861 RefPtr
<nsIScreen
> screen
=
1862 widget::ScreenManager::GetSingleton().ScreenForRect(desktopRect
);
1863 MOZ_ASSERT(screen
, "We always fall back to the primary screen");
1864 // Non-top-level popups (which will always be panels) should never overlap
1866 const bool canOverlapOSBar
=
1867 aPopupLevel
== PopupLevel::Top
&&
1868 LookAndFeel::GetInt(LookAndFeel::IntID::MenusCanOverlapOSBar
) &&
1870 // Get the total screen area if the popup is allowed to overlap it.
1871 const auto screenRect
=
1872 canOverlapOSBar
? screen
->GetRect() : screen
->GetAvailRect();
1873 AddConstraint(LayoutDeviceRect::ToAppUnits(screenRect
, a2d
));
1876 if (mInContentShell
) {
1877 // For content shells, clip to the client area rather than the screen area
1878 AddConstraint(aRootScreenRect
);
1879 } else if (!mOverrideConstraintRect
.IsEmpty()) {
1880 AddConstraint(mOverrideConstraintRect
);
1881 // This is currently only used for <select> elements where we want to
1882 // constrain vertically to the screen but not horizontally, so do the
1883 // intersection and then reset the horizontal values.
1885 // FIXME(emilio): This doesn't make any sense to me...
1886 result
->x
= mOverrideConstraintRect
.x
;
1887 result
->width
= mOverrideConstraintRect
.width
;
1890 // Expand the allowable screen rect by the input margin (which can't be
1891 // interacted with).
1893 const nscoord inputMargin
=
1894 StyleUIReset()->mMozWindowInputRegionMargin
.ToAppUnits();
1895 result
->Inflate(inputMargin
);
1900 ConsumeOutsideClicksResult
nsMenuPopupFrame::ConsumeOutsideClicks() {
1901 if (mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
1902 nsGkAtoms::consumeoutsideclicks
,
1903 nsGkAtoms::_true
, eCaseMatters
)) {
1904 return ConsumeOutsideClicks_True
;
1906 if (mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
1907 nsGkAtoms::consumeoutsideclicks
,
1908 nsGkAtoms::_false
, eCaseMatters
)) {
1909 return ConsumeOutsideClicks_ParentOnly
;
1911 if (mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
1912 nsGkAtoms::consumeoutsideclicks
,
1913 nsGkAtoms::never
, eCaseMatters
)) {
1914 return ConsumeOutsideClicks_Never
;
1917 nsCOMPtr
<nsIContent
> parentContent
= mContent
->GetParent();
1918 if (parentContent
) {
1919 dom::NodeInfo
* ni
= parentContent
->NodeInfo();
1920 if (ni
->Equals(nsGkAtoms::menulist
, kNameSpaceID_XUL
)) {
1921 return ConsumeOutsideClicks_True
; // Consume outside clicks for combo
1922 // boxes on all platforms
1925 // Don't consume outside clicks for menus in Windows
1926 if (ni
->Equals(nsGkAtoms::menu
, kNameSpaceID_XUL
) ||
1927 ni
->Equals(nsGkAtoms::popupset
, kNameSpaceID_XUL
) ||
1928 ((ni
->Equals(nsGkAtoms::button
, kNameSpaceID_XUL
) ||
1929 ni
->Equals(nsGkAtoms::toolbarbutton
, kNameSpaceID_XUL
)) &&
1930 parentContent
->AsElement()->AttrValueIs(
1931 kNameSpaceID_None
, nsGkAtoms::type
, nsGkAtoms::menu
,
1933 return ConsumeOutsideClicks_Never
;
1938 return ConsumeOutsideClicks_True
;
1941 static nsIScrollableFrame
* DoGetScrollFrame(const nsIFrame
* aFrame
) {
1942 if (const nsIScrollableFrame
* sf
= do_QueryFrame(aFrame
)) {
1943 return const_cast<nsIScrollableFrame
*>(sf
);
1945 for (nsIFrame
* childFrame
: aFrame
->PrincipalChildList()) {
1946 if (auto* sf
= DoGetScrollFrame(childFrame
)) {
1953 // XXXroc this is megalame. Fossicking around for a frame of the right
1954 // type is a recipe for disaster in the long term.
1955 nsIScrollableFrame
* nsMenuPopupFrame::GetScrollFrame() const {
1956 return DoGetScrollFrame(this);
1959 void nsMenuPopupFrame::ChangeByPage(bool aIsUp
) {
1960 // Only scroll by page within menulists.
1961 if (!IsMenuList()) {
1965 nsIScrollableFrame
* scrollframe
= GetScrollFrame();
1967 RefPtr popup
= &PopupElement();
1968 XULButtonElement
* currentMenu
= popup
->GetActiveMenuChild();
1969 XULButtonElement
* newMenu
= nullptr;
1971 // If there is no current menu item, get the first item. When moving up,
1972 // just use this as the newMenu and leave currentMenu null so that no check
1973 // for a later element is performed. When moving down, set currentMenu so
1974 // that we look for one page down from the first item.
1975 newMenu
= popup
->GetFirstMenuItem();
1977 currentMenu
= newMenu
;
1981 if (currentMenu
&& currentMenu
->GetPrimaryFrame()) {
1982 const nscoord scrollHeight
=
1983 scrollframe
? scrollframe
->GetScrollPortRect().height
: mRect
.height
;
1984 const nsRect currentRect
= currentMenu
->GetPrimaryFrame()->GetRect();
1985 const XULButtonElement
* startMenu
= currentMenu
;
1987 // Get the position of the current item and add or subtract one popup's
1988 // height to or from it.
1989 const nscoord targetPos
= aIsUp
? currentRect
.YMost() - scrollHeight
1990 : currentRect
.y
+ scrollHeight
;
1991 // Look for the next child which is just past the target position. This
1992 // child will need to be selected.
1994 currentMenu
= aIsUp
? popup
->GetPrevMenuItemFrom(*currentMenu
)
1995 : popup
->GetNextMenuItemFrom(*currentMenu
)) {
1996 if (!currentMenu
->GetPrimaryFrame()) {
1999 const nsRect curRect
= currentMenu
->GetPrimaryFrame()->GetRect();
2000 const nscoord curPos
= aIsUp
? curRect
.y
: curRect
.YMost();
2001 // If the right position was found, break out. Otherwise, look for another
2003 if (aIsUp
? (curPos
< targetPos
) : (curPos
> targetPos
)) {
2004 if (!newMenu
|| newMenu
== startMenu
) {
2005 newMenu
= currentMenu
;
2010 // Assign this item to newMenu. This item will be selected in case we
2011 // don't find any more.
2012 newMenu
= currentMenu
;
2016 // Select the new menuitem.
2017 if (RefPtr newMenuRef
= newMenu
) {
2018 popup
->SetActiveMenuChild(newMenuRef
);
2022 dom::XULPopupElement
& nsMenuPopupFrame::PopupElement() const {
2023 auto* popup
= dom::XULPopupElement::FromNode(GetContent());
2024 MOZ_DIAGNOSTIC_ASSERT(popup
);
2028 XULButtonElement
* nsMenuPopupFrame::GetCurrentMenuItem() const {
2029 return PopupElement().GetActiveMenuChild();
2032 nsIFrame
* nsMenuPopupFrame::GetCurrentMenuItemFrame() const {
2033 auto* child
= GetCurrentMenuItem();
2034 return child
? child
->GetPrimaryFrame() : nullptr;
2037 void nsMenuPopupFrame::HandleEnterKeyPress(WidgetEvent
& aEvent
) {
2038 mIncrementalString
.Truncate();
2039 RefPtr popup
= &PopupElement();
2040 popup
->HandleEnterKeyPress(aEvent
);
2043 XULButtonElement
* nsMenuPopupFrame::FindMenuWithShortcut(
2044 mozilla::dom::KeyboardEvent
& aKeyEvent
, bool& aDoAction
) {
2045 uint32_t charCode
= aKeyEvent
.CharCode();
2046 uint32_t keyCode
= aKeyEvent
.KeyCode();
2050 // Enumerate over our list of frames.
2051 const bool isMenu
= !IsMenuList();
2052 TimeStamp keyTime
= aKeyEvent
.WidgetEventPtr()->mTimeStamp
;
2053 if (charCode
== 0) {
2054 if (keyCode
== dom::KeyboardEvent_Binding::DOM_VK_BACK_SPACE
) {
2055 if (!isMenu
&& !mIncrementalString
.IsEmpty()) {
2056 mIncrementalString
.SetLength(mIncrementalString
.Length() - 1);
2060 if (nsCOMPtr
<nsISound
> sound
= do_GetService("@mozilla.org/sound;1")) {
2063 #endif // #ifdef XP_WIN
2067 char16_t uniChar
= ToLowerCase(static_cast<char16_t
>(charCode
));
2069 // Menu supports only first-letter navigation
2070 mIncrementalString
= uniChar
;
2071 } else if (IsWithinIncrementalTime(keyTime
)) {
2072 mIncrementalString
.Append(uniChar
);
2074 // Interval too long, treat as new typing
2075 mIncrementalString
= uniChar
;
2078 // See bug 188199 & 192346, if all letters in incremental string are same,
2079 // just try to match the first one
2080 nsAutoString
incrementalString(mIncrementalString
);
2081 uint32_t charIndex
= 1, stringLength
= incrementalString
.Length();
2082 while (charIndex
< stringLength
&&
2083 incrementalString
[charIndex
] == incrementalString
[charIndex
- 1]) {
2086 if (charIndex
== stringLength
) {
2087 incrementalString
.Truncate(1);
2091 sLastKeyTime
= keyTime
;
2094 PopupElement().FindMenuWithShortcut(incrementalString
, aDoAction
);
2099 // If we don't match anything, rollback the last typing
2100 mIncrementalString
.SetLength(mIncrementalString
.Length() - 1);
2102 // didn't find a matching menu item
2104 // behavior on Windows - this item is in a menu popup off of the
2105 // menu bar, so beep and do nothing else
2107 if (nsCOMPtr
<nsISound
> sound
= do_GetService("@mozilla.org/sound;1")) {
2111 #endif // #ifdef XP_WIN
2116 nsIWidget
* nsMenuPopupFrame::GetWidget() const {
2117 return mView
? mView
->GetWidget() : nullptr;
2120 // helpers /////////////////////////////////////////////////////////////
2122 nsresult
nsMenuPopupFrame::AttributeChanged(int32_t aNameSpaceID
,
2128 nsBlockFrame::AttributeChanged(aNameSpaceID
, aAttribute
, aModType
);
2130 if (aAttribute
== nsGkAtoms::left
|| aAttribute
== nsGkAtoms::top
) {
2131 MoveToAttributePosition();
2134 if (aAttribute
== nsGkAtoms::remote
) {
2135 // When the remote attribute changes, we need to create a new widget to
2136 // ensure that it has the correct compositor and transparency settings to
2137 // match the new value.
2138 PrepareWidget(true);
2141 if (aAttribute
== nsGkAtoms::followanchor
) {
2142 if (nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance()) {
2143 pm
->UpdateFollowAnchor(this);
2147 if (aAttribute
== nsGkAtoms::label
) {
2148 // set the label for the titlebar
2149 nsView
* view
= GetView();
2151 nsIWidget
* widget
= view
->GetWidget();
2154 mContent
->AsElement()->GetAttr(nsGkAtoms::label
, title
);
2155 if (!title
.IsEmpty()) {
2156 widget
->SetTitle(title
);
2160 } else if (aAttribute
== nsGkAtoms::ignorekeys
) {
2161 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
2163 nsAutoString ignorekeys
;
2164 mContent
->AsElement()->GetAttr(nsGkAtoms::ignorekeys
, ignorekeys
);
2165 pm
->UpdateIgnoreKeys(ignorekeys
.EqualsLiteral("true"));
2172 void nsMenuPopupFrame::MoveToAttributePosition() {
2173 // Move the widget around when the user sets the |left| and |top| attributes.
2174 // Note that this is not the best way to move the widget, as it results in
2175 // lots of FE notifications and is likely to be slow as molasses. Use |moveTo|
2176 // on the element if possible.
2177 nsAutoString left
, top
;
2178 mContent
->AsElement()->GetAttr(nsGkAtoms::left
, left
);
2179 mContent
->AsElement()->GetAttr(nsGkAtoms::top
, top
);
2180 nsresult err1
, err2
;
2181 mozilla::CSSIntPoint
pos(left
.ToInteger(&err1
), top
.ToInteger(&err2
));
2183 if (NS_SUCCEEDED(err1
) && NS_SUCCEEDED(err2
)) MoveTo(pos
, false);
2185 PresShell()->FrameNeedsReflow(
2186 this, IntrinsicDirty::FrameAncestorsAndDescendants
, NS_FRAME_IS_DIRTY
);
2189 void nsMenuPopupFrame::Destroy(DestroyContext
& aContext
) {
2190 // XXX: Currently we don't fire popuphidden for these popups, that seems wrong
2191 // but alas, also pre-existing.
2192 HidePopup(/* aDeselectMenu = */ false, ePopupClosed
,
2193 /* aFromFrameDestruction = */ true);
2195 if (RefPtr
<nsXULPopupManager
> pm
= nsXULPopupManager::GetInstance()) {
2196 pm
->PopupDestroyed(this);
2199 nsBlockFrame::Destroy(aContext
);
2202 nsMargin
nsMenuPopupFrame::GetMargin() const {
2204 StyleMargin()->GetMargin(margin
);
2205 if (mIsTopLevelContextMenu
) {
2206 const CSSIntPoint
offset(
2207 LookAndFeel::GetInt(LookAndFeel::IntID::ContextMenuOffsetHorizontal
),
2208 LookAndFeel::GetInt(LookAndFeel::IntID::ContextMenuOffsetVertical
));
2209 auto auOffset
= CSSIntPoint::ToAppUnits(offset
);
2210 margin
.top
+= auOffset
.y
;
2211 margin
.bottom
+= auOffset
.y
;
2212 margin
.left
+= auOffset
.x
;
2213 margin
.right
+= auOffset
.x
;
2218 void nsMenuPopupFrame::MoveTo(const CSSPoint
& aPos
, bool aUpdateAttrs
,
2219 bool aByMoveToRect
) {
2220 nsIWidget
* widget
= GetWidget();
2221 nsPoint appUnitsPos
= CSSPixel::ToAppUnits(aPos
);
2223 // reposition the popup at the specified coordinates. Don't clear the anchor
2224 // and position, because the popup can be reset to its anchor position by
2225 // using (-1, -1) as coordinates.
2227 // Subtract off the margin as it will be added to the position when
2228 // SetPopupPosition is called.
2230 nsMargin margin
= GetMargin();
2231 if (mIsContextMenu
&& IsDirectionRTL()) {
2232 appUnitsPos
.x
+= margin
.right
+ mRect
.Width();
2234 appUnitsPos
.x
-= margin
.left
;
2236 appUnitsPos
.y
-= margin
.top
;
2239 if (mScreenRect
.TopLeft() == appUnitsPos
&&
2240 (!widget
|| widget
->GetClientOffset() == mLastClientOffset
)) {
2244 mPositionedByMoveToRect
= aByMoveToRect
;
2245 mScreenRect
.MoveTo(appUnitsPos
);
2246 if (mAnchorType
== MenuPopupAnchorType_Rect
) {
2247 // This ensures that the anchor width is still honored, to prevent it from
2248 // changing spuriously.
2249 mScreenRect
.height
= 0;
2250 // But we still need to make sure that our top left position ends up in
2252 mPopupAlignment
= POPUPALIGNMENT_TOPLEFT
;
2253 mPopupAnchor
= POPUPALIGNMENT_BOTTOMLEFT
;
2256 mAnchorType
= MenuPopupAnchorType_Point
;
2259 SetPopupPosition(true);
2261 RefPtr
<Element
> popup
= mContent
->AsElement();
2263 (popup
->HasAttr(nsGkAtoms::left
) || popup
->HasAttr(nsGkAtoms::top
))) {
2264 nsAutoString left
, top
;
2265 left
.AppendInt(RoundedToInt(aPos
).x
);
2266 top
.AppendInt(RoundedToInt(aPos
).y
);
2267 popup
->SetAttr(kNameSpaceID_None
, nsGkAtoms::left
, left
, false);
2268 popup
->SetAttr(kNameSpaceID_None
, nsGkAtoms::top
, top
, false);
2272 void nsMenuPopupFrame::MoveToAnchor(nsIContent
* aAnchorContent
,
2273 const nsAString
& aPosition
, int32_t aXPos
,
2274 int32_t aYPos
, bool aAttributesOverride
) {
2275 NS_ASSERTION(IsVisible(), "popup must be visible to move it");
2277 nsPopupState oldstate
= mPopupState
;
2278 InitializePopup(aAnchorContent
, mTriggerContent
, aPosition
, aXPos
, aYPos
,
2279 MenuPopupAnchorType_Node
, aAttributesOverride
);
2280 // InitializePopup changed the state so reset it.
2281 mPopupState
= oldstate
;
2283 // Pass false here so that flipping and adjusting to fit on the screen happen.
2284 SetPopupPosition(false);
2287 int8_t nsMenuPopupFrame::GetAlignmentPosition() const {
2288 // The code below handles most cases of alignment, anchor and position values.
2289 // Those that are not handled just return POPUPPOSITION_UNKNOWN.
2291 if (mPosition
== POPUPPOSITION_OVERLAP
||
2292 mPosition
== POPUPPOSITION_AFTERPOINTER
||
2293 mPosition
== POPUPPOSITION_SELECTION
) {
2297 int8_t position
= mPosition
;
2299 if (position
== POPUPPOSITION_UNKNOWN
) {
2300 switch (mPopupAnchor
) {
2301 case POPUPALIGNMENT_BOTTOMRIGHT
:
2302 case POPUPALIGNMENT_BOTTOMLEFT
:
2303 case POPUPALIGNMENT_BOTTOMCENTER
:
2304 position
= mPopupAlignment
== POPUPALIGNMENT_TOPRIGHT
2305 ? POPUPPOSITION_AFTEREND
2306 : POPUPPOSITION_AFTERSTART
;
2308 case POPUPALIGNMENT_TOPRIGHT
:
2309 case POPUPALIGNMENT_TOPLEFT
:
2310 case POPUPALIGNMENT_TOPCENTER
:
2311 position
= mPopupAlignment
== POPUPALIGNMENT_BOTTOMRIGHT
2312 ? POPUPPOSITION_BEFOREEND
2313 : POPUPPOSITION_BEFORESTART
;
2315 case POPUPALIGNMENT_LEFTCENTER
:
2316 position
= mPopupAlignment
== POPUPALIGNMENT_BOTTOMRIGHT
2317 ? POPUPPOSITION_STARTAFTER
2318 : POPUPPOSITION_STARTBEFORE
;
2320 case POPUPALIGNMENT_RIGHTCENTER
:
2321 position
= mPopupAlignment
== POPUPALIGNMENT_BOTTOMLEFT
2322 ? POPUPPOSITION_ENDAFTER
2323 : POPUPPOSITION_ENDBEFORE
;
2331 position
= POPUPPOSITION_HFLIP(position
);
2335 position
= POPUPPOSITION_VFLIP(position
);
2342 * KEEP THIS IN SYNC WITH nsIFrame::CreateView
2343 * as much as possible. Until we get rid of views finally...
2345 void nsMenuPopupFrame::CreatePopupView() {
2350 nsViewManager
* viewManager
= PresContext()->GetPresShell()->GetViewManager();
2351 NS_ASSERTION(nullptr != viewManager
, "null view manager");
2354 nsView
* parentView
= viewManager
->GetRootView();
2355 auto visibility
= ViewVisibility::Hide
;
2357 NS_ASSERTION(parentView
, "no parent view");
2360 nsView
* view
= viewManager
->CreateView(GetRect(), parentView
, visibility
);
2361 auto zIndex
= ZIndex();
2362 viewManager
->SetViewZIndex(view
, zIndex
.isNothing(), zIndex
.valueOr(0));
2363 // XXX put view last in document order until we can do better
2364 viewManager
->InsertChild(parentView
, view
, nullptr, true);
2366 // Remember our view
2370 NS_FRAME_TRACE_CALLS
,
2371 ("nsMenuPopupFrame::CreatePopupView: frame=%p view=%p", this, view
));
2374 bool nsMenuPopupFrame::ShouldFollowAnchor() const {
2375 if (mAnchorType
!= MenuPopupAnchorType_Node
|| !mAnchorContent
) {
2379 // Follow anchor mode is used when followanchor="true" is set or for arrow
2381 if (mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
2382 nsGkAtoms::followanchor
,
2383 nsGkAtoms::_true
, eCaseMatters
)) {
2387 if (mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
2388 nsGkAtoms::followanchor
,
2389 nsGkAtoms::_false
, eCaseMatters
)) {
2393 return mPopupType
== PopupType::Panel
&&
2394 mContent
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
2395 nsGkAtoms::arrow
, eCaseMatters
);
2398 bool nsMenuPopupFrame::ShouldFollowAnchor(nsRect
& aRect
) {
2399 if (!ShouldFollowAnchor()) {
2403 if (nsIFrame
* anchorFrame
= GetAnchorFrame()) {
2404 if (nsPresContext
* rootPresContext
= PresContext()->GetRootPresContext()) {
2405 aRect
= ComputeAnchorRect(rootPresContext
, anchorFrame
);
2412 bool nsMenuPopupFrame::IsDirectionRTL() const {
2413 const nsIFrame
* anchor
= GetAnchorFrame();
2414 const nsIFrame
* f
= anchor
? anchor
: this;
2415 return f
->StyleVisibility()->mDirection
== StyleDirection::Rtl
;
2418 nsIFrame
* nsMenuPopupFrame::GetAnchorFrame() const {
2419 nsIContent
* anchor
= mAnchorContent
;
2423 return MaybeDelegatedAnchorFrame(anchor
->GetPrimaryFrame());
2426 void nsMenuPopupFrame::CheckForAnchorChange(nsRect
& aRect
) {
2427 // Don't update if the popup isn't visible or we shouldn't be following the
2429 if (!IsVisible() || !ShouldFollowAnchor()) {
2433 bool shouldHide
= false;
2435 nsPresContext
* rootPresContext
= PresContext()->GetRootPresContext();
2437 // If the frame for the anchor has gone away, hide the popup.
2438 nsIFrame
* anchor
= GetAnchorFrame();
2439 if (!anchor
|| !rootPresContext
) {
2441 } else if (!anchor
->IsVisibleConsideringAncestors(
2442 VISIBILITY_CROSS_CHROME_CONTENT_BOUNDARY
)) {
2443 // If the anchor is now inside something that is invisible, hide the popup.
2446 // If the anchor is now inside a hidden parent popup, hide the popup.
2447 nsIFrame
* frame
= anchor
;
2449 nsMenuPopupFrame
* popup
= do_QueryFrame(frame
);
2450 if (popup
&& popup
->PopupState() != ePopupShown
) {
2455 frame
= frame
->GetParent();
2460 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
2462 // As the caller will be iterating over the open popups, hide
2464 pm
->HidePopup(mContent
->AsElement(),
2465 {HidePopupOption::DeselectMenu
, HidePopupOption::Async
});
2471 nsRect anchorRect
= ComputeAnchorRect(rootPresContext
, anchor
);
2473 // If the rectangles are different, move the popup.
2474 if (!anchorRect
.IsEqualEdges(aRect
)) {
2476 SetPopupPosition(true);