Chromium Code Reviews| OLD | NEW |
|---|---|
| 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); | |
|
leviw_travelin_and_unemployed
2015/05/20 20:24:37
As discussed in our meeting, hit testing is expens
mfomitchev
2015/05/20 21:37:29
Let me think about/work on this.
| |
| 95 | |
| 96 // Apply the offset. | |
| 97 IntPoint offsetExtentPoint = extentPoint; | |
| 98 int dx = extentPoint.x() - m_extentPoint.x(); | |
|
leviw_travelin_and_unemployed
2015/05/20 20:24:37
This is just wrong in the presence of transforms o
mfomitchev
2015/05/20 21:37:29
Yup.
| |
| 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 if (!inSameLine(offsetExtentPosition, oldOffsetExtentPosition)) { | |
| 111 m_offset = 0; | |
| 112 offsetExtentPoint = extentPoint; | |
| 113 offsetExtentPosition = visiblePositionForContentsPoint(extentPoint, fram e); | |
| 114 } | |
| 115 | |
| 116 const VisiblePosition base = selection.visibleBase(); | |
| 117 int extentBaseOrder = comparePositions(offsetExtentPosition, base); | |
| 118 | |
| 119 // Do not allow selection and extent to be at the same position | |
| 120 if (extentBaseOrder == 0) | |
| 66 return selection; | 121 return selection; |
| 67 | 122 |
| 68 const VisiblePosition base = selection.visibleBase(); | 123 int oldExtentBaseOrder = comparePositions(selection.visibleExtent(), base); |
| 69 const VisiblePosition oldExtentWithGranularity = selection.isBaseFirst() ? s election.visibleEnd() : selection.visibleStart(); | 124 bool extentBaseOrderSwitched = !arePositionsInOrder(offsetExtentPosition, ba se, oldExtentBaseOrder); |
|
leviw_travelin_and_unemployed
2015/05/20 20:24:37
Is it possible to avoid three calls to comparePosi
mfomitchev
2015/05/20 21:37:29
Let me think about this as well.
| |
| 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 | 125 |
| 77 // Determine the boundary of the 'current word', i.e. the boundary extending | 126 // Determine the boundary of the 'current word', i.e. the boundary extending |
| 78 // beyond which should change the granularity to WordGranularity. | 127 // beyond which should change the granularity to WordGranularity. |
| 79 // If the last move has shrunk the selection and is now exactly on the word | 128 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) { | 129 if (extentBaseOrderSwitched) { |
| 130 // Special case. | |
| 131 // If the extent-base order was switched, then the selection is now | |
| 132 // expanding in a different direction than before. Therefore we | |
| 133 // calculate the current word boundary in this new direction and | |
| 134 // based on the |base| position. | |
| 94 currentWordBoundary = nextWordBound( | 135 currentWordBoundary = nextWordBound( |
| 95 base, | 136 base, |
| 96 extentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirecti on::SearchBackwards, | 137 extentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirecti on::SearchBackwards, |
| 97 BoundAdjust::NextBoundIfOnBound); | 138 BoundAdjust::NextBoundIfOnBound); |
| 98 m_granularity = CharacterGranularity; | 139 m_granularity = CharacterGranularity; |
| 99 // When the base/extent order switches it doesn't count as shrinking sel ection. | 140 } else { |
| 100 thisMoveShrunkSelection = false; | 141 // Calculate the current word boundary based on |oldExtentWithGranularit y|. |
| 142 // If selection was shrunk in the last update and the extent is now | |
| 143 // exactly on the word boundary - we need to take the next bound as the | |
| 144 // bound of the current word. | |
| 145 currentWordBoundary = nextWordBound( | |
| 146 oldOffsetExtentPosition, | |
| 147 selection.isBaseFirst() ? SearchDirection::SearchForward : SearchDir ection::SearchBackwards, | |
| 148 m_state == StrategyState::Shrinking ? BoundAdjust::NextBoundIfOnBoun d : BoundAdjust::CurrentPosIfOnBound); | |
| 101 } | 149 } |
| 102 | 150 |
| 103 bool expandedBeyondWordBoundary; | 151 bool expandedBeyondWordBoundary = arePositionsInOrder(offsetExtentPosition, currentWordBoundary, extentBaseOrder); |
|
leviw_travelin_and_unemployed
2015/05/20 20:24:37
More comparePositions...
| |
| 104 if (extentBaseOrder > 0) | 152 |
| 105 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) > 0; | 153 // The selection is shrunk if the extent changes position to be closer to |
| 106 else | 154 // the base, and the extent/base order wasn't switched. |
| 107 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) < 0; | 155 bool thisMoveShrunkSelection = |
| 108 if (expandedBeyondWordBoundary) { | 156 !extentBaseOrderSwitched |
| 157 && oldOffsetExtentPosition != offsetExtentPosition | |
| 158 && arePositionsInOrder(oldOffsetExtentPosition, offsetExtentPosition, ex tentBaseOrder); | |
|
leviw_travelin_and_unemployed
2015/05/20 20:24:37
And *another* comparePositions. This needs to be c
| |
| 159 | |
| 160 if (expandedBeyondWordBoundary) | |
| 109 m_granularity = WordGranularity; | 161 m_granularity = WordGranularity; |
| 110 } else if (thisMoveShrunkSelection) { | 162 else if (thisMoveShrunkSelection) |
| 111 m_granularity = CharacterGranularity; | 163 m_granularity = CharacterGranularity; |
| 112 m_lastMoveShrunkSelection = true; | 164 |
| 165 VisiblePosition newSelectionExtent = offsetExtentPosition; | |
| 166 if (m_granularity == WordGranularity) { | |
| 167 // Determine the bounds of the word where the extent is located. | |
| 168 // Set the selection extent to one of the two bounds depending on | |
| 169 // whether the extent is passed the middle of the word. | |
| 170 VisiblePosition boundBeforeExtent = nextWordBound(offsetExtentPosition, SearchDirection::SearchBackwards, BoundAdjust::CurrentPosIfOnBound); | |
| 171 VisiblePosition boundAfterExtent = nextWordBound(offsetExtentPosition, S earchDirection::SearchForward, BoundAdjust::CurrentPosIfOnBound); | |
| 172 int xMiddleBetweenBounds = (boundAfterExtent.absoluteCaretBounds().x() + boundBeforeExtent.absoluteCaretBounds().x()) / 2; | |
| 173 bool extentBeforeMiddle = offsetExtentPoint.x() < xMiddleBetweenBounds; | |
| 174 | |
| 175 newSelectionExtent = extentBeforeMiddle ? boundBeforeExtent : boundAfter Extent; | |
| 176 // Update the offset if needed. | |
| 177 if (!inSameLine(newSelectionExtent, selection.visibleExtent())) | |
| 178 m_offset = 0; | |
| 179 else if ((extentBaseOrder > 0 && !extentBeforeMiddle) || (extentBaseOrde r < 0 && extentBeforeMiddle)) | |
| 180 m_offset = newSelectionExtent.absoluteCaretBounds().x() - extentPoin t.x(); | |
| 113 } | 181 } |
| 114 | 182 |
| 115 m_lastMoveShrunkSelection = thisMoveShrunkSelection; | 183 // Only update the state if the selection actually changed as a result of |
| 184 // this move. | |
| 185 if (newSelectionExtent != selection.visibleExtent()) | |
| 186 m_state = thisMoveShrunkSelection ? StrategyState::Shrinking : StrategyS tate::Expanding; | |
| 187 | |
| 116 VisibleSelection newSelection = selection; | 188 VisibleSelection newSelection = selection; |
| 117 newSelection.setExtent(extentPosition); | 189 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; | 190 return newSelection; |
| 126 } | 191 } |
| 127 | 192 |
| 128 } // namespace blink | 193 } // namespace blink |
| OLD | NEW |