OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 10 matching lines...) Expand all Loading... |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "core/editing/FrameCaret.h" | 26 #include "core/editing/FrameCaret.h" |
27 | 27 |
28 #include "core/dom/TaskRunnerHelper.h" | 28 #include "core/dom/TaskRunnerHelper.h" |
29 #include "core/editing/CaretDisplayItemClient.h" | 29 #include "core/editing/CaretDisplayItemClient.h" |
30 #include "core/editing/EditingUtilities.h" | 30 #include "core/editing/EditingUtilities.h" |
31 #include "core/editing/Editor.h" | |
32 #include "core/editing/SelectionEditor.h" | 31 #include "core/editing/SelectionEditor.h" |
33 #include "core/editing/commands/CompositeEditCommand.h" | |
34 #include "core/frame/FrameView.h" | 32 #include "core/frame/FrameView.h" |
35 #include "core/frame/LocalFrame.h" | 33 #include "core/frame/LocalFrame.h" |
36 #include "core/frame/Settings.h" | 34 #include "core/frame/Settings.h" |
37 #include "core/html/TextControlElement.h" | 35 #include "core/html/TextControlElement.h" |
38 #include "core/layout/LayoutBlock.h" | 36 #include "core/layout/LayoutBlock.h" |
39 #include "core/layout/LayoutTheme.h" | 37 #include "core/layout/LayoutTheme.h" |
40 #include "core/layout/api/LayoutPartItem.h" | 38 #include "core/layout/api/LayoutPartItem.h" |
41 #include "core/page/Page.h" | 39 #include "core/page/Page.h" |
42 #include "public/platform/WebTraceLocation.h" | 40 #include "public/platform/WebTraceLocation.h" |
43 | 41 |
44 namespace blink { | 42 namespace blink { |
45 | 43 |
46 FrameCaret::FrameCaret(LocalFrame& frame, | 44 FrameCaret::FrameCaret(LocalFrame& frame, |
47 const SelectionEditor& selectionEditor) | 45 const SelectionEditor& selectionEditor) |
48 : m_selectionEditor(&selectionEditor), | 46 : m_selectionEditor(&selectionEditor), |
49 m_frame(frame), | 47 m_frame(frame), |
50 m_displayItemClient(new CaretDisplayItemClient()), | 48 m_displayItemClient(new CaretDisplayItemClient()), |
51 m_caretVisibility(CaretVisibility::Hidden), | 49 m_caretVisibility(CaretVisibility::Hidden), |
52 m_caretBlinkTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), | 50 m_caretBlinkTimer(new TaskRunnerTimer<FrameCaret>( |
53 this, | 51 TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), |
54 &FrameCaret::caretBlinkTimerFired), | 52 this, |
| 53 &FrameCaret::caretBlinkTimerFired)), |
55 m_shouldPaintCaret(true), | 54 m_shouldPaintCaret(true), |
56 m_isCaretBlinkingSuspended(false), | 55 m_isCaretBlinkingSuspended(false), |
57 m_shouldShowBlockCursor(false) {} | 56 m_shouldShowBlockCursor(false) {} |
58 | 57 |
59 FrameCaret::~FrameCaret() = default; | 58 FrameCaret::~FrameCaret() = default; |
60 | 59 |
61 DEFINE_TRACE(FrameCaret) { | 60 DEFINE_TRACE(FrameCaret) { |
62 visitor->trace(m_selectionEditor); | 61 visitor->trace(m_selectionEditor); |
63 visitor->trace(m_frame); | 62 visitor->trace(m_frame); |
64 } | 63 } |
65 | 64 |
66 const DisplayItemClient& FrameCaret::displayItemClient() const { | 65 const DisplayItemClient& FrameCaret::displayItemClient() const { |
67 return *m_displayItemClient; | 66 return *m_displayItemClient; |
68 } | 67 } |
69 | 68 |
70 const PositionWithAffinity FrameCaret::caretPosition() const { | 69 const PositionWithAffinity FrameCaret::caretPosition() const { |
71 const VisibleSelection& selection = | 70 const VisibleSelection& selection = |
72 m_selectionEditor->computeVisibleSelectionInDOMTree(); | 71 m_selectionEditor->computeVisibleSelectionInDOMTree(); |
73 if (!selection.isCaret()) | 72 if (!selection.isCaret()) |
74 return PositionWithAffinity(); | 73 return PositionWithAffinity(); |
75 return PositionWithAffinity(selection.start(), selection.affinity()); | 74 return PositionWithAffinity(selection.start(), selection.affinity()); |
76 } | 75 } |
77 | 76 |
78 inline static bool shouldStopBlinkingDueToTypingCommand(LocalFrame* frame) { | |
79 return frame->editor().lastEditCommand() && | |
80 frame->editor().lastEditCommand()->shouldStopCaretBlinking(); | |
81 } | |
82 | |
83 void FrameCaret::updateAppearance() { | 77 void FrameCaret::updateAppearance() { |
84 DCHECK_GE(m_frame->document()->lifecycle().state(), | 78 DCHECK_GE(m_frame->document()->lifecycle().state(), |
85 DocumentLifecycle::LayoutClean); | 79 DocumentLifecycle::LayoutClean); |
86 // Paint a block cursor instead of a caret in overtype mode unless the caret | 80 // Paint a block cursor instead of a caret in overtype mode unless the caret |
87 // is at the end of a line (in this case the FrameSelection will paint a | 81 // is at the end of a line (in this case the FrameSelection will paint a |
88 // blinking caret as usual). | 82 // blinking caret as usual). |
89 const bool paintBlockCursor = | 83 const bool paintBlockCursor = |
90 m_shouldShowBlockCursor && isActive() && | 84 m_shouldShowBlockCursor && isActive() && |
91 !isLogicalEndOfLine(createVisiblePosition(caretPosition())); | 85 !isLogicalEndOfLine(createVisiblePosition(caretPosition())); |
92 | 86 |
93 bool shouldBlink = !paintBlockCursor && shouldBlinkCaret(); | 87 bool shouldBlink = !paintBlockCursor && shouldBlinkCaret(); |
94 | 88 if (!shouldBlink) { |
95 // If the caret moved, stop the blink timer so we can restart with a | |
96 // black caret in the new location. | |
97 if (!shouldBlink || shouldStopBlinkingDueToTypingCommand(m_frame)) | |
98 stopCaretBlinkTimer(); | 89 stopCaretBlinkTimer(); |
99 | 90 return; |
| 91 } |
100 // Start blinking with a black caret. Be sure not to restart if we're | 92 // Start blinking with a black caret. Be sure not to restart if we're |
101 // already blinking in the right location. | 93 // already blinking in the right location. |
102 if (shouldBlink) | 94 startBlinkCaret(); |
103 startBlinkCaret(); | |
104 } | 95 } |
105 | 96 |
106 void FrameCaret::stopCaretBlinkTimer() { | 97 void FrameCaret::stopCaretBlinkTimer() { |
107 if (m_caretBlinkTimer.isActive() || m_shouldPaintCaret) | 98 if (m_caretBlinkTimer->isActive() || m_shouldPaintCaret) |
108 scheduleVisualUpdateForPaintInvalidationIfNeeded(); | 99 scheduleVisualUpdateForPaintInvalidationIfNeeded(); |
109 m_shouldPaintCaret = false; | 100 m_shouldPaintCaret = false; |
110 m_caretBlinkTimer.stop(); | 101 m_caretBlinkTimer->stop(); |
111 } | 102 } |
112 | 103 |
113 void FrameCaret::startBlinkCaret() { | 104 void FrameCaret::startBlinkCaret() { |
114 // Start blinking with a black caret. Be sure not to restart if we're | 105 // Start blinking with a black caret. Be sure not to restart if we're |
115 // already blinking in the right location. | 106 // already blinking in the right location. |
116 if (m_caretBlinkTimer.isActive()) | 107 if (m_caretBlinkTimer->isActive()) |
117 return; | 108 return; |
118 | 109 |
119 if (double blinkInterval = LayoutTheme::theme().caretBlinkInterval()) | 110 if (double blinkInterval = LayoutTheme::theme().caretBlinkInterval()) |
120 m_caretBlinkTimer.startRepeating(blinkInterval, BLINK_FROM_HERE); | 111 m_caretBlinkTimer->startRepeating(blinkInterval, BLINK_FROM_HERE); |
121 | 112 |
122 m_shouldPaintCaret = true; | 113 m_shouldPaintCaret = true; |
123 scheduleVisualUpdateForPaintInvalidationIfNeeded(); | 114 scheduleVisualUpdateForPaintInvalidationIfNeeded(); |
124 } | 115 } |
125 | 116 |
126 void FrameCaret::setCaretVisibility(CaretVisibility visibility) { | 117 void FrameCaret::setCaretVisibility(CaretVisibility visibility) { |
127 if (m_caretVisibility == visibility) | 118 if (m_caretVisibility == visibility) |
128 return; | 119 return; |
129 | 120 |
130 m_caretVisibility = visibility; | 121 m_caretVisibility = visibility; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 return; | 222 return; |
232 m_shouldPaintCaret = !m_shouldPaintCaret; | 223 m_shouldPaintCaret = !m_shouldPaintCaret; |
233 scheduleVisualUpdateForPaintInvalidationIfNeeded(); | 224 scheduleVisualUpdateForPaintInvalidationIfNeeded(); |
234 } | 225 } |
235 | 226 |
236 void FrameCaret::scheduleVisualUpdateForPaintInvalidationIfNeeded() { | 227 void FrameCaret::scheduleVisualUpdateForPaintInvalidationIfNeeded() { |
237 if (FrameView* frameView = m_frame->view()) | 228 if (FrameView* frameView = m_frame->view()) |
238 frameView->scheduleVisualUpdateForPaintInvalidationIfNeeded(); | 229 frameView->scheduleVisualUpdateForPaintInvalidationIfNeeded(); |
239 } | 230 } |
240 | 231 |
| 232 void FrameCaret::recreateCaretBlinkTimerForTesting( |
| 233 RefPtr<WebTaskRunner> taskRunner) { |
| 234 m_caretBlinkTimer.reset(new TaskRunnerTimer<FrameCaret>( |
| 235 std::move(taskRunner), this, &FrameCaret::caretBlinkTimerFired)); |
| 236 } |
| 237 |
241 } // namespace blink | 238 } // namespace blink |
OLD | NEW |