Bug 793157, add a popup method to move a popup relative to an anchor after it has opened, r=neil

This commit is contained in:
Neil Deakin 2012-10-23 08:11:13 -04:00
parent d7944e2171
commit 63abc12c82
7 changed files with 152 additions and 2 deletions

View File

@ -10,7 +10,7 @@ interface nsIDOMNode;
interface nsIDOMEvent;
interface nsIDOMClientRect;
[scriptable, uuid(6AD1B199-95D3-448B-98D7-896BCE3A1DCD)]
[scriptable, uuid(ACCEA57B-C3D8-4B6E-9101-90F04EE9DEA0)]
interface nsIPopupBoxObject : nsISupports
{
/**
@ -151,11 +151,21 @@ interface nsIPopupBoxObject : nsISupports
*/
readonly attribute nsIDOMElement anchorNode;
/*
/**
* Retrieve the screen rectangle of the popup, including the area occupied by
* any titlebar or borders present.
*/
nsIDOMClientRect getOuterScreenRect();
/**
* Move an open popup to the given anchor position. The arguments have the same
* meaning as the corresponding argument to openPopup. This method has no effect
* on popups that are not open.
*/
void moveToAnchor(in nsIDOMElement anchorElement,
in AString position,
in long x, in long y,
in boolean attributesOverride);
};
%{C++

View File

@ -1867,6 +1867,23 @@ nsMenuPopupFrame::MoveTo(int32_t aLeft, int32_t aTop, bool aUpdateAttrs)
}
}
void
nsMenuPopupFrame::MoveToAnchor(nsIContent* aAnchorContent,
const nsAString& aPosition,
int32_t aXPos, int32_t aYPos,
bool aAttributesOverride)
{
NS_ASSERTION(mPopupState == ePopupOpenAndVisible, "popup must be open to move it");
InitializePopup(aAnchorContent, mTriggerContent, aPosition,
aXPos, aYPos, aAttributesOverride);
// InitializePopup changed the state so reset it.
mPopupState = ePopupOpenAndVisible;
// Pass false here so that flipping and adjusting to fit on the screen happen.
SetPopupPosition(nullptr, false);
}
bool
nsMenuPopupFrame::GetAutoPosition()
{

View File

@ -269,6 +269,11 @@ public:
// The frame may be destroyed by this method.
void MoveTo(int32_t aLeft, int32_t aTop, bool aUpdateAttrs);
void MoveToAnchor(nsIContent* aAnchorContent,
const nsAString& aPosition,
int32_t aXPos, int32_t aYPos,
bool aAttributesOverride);
bool GetAutoPosition();
void SetAutoPosition(bool aShouldAutoPosition);
void SetConsumeRollupEvent(uint32_t aConsumeMode);

View File

@ -115,6 +115,25 @@ nsPopupBoxObject::MoveTo(int32_t aLeft, int32_t aTop)
return NS_OK;
}
NS_IMETHODIMP
nsPopupBoxObject::MoveToAnchor(nsIDOMElement* aAnchorElement,
const nsAString& aPosition,
int32_t aXPos, int32_t aYPos,
bool aAttributesOverride)
{
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm && mContent) {
nsCOMPtr<nsIContent> anchorContent(do_QueryInterface(aAnchorElement));
nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(mContent->GetPrimaryFrame());
if (menuPopupFrame && menuPopupFrame->PopupState() == ePopupOpenAndVisible) {
menuPopupFrame->MoveToAnchor(anchorContent, aPosition, aXPos, aYPos, aAttributesOverride);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsPopupBoxObject::SizeTo(int32_t aWidth, int32_t aHeight)
{

View File

@ -159,6 +159,7 @@ MOCHITEST_CHROME_FILES = findbar_window.xul \
test_mousecapture.xul \
test_arrowpanel.xul \
test_menuitem_commands.xul \
test_popup_moveToAnchor.xul \
$(NULL)
# test_panel_focus.xul won't work if the Full Keyboard Access preference is set to

View File

@ -0,0 +1,85 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<title>Popup moveToAnchor Tests</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<vbox align="start">
<button id="button1" label="Button 1" style="margin-top: 40px;"/>
<button id="button2" label="Button 2" style="margin-top: 50px;"/>
</vbox>
<menupopup id="popup" onpopupshown="popupshown()" onpopuphidden="SimpleTest.finish()">
<menuitem label="One"/>
<menuitem label="Two"/>
</menupopup>
<script>
SimpleTest.waitForExplicitFinish();
function runTest(id)
{
$("popup").openPopup($("button1"), "after_start", 0, 0);
}
function popupshown()
{
var popup = $("popup");
var popupheight = popup.getBoundingClientRect().height;
var button1rect = $("button1").getBoundingClientRect();
var button2rect = $("button2").getBoundingClientRect();
checkCoords(popup, button1rect.left, button1rect.bottom, "initial");
popup.moveToAnchor($("button1"), "after_start", 0, 8, false);
checkCoords(popup, button1rect.left, button1rect.bottom + 8, "move anchor top + 8");
popup.moveToAnchor($("button1"), "after_start", 6, -10, false);
checkCoords(popup, button1rect.left + 6, button1rect.bottom - 10, "move anchor left + 6, top - 10");
popup.moveToAnchor($("button1"), "before_start", -2, 0, false);
checkCoords(popup, button1rect.left - 2, button1rect.top - popupheight, "move anchor before_start");
popup.moveToAnchor($("button2"), "before_start", 0, 0, false);
checkCoords(popup, button2rect.left, button2rect.top - popupheight, "move button2");
popup.moveToAnchor($("button1"), "end_before", 0, 0, false);
checkCoords(popup, button1rect.right, button1rect.top, "move anchor end_before");
popup.moveToAnchor($("button2"), "after_start", 5, 4, false);
checkCoords(popup, button2rect.left + 5, button2rect.bottom + 4, "move button2 left + 5, top + 4");
popup.moveTo($("button1").boxObject.screenX + 10, $("button1").boxObject.screenY + 12);
checkCoords(popup, button1rect.left + 10, button1rect.top + 12, "move to button1 screen with offset");
popup.moveToAnchor($("button1"), "after_start", 1, 2, false);
checkCoords(popup, button1rect.left + 1, button1rect.bottom + 2, "move button2 after screen");
popup.hidePopup();
}
function checkCoords(popup, expectedx, expectedy, testid)
{
var rect = popup.getBoundingClientRect();
is(Math.round(rect.left), Math.round(expectedx), testid + " left");
is(Math.round(rect.top), Math.round(expectedy), testid + " top");
}
SimpleTest.waitForFocus(runTest);
</script>
<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</window>

View File

@ -165,6 +165,19 @@
</body>
</method>
<method name="moveToAnchor">
<parameter name="aAnchorElement"/>
<parameter name="aPosition"/>
<parameter name="aX"/>
<parameter name="aY"/>
<parameter name="aAttributesOverride"/>
<body>
<![CDATA[
this.popupBoxObject.moveToAnchor(aAnchorElement, aPosition, aX, aY, aAttributesOverride);
]]>
</body>
</method>
<method name="getOuterScreenRect">
<body>
<![CDATA[