Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: third_party/WebKit/Source/core/editing/CaretBase.cpp

Issue 2623053006: Get rid of CaretBase::m_caretLocalRect (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 29 matching lines...) Expand all
40 #include "platform/graphics/GraphicsLayer.h" 40 #include "platform/graphics/GraphicsLayer.h"
41 #include "platform/graphics/paint/DrawingRecorder.h" 41 #include "platform/graphics/paint/DrawingRecorder.h"
42 42
43 namespace blink { 43 namespace blink {
44 44
45 CaretBase::CaretBase() = default; 45 CaretBase::CaretBase() = default;
46 CaretBase::~CaretBase() = default; 46 CaretBase::~CaretBase() = default;
47 47
48 DEFINE_TRACE(CaretBase) {} 48 DEFINE_TRACE(CaretBase) {}
49 49
50 void CaretBase::clearCaretRect() {
51 m_caretLocalRect = LayoutRect();
52 }
53
54 static inline bool caretRendersInsideNode(Node* node) { 50 static inline bool caretRendersInsideNode(Node* node) {
55 return node && !isDisplayInsideTable(node) && !editingIgnoresContent(*node); 51 return node && !isDisplayInsideTable(node) && !editingIgnoresContent(*node);
56 } 52 }
57 53
58 LayoutBlock* CaretBase::caretLayoutObject(Node* node) { 54 LayoutBlock* CaretBase::caretLayoutObject(Node* node) {
59 if (!node) 55 if (!node)
60 return nullptr; 56 return nullptr;
61 57
62 LayoutObject* layoutObject = node->layoutObject(); 58 LayoutObject* layoutObject = node->layoutObject();
63 if (!layoutObject) 59 if (!layoutObject)
64 return nullptr; 60 return nullptr;
65 61
66 // if caretNode is a block and caret is inside it then caret should be painted 62 // if caretNode is a block and caret is inside it then caret should be painted
67 // by that block 63 // by that block
68 bool paintedByBlock = 64 bool paintedByBlock =
69 layoutObject->isLayoutBlock() && caretRendersInsideNode(node); 65 layoutObject->isLayoutBlock() && caretRendersInsideNode(node);
70 // TODO(yoichio): This function is called at least 66 // TODO(yoichio): This function is called at least
71 // DocumentLifeCycle::LayoutClean but caretRendersInsideNode above can 67 // DocumentLifeCycle::LayoutClean but caretRendersInsideNode above can
72 // layout. Thus |node->layoutObject()| can be changed then this is bad 68 // layout. Thus |node->layoutObject()| can be changed then this is bad
73 // design. We should make caret painting algorithm clean. 69 // design. We should make caret painting algorithm clean.
74 CHECK_EQ(layoutObject, node->layoutObject()) 70 CHECK_EQ(layoutObject, node->layoutObject())
75 << "Layout tree should not changed"; 71 << "Layout tree should not changed";
76 return paintedByBlock ? toLayoutBlock(layoutObject) 72 return paintedByBlock ? toLayoutBlock(layoutObject)
77 : layoutObject->containingBlock(); 73 : layoutObject->containingBlock();
78 } 74 }
79 75
80 static void mapCaretRectToCaretPainter(LayoutItem caretLayoutItem, 76 static LayoutRect mapCaretRectToCaretPainter(
81 LayoutBlockItem caretPainterItem, 77 LayoutItem caretLayoutItem,
82 LayoutRect& caretRect) { 78 LayoutBlockItem caretPainterItem,
79 const LayoutRect& passedCaretRect) {
83 // FIXME: This shouldn't be called on un-rooted subtrees. 80 // FIXME: This shouldn't be called on un-rooted subtrees.
84 // FIXME: This should probably just use mapLocalToAncestor. 81 // FIXME: This should probably just use mapLocalToAncestor.
85 // Compute an offset between the caretLayoutItem and the caretPainterItem. 82 // Compute an offset between the caretLayoutItem and the caretPainterItem.
86 83
87 DCHECK(caretLayoutItem.isDescendantOf(caretPainterItem)); 84 DCHECK(caretLayoutItem.isDescendantOf(caretPainterItem));
88 85
89 bool unrooted = false; 86 LayoutRect caretRect = passedCaretRect;
90 while (caretLayoutItem != caretPainterItem) { 87 while (caretLayoutItem != caretPainterItem) {
91 LayoutItem containerItem = caretLayoutItem.container(); 88 LayoutItem containerItem = caretLayoutItem.container();
92 if (containerItem.isNull()) { 89 if (containerItem.isNull())
93 unrooted = true; 90 return LayoutRect();
94 break;
95 }
96 caretRect.move(caretLayoutItem.offsetFromContainer(containerItem)); 91 caretRect.move(caretLayoutItem.offsetFromContainer(containerItem));
97 caretLayoutItem = containerItem; 92 caretLayoutItem = containerItem;
98 } 93 }
99 94 return caretRect;
100 if (unrooted)
101 caretRect = LayoutRect();
102 } 95 }
103 96
104 void CaretBase::updateCaretRect(const PositionWithAffinity& caretPosition) { 97 LayoutRect CaretBase::computeCaretRect(
105 m_caretLocalRect = LayoutRect(); 98 const PositionWithAffinity& caretPosition) {
106
107 if (caretPosition.isNull()) 99 if (caretPosition.isNull())
108 return; 100 return LayoutRect();
109 101
110 DCHECK(caretPosition.anchorNode()->layoutObject()); 102 DCHECK(caretPosition.anchorNode()->layoutObject());
111 103
112 // First compute a rect local to the layoutObject at the selection start. 104 // First compute a rect local to the layoutObject at the selection start.
113 LayoutObject* layoutObject; 105 LayoutObject* layoutObject;
114 m_caretLocalRect = localCaretRectOfPosition(caretPosition, layoutObject); 106 LayoutRect caretLocalRect =
yoichio 2017/01/12 09:22:22 nit: const LayoutRect
107 localCaretRectOfPosition(caretPosition, layoutObject);
115 108
116 // Get the layoutObject that will be responsible for painting the caret 109 // Get the layoutObject that will be responsible for painting the caret
117 // (which is either the layoutObject we just found, or one of its containers). 110 // (which is either the layoutObject we just found, or one of its containers).
118 LayoutBlockItem caretPainterItem = 111 LayoutBlockItem caretPainterItem =
119 LayoutBlockItem(caretLayoutObject(caretPosition.anchorNode())); 112 LayoutBlockItem(caretLayoutObject(caretPosition.anchorNode()));
120 113
121 mapCaretRectToCaretPainter(LayoutItem(layoutObject), caretPainterItem, 114 return mapCaretRectToCaretPainter(LayoutItem(layoutObject), caretPainterItem,
122 m_caretLocalRect); 115 caretLocalRect);
123 } 116 }
124 117
125 void CaretBase::updateCaretRect(const VisiblePosition& caretPosition) { 118 LayoutRect CaretBase::computeCaretRect(const VisiblePosition& caretPosition) {
126 updateCaretRect(caretPosition.toPositionWithAffinity()); 119 return computeCaretRect(caretPosition.toPositionWithAffinity());
127 } 120 }
128 121
129 IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, 122 IntRect CaretBase::absoluteBoundsForLocalRect(Node* node,
130 const LayoutRect& rect) const { 123 const LayoutRect& rect) const {
131 LayoutBlock* caretPainter = caretLayoutObject(node); 124 LayoutBlock* caretPainter = caretLayoutObject(node);
132 if (!caretPainter) 125 if (!caretPainter)
133 return IntRect(); 126 return IntRect();
134 127
135 LayoutRect localRect(rect); 128 LayoutRect localRect(rect);
136 caretPainter->flipForWritingMode(localRect); 129 caretPainter->flipForWritingMode(localRect);
(...skipping 24 matching lines...) Expand all
161 154
162 bool CaretBase::shouldRepaintCaret(Node& node) const { 155 bool CaretBase::shouldRepaintCaret(Node& node) const {
163 // If PositionAnchorType::BeforeAnchor or PositionAnchorType::AfterAnchor, 156 // If PositionAnchorType::BeforeAnchor or PositionAnchorType::AfterAnchor,
164 // carets need to be repainted not only when the node is contentEditable but 157 // carets need to be repainted not only when the node is contentEditable but
165 // also when its parentNode() is contentEditable. 158 // also when its parentNode() is contentEditable.
166 node.document().updateStyleAndLayoutTree(); 159 node.document().updateStyleAndLayoutTree();
167 return hasEditableStyle(node) || 160 return hasEditableStyle(node) ||
168 (node.parentNode() && hasEditableStyle(*node.parentNode())); 161 (node.parentNode() && hasEditableStyle(*node.parentNode()));
169 } 162 }
170 163
171 void CaretBase::invalidateCaretRect(Node* node) { 164 void CaretBase::invalidateCaretRect(Node* node,
165 const LayoutRect& caretLocalRect) {
172 node->document().updateStyleAndLayoutTree(); 166 node->document().updateStyleAndLayoutTree();
173 if (hasEditableStyle(*node)) 167 if (hasEditableStyle(*node))
174 invalidateLocalCaretRect(node, localCaretRectWithoutUpdate()); 168 invalidateLocalCaretRect(node, caretLocalRect);
175 } 169 }
176 170
177 void CaretBase::paintCaret(Node* node, 171 void CaretBase::paintCaret(Node* node,
178 GraphicsContext& context, 172 GraphicsContext& context,
173 const DisplayItemClient& client,
174 const LayoutRect& caretLocalRect,
179 const LayoutPoint& paintOffset, 175 const LayoutPoint& paintOffset,
180 DisplayItem::Type displayItemType) const { 176 DisplayItem::Type displayItemType) {
181 if (DrawingRecorder::useCachedDrawingIfPossible(context, *this, 177 if (DrawingRecorder::useCachedDrawingIfPossible(context, client,
182 displayItemType)) 178 displayItemType))
183 return; 179 return;
184 180
185 LayoutRect drawingRect = localCaretRectWithoutUpdate(); 181 LayoutRect drawingRect = caretLocalRect;
186 if (LayoutBlock* layoutObject = caretLayoutObject(node)) 182 if (LayoutBlock* layoutObject = caretLayoutObject(node))
187 layoutObject->flipForWritingMode(drawingRect); 183 layoutObject->flipForWritingMode(drawingRect);
188 drawingRect.moveBy(paintOffset); 184 drawingRect.moveBy(paintOffset);
189 185
190 const Color caretColor = 186 const Color caretColor =
191 node->layoutObject()->resolveColor(CSSPropertyCaretColor); 187 node->layoutObject()->resolveColor(CSSPropertyCaretColor);
192 IntRect paintRect = pixelSnappedIntRect(drawingRect); 188 IntRect paintRect = pixelSnappedIntRect(drawingRect);
193 DrawingRecorder drawingRecorder(context, *this, DisplayItem::kCaret, 189 DrawingRecorder drawingRecorder(context, client, DisplayItem::kCaret,
194 paintRect); 190 paintRect);
195 context.fillRect(paintRect, caretColor); 191 context.fillRect(paintRect, caretColor);
196 } 192 }
197 193
198 String CaretBase::debugName() const { 194 String CaretBase::debugName() const {
199 return "Caret"; 195 return "Caret";
200 } 196 }
201 197
202 LayoutRect CaretBase::visualRect() const { 198 LayoutRect CaretBase::visualRect() const {
203 return m_visualRect; 199 return m_visualRect;
204 } 200 }
205 201
206 } // namespace blink 202 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698