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

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

Issue 988023005: Implementing directional selection strategy in Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Addressing feedback. Created 5 years, 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/editing/GranularityStrategy.h"
7
8 #include "core/editing/htmlediting.h"
9
10 namespace blink {
11
12 /////////////////////////////////////////////////////////////////////
yosin_UTC9 2015/04/23 03:38:47 nit: We don't need have HR like banner comments.
mfomitchev 2015/04/23 15:08:11 Done.
13 // GranularityStrategy implementation
14
15 GranularityStrategy::GranularityStrategy() { }
16
17 GranularityStrategy::~GranularityStrategy() { }
18
19 /////////////////////////////////////////////////////////////////////
20 // CharacterGranularityStrategy implementation
21
22 CharacterGranularityStrategy::CharacterGranularityStrategy() { }
23
24 CharacterGranularityStrategy::~CharacterGranularityStrategy() { }
25
26 SelectionStrategy CharacterGranularityStrategy::GetType() const
27 {
28 return SelectionStrategy::Character;
29 }
30
31 void CharacterGranularityStrategy::Clear() { };
32
33 VisibleSelection CharacterGranularityStrategy::updateExtent(const VisiblePositio n& extentPosition, const VisibleSelection& selection)
34 {
35 return VisibleSelection(selection.visibleBase(), extentPosition);
36 }
37
38 /////////////////////////////////////////////////////////////////////
39 // DirectionGranularityStrategy implementation
40
41 DirectionGranularityStrategy::DirectionGranularityStrategy()
42 : m_granularity(CharacterGranularity)
43 , m_lastMoveShrunkSelection(false) { }
44
45 DirectionGranularityStrategy::~DirectionGranularityStrategy() { }
46
47 SelectionStrategy DirectionGranularityStrategy::GetType() const
48 {
49 return SelectionStrategy::Direction;
50 }
51
52 void DirectionGranularityStrategy::Clear()
53 {
54 m_granularity = CharacterGranularity;
55 m_lastMoveShrunkSelection = false;
56 }
57
58 VisiblePosition DirectionGranularityStrategy::nextWordBound(
59 const VisiblePosition& pos,
60 SearchDirection direction,
61 BoundAdjust wordBoundAdjust)
62 {
63 bool nextBoundIfOnBound = wordBoundAdjust == BoundAdjust::NextBoundIfOnBoun d;
64 if (direction == SearchDirection::SearchForward) {
65 EWordSide wordSide = nextBoundIfOnBound ? RightWordIfOnBoundary : LeftWo rdIfOnBoundary;
66 return endOfWord(pos, wordSide);
67 }
68 EWordSide wordSide = nextBoundIfOnBound ? LeftWordIfOnBoundary : RightWordIf OnBoundary;
69 return startOfWord(pos, wordSide);
70 }
71
72 VisibleSelection DirectionGranularityStrategy::updateExtent(const VisiblePositio n& extentPosition, const VisibleSelection& selection)
73 {
74 if (extentPosition == selection.visibleExtent())
75 return selection;
76
77 const VisiblePosition base = selection.visibleBase();
78 const VisiblePosition oldExtentWithGranularity = selection.isBaseFirst() ? s election.visibleEnd() : selection.visibleStart();
79
80 int extentBaseOrder = comparePositions(extentPosition, base);
81 int oldExtentBaseOrder = comparePositions(oldExtentWithGranularity, base);
82
83 bool extentBaseOrderSwitched = (extentBaseOrder > 0 && oldExtentBaseOrder < 0)
84 || (extentBaseOrder < 0 && oldExtentBaseOrder > 0);
85
86 // Determine the boundary of the 'current word', i.e. the boundary extending beyond which
yosin_UTC9 2015/04/23 03:38:47 OPTIONAL: It is nice to format comment less than 8
mfomitchev 2015/04/23 15:08:11 Done.
87 // should change the granularity to WordGranularity.
88 // If the last move has shrunk the selection and is now exactly on the word boundary -
89 // we need to take the next bound as the bound of the "current word".
90 VisiblePosition currentWordBoundary = nextWordBound(
91 oldExtentWithGranularity,
92 oldExtentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirectio n::SearchBackwards,
93 m_lastMoveShrunkSelection ? BoundAdjust::NextBoundIfOnBound : BoundAdjus t::CurrentPosIfOnBound);
94
95 bool thisMoveShrunkSelection = (extentBaseOrder > 0 && comparePositions(exte ntPosition, selection.visibleExtent()) < 0)
96 || (extentBaseOrder < 0 && comparePositions(extentPosition, selection.vi sibleExtent()) > 0);
97 // If the extent-base order was switched, then the selection is now expandin g in a different
98 // direction than before. Therefore we need to calculate the boundary of the 'current word'
99 // in this new direction in order to be able to tell if the selection expand ed beyond it.
100 if (extentBaseOrderSwitched) {
101 currentWordBoundary = nextWordBound(
102 base,
103 extentBaseOrder > 0 ? SearchDirection::SearchForward : SearchDirecti on::SearchBackwards,
104 BoundAdjust::NextBoundIfOnBound);
105 m_granularity = CharacterGranularity;
106 // When the base/extent order switches it doesn't count as shrinking sel ection.
107 thisMoveShrunkSelection = false;
108 }
109
110 bool expandedBeyondWordBoundary;
111 if (extentBaseOrder > 0)
112 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) > 0;
113 else
114 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) < 0;
115 if (expandedBeyondWordBoundary) {
116 m_granularity = WordGranularity;
117 } else if (thisMoveShrunkSelection) {
118 m_granularity = CharacterGranularity;
119 m_lastMoveShrunkSelection = true;
120 }
121
122 m_lastMoveShrunkSelection = thisMoveShrunkSelection;
123 VisibleSelection newSelection = selection;
124 newSelection.setExtent(extentPosition);
125 if (m_granularity == WordGranularity) {
126 if (extentBaseOrder > 0)
127 newSelection.setEndRespectingGranularity(m_granularity, LeftWordIfOn Boundary);
128 else
129 newSelection.setStartRespectingGranularity(m_granularity, RightWordI fOnBoundary);
130 }
131
132 return newSelection;
133 }
134
135 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698