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

Side by Side Diff: third_party/WebKit/Source/core/page/DragController.cpp

Issue 2288913002: [InputEvent] Support |deleteByDrag| and |insertFromDrop| (Closed)
Patch Set: Created 4 years, 3 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 /* 1 /*
2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Google Inc. 3 * Copyright (C) 2008 Google Inc.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 #include "wtf/CurrentTime.h" 85 #include "wtf/CurrentTime.h"
86 #include "wtf/RefPtr.h" 86 #include "wtf/RefPtr.h"
87 #include <memory> 87 #include <memory>
88 88
89 #if OS(WIN) 89 #if OS(WIN)
90 #include <windows.h> 90 #include <windows.h>
91 #endif 91 #endif
92 92
93 namespace blink { 93 namespace blink {
94 94
95 namespace {
96
97 DataTransfer* createDraggingDataTransfer(DataTransferAccessPolicy policy, DragDa ta* dragData)
98 {
99 return DataTransfer::create(DataTransfer::DragAndDrop, policy, dragData->pla tformData());
100 }
101
102 enum class DragDropBeforeInputResult {
103 None,
104 DeleteOnly,
105 InsertOnly,
106 DeleteAndInsert,
107 };
108
109 DragDropBeforeInputResult dispatchDragDropBeforeInput(Element* dragTarget, Eleme nt* dropTarget, DragData* dragData, bool dragIsMove)
110 {
111 LocalFrame* dragFrame = dragTarget->document().frame();
112 LocalFrame* dropFrame = dropTarget->document().frame();
113
114 // Handle DeleteByDrag.
115 bool shouldDelete = dragIsMove;
116 if (shouldDelete) {
117 DispatchEventResult dragBeforeInputResult = dispatchBeforeInputEditorCom mand(dragTarget, InputEvent::InputType::DeleteByDrag, emptyString(), nullptr);
118 shouldDelete = (dragBeforeInputResult == DispatchEventResult::NotCancele d);
yosin_UTC9 2016/08/31 01:20:34 nit: We don't need to have parenthesis. Edit
chongz 2016/09/02 18:02:10 Done.
119 }
120
121 // 'beforeinput' event handler may destroy frame.
122 if (dragFrame != dragTarget->document().frame() || dropFrame != dropTarget-> document().frame())
yosin_UTC9 2016/08/31 01:20:34 What is happened when |dropTarget| is removed?
chongz 2016/09/02 18:02:10 Added check for removed element. So if |dropTarget
123 return DragDropBeforeInputResult::None;
124
125 // Handle InsertFromDrop.
126 DataTransfer* dataTransfer = createDraggingDataTransfer(DataTransferReadable , dragData);
127 dataTransfer->setSourceOperation(dragData->draggingSourceOperationMask());
128 DispatchEventResult dropBeforeInputResult = dispatchBeforeInputDataTransfer( dropTarget, InputEvent::InputType::InsertFromDrop, dataTransfer, nullptr);
129 bool shouldInsert = (dropBeforeInputResult == DispatchEventResult::NotCancel ed);
yosin_UTC9 2016/08/31 01:20:34 nit: We don't need to have parenthesis.
chongz 2016/09/02 18:02:10 Done.
130
131 // 'beforeinput' event handler may destroy frame.
132 if (dragFrame != dragTarget->document().frame() || dropFrame != dropTarget-> document().frame())
133 return DragDropBeforeInputResult::None;
134
135 // Generate dispatch result.
136 if (shouldDelete && shouldInsert)
137 return DragDropBeforeInputResult::DeleteAndInsert;
138 if (shouldDelete)
139 return DragDropBeforeInputResult::DeleteOnly;
140 if (shouldInsert)
141 return DragDropBeforeInputResult::InsertOnly;
142 return DragDropBeforeInputResult::None;
143 }
144
145 } // anonymous namespace
146
95 const int DragController::DragIconRightInset = 7; 147 const int DragController::DragIconRightInset = 7;
96 const int DragController::DragIconBottomInset = 3; 148 const int DragController::DragIconBottomInset = 3;
97 149
98 static const int MaxOriginalImageArea = 1500 * 1500; 150 static const int MaxOriginalImageArea = 1500 * 1500;
99 static const int LinkDragBorderInset = 2; 151 static const int LinkDragBorderInset = 2;
100 static const float DragImageAlpha = 0.75f; 152 static const float DragImageAlpha = 0.75f;
101 153
102 #if ENABLE(ASSERT) 154 #if ENABLE(ASSERT)
103 static bool dragTypeIsValid(DragSourceAction action) 155 static bool dragTypeIsValid(DragSourceAction action)
104 { 156 {
(...skipping 12 matching lines...) Expand all
117 #endif 169 #endif
118 170
119 static PlatformMouseEvent createMouseEvent(DragData* dragData) 171 static PlatformMouseEvent createMouseEvent(DragData* dragData)
120 { 172 {
121 return PlatformMouseEvent(dragData->clientPosition(), dragData->globalPositi on(), 173 return PlatformMouseEvent(dragData->clientPosition(), dragData->globalPositi on(),
122 WebPointerProperties::Button::Left, PlatformEvent::MouseMoved, 0, 174 WebPointerProperties::Button::Left, PlatformEvent::MouseMoved, 0,
123 static_cast<PlatformEvent::Modifiers>(dragData->modifiers()), 175 static_cast<PlatformEvent::Modifiers>(dragData->modifiers()),
124 PlatformMouseEvent::RealOrIndistinguishable, monotonicallyIncreasingTime ()); 176 PlatformMouseEvent::RealOrIndistinguishable, monotonicallyIncreasingTime ());
125 } 177 }
126 178
127 static DataTransfer* createDraggingDataTransfer(DataTransferAccessPolicy policy, DragData* dragData)
128 {
129 return DataTransfer::create(DataTransfer::DragAndDrop, policy, dragData->pla tformData());
130 }
131
132 DragController::DragController(Page* page) 179 DragController::DragController(Page* page)
133 : m_page(page) 180 : m_page(page)
134 , m_documentUnderMouse(nullptr) 181 , m_documentUnderMouse(nullptr)
135 , m_dragInitiator(nullptr) 182 , m_dragInitiator(nullptr)
136 , m_fileInputElementUnderMouse(nullptr) 183 , m_fileInputElementUnderMouse(nullptr)
137 , m_documentIsHandlingDrag(false) 184 , m_documentIsHandlingDrag(false)
138 , m_dragDestinationAction(DragDestinationActionNone) 185 , m_dragDestinationAction(DragDestinationActionNone)
139 , m_didInitiateDrag(false) 186 , m_didInitiateDrag(false)
140 { 187 {
141 } 188 }
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 return false; 543 return false;
497 } 544 }
498 Range* range = createRange(dragCaret.toNormalizedEphemeralRange()); 545 Range* range = createRange(dragCaret.toNormalizedEphemeralRange());
499 Element* rootEditableElement = innerFrame->selection().rootEditableElement() ; 546 Element* rootEditableElement = innerFrame->selection().rootEditableElement() ;
500 547
501 // For range to be null a WebKit client must have done something bad while 548 // For range to be null a WebKit client must have done something bad while
502 // manually controlling drag behaviour 549 // manually controlling drag behaviour
503 if (!range) 550 if (!range)
504 return false; 551 return false;
505 ResourceFetcher* fetcher = range->ownerDocument().fetcher(); 552 ResourceFetcher* fetcher = range->ownerDocument().fetcher();
506 ResourceCacheValidationSuppressor validationSuppressor(fetcher); 553 ResourceCacheValidationSuppressor validationSuppressorr(fetcher);
507 if (dragIsMove(innerFrame->selection(), dragData) || dragCaret.isContentRich lyEditable()) { 554
555 DragDropBeforeInputResult beforeInputResult = dispatchDragDropBeforeInput(in nerFrame->editor().findEventTargetFromSelection(), element, dragData, dragIsMove (innerFrame->selection(), dragData));
556
557 switch (beforeInputResult) {
558 case DragDropBeforeInputResult::None:
559 default:
560 return true;
561
562 case DragDropBeforeInputResult::DeleteOnly:
563 innerFrame->editor().deleteSelectionWithSmartDelete(innerFrame->editor() .smartInsertDeleteEnabled(), InputEvent::InputType::DeleteByDrag);
564 break;
565
566 case DragDropBeforeInputResult::InsertOnly:
567 if (dragCaret.isContentRichlyEditable()) {
568 bool chosePlainText = false;
569 DocumentFragment* fragment = documentFragmentFromDragData(dragData, innerFrame, range, true, chosePlainText);
570 if (!fragment)
571 return false;
572 if (setSelectionToDragCaret(innerFrame, dragCaret, range, point)) {
573 DCHECK(m_documentUnderMouse);
574 m_documentUnderMouse->frame()->editor().replaceSelectionAfterDra gging(fragment, dragData->canSmartReplace(), chosePlainText);
575 }
576 } else {
577 String text = dragData->asPlainText();
578 if (text.isEmpty())
579 return false;
580
581 if (setSelectionToDragCaret(innerFrame, dragCaret, range, point)) {
582 const bool canSmartReplace = false;
583 const bool chosePlainText = true;
584 DCHECK(m_documentUnderMouse);
585 m_documentUnderMouse->frame()->editor().replaceSelectionAfterDra gging(createFragmentFromText(EphemeralRange(range), text), canSmartReplace, chos ePlainText);
586 }
587 }
588 break;
589
590 case DragDropBeforeInputResult::DeleteAndInsert:
508 bool chosePlainText = false; 591 bool chosePlainText = false;
509 DocumentFragment* fragment = documentFragmentFromDragData(dragData, inne rFrame, range, true, chosePlainText); 592 DocumentFragment* fragment = documentFragmentFromDragData(dragData, inne rFrame, range, true, chosePlainText);
510 if (!fragment) 593 if (!fragment)
511 return false; 594 return false;
512 595 // NSTextView behavior is to always smart delete on moving a selection,
513 if (dragIsMove(innerFrame->selection(), dragData)) { 596 // but only to smart insert if the selection granularity is word granula rity.
514 // NSTextView behavior is to always smart delete on moving a selecti on, 597 bool smartDelete = innerFrame->editor().smartInsertDeleteEnabled();
515 // but only to smart insert if the selection granularity is word gra nularity. 598 bool smartInsert = smartDelete && innerFrame->selection().granularity() == WordGranularity && dragData->canSmartReplace();
516 bool smartDelete = innerFrame->editor().smartInsertDeleteEnabled(); 599 innerFrame->editor().moveSelectionAfterDragging(fragment, dragCaret.base (), smartInsert, smartDelete);
517 bool smartInsert = smartDelete && innerFrame->selection().granularit y() == WordGranularity && dragData->canSmartReplace(); 600 break;
518 innerFrame->editor().moveSelectionAfterDragging(fragment, dragCaret. base(), smartInsert, smartDelete);
519 } else {
520 if (setSelectionToDragCaret(innerFrame, dragCaret, range, point)) {
521 ASSERT(m_documentUnderMouse);
522 m_documentUnderMouse->frame()->editor().replaceSelectionAfterDra gging(fragment, dragData->canSmartReplace(), chosePlainText);
523 }
524 }
525 } else {
526 String text = dragData->asPlainText();
527 if (text.isEmpty())
528 return false;
529
530 if (setSelectionToDragCaret(innerFrame, dragCaret, range, point)) {
531 const bool canSmartReplace = false;
532 const bool chosePlainText = true;
533 ASSERT(m_documentUnderMouse);
534 m_documentUnderMouse->frame()->editor().replaceSelectionAfterDraggin g(createFragmentFromText(EphemeralRange(range), text), canSmartReplace, chosePla inText);
535 }
536 } 601 }
537 602
538 if (rootEditableElement) { 603 if (rootEditableElement) {
539 if (LocalFrame* frame = rootEditableElement->document().frame()) 604 if (LocalFrame* frame = rootEditableElement->document().frame())
540 frame->eventHandler().updateDragStateAfterEditDragIfNeeded(rootEdita bleElement); 605 frame->eventHandler().updateDragStateAfterEditDragIfNeeded(rootEdita bleElement);
541 } 606 }
542 607
543 return true; 608 return true;
544 } 609 }
545 610
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
997 1062
998 DEFINE_TRACE(DragController) 1063 DEFINE_TRACE(DragController)
999 { 1064 {
1000 visitor->trace(m_page); 1065 visitor->trace(m_page);
1001 visitor->trace(m_documentUnderMouse); 1066 visitor->trace(m_documentUnderMouse);
1002 visitor->trace(m_dragInitiator); 1067 visitor->trace(m_dragInitiator);
1003 visitor->trace(m_fileInputElementUnderMouse); 1068 visitor->trace(m_fileInputElementUnderMouse);
1004 } 1069 }
1005 1070
1006 } // namespace blink 1071 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698