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 #ifndef GranularityStrategy_h | 5 #ifndef GranularityStrategy_h |
| 6 #define GranularityStrategy_h | 6 #define GranularityStrategy_h |
| 7 | 7 |
| 8 #include "core/editing/SelectionStrategy.h" | 8 #include "core/editing/SelectionStrategy.h" |
| 9 #include "core/editing/VisibleSelection.h" | 9 #include "core/editing/VisibleSelection.h" |
| 10 | 10 |
| 11 namespace blink { | 11 namespace blink { |
| 12 | 12 |
| 13 class GranularityStrategy { | 13 class GranularityStrategy { |
| 14 public: | 14 public: |
| 15 virtual ~GranularityStrategy(); | 15 virtual ~GranularityStrategy(); |
| 16 virtual SelectionStrategy GetType() const = 0; | 16 virtual SelectionStrategy GetType() const = 0; |
| 17 virtual void Clear() = 0; | 17 virtual void Clear() = 0; |
| 18 | 18 |
| 19 // Calculates and returns the new selection based on the updated user | 19 // Calculates and returns the new selection based on the updated extent |
| 20 // selection extent |extentPosition| and the granularity strategy. | 20 // location in absolute coordinates. |
| 21 virtual VisibleSelection updateExtent(const VisiblePosition& extentPosition, const VisibleSelection&) = 0; | 21 virtual VisibleSelection updateExtent(const IntPoint&, LocalFrame*) = 0; |
| 22 | 22 |
| 23 protected: | 23 protected: |
| 24 GranularityStrategy(); | 24 GranularityStrategy(); |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 // Always uses character granularity. | 27 // Always uses character granularity. |
| 28 class CharacterGranularityStrategy final : public GranularityStrategy { | 28 class CharacterGranularityStrategy final : public GranularityStrategy { |
| 29 public: | 29 public: |
| 30 CharacterGranularityStrategy(); | 30 CharacterGranularityStrategy(); |
| 31 ~CharacterGranularityStrategy() final; | 31 ~CharacterGranularityStrategy() final; |
| 32 | 32 |
| 33 // GranularityStrategy: | 33 // GranularityStrategy: |
| 34 SelectionStrategy GetType() const final; | 34 SelectionStrategy GetType() const final; |
| 35 void Clear() final; | 35 void Clear() final; |
| 36 VisibleSelection updateExtent(const VisiblePosition& extentPosition, const V isibleSelection&) final; | 36 VisibleSelection updateExtent(const IntPoint&, LocalFrame*) final; |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 // "Expand by word, shrink by character" selection strategy. | 39 // "Expand by word, shrink by character" selection strategy. |
| 40 // Uses character granularity when selection is shrinking. If the selection is | 40 // Uses character granularity when selection is shrinking. If the selection is |
| 41 // expanding, granularity doesn't change until a word boundary is passed, after | 41 // expanding, granularity doesn't change until a word boundary is passed, after |
| 42 // which the granularity switches to "word". | 42 // which the granularity switches to "word". |
| 43 // In word granularity, the word is not selected until the extent point passes | |
| 44 // the middle of the word. | |
| 45 // | |
| 46 // The "offset" feature: | |
| 47 // The offset is the horizontal distance between the extent point (passed in | |
| 48 // updateExtent) and the end of the selection. In character granularity the | |
| 49 // offset is typically zero or near zero, however in word granularity it can be | |
| 50 // significant. When the offset is positive and the extent point moved to the | |
| 51 // left, the offset is preserved, i.e. the selection tracks the extent point | |
| 52 // with the constant offset. When the offset is positive and the extent point | |
| 53 // is moved to the right, the offset gets reduced. Selection will not grow | |
| 54 // until the offset is reduced all the way to zero. | |
| 55 // | |
| 56 // This behavior is best illustrated by an example: | |
| 57 // | |
| 58 // ^ marks base, | marks extent point, > marks selection end: | |
| 59 // Lorem ip^sum|> dolor sit amet, consectetur | |
| 60 // | |
| 61 // Move extent over the middle of "dolor". Granularity should change to word | |
| 62 // granularity and the selection end should jump to the end of the word. | |
| 63 // Lorem ip^sum dolo|r> sit amet, consectetur | |
| 64 // | |
| 65 // Move extent back one character. Granularity changes to "character". The | |
| 66 // selection end should move back one character as well. Note an offset between | |
| 67 // the extent and the selection end. | |
| 68 // Lorem ip^sum dol|o>r sit amet, consectetur | |
| 69 // | |
| 70 // Move extent forward one character. The offset is reduced to 0. Selection end | |
| 71 // doesn't change. | |
| 72 // Lorem ip^sum dolo|>r sit amet, consectetur | |
| 73 // | |
| 74 // Move forward one character. End moves with extent in character granularity. | |
| 75 // Lorem ip^sum dolor|> sit amet, consectetur | |
| 43 class DirectionGranularityStrategy final : public GranularityStrategy { | 76 class DirectionGranularityStrategy final : public GranularityStrategy { |
| 44 public: | 77 public: |
| 45 DirectionGranularityStrategy(); | 78 DirectionGranularityStrategy(); |
| 46 ~DirectionGranularityStrategy() final; | 79 ~DirectionGranularityStrategy() final; |
| 47 | 80 |
| 48 // GranularityStrategy: | 81 // GranularityStrategy: |
| 49 SelectionStrategy GetType() const final; | 82 SelectionStrategy GetType() const final; |
| 50 void Clear() final; | 83 void Clear() final; |
| 51 VisibleSelection updateExtent(const VisiblePosition&, const VisibleSelection &) final; | 84 VisibleSelection updateExtent(const IntPoint&, LocalFrame*) final; |
| 52 | 85 |
| 53 private: | 86 private: |
| 54 enum class BoundAdjust {CurrentPosIfOnBound, NextBoundIfOnBound}; | 87 enum class StrategyState { |
| 55 enum class SearchDirection {SearchBackwards, SearchForward}; | 88 // Starting state. |
| 89 // Selection was cleared and there were no extent updates since then. | |
| 90 // One an update is performed, the strategy goes into the Expanding | |
| 91 // state unless the update shrinks the selection without changing | |
| 92 // relative base/extent order, in which case the strategy goes into the | |
| 93 // Shrinking state. | |
| 94 Cleared, | |
| 95 // Last time the selection was changed by updateExtent - it was expanded | |
| 96 // or the relative base/extent order was changed. | |
| 97 Expanding, | |
| 98 // Last time the selection was changed by updateExtent - it was shrunk | |
| 99 // (without changing relative base/extent order). | |
| 100 Shrinking | |
| 101 }; | |
| 56 | 102 |
| 57 // Returns the next word boundary starting from |pos|. |direction| specifies | 103 StrategyState m_state; |
| 58 // the direction in which to search for the next bound. nextIfOnBound | |
| 59 // controls whether |pos| or the next boundary is returned when |pos| is | |
| 60 // located exactly on word boundary. | |
| 61 VisiblePosition nextWordBound(const VisiblePosition& /*pos*/, SearchDirectio n /*direction*/, BoundAdjust /*nextIfOnBound*/); | |
| 62 | 104 |
| 63 // Current selection granularity being used | 105 // Current selection granularity being used. |
| 64 TextGranularity m_granularity; | 106 TextGranularity m_granularity; |
| 65 // Set to true if the selection was shrunk (without changing relative | 107 |
| 66 // base/extent order) as a result of the most recent updateExtent call. | 108 // Horizontal offset in pixels in absolute coordinates applied to the extent point. |
| 67 bool m_lastMoveShrunkSelection; | 109 int m_offset; |
| 110 | |
| 111 // This defines location of the offset-adjusted extent point (from the | |
| 112 // latest updateExtent call) relative to the location of extent's | |
| 113 // VisiblePosition. It is used to detect sub-position extent movement. | |
| 114 IntSize m_diffExtentPointFromExtentPosition; | |
|
leviw_travelin_and_unemployed
2015/06/08 18:09:57
I don't love this name, but it's better.
mfomitchev
2015/06/08 18:35:58
Yeah, I don't love it either, but I can't come up
| |
| 68 }; | 115 }; |
| 69 | 116 |
| 70 } // namespace blink | 117 } // namespace blink |
| 71 | 118 |
| 72 #endif // GranularityStrategy_h | 119 #endif // GranularityStrategy_h |
| OLD | NEW |