1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/core/editing/frame_caret.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/core/editing/caret_display_item_client.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/local_caret_rect.h"
#include "third_party/blink/renderer/core/editing/position_with_affinity.h"
#include "third_party/blink/renderer/core/editing/selection_editor.h"
#include "third_party/blink/renderer/core/editing/visible_position.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/html/forms/text_control_element.h"
#include "third_party/blink/renderer/core/layout/layout_block.h"
#include "third_party/blink/renderer/core/layout/layout_embedded_content.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/style/computed_style_base_constants.h"
#include "third_party/blink/renderer/platform/graphics/compositing/paint_artifact_compositor.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context.h"
#include "third_party/blink/renderer/platform/graphics/paint/scoped_paint_chunk_properties.h"
#include "third_party/blink/renderer/platform/widget/frame_widget.h"
#include "ui/gfx/selection_bound.h"
namespace blink {
namespace {
} // anonymous namespace
FrameCaret::FrameCaret(LocalFrame& frame,
const SelectionEditor& selection_editor)
: selection_editor_(&selection_editor),
frame_(frame),
display_item_client_(MakeGarbageCollected<CaretDisplayItemClient>()),
caret_blink_timer_(frame.GetTaskRunner(TaskType::kInternalDefault),
this,
&FrameCaret::CaretBlinkTimerFired),
effect_(EffectPaintPropertyNode::Create(
EffectPaintPropertyNode::Root(),
CaretEffectNodeState(/*visible*/ true,
TransformPaintPropertyNode::Root()))) {
#if DCHECK_IS_ON()
effect_->SetDebugName("Caret");
#endif
}
FrameCaret::~FrameCaret() = default;
void FrameCaret::Trace(Visitor* visitor) const {
visitor->Trace(selection_editor_);
visitor->Trace(frame_);
visitor->Trace(display_item_client_);
visitor->Trace(caret_blink_timer_);
visitor->Trace(effect_);
}
EffectPaintPropertyNode::State FrameCaret::CaretEffectNodeState(
bool visible,
const TransformPaintPropertyNodeOrAlias& local_transform_space) const {
EffectPaintPropertyNode::State state;
// Use 0.001f instead of 0 to ensure cc will add quad for the caret layer.
// This is especially useful on Mac to limit the damage during caret blinking
// within the CALayer for the caret.
state.opacity = visible ? 1.f : 0.001f;
state.local_transform_space = &local_transform_space;
DEFINE_STATIC_LOCAL(
CompositorElementId, element_id,
(CompositorElementIdFromUniqueObjectId(
NewUniqueObjectId(), CompositorElementIdNamespace::kPrimaryEffect)));
state.compositor_element_id = element_id;
state.direct_compositing_reasons = CompositingReason::kActiveOpacityAnimation;
return state;
}
const PositionWithAffinity FrameCaret::CaretPosition() const {
const VisibleSelection& selection =
selection_editor_->ComputeVisibleSelectionInDOMTree();
if (!selection.IsCaret())
return PositionWithAffinity();
DCHECK(selection.Start().IsValidFor(*frame_->GetDocument()));
return PositionWithAffinity(selection.Start(), selection.Affinity());
}
bool FrameCaret::IsActive() const {
return CaretPosition().IsNotNull();
}
PositionWithAffinity FrameCaret::UpdateAppearance() {
DCHECK_GE(frame_->GetDocument()->Lifecycle().GetState(),
DocumentLifecycle::kLayoutClean);
bool new_should_show_caret = ShouldShowCaret();
if (new_should_show_caret != IsCaretShown()) {
SetCaretShown(new_should_show_caret);
ScheduleVisualUpdateForPaintInvalidationIfNeeded();
}
if (!IsCaretShown()) {
StopCaretBlinkTimer();
return PositionWithAffinity();
}
PositionWithAffinity caret_position = CaretPosition();
SetBlinkingDisabled(false);
if (RuntimeEnabledFeatures::CSSCaretAnimationEnabled() &&
caret_position.AnchorNode() &&
GetComputedStyleForElementOrLayoutObject(*caret_position.AnchorNode())
->CaretAnimation() == ECaretAnimation::kManual) {
SetBlinkingDisabled(true);
}
// Start blinking with a black caret. Be sure not to restart if we're
// already blinking in the right location.
StartBlinkCaret();
return caret_position;
}
void FrameCaret::StopCaretBlinkTimer() {
if (caret_blink_timer_.IsActive() || IsVisibleIfActive())
ScheduleVisualUpdateForPaintInvalidationIfNeeded();
caret_blink_timer_.Stop();
display_item_client_->SetActive(false);
SetVisibleIfActive(false);
}
void FrameCaret::StartBlinkCaret() {
// Start blinking with a black caret. Be sure not to restart if we're
// already blinking in the right location at the right rate.
base::TimeDelta blink_interval =
IsBlinkingDisabled() ? base::TimeDelta()
: LayoutTheme::GetTheme().CaretBlinkInterval();
if (caret_blink_timer_.IsActive()) {
if (blink_interval == caret_blink_timer_.RepeatInterval()) {
// Already blinking at the right rate.
return;
}
// If it was active but we are changing the blink rate, reset state.
StopCaretBlinkTimer();
}
if (!blink_interval.is_zero()) {
caret_blink_timer_.StartRepeating(blink_interval, FROM_HERE);
}
display_item_client_->SetActive(true);
SetVisibleIfActive(true);
ScheduleVisualUpdateForPaintInvalidationIfNeeded();
}
void FrameCaret::SetCaretEnabled(bool enabled) {
if (IsCaretEnabled() == enabled) {
return;
}
caret_status_bits_.set<CaretEnabledFlag>(enabled);
if (!IsCaretEnabled()) {
StopCaretBlinkTimer();
}
ScheduleVisualUpdateForPaintInvalidationIfNeeded();
}
void FrameCaret::LayoutBlockWillBeDestroyed(const LayoutBlock& block) {
display_item_client_->LayoutBlockWillBeDestroyed(block);
}
void FrameCaret::UpdateStyleAndLayoutIfNeeded() {
DCHECK_GE(frame_->GetDocument()->Lifecycle().GetState(),
DocumentLifecycle::kLayoutClean);
PositionWithAffinity caret_position = UpdateAppearance();
display_item_client_->UpdateStyleAndLayoutIfNeeded(caret_position);
}
void FrameCaret::InvalidatePaint(const LayoutBlock& block,
const PaintInvalidatorContext& context) {
display_item_client_->InvalidatePaint(block, context);
}
gfx::Rect FrameCaret::AbsoluteCaretBounds() const {
DCHECK_NE(frame_->GetDocument()->Lifecycle().GetState(),
DocumentLifecycle::kInPrePaint);
DCHECK(!frame_->GetDocument()->NeedsLayoutTreeUpdate());
DocumentLifecycle::DisallowTransitionScope disallow_transition(
frame_->GetDocument()->Lifecycle());
return AbsoluteCaretBoundsOf(CaretPosition());
}
void FrameCaret::EnsureInvalidationOfPreviousLayoutBlock() {
display_item_client_->EnsureInvalidationOfPreviousLayoutBlock();
}
bool FrameCaret::ShouldPaintCaret(const LayoutBlock& block) const {
return display_item_client_->ShouldPaintCaret(block);
}
bool FrameCaret::ShouldPaintCaret(
const PhysicalBoxFragment& box_fragment) const {
return display_item_client_->ShouldPaintCaret(box_fragment);
}
void FrameCaret::SetVisibleIfActive(bool visible) {
if (visible == IsVisibleIfActive())
return;
DCHECK(frame_);
DCHECK(effect_);
if (!frame_->View())
return;
auto change_type = effect_->Update(
*effect_->Parent(),
CaretEffectNodeState(visible, effect_->LocalTransformSpace()));
DCHECK_EQ(PaintPropertyChangeType::kChangedOnlySimpleValues, change_type);
if (auto* compositor = frame_->View()->GetPaintArtifactCompositor()) {
if (compositor->DirectlyUpdateCompositedOpacityValue(*effect_)) {
effect_->CompositorSimpleValuesUpdated();
return;
}
}
// Fallback to full update if direct update is not available.
frame_->View()->SetPaintArtifactCompositorNeedsUpdate();
}
void FrameCaret::PaintCaret(GraphicsContext& context,
const PhysicalOffset& paint_offset) const {
if (effect_->Update(
context.GetPaintController().CurrentPaintChunkProperties().Effect(),
CaretEffectNodeState(IsVisibleIfActive(),
context.GetPaintController()
.CurrentPaintChunkProperties()
.Transform())) !=
PaintPropertyChangeType::kUnchanged) {
// Needs full PaintArtifactCompositor update if the parent or the local
// transform space changed.
frame_->View()->SetPaintArtifactCompositorNeedsUpdate();
}
ScopedPaintChunkProperties scoped_properties(context.GetPaintController(),
*effect_, *display_item_client_,
DisplayItem::kCaret);
display_item_client_->PaintCaret(context, paint_offset, DisplayItem::kCaret);
if (!frame_->Selection().IsHidden()) {
auto type = frame_->Selection().IsHandleVisible()
? gfx::SelectionBound::Type::CENTER
: gfx::SelectionBound::Type::HIDDEN;
display_item_client_->RecordSelection(context, paint_offset, type);
}
}
bool FrameCaret::ShouldShowCaret() const {
// Don't show the caret if it isn't visible or positioned.
if (!IsCaretEnabled() || !IsActive()) {
return false;
}
Element* root = RootEditableElementOf(CaretPosition().GetPosition());
if (root) {
// Caret is contained in editable content. If there is no focused element,
// don't show the caret.
Element* focused_element = root->GetDocument().FocusedElement();
if (!focused_element)
return false;
} else {
// Caret is not contained in editable content--see if caret browsing is
// enabled. If it isn't, don't show the caret.
if (!frame_->IsCaretBrowsingEnabled())
return false;
}
if (!IsEditablePosition(
selection_editor_->ComputeVisibleSelectionInDOMTree().Start()) &&
!frame_->IsCaretBrowsingEnabled())
return false;
// Only show the caret if the selection has focus.
return frame_->Selection().SelectionHasFocus();
}
void FrameCaret::CaretBlinkTimerFired(TimerBase*) {
DCHECK(IsCaretEnabled());
if (IsCaretBlinkingSuspended() && IsVisibleIfActive())
return;
SetVisibleIfActive(!IsVisibleIfActive());
ScheduleVisualUpdateForPaintInvalidationIfNeeded();
}
void FrameCaret::ScheduleVisualUpdateForPaintInvalidationIfNeeded() {
if (LocalFrameView* frame_view = frame_->View())
frame_view->ScheduleVisualUpdateForPaintInvalidationIfNeeded();
}
void FrameCaret::RecreateCaretBlinkTimerForTesting(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const base::TickClock* tick_clock) {
caret_blink_timer_.SetTaskRunnerForTesting(std::move(task_runner),
tick_clock);
}
} // namespace blink
|