Bug 1068525 - Ignore zoom level for AccessibleCaret. f=mtseng, r=roc

We want AccessibleCaret be of the same size regardless of the zoom
level. We simply divide the caret's width, height, margin-left, and the
text selection bar's margin-left by current zoom level.

The margin-left of the caret is adjusted from -23px to -23.5px for better
looking.
This commit is contained in:
Ting-Yu Lin 2015-05-16 00:05:00 +08:00
parent a5589141de
commit 053054ff69
4 changed files with 57 additions and 14 deletions

View File

@ -26,6 +26,11 @@ using namespace dom;
NS_IMPL_ISUPPORTS(AccessibleCaret::DummyTouchListener, nsIDOMEventListener)
float AccessibleCaret::sWidth = 0.0f;
float AccessibleCaret::sHeight = 0.0f;
float AccessibleCaret::sMarginLeft = 0.0f;
float AccessibleCaret::sBarWidth = 0.0f;
// -----------------------------------------------------------------------------
// Implementation of AccessibleCaret methods
@ -40,6 +45,15 @@ AccessibleCaret::AccessibleCaret(nsIPresShell* aPresShell)
MOZ_ASSERT(mPresShell->GetCanvasFrame()->GetCustomContentContainer());
InjectCaretElement(mPresShell->GetDocument());
static bool prefsAdded = false;
if (!prefsAdded) {
Preferences::AddFloatVarCache(&sWidth, "layout.accessiblecaret.width");
Preferences::AddFloatVarCache(&sHeight, "layout.accessiblecaret.height");
Preferences::AddFloatVarCache(&sMarginLeft, "layout.accessiblecaret.margin-left");
Preferences::AddFloatVarCache(&sBarWidth, "layout.accessiblecaret.bar.width");
prefsAdded = true;
}
}
AccessibleCaret::~AccessibleCaret()
@ -215,13 +229,13 @@ AccessibleCaret::SetPosition(nsIFrame* aFrame, int32_t aOffset)
mImaginaryCaretRect = imaginaryCaretRect;
// SetCaretElementPosition() and SetSelectionBarElementPosition() require the
// SetCaretElementStyle() and SetSelectionBarElementStyle() require the
// input rect relative to container frame.
nsRect imaginaryCaretRectInContainerFrame = imaginaryCaretRectInFrame;
nsLayoutUtils::TransformRect(aFrame, CustomContentContainerFrame(),
imaginaryCaretRectInContainerFrame);
SetCaretElementPosition(imaginaryCaretRectInContainerFrame);
SetSelectionBarElementPosition(imaginaryCaretRectInContainerFrame);
SetCaretElementStyle(imaginaryCaretRectInContainerFrame);
SetSelectionBarElementStyle(imaginaryCaretRectInContainerFrame);
return PositionChangedResult::Changed;
}
@ -236,7 +250,7 @@ AccessibleCaret::CustomContentContainerFrame() const
}
void
AccessibleCaret::SetCaretElementPosition(const nsRect& aRect)
AccessibleCaret::SetCaretElementStyle(const nsRect& aRect)
{
nsPoint position = CaretElementPosition(aRect);
nsAutoString styleStr;
@ -244,6 +258,12 @@ AccessibleCaret::SetCaretElementPosition(const nsRect& aRect)
nsPresContext::AppUnitsToIntCSSPixels(position.x),
nsPresContext::AppUnitsToIntCSSPixels(position.y));
float zoomLevel = GetZoomLevel();
styleStr.AppendPrintf(" width: %.2fpx; height: %.2fpx; margin-left: %.2fpx",
sWidth / zoomLevel,
sHeight / zoomLevel,
sMarginLeft / zoomLevel);
ErrorResult rv;
CaretElement()->SetAttribute(NS_LITERAL_STRING("style"), styleStr, rv);
MOZ_ASSERT(!rv.Failed());
@ -252,13 +272,16 @@ AccessibleCaret::SetCaretElementPosition(const nsRect& aRect)
}
void
AccessibleCaret::SetSelectionBarElementPosition(const nsRect& aRect)
AccessibleCaret::SetSelectionBarElementStyle(const nsRect& aRect)
{
int32_t height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
nsAutoString barStyleStr;
barStyleStr.AppendPrintf("margin-top: -%dpx; height: %dpx;",
height, height);
float zoomLevel = GetZoomLevel();
barStyleStr.AppendPrintf(" width: %.2fpx;", sBarWidth / zoomLevel);
ErrorResult rv;
SelectionBarElement()->SetAttribute(NS_LITERAL_STRING("style"), barStyleStr, rv);
MOZ_ASSERT(!rv.Failed());
@ -266,4 +289,16 @@ AccessibleCaret::SetSelectionBarElementPosition(const nsRect& aRect)
AC_LOG("Set bar style: %s", NS_ConvertUTF16toUTF8(barStyleStr).get());
}
float
AccessibleCaret::GetZoomLevel()
{
// Full zoom on desktop.
float fullZoom = mPresShell->GetPresContext()->GetFullZoom();
// Pinch-zoom on B2G.
float resolution = mPresShell->GetCumulativeResolution();
return fullZoom * resolution;
}
} // namespace mozilla

View File

@ -129,8 +129,11 @@ public:
private:
// Argument aRect should be relative to CustomContentContainerFrame().
void SetCaretElementPosition(const nsRect& aRect);
void SetSelectionBarElementPosition(const nsRect& aRect);
void SetCaretElementStyle(const nsRect& aRect);
void SetSelectionBarElementStyle(const nsRect& aRect);
// Get current zoom level.
float GetZoomLevel();
// Element which contains the caret image for 'Contains' test.
dom::Element* CaretImageElement() const
@ -202,6 +205,12 @@ private:
// the caret.
nsRefPtr<DummyTouchListener> mDummyTouchListener{new DummyTouchListener()};
// Static class variables
static float sWidth;
static float sHeight;
static float sMarginLeft;
static float sBarWidth;
}; // class AccessibleCaret
} // namespace mozilla

View File

@ -321,12 +321,6 @@ div:-moz-native-anonymous.moz-accessiblecaret > div.bar {
z-index: 2147483647;
}
div:-moz-native-anonymous.moz-accessiblecaret {
width: 44px;
height: 47px;
margin-left: -23px;
}
div:-moz-native-anonymous.moz-accessiblecaret > div.image {
background-position: center center;
background-size: 100% 100%;
@ -340,7 +334,6 @@ div:-moz-native-anonymous.moz-accessiblecaret > div.image {
div:-moz-native-anonymous.moz-accessiblecaret > div.bar {
margin-left: 49%;
width: 2px;
background-color: #008aa0;
}

View File

@ -4606,6 +4606,12 @@ pref("caret.manages-android-actionbar", false);
// New implementation to unify touch-caret and selection-carets.
pref("layout.accessiblecaret.enabled", false);
// CSS attributes of the AccessibleCaret in CSS pixels.
pref("layout.accessiblecaret.width", "44.0");
pref("layout.accessiblecaret.height", "47.0");
pref("layout.accessiblecaret.margin-left", "-23.5");
pref("layout.accessiblecaret.bar.width", "2.0");
// Timeout in milliseconds to hide the accessiblecaret under cursor mode while
// no one touches it. Set the value to 0 to disable this feature.
pref("layout.accessiblecaret.timeout_ms", 3000);