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

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

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 #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 contents-space 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 passes the
44 // middle of the word. If the selection is shrunk when there is a distance
45 // between the extent and the selection end on extent's side of the base, then
46 // this distance is applied to the extent as an offset for the purpose of
47 // determining the selection bound. This prevents the selection edge from
48 // "jumping" back to extent position when it is shrunk after extending. This
49 // offset can be reduced or set to 0 if the extent is moved in the opposite
50 // direction.
51 //
52 // Example:
53 // ^ marks base, | marks extent passed in updateExtent, > marks selection end:
54 // Lorem ip^sum|> dolor sit amet, consectetur
55 //
56 // Move extent over the middle of "dolor", granularity should change to word
57 // granularity and the selection end should jump to the end of the word.
58 // Lorem ip^sum dolo|r> sit amet, consectetur
59 //
60 // Move extent back one character. Granularity changes to "character". The
61 // selection end should move back one character as well. Note an offset between
62 // the extent and the selection end.
63 // Lorem ip^sum dol|o>r sit amet, consectetur
64 //
65 // Move extent forward one character. The offset is reduced to 0. Selection end
66 // doesn't change.
67 // Lorem ip^sum dolo|>r sit amet, consectetur
68 //
69 // Move forward one character. End moves with extent in character granularity.
70 // Lorem ip^sum dolor|> sit amet, consectetur
43 class DirectionGranularityStrategy final : public GranularityStrategy { 71 class DirectionGranularityStrategy final : public GranularityStrategy {
44 public: 72 public:
45 DirectionGranularityStrategy(); 73 DirectionGranularityStrategy();
46 ~DirectionGranularityStrategy() final; 74 ~DirectionGranularityStrategy() final;
47 75
48 // GranularityStrategy: 76 // GranularityStrategy:
49 SelectionStrategy GetType() const final; 77 SelectionStrategy GetType() const final;
50 void Clear() final; 78 void Clear() final;
51 VisibleSelection updateExtent(const VisiblePosition&, const VisibleSelection &) final; 79 VisibleSelection updateExtent(const IntPoint&, LocalFrame*) final;
52 80
53 private: 81 private:
82 enum class StrategyState {
83 // Starting state.
84 // Selection was cleared and there were no extent updates since then.
85 // One an update is performed, the strategy goes into the Expanding
86 // state unless the upate shrinks the selection without changing
87 // relative base/extent order, in which case the strategy goes into the
88 // Shrinking state.
89 Cleared,
90 // Last time the selection was changed by updateExtent - it was expanded
91 // or the relative base/extent order was changed.
92 Expanding,
93 // Last time the selection was changed by updateExtent - it was shrunk
94 // (without changing relative base/extent order).
95 Shrinking
96 };
54 enum class BoundAdjust {CurrentPosIfOnBound, NextBoundIfOnBound}; 97 enum class BoundAdjust {CurrentPosIfOnBound, NextBoundIfOnBound};
55 enum class SearchDirection {SearchBackwards, SearchForward}; 98 enum class SearchDirection {SearchBackwards, SearchForward};
56 99
100 // We use the bottom-left corner of the caret rect to represent the
101 // location of a VisiblePosition. This way locations corresponding to
102 // VisiblePositions on the same line will all have the same y coordinate
103 // unless the text is transformed.
104 static IntPoint positionLocation(const VisiblePosition& vp) { return vp.abso luteCaretBounds().minXMaxYCorner(); }
leviw_travelin_and_unemployed 2015/06/03 22:29:48 Why not put this in the cpp file?
mfomitchev 2015/06/05 17:38:39 Ok. I've put this and a couple of other methods in
105
106 // Order is specified using the same contract as comparePositions.
107 bool arePositionsInSpecifiedOrder(const VisiblePosition& vp1, const VisibleP osition& vp2, int specifiedOrder);
leviw_travelin_and_unemployed 2015/06/03 22:29:48 Nit: you don't need the 'vp1' and 'vp2' names here
mfomitchev 2015/06/05 17:38:39 Done.
108
57 // Returns the next word boundary starting from |pos|. |direction| specifies 109 // Returns the next word boundary starting from |pos|. |direction| specifies
58 // the direction in which to search for the next bound. nextIfOnBound 110 // the direction in which to search for the next bound. nextIfOnBound
59 // controls whether |pos| or the next boundary is returned when |pos| is 111 // controls whether |pos| or the next boundary is returned when |pos| is
60 // located exactly on word boundary. 112 // located exactly on word boundary.
61 VisiblePosition nextWordBound(const VisiblePosition& /*pos*/, SearchDirectio n /*direction*/, BoundAdjust /*nextIfOnBound*/); 113 VisiblePosition nextWordBound(const VisiblePosition& /*pos*/, SearchDirectio n /*direction*/, BoundAdjust /*nextIfOnBound*/);
62 114
63 // Current selection granularity being used 115 StrategyState m_state;
116
117 // Current selection granularity being used.
64 TextGranularity m_granularity; 118 TextGranularity m_granularity;
65 // Set to true if the selection was shrunk (without changing relative 119
66 // base/extent order) as a result of the most recent updateExtent call. 120 // Horizontal offset in pixels in content space applied to the extent point.
leviw_travelin_and_unemployed 2015/06/03 22:29:48 You use "content space" repeatedly in this CL, but
mfomitchev 2015/06/05 17:38:39 Done.. I was trying to use the terminology establi
67 bool m_lastMoveShrunkSelection; 121 int m_offset;
122
123 // Vector defining location of the extent point relative to the location of
124 // extent's VisiblePosition.
125 // The start of the vector is the bottom-left of the caret rect of the
leviw_travelin_and_unemployed 2015/06/03 22:29:48 A couple things: - Again I don't think you should
mfomitchev 2015/06/03 22:36:55 If I call it Euclidean vector, or geometric vector
mfomitchev 2015/06/05 17:38:39 Done.
126 // VisiblePosition corresponding to the selection extent. The end of
127 // the vector is the extent point (from the last updateExtent call) shifted
128 // horizontally by |m_offset| pixels.
129 IntSize m_subPositionCorrection;
68 }; 130 };
69 131
70 } // namespace blink 132 } // namespace blink
71 133
72 #endif // GranularityStrategy_h 134 #endif // GranularityStrategy_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698