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

Side by Side Diff: Source/core/editing/GranularityStrategy.cpp

Issue 1123563003: Improving direction-based selection strategy. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Addressing remaining review feedback. Created 5 years, 6 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 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/editing/GranularityStrategy.h" 6 #include "core/editing/GranularityStrategy.h"
7 7
8 #include "core/editing/FrameSelection.h"
8 #include "core/editing/htmlediting.h" 9 #include "core/editing/htmlediting.h"
9 10
10 namespace blink { 11 namespace blink {
11 12
12 GranularityStrategy::GranularityStrategy() { } 13 GranularityStrategy::GranularityStrategy() { }
13 14
14 GranularityStrategy::~GranularityStrategy() { } 15 GranularityStrategy::~GranularityStrategy() { }
15 16
16 CharacterGranularityStrategy::CharacterGranularityStrategy() { } 17 CharacterGranularityStrategy::CharacterGranularityStrategy() { }
17 18
18 CharacterGranularityStrategy::~CharacterGranularityStrategy() { } 19 CharacterGranularityStrategy::~CharacterGranularityStrategy() { }
19 20
20 SelectionStrategy CharacterGranularityStrategy::GetType() const 21 SelectionStrategy CharacterGranularityStrategy::GetType() const
21 { 22 {
22 return SelectionStrategy::Character; 23 return SelectionStrategy::Character;
23 } 24 }
24 25
25 void CharacterGranularityStrategy::Clear() { }; 26 void CharacterGranularityStrategy::Clear() { };
26 27
27 VisibleSelection CharacterGranularityStrategy::updateExtent(const VisiblePositio n& extentPosition, const VisibleSelection& selection) 28 VisibleSelection CharacterGranularityStrategy::updateExtent(const IntPoint& exte ntPoint, LocalFrame* frame)
28 { 29 {
30 const VisiblePosition& extentPosition = visiblePositionForContentsPoint(exte ntPoint, frame);
31 const VisibleSelection& selection = frame->selection().selection();
32 if (selection.visibleBase() == extentPosition)
33 return selection;
29 return VisibleSelection(selection.visibleBase(), extentPosition); 34 return VisibleSelection(selection.visibleBase(), extentPosition);
30 } 35 }
31 36
32 DirectionGranularityStrategy::DirectionGranularityStrategy() 37 DirectionGranularityStrategy::DirectionGranularityStrategy()
33 : m_granularity(CharacterGranularity) 38 : m_state(StrategyState::Cleared)
34 , m_lastMoveShrunkSelection(false) { } 39 , m_granularity(CharacterGranularity)
40 , m_offset(0) { }
35 41
36 DirectionGranularityStrategy::~DirectionGranularityStrategy() { } 42 DirectionGranularityStrategy::~DirectionGranularityStrategy() { }
37 43
38 SelectionStrategy DirectionGranularityStrategy::GetType() const 44 SelectionStrategy DirectionGranularityStrategy::GetType() const
39 { 45 {
40 return SelectionStrategy::Direction; 46 return SelectionStrategy::Direction;
41 } 47 }
42 48
43 void DirectionGranularityStrategy::Clear() 49 void DirectionGranularityStrategy::Clear()
44 { 50 {
51 m_state = StrategyState::Cleared;
45 m_granularity = CharacterGranularity; 52 m_granularity = CharacterGranularity;
46 m_lastMoveShrunkSelection = false; 53 m_offset = 0;
54 m_subPositionCorrection = IntSize();
47 } 55 }
48 56
57 bool DirectionGranularityStrategy::arePositionsInSpecifiedOrder(
58 const VisiblePosition& vp1,
59 const VisiblePosition& vp2,
60 int specifiedOrder)
61 {
62 int positionOrder = comparePositions(vp1, vp2);
63 if (specifiedOrder == 0)
64 return positionOrder == 0;
65 return specifiedOrder > 0 ? positionOrder > 0 : positionOrder < 0;
66 }
67
68
49 VisiblePosition DirectionGranularityStrategy::nextWordBound( 69 VisiblePosition DirectionGranularityStrategy::nextWordBound(
50 const VisiblePosition& pos, 70 const VisiblePosition& pos,
51 SearchDirection direction, 71 SearchDirection direction,
52 BoundAdjust wordBoundAdjust) 72 BoundAdjust wordBoundAdjust)
53 { 73 {
54 bool nextBoundIfOnBound = wordBoundAdjust == BoundAdjust::NextBoundIfOnBoun d; 74 bool nextBoundIfOnBound = wordBoundAdjust == BoundAdjust::NextBoundIfOnBoun d;
55 if (direction == SearchDirection::SearchForward) { 75 if (direction == SearchDirection::SearchForward) {
56 EWordSide wordSide = nextBoundIfOnBound ? RightWordIfOnBoundary : LeftWo rdIfOnBoundary; 76 EWordSide wordSide = nextBoundIfOnBound ? RightWordIfOnBoundary : LeftWo rdIfOnBoundary;
57 return endOfWord(pos, wordSide); 77 return endOfWord(pos, wordSide);
58 } 78 }
59 EWordSide wordSide = nextBoundIfOnBound ? LeftWordIfOnBoundary : RightWordIf OnBoundary; 79 EWordSide wordSide = nextBoundIfOnBound ? LeftWordIfOnBoundary : RightWordIf OnBoundary;
60 return startOfWord(pos, wordSide); 80 return startOfWord(pos, wordSide);
61 } 81 }
62 82
63 VisibleSelection DirectionGranularityStrategy::updateExtent(const VisiblePositio n& extentPosition, const VisibleSelection& selection) 83 VisibleSelection DirectionGranularityStrategy::updateExtent(const IntPoint& exte ntPoint, LocalFrame* frame)
64 { 84 {
65 if (extentPosition == selection.visibleExtent()) 85 const VisibleSelection& selection = frame->selection().selection();
86
87 if (m_state == StrategyState::Cleared)
88 m_state = StrategyState::Expanding;
89
90 VisiblePosition oldOffsetExtentPosition = selection.visibleExtent();
91 IntPoint oldExtentLocation = positionLocation(oldOffsetExtentPosition);
92 IntPoint oldOffsetExtentPoint = oldExtentLocation + m_subPositionCorrection;
93 IntPoint oldExtentPoint = IntPoint(oldOffsetExtentPoint.x() - m_offset, oldO ffsetExtentPoint.y());
94
95 // Apply the offset.
96 IntPoint newOffsetExtentPoint = extentPoint;
97 int dx = extentPoint.x() - oldExtentPoint.x();
98 if (m_offset != 0) {
99 if (m_offset > 0 && dx > 0)
100 m_offset = std::max(0, m_offset - dx);
101 else if (m_offset < 0 && dx < 0)
102 m_offset = std::min(0, m_offset - dx);
103 newOffsetExtentPoint.move(m_offset, 0);
104 }
105
106 VisiblePosition newOffsetExtentPosition = visiblePositionForContentsPoint(ne wOffsetExtentPoint, frame);
107 IntPoint newOffsetLocation = positionLocation(newOffsetExtentPosition);
108
109 // Reset the offset in case of a vertical change in the location (could be
110 // due to a line change or due to an unusual layout, e.g. rotated text).
111 bool verticalChange = newOffsetLocation.y() != oldExtentLocation.y();
112 if (verticalChange) {
113 m_offset = 0;
114 m_granularity = CharacterGranularity;
115 newOffsetExtentPoint = extentPoint;
116 newOffsetExtentPosition = visiblePositionForContentsPoint(extentPoint, f rame);
117 }
118
119 const VisiblePosition base = selection.visibleBase();
120
121 // Do not allow empty selection.
122 if (newOffsetExtentPosition == base)
66 return selection; 123 return selection;
67 124
68 const VisiblePosition base = selection.visibleBase(); 125 // The direction granularity strategy, particularly the "offset" feature
69 const VisiblePosition oldExtentWithGranularity = selection.isBaseFirst() ? s election.visibleEnd() : selection.visibleStart(); 126 // doesn't work with non-horizontal text (e.g. when the text is rotated).
127 // So revert to the behavior equivalent to the character granularity
128 // strategy if we detect that the text's baseline coordinate changed
129 // without a line change.
130 if (verticalChange && inSameLine(newOffsetExtentPosition, oldOffsetExtentPos ition))
131 return VisibleSelection(selection.visibleBase(), newOffsetExtentPosition );
70 132
71 int extentBaseOrder = comparePositions(extentPosition, base); 133 int oldExtentBaseOrder = selection.isBaseFirst() ? 1 : -1;
72 int oldExtentBaseOrder = comparePositions(oldExtentWithGranularity, base);
73 134
74 bool extentBaseOrderSwitched = (extentBaseOrder > 0 && oldExtentBaseOrder < 0) 135 int newExtentBaseOrder;
75 || (extentBaseOrder < 0 && oldExtentBaseOrder > 0); 136 bool thisMoveShrunkSelection;
137 if (newOffsetExtentPosition == oldOffsetExtentPosition) {
138 if (m_granularity == CharacterGranularity)
139 return selection;
76 140
77 // Determine the boundary of the 'current word', i.e. the boundary extending 141 // If we are in Word granularity, we cannot exit here, since we may pass
78 // beyond which should change the granularity to WordGranularity. 142 // the middle of the word without changing the position (in which case
79 // If the last move has shrunk the selection and is now exactly on the word 143 // the selection needs to expand).
80 // boundary - we need to take the next bound as the bound of the "current 144 thisMoveShrunkSelection = false;
81 // word". 145 newExtentBaseOrder = oldExtentBaseOrder;
82 VisiblePosition currentWordBoundary = nextWordBound( 146 } else {
83 oldExtentWithGranularity, 147 bool selectionExpanded = arePositionsInSpecifiedOrder(newOffsetExtentPos ition, oldOffsetExtentPosition, oldExtentBaseOrder);
84 oldExtentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirectio n::SearchBackwards, 148 bool extentBaseOrderSwitched = selectionExpanded ? false : !arePositions InSpecifiedOrder(newOffsetExtentPosition, base, oldExtentBaseOrder);
85 m_lastMoveShrunkSelection ? BoundAdjust::NextBoundIfOnBound : BoundAdjus t::CurrentPosIfOnBound); 149 newExtentBaseOrder = extentBaseOrderSwitched ? -oldExtentBaseOrder : old ExtentBaseOrder;
86 150
87 bool thisMoveShrunkSelection = (extentBaseOrder > 0 && comparePositions(exte ntPosition, selection.visibleExtent()) < 0) 151 // Determine the word boundary, i.e. the boundary extending beyond which
88 || (extentBaseOrder < 0 && comparePositions(extentPosition, selection.vi sibleExtent()) > 0); 152 // should change the granularity to WordGranularity.
89 // If the extent-base order was switched, then the selection is now 153 VisiblePosition wordBoundary;
90 // expanding in a different direction than before. Therefore we need to 154 if (extentBaseOrderSwitched) {
91 // calculate the boundary of the 'current word' in this new direction in 155 // Special case.
92 // order to be able to tell if the selection expanded beyond it. 156 // If the extent-base order was switched, then the selection is now
93 if (extentBaseOrderSwitched) { 157 // expanding in a different direction than before. Therefore we
94 currentWordBoundary = nextWordBound( 158 // calculate the word boundary in this new direction and based on
95 base, 159 // the |base| position.
96 extentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirecti on::SearchBackwards, 160 wordBoundary = nextWordBound(
97 BoundAdjust::NextBoundIfOnBound); 161 base,
98 m_granularity = CharacterGranularity; 162 newExtentBaseOrder > 0 ? SearchDirection::SearchForward : Search Direction::SearchBackwards,
99 // When the base/extent order switches it doesn't count as shrinking sel ection. 163 BoundAdjust::NextBoundIfOnBound);
100 thisMoveShrunkSelection = false; 164 m_granularity = CharacterGranularity;
165 } else {
166 // Calculate the word boundary based on |oldExtentWithGranularity|.
167 // If selection was shrunk in the last update and the extent is now
168 // exactly on the word boundary - we need to take the next bound as
169 // the bound of the current word.
170 wordBoundary = nextWordBound(
171 oldOffsetExtentPosition,
172 oldExtentBaseOrder > 0 ? SearchDirection::SearchForward : Search Direction::SearchBackwards,
173 m_state == StrategyState::Shrinking ? BoundAdjust::NextBoundIfOn Bound : BoundAdjust::CurrentPosIfOnBound);
174 }
175
176 bool expandedBeyondWordBoundary;
177 if (selectionExpanded)
178 expandedBeyondWordBoundary = arePositionsInSpecifiedOrder(newOffsetE xtentPosition, wordBoundary, newExtentBaseOrder);
179 else if (extentBaseOrderSwitched)
180 expandedBeyondWordBoundary = arePositionsInSpecifiedOrder(newOffsetE xtentPosition, wordBoundary, newExtentBaseOrder);
181 else
182 expandedBeyondWordBoundary = false;
183
184 // The selection is shrunk if the extent changes position to be closer t o
185 // the base, and the extent/base order wasn't switched.
186 thisMoveShrunkSelection = !extentBaseOrderSwitched && !selectionExpanded ;
187
188 if (expandedBeyondWordBoundary)
189 m_granularity = WordGranularity;
190 else if (thisMoveShrunkSelection)
191 m_granularity = CharacterGranularity;
101 } 192 }
102 193
103 bool expandedBeyondWordBoundary; 194 VisiblePosition newSelectionExtent = newOffsetExtentPosition;
104 if (extentBaseOrder > 0) 195 if (m_granularity == WordGranularity) {
105 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) > 0; 196 // Determine the bounds of the word where the extent is located.
106 else 197 // Set the selection extent to one of the two bounds depending on
107 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) < 0; 198 // whether the extent is passed the middle of the word.
108 if (expandedBeyondWordBoundary) { 199 VisiblePosition boundBeforeExtent = nextWordBound(newOffsetExtentPositio n, SearchDirection::SearchBackwards, BoundAdjust::CurrentPosIfOnBound);
109 m_granularity = WordGranularity; 200 VisiblePosition boundAfterExtent = nextWordBound(newOffsetExtentPosition , SearchDirection::SearchForward, BoundAdjust::CurrentPosIfOnBound);
110 } else if (thisMoveShrunkSelection) { 201 int xMiddleBetweenBounds = (positionLocation(boundAfterExtent).x() + pos itionLocation(boundBeforeExtent).x()) / 2;
111 m_granularity = CharacterGranularity; 202 bool offsetExtentBeforeMiddle = newOffsetExtentPoint.x() < xMiddleBetwee nBounds;
112 m_lastMoveShrunkSelection = true; 203 newSelectionExtent = offsetExtentBeforeMiddle ? boundBeforeExtent : boun dAfterExtent;
204 // Update the offset if selection expanded in word granularity.
205 if (newSelectionExtent != selection.visibleExtent()
206 && ((newExtentBaseOrder > 0 && !offsetExtentBeforeMiddle) || (newExt entBaseOrder < 0 && offsetExtentBeforeMiddle))) {
207 m_offset = positionLocation(newSelectionExtent).x() - extentPoint.x( );
208 }
113 } 209 }
114 210
115 m_lastMoveShrunkSelection = thisMoveShrunkSelection; 211 // Only update the state if the selection actually changed as a result of
212 // this move.
213 if (newSelectionExtent != selection.visibleExtent())
214 m_state = thisMoveShrunkSelection ? StrategyState::Shrinking : StrategyS tate::Expanding;
215
216 m_subPositionCorrection = extentPoint + IntSize(m_offset, 0) - positionLocat ion(newSelectionExtent);
leviw_travelin_and_unemployed 2015/06/03 22:29:48 I think we need a better variable name for m_subPo
mfomitchev 2015/06/05 17:38:39 Done.
116 VisibleSelection newSelection = selection; 217 VisibleSelection newSelection = selection;
117 newSelection.setExtent(extentPosition); 218 newSelection.setExtent(newSelectionExtent);
118 if (m_granularity == WordGranularity) {
119 if (extentBaseOrder > 0)
120 newSelection.setEndRespectingGranularity(m_granularity, LeftWordIfOn Boundary);
121 else
122 newSelection.setStartRespectingGranularity(m_granularity, RightWordI fOnBoundary);
123 }
124
125 return newSelection; 219 return newSelection;
126 } 220 }
127 221
128 } // namespace blink 222 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698