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

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 rotated text + comment cleanup Created 5 years, 7 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_extentPoint.zero();
47 } 55 }
48 56
57 bool DirectionGranularityStrategy::arePositionsInOrder(
58 const VisiblePosition& pos1,
59 const VisiblePosition& pos2,
60 int order)
61 {
62 int positionOrder = comparePositions(pos1, pos2);
63 if (order == 0)
64 return positionOrder == 0;
65 return order > 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 // Initialize m_extentPoint if this is the first update after a clear.
87 if (m_state == StrategyState::Cleared) {
88 VisiblePosition pos = selection.isBaseFirst() ? selection.visibleEnd() : selection.visibleStart();
89 m_extentPoint = IntPoint(pos.absoluteCaretBounds().x(), pos.absoluteCare tBounds().y());
90 m_state = StrategyState::Expanding;
91 }
92
93 IntPoint oldOffsetExtentPoint(m_extentPoint.x() + m_offset, m_extentPoint.y( ));
94 VisiblePosition oldOffsetExtentPosition = visiblePositionForContentsPoint(ol dOffsetExtentPoint, frame);
95
96 // Apply the offset.
97 IntPoint offsetExtentPoint = extentPoint;
98 int dx = extentPoint.x() - m_extentPoint.x();
99 if (m_offset != 0) {
100 if (m_offset > 0 && dx > 0)
101 m_offset = std::max(0, m_offset - dx);
102 else if (m_offset < 0 && dx < 0)
103 m_offset = std::min(0, m_offset - dx);
104 offsetExtentPoint.move(m_offset, 0);
105 }
106 m_extentPoint = extentPoint;
107
108 VisiblePosition offsetExtentPosition = visiblePositionForContentsPoint(offse tExtentPoint, frame);
109
110 // Reset the offset in case of a line change. Also reset it if we detect a y
111 // component to the vector between the old and the new positions which would
112 // happen if the text is rotated. The offset feature doesn't work for
113 // non-horizontal text.
114 if (!inSameLine(offsetExtentPosition, oldOffsetExtentPosition)
115 || (offsetExtentPosition.absoluteCaretBounds().maxY() - oldOffsetExtentP osition.absoluteCaretBounds().maxY()) != 0) {
116 m_offset = 0;
117 offsetExtentPoint = extentPoint;
118 offsetExtentPosition = visiblePositionForContentsPoint(extentPoint, fram e);
119 }
120
121 const VisiblePosition base = selection.visibleBase();
122 int extentBaseOrder = comparePositions(offsetExtentPosition, base);
123
124 // Do not allow selection and extent to be at the same position
125 if (extentBaseOrder == 0)
66 return selection; 126 return selection;
67 127
68 const VisiblePosition base = selection.visibleBase(); 128 int oldExtentBaseOrder = comparePositions(selection.visibleExtent(), base);
69 const VisiblePosition oldExtentWithGranularity = selection.isBaseFirst() ? s election.visibleEnd() : selection.visibleStart(); 129 bool extentBaseOrderSwitched = !arePositionsInOrder(offsetExtentPosition, ba se, oldExtentBaseOrder);
70
71 int extentBaseOrder = comparePositions(extentPosition, base);
72 int oldExtentBaseOrder = comparePositions(oldExtentWithGranularity, base);
73
74 bool extentBaseOrderSwitched = (extentBaseOrder > 0 && oldExtentBaseOrder < 0)
75 || (extentBaseOrder < 0 && oldExtentBaseOrder > 0);
76 130
77 // Determine the boundary of the 'current word', i.e. the boundary extending 131 // Determine the boundary of the 'current word', i.e. the boundary extending
78 // beyond which should change the granularity to WordGranularity. 132 // beyond which should change the granularity to WordGranularity.
79 // If the last move has shrunk the selection and is now exactly on the word 133 VisiblePosition currentWordBoundary;
80 // boundary - we need to take the next bound as the bound of the "current
81 // word".
82 VisiblePosition currentWordBoundary = nextWordBound(
83 oldExtentWithGranularity,
84 oldExtentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirectio n::SearchBackwards,
85 m_lastMoveShrunkSelection ? BoundAdjust::NextBoundIfOnBound : BoundAdjus t::CurrentPosIfOnBound);
86
87 bool thisMoveShrunkSelection = (extentBaseOrder > 0 && comparePositions(exte ntPosition, selection.visibleExtent()) < 0)
88 || (extentBaseOrder < 0 && comparePositions(extentPosition, selection.vi sibleExtent()) > 0);
89 // If the extent-base order was switched, then the selection is now
90 // expanding in a different direction than before. Therefore we need to
91 // calculate the boundary of the 'current word' in this new direction in
92 // order to be able to tell if the selection expanded beyond it.
93 if (extentBaseOrderSwitched) { 134 if (extentBaseOrderSwitched) {
135 // Special case.
136 // If the extent-base order was switched, then the selection is now
137 // expanding in a different direction than before. Therefore we
138 // calculate the current word boundary in this new direction and
139 // based on the |base| position.
94 currentWordBoundary = nextWordBound( 140 currentWordBoundary = nextWordBound(
95 base, 141 base,
96 extentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirecti on::SearchBackwards, 142 extentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirecti on::SearchBackwards,
97 BoundAdjust::NextBoundIfOnBound); 143 BoundAdjust::NextBoundIfOnBound);
98 m_granularity = CharacterGranularity; 144 m_granularity = CharacterGranularity;
99 // When the base/extent order switches it doesn't count as shrinking sel ection. 145 } else {
100 thisMoveShrunkSelection = false; 146 // Calculate the current word boundary based on |oldExtentWithGranularit y|.
147 // If selection was shrunk in the last update and the extent is now
148 // exactly on the word boundary - we need to take the next bound as the
149 // bound of the current word.
150 currentWordBoundary = nextWordBound(
151 oldOffsetExtentPosition,
152 selection.isBaseFirst() ? SearchDirection::SearchForward : SearchDir ection::SearchBackwards,
153 m_state == StrategyState::Shrinking ? BoundAdjust::NextBoundIfOnBoun d : BoundAdjust::CurrentPosIfOnBound);
101 } 154 }
102 155
103 bool expandedBeyondWordBoundary; 156 bool expandedBeyondWordBoundary = arePositionsInOrder(offsetExtentPosition, currentWordBoundary, extentBaseOrder);
104 if (extentBaseOrder > 0) 157
105 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) > 0; 158 // The selection is shrunk if the extent changes position to be closer to
106 else 159 // the base, and the extent/base order wasn't switched.
107 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) < 0; 160 bool thisMoveShrunkSelection =
108 if (expandedBeyondWordBoundary) { 161 !extentBaseOrderSwitched
162 && oldOffsetExtentPosition != offsetExtentPosition
163 && arePositionsInOrder(oldOffsetExtentPosition, offsetExtentPosition, ex tentBaseOrder);
164
165 if (expandedBeyondWordBoundary)
109 m_granularity = WordGranularity; 166 m_granularity = WordGranularity;
110 } else if (thisMoveShrunkSelection) { 167 else if (thisMoveShrunkSelection)
111 m_granularity = CharacterGranularity; 168 m_granularity = CharacterGranularity;
112 m_lastMoveShrunkSelection = true; 169
170 VisiblePosition newSelectionExtent = offsetExtentPosition;
171 if (m_granularity == WordGranularity) {
172 // Determine the bounds of the word where the extent is located.
173 // Set the selection extent to one of the two bounds depending on
174 // whether the extent is passed the middle of the word.
175 VisiblePosition boundBeforeExtent = nextWordBound(offsetExtentPosition, SearchDirection::SearchBackwards, BoundAdjust::CurrentPosIfOnBound);
176 VisiblePosition boundAfterExtent = nextWordBound(offsetExtentPosition, S earchDirection::SearchForward, BoundAdjust::CurrentPosIfOnBound);
177 int xMiddleBetweenBounds = (boundAfterExtent.absoluteCaretBounds().x() + boundBeforeExtent.absoluteCaretBounds().x()) / 2;
178 bool extentBeforeMiddle = offsetExtentPoint.x() < xMiddleBetweenBounds;
179
180 newSelectionExtent = extentBeforeMiddle ? boundBeforeExtent : boundAfter Extent;
181 // Update the offset if needed.
182 if (!inSameLine(newSelectionExtent, selection.visibleExtent()))
183 m_offset = 0;
184 else if ((extentBaseOrder > 0 && !extentBeforeMiddle) || (extentBaseOrde r < 0 && extentBeforeMiddle))
185 m_offset = newSelectionExtent.absoluteCaretBounds().x() - extentPoin t.x();
113 } 186 }
114 187
115 m_lastMoveShrunkSelection = thisMoveShrunkSelection; 188 // Only update the state if the selection actually changed as a result of
189 // this move.
190 if (newSelectionExtent != selection.visibleExtent())
191 m_state = thisMoveShrunkSelection ? StrategyState::Shrinking : StrategyS tate::Expanding;
192
116 VisibleSelection newSelection = selection; 193 VisibleSelection newSelection = selection;
117 newSelection.setExtent(extentPosition); 194 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; 195 return newSelection;
126 } 196 }
127 197
128 } // namespace blink 198 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698