Commit 05fd6942 authored by yosin's avatar yosin Committed by Commit bot

Get rid of CaretBase::m_caretLocalRect

This patch gets rid of |CaretBase::m_caretLocalRect| to reduce number of states
in |CaretBase| to simplify class hiarachity for improving code health.

After this patch, |CaretBase| represents |DisplayItemClient| for caret.
Following patch will rename |CaretBase| to |DisplayCaretItemClient| and
|DragCaretController|, which should rename to |DragCaret| is derived from
|DisplayCaretItemClient| and makes |DisplayCaretItemClient| had no public
member except for destructor.

This patch is a preparation of [1]

[1] http://crrev.com/1958093002 On-demand visible selection canonicalizataion

BUG=680384
TEST=n/a; no behavior changes

Review-Url: https://codereview.chromium.org/2623053006
Cr-Commit-Position: refs/heads/master@{#443207}
parent 9438f509
...@@ -47,10 +47,6 @@ CaretBase::~CaretBase() = default; ...@@ -47,10 +47,6 @@ CaretBase::~CaretBase() = default;
DEFINE_TRACE(CaretBase) {} DEFINE_TRACE(CaretBase) {}
void CaretBase::clearCaretRect() {
m_caretLocalRect = LayoutRect();
}
static inline bool caretRendersInsideNode(Node* node) { static inline bool caretRendersInsideNode(Node* node) {
return node && !isDisplayInsideTable(node) && !editingIgnoresContent(*node); return node && !isDisplayInsideTable(node) && !editingIgnoresContent(*node);
} }
...@@ -77,53 +73,50 @@ LayoutBlock* CaretBase::caretLayoutObject(Node* node) { ...@@ -77,53 +73,50 @@ LayoutBlock* CaretBase::caretLayoutObject(Node* node) {
: layoutObject->containingBlock(); : layoutObject->containingBlock();
} }
static void mapCaretRectToCaretPainter(LayoutItem caretLayoutItem, static LayoutRect mapCaretRectToCaretPainter(
LayoutItem caretLayoutItem,
LayoutBlockItem caretPainterItem, LayoutBlockItem caretPainterItem,
LayoutRect& caretRect) { const LayoutRect& passedCaretRect) {
// FIXME: This shouldn't be called on un-rooted subtrees. // FIXME: This shouldn't be called on un-rooted subtrees.
// FIXME: This should probably just use mapLocalToAncestor. // FIXME: This should probably just use mapLocalToAncestor.
// Compute an offset between the caretLayoutItem and the caretPainterItem. // Compute an offset between the caretLayoutItem and the caretPainterItem.
DCHECK(caretLayoutItem.isDescendantOf(caretPainterItem)); DCHECK(caretLayoutItem.isDescendantOf(caretPainterItem));
bool unrooted = false; LayoutRect caretRect = passedCaretRect;
while (caretLayoutItem != caretPainterItem) { while (caretLayoutItem != caretPainterItem) {
LayoutItem containerItem = caretLayoutItem.container(); LayoutItem containerItem = caretLayoutItem.container();
if (containerItem.isNull()) { if (containerItem.isNull())
unrooted = true; return LayoutRect();
break;
}
caretRect.move(caretLayoutItem.offsetFromContainer(containerItem)); caretRect.move(caretLayoutItem.offsetFromContainer(containerItem));
caretLayoutItem = containerItem; caretLayoutItem = containerItem;
} }
return caretRect;
if (unrooted)
caretRect = LayoutRect();
} }
void CaretBase::updateCaretRect(const PositionWithAffinity& caretPosition) { LayoutRect CaretBase::computeCaretRect(
m_caretLocalRect = LayoutRect(); const PositionWithAffinity& caretPosition) {
if (caretPosition.isNull()) if (caretPosition.isNull())
return; return LayoutRect();
DCHECK(caretPosition.anchorNode()->layoutObject()); DCHECK(caretPosition.anchorNode()->layoutObject());
// First compute a rect local to the layoutObject at the selection start. // First compute a rect local to the layoutObject at the selection start.
LayoutObject* layoutObject; LayoutObject* layoutObject;
m_caretLocalRect = localCaretRectOfPosition(caretPosition, layoutObject); const LayoutRect& caretLocalRect =
localCaretRectOfPosition(caretPosition, layoutObject);
// Get the layoutObject that will be responsible for painting the caret // Get the layoutObject that will be responsible for painting the caret
// (which is either the layoutObject we just found, or one of its containers). // (which is either the layoutObject we just found, or one of its containers).
LayoutBlockItem caretPainterItem = LayoutBlockItem caretPainterItem =
LayoutBlockItem(caretLayoutObject(caretPosition.anchorNode())); LayoutBlockItem(caretLayoutObject(caretPosition.anchorNode()));
mapCaretRectToCaretPainter(LayoutItem(layoutObject), caretPainterItem, return mapCaretRectToCaretPainter(LayoutItem(layoutObject), caretPainterItem,
m_caretLocalRect); caretLocalRect);
} }
void CaretBase::updateCaretRect(const VisiblePosition& caretPosition) { LayoutRect CaretBase::computeCaretRect(const VisiblePosition& caretPosition) {
updateCaretRect(caretPosition.toPositionWithAffinity()); return computeCaretRect(caretPosition.toPositionWithAffinity());
} }
IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, IntRect CaretBase::absoluteBoundsForLocalRect(Node* node,
...@@ -168,21 +161,24 @@ bool CaretBase::shouldRepaintCaret(Node& node) const { ...@@ -168,21 +161,24 @@ bool CaretBase::shouldRepaintCaret(Node& node) const {
(node.parentNode() && hasEditableStyle(*node.parentNode())); (node.parentNode() && hasEditableStyle(*node.parentNode()));
} }
void CaretBase::invalidateCaretRect(Node* node) { void CaretBase::invalidateCaretRect(Node* node,
const LayoutRect& caretLocalRect) {
node->document().updateStyleAndLayoutTree(); node->document().updateStyleAndLayoutTree();
if (hasEditableStyle(*node)) if (hasEditableStyle(*node))
invalidateLocalCaretRect(node, localCaretRectWithoutUpdate()); invalidateLocalCaretRect(node, caretLocalRect);
} }
void CaretBase::paintCaret(Node* node, void CaretBase::paintCaret(Node* node,
GraphicsContext& context, GraphicsContext& context,
const DisplayItemClient& client,
const LayoutRect& caretLocalRect,
const LayoutPoint& paintOffset, const LayoutPoint& paintOffset,
DisplayItem::Type displayItemType) const { DisplayItem::Type displayItemType) {
if (DrawingRecorder::useCachedDrawingIfPossible(context, *this, if (DrawingRecorder::useCachedDrawingIfPossible(context, client,
displayItemType)) displayItemType))
return; return;
LayoutRect drawingRect = localCaretRectWithoutUpdate(); LayoutRect drawingRect = caretLocalRect;
if (LayoutBlock* layoutObject = caretLayoutObject(node)) if (LayoutBlock* layoutObject = caretLayoutObject(node))
layoutObject->flipForWritingMode(drawingRect); layoutObject->flipForWritingMode(drawingRect);
drawingRect.moveBy(paintOffset); drawingRect.moveBy(paintOffset);
...@@ -190,7 +186,7 @@ void CaretBase::paintCaret(Node* node, ...@@ -190,7 +186,7 @@ void CaretBase::paintCaret(Node* node,
const Color caretColor = const Color caretColor =
node->layoutObject()->resolveColor(CSSPropertyCaretColor); node->layoutObject()->resolveColor(CSSPropertyCaretColor);
IntRect paintRect = pixelSnappedIntRect(drawingRect); IntRect paintRect = pixelSnappedIntRect(drawingRect);
DrawingRecorder drawingRecorder(context, *this, DisplayItem::kCaret, DrawingRecorder drawingRecorder(context, client, DisplayItem::kCaret,
paintRect); paintRect);
context.fillRect(paintRect, caretColor); context.fillRect(paintRect, caretColor);
} }
......
...@@ -48,23 +48,29 @@ class CORE_EXPORT CaretBase : public GarbageCollectedFinalized<CaretBase>, ...@@ -48,23 +48,29 @@ class CORE_EXPORT CaretBase : public GarbageCollectedFinalized<CaretBase>,
CaretBase(); CaretBase();
virtual ~CaretBase(); virtual ~CaretBase();
void invalidateCaretRect(Node*); void invalidateCaretRect(Node*, const LayoutRect&);
void clearCaretRect();
// Creating VisiblePosition causes synchronous layout so we should use the // Creating VisiblePosition causes synchronous layout so we should use the
// PositionWithAffinity version if possible. // PositionWithAffinity version if possible.
// A position in HTMLTextFromControlElement is a typical example. // A position in HTMLTextFromControlElement is a typical example.
void updateCaretRect(const PositionWithAffinity& caretPosition); static LayoutRect computeCaretRect(const PositionWithAffinity& caretPosition);
void updateCaretRect(const VisiblePosition& caretPosition);
// TODO(yosin): We should move |computeCaretRect()| with |VisiblePosition| to
// "FrameCaret.cpp" as static file local function.
static LayoutRect computeCaretRect(const VisiblePosition& caretPosition);
// TODO(yosin): We should move |absoluteBoundsForLocalRect()| with
// |VisiblePosition| to "FrameCaret.cpp" as static file local function.
IntRect absoluteBoundsForLocalRect(Node*, const LayoutRect&) const; IntRect absoluteBoundsForLocalRect(Node*, const LayoutRect&) const;
// TODO(yosin): We should move |shouldRepaintCaret()| to "FrameCaret.cpp" as
// static file local function.
bool shouldRepaintCaret(Node&) const; bool shouldRepaintCaret(Node&) const;
void paintCaret(Node*, static void paintCaret(Node*,
GraphicsContext&, GraphicsContext&,
const DisplayItemClient&,
const LayoutRect& caretLocalRect,
const LayoutPoint&, const LayoutPoint&,
DisplayItem::Type) const; DisplayItem::Type);
const LayoutRect& localCaretRectWithoutUpdate() const {
return m_caretLocalRect;
}
static LayoutBlock* caretLayoutObject(Node*); static LayoutBlock* caretLayoutObject(Node*);
void invalidateLocalCaretRect(Node*, const LayoutRect&); void invalidateLocalCaretRect(Node*, const LayoutRect&);
...@@ -76,9 +82,6 @@ class CORE_EXPORT CaretBase : public GarbageCollectedFinalized<CaretBase>, ...@@ -76,9 +82,6 @@ class CORE_EXPORT CaretBase : public GarbageCollectedFinalized<CaretBase>,
DECLARE_VIRTUAL_TRACE(); DECLARE_VIRTUAL_TRACE();
private: private:
// caret rect in coords local to the layoutObject responsible for painting the
// caret
LayoutRect m_caretLocalRect;
LayoutRect m_visualRect; LayoutRect m_visualRect;
}; };
......
...@@ -61,20 +61,20 @@ void DragCaretController::setCaretPosition( ...@@ -61,20 +61,20 @@ void DragCaretController::setCaretPosition(
DisableCompositingQueryAsserts disabler; DisableCompositingQueryAsserts disabler;
if (Node* node = m_position.anchorNode()) if (Node* node = m_position.anchorNode())
m_caretBase->invalidateCaretRect(node); m_caretBase->invalidateCaretRect(node, m_caretLocalRect);
m_position = createVisiblePosition(position).toPositionWithAffinity(); m_position = createVisiblePosition(position).toPositionWithAffinity();
Document* document = nullptr; Document* document = nullptr;
if (Node* node = m_position.anchorNode()) { if (Node* node = m_position.anchorNode()) {
m_caretBase->invalidateCaretRect(node); m_caretBase->invalidateCaretRect(node, m_caretLocalRect);
document = &node->document(); document = &node->document();
setContext(document); setContext(document);
} }
if (m_position.isNull()) { if (m_position.isNull()) {
m_caretBase->clearCaretRect(); m_caretLocalRect = LayoutRect();
} else { } else {
DCHECK(!m_position.isOrphan()); DCHECK(!m_position.isOrphan());
document->updateStyleAndLayoutTree(); document->updateStyleAndLayoutTree();
m_caretBase->updateCaretRect(m_position); m_caretLocalRect = CaretBase::computeCaretRect(m_position);
} }
} }
...@@ -112,7 +112,8 @@ void DragCaretController::paintDragCaret(LocalFrame* frame, ...@@ -112,7 +112,8 @@ void DragCaretController::paintDragCaret(LocalFrame* frame,
GraphicsContext& context, GraphicsContext& context,
const LayoutPoint& paintOffset) const { const LayoutPoint& paintOffset) const {
if (m_position.anchorNode()->document().frame() == frame) { if (m_position.anchorNode()->document().frame() == frame) {
m_caretBase->paintCaret(m_position.anchorNode(), context, paintOffset, CaretBase::paintCaret(m_position.anchorNode(), context, *m_caretBase,
m_caretLocalRect, paintOffset,
DisplayItem::kDragCaret); DisplayItem::kDragCaret);
} }
} }
......
...@@ -63,6 +63,9 @@ class DragCaretController final ...@@ -63,6 +63,9 @@ class DragCaretController final
void nodeWillBeRemoved(Node&) final; void nodeWillBeRemoved(Node&) final;
PositionWithAffinity m_position; PositionWithAffinity m_position;
// caret rect in coords local to the layoutObject responsible for painting the
// caret
LayoutRect m_caretLocalRect;
const Member<CaretBase> m_caretBase; const Member<CaretBase> m_caretBase;
}; };
......
...@@ -209,6 +209,7 @@ void FrameCaret::invalidateCaretRect(bool forceInvalidation) { ...@@ -209,6 +209,7 @@ void FrameCaret::invalidateCaretRect(bool forceInvalidation) {
m_previousCaretVisibility = m_caretVisibility; m_previousCaretVisibility = m_caretVisibility;
} }
// TDOO(yosin): We should mark |FrameCaret::absoluteCaretBounds()| to |const|.
IntRect FrameCaret::absoluteCaretBounds() { IntRect FrameCaret::absoluteCaretBounds() {
DCHECK_NE(m_frame->document()->lifecycle().state(), DCHECK_NE(m_frame->document()->lifecycle().state(),
DocumentLifecycle::InPaintInvalidation); DocumentLifecycle::InPaintInvalidation);
...@@ -216,20 +217,18 @@ IntRect FrameCaret::absoluteCaretBounds() { ...@@ -216,20 +217,18 @@ IntRect FrameCaret::absoluteCaretBounds() {
DocumentLifecycle::DisallowTransitionScope disallowTransition( DocumentLifecycle::DisallowTransitionScope disallowTransition(
m_frame->document()->lifecycle()); m_frame->document()->lifecycle());
if (!isActive()) { Node* const caretNode = caretPosition().anchorNode();
clearCaretRect(); if (!isActive())
} else { return absoluteBoundsForLocalRect(caretNode, LayoutRect());
if (enclosingTextControl(caretPosition().position())) { // TODO(yosin): We should get rid of text control short path since layout
if (isVisuallyEquivalentCandidate(caretPosition().position())) // tree is clean.
updateCaretRect(caretPosition()); if (enclosingTextControl(caretPosition().position()) &&
else isVisuallyEquivalentCandidate(caretPosition().position())) {
updateCaretRect(createVisiblePosition(caretPosition())); return absoluteBoundsForLocalRect(caretNode,
} else { computeCaretRect(caretPosition()));
updateCaretRect(createVisiblePosition(caretPosition()));
}
} }
return absoluteBoundsForLocalRect(caretPosition().anchorNode(), return absoluteBoundsForLocalRect(
localCaretRectWithoutUpdate()); caretNode, computeCaretRect(createVisiblePosition(caretPosition())));
} }
void FrameCaret::setShouldShowBlockCursor(bool shouldShowBlockCursor) { void FrameCaret::setShouldShowBlockCursor(bool shouldShowBlockCursor) {
...@@ -248,9 +247,9 @@ void FrameCaret::paintCaret(GraphicsContext& context, ...@@ -248,9 +247,9 @@ void FrameCaret::paintCaret(GraphicsContext& context,
if (!(isActive() && m_shouldPaintCaret)) if (!(isActive() && m_shouldPaintCaret))
return; return;
updateCaretRect(caretPosition()); const LayoutRect caretLocalRect = computeCaretRect(caretPosition());
CaretBase::paintCaret(caretPosition().anchorNode(), context, paintOffset, CaretBase::paintCaret(caretPosition().anchorNode(), context, *this,
DisplayItem::kCaret); caretLocalRect, paintOffset, DisplayItem::kCaret);
} }
void FrameCaret::dataWillChange(const CharacterData& node) { void FrameCaret::dataWillChange(const CharacterData& node) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment