From: abdullah Date: Mon, 2 Aug 2021 15:48:39 +0000 (+0300) Subject: Add SelectionChanged signal X-Git-Tag: dali_2.0.41~7^2 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=55913cc4a36f6b7171482d01f365c4b67d62660f Add SelectionChanged signal added signal which will be called when selection has been changed void OnSelectionChanged(TextEditor textEditor, uint32_t oldStart, uint32_t oldEnd) Change-Id: I2f769fe11f487462309d8a474fd8ac5aa5c1f2d8 --- diff --git a/automated-tests/src/dali-toolkit/utc-Dali-TextEditor.cpp b/automated-tests/src/dali-toolkit/utc-Dali-TextEditor.cpp index d9a70d2..9aef457 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-TextEditor.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-TextEditor.cpp @@ -133,6 +133,9 @@ const char* HANDLE_RIGHT_SELECTION_FILE_NAME = TEST_RESOURCE_DIR "/selection_han const std::string DEFAULT_DEVICE_NAME("hwKeyboard"); +static bool gSelectionChangedCallbackCalled; +static uint32_t oldSelectionStart; +static uint32_t oldSelectionEnd; static bool gAnchorClickedCallBackCalled; static bool gAnchorClickedCallBackNotCalled; static bool gTextChangedCallBackCalled; @@ -158,6 +161,15 @@ struct CallbackFunctor bool* mCallbackFlag; }; +static void TestSelectionChangedCallback(TextEditor control, uint32_t oldStart, uint32_t oldEnd) +{ + tet_infoline(" TestSelectionChangedCallback"); + + gSelectionChangedCallbackCalled = true; + oldSelectionStart = oldStart; + oldSelectionEnd = oldEnd; +} + static void TestAnchorClickedCallback(TextEditor control, const char* href, unsigned int hrefLength) { tet_infoline(" TestAnchorClickedCallback"); @@ -4142,4 +4154,137 @@ int utcDaliTextEditorCursorPositionChangedSignal(void) DALI_TEST_EQUALS(oldCursorPos, 5, TEST_LOCATION); END_TEST; +} + +int utcDaliTextEditorSelectionChangedSignal(void) +{ + ToolkitTestApplication application; + tet_infoline(" utcDaliTextEditorSelectionChangedSignal"); + + TextEditor editor = TextEditor::New(); + DALI_TEST_CHECK( editor ); + + application.GetScene().Add( editor ); + + // connect to the selection changed signal. + ConnectionTracker* testTracker = new ConnectionTracker(); + DevelTextEditor::SelectionChangedSignal(editor).Connect(&TestSelectionChangedCallback); + bool selectionChangedSignal = false; + editor.ConnectSignal( testTracker, "selectionChanged", CallbackFunctor(&selectionChangedSignal) ); + + editor.SetProperty( TextEditor::Property::TEXT, "Hello\nworld\nHello world" ); + editor.SetProperty( TextEditor::Property::POINT_SIZE, 10.f ); + editor.SetProperty( Actor::Property::SIZE, Vector2( 100.f, 50.f ) ); + editor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT ); + editor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT ); + + // Avoid a crash when core load gl resources. + application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Tap on the text editor + TestGenerateTap( application, 3.0f, 25.0f ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Move to second line of the text. + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_CURSOR_DOWN, 0, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Select some text in the right of the current cursor position + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_SHIFT_LEFT, 0, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_CURSOR_RIGHT, KEY_SHIFT_MODIFIER, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, oldSelectionEnd, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_CURSOR_RIGHT, KEY_SHIFT_MODIFIER, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 6, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 7, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_ESCAPE, 0, 0, Integration::KeyEvent::UP, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 6, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 8, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + editor.SetKeyInputFocus(); + + // Render and notify + application.SendNotification(); + application.Render(); + + DevelTextEditor::SelectText( editor ,0, 5 ); + + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, oldSelectionEnd, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + editor.SetProperty( DevelTextEditor::Property::PRIMARY_CURSOR_POSITION, 3); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 0, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 5, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + // select all text + DevelTextEditor::SelectWholeText(editor); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, oldSelectionEnd, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + // select none + DevelTextEditor::SelectNone(editor); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 0, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 23, TEST_LOCATION); + + END_TEST; } \ No newline at end of file diff --git a/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp b/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp index d824c04..eafc4d5 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp @@ -121,8 +121,13 @@ const int KEY_RETURN_CODE = 36; const int KEY_A_CODE = 38; const int KEY_D_CODE = 40; +const int KEY_SHIFT_MODIFIER = 257; + const std::string DEFAULT_DEVICE_NAME("hwKeyboard"); +static bool gSelectionChangedCallbackCalled; +static uint32_t oldSelectionStart; +static uint32_t oldSelectionEnd; static bool gAnchorClickedCallBackCalled; static bool gAnchorClickedCallBackNotCalled; static bool gTextChangedCallBackCalled; @@ -214,6 +219,15 @@ struct CallbackFunctor bool* mCallbackFlag; }; +static void TestSelectionChangedCallback(TextField control, uint32_t oldStart, uint32_t oldEnd) +{ + tet_infoline(" TestSelectionChangedCallback"); + + gSelectionChangedCallbackCalled = true; + oldSelectionStart = oldStart; + oldSelectionEnd = oldEnd; +} + static void TestAnchorClickedCallback(TextField control, const char* href, unsigned int hrefLength) { tet_infoline(" TestAnchorClickedCallback"); @@ -4088,4 +4102,130 @@ int utcDaliTextFieldCursorPositionChangedSignal(void) DALI_TEST_EQUALS(oldCursorPos, 5, TEST_LOCATION); END_TEST; +} + +int utcDaliTextFieldSelectionChangedSignal(void) +{ + ToolkitTestApplication application; + tet_infoline(" utcDaliTextFieldSelectionChangedSignal"); + + TextField field = TextField::New(); + DALI_TEST_CHECK( field ); + + application.GetScene().Add( field ); + + // connect to the selection changed signal. + ConnectionTracker* testTracker = new ConnectionTracker(); + DevelTextField::SelectionChangedSignal(field).Connect(&TestSelectionChangedCallback); + bool selectionChangedSignal = false; + field.ConnectSignal( testTracker, "selectionChanged", CallbackFunctor(&selectionChangedSignal) ); + + field.SetProperty( TextField::Property::TEXT, "Hello world Hello world" ); + field.SetProperty( TextField::Property::POINT_SIZE, 10.f ); + field.SetProperty( Actor::Property::SIZE, Vector2( 100.f, 50.f ) ); + field.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT ); + field.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT ); + + // Avoid a crash when core load gl resources. + application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Tap on the text field + TestGenerateTap( application, 3.0f, 25.0f ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Select some text in the right of the current cursor position + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_SHIFT_LEFT, 0, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_CURSOR_RIGHT, KEY_SHIFT_MODIFIER, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, oldSelectionEnd, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_CURSOR_RIGHT, KEY_SHIFT_MODIFIER, 0, Integration::KeyEvent::DOWN, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 0, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 1, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + application.ProcessEvent( GenerateKey( "", "", "", DALI_KEY_ESCAPE, 0, 0, Integration::KeyEvent::UP, "", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 0, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 2, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + field.SetKeyInputFocus(); + + // Render and notify + application.SendNotification(); + application.Render(); + + DevelTextField::SelectText( field ,0, 5 ); + + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, oldSelectionEnd, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + field.SetProperty( DevelTextField::Property::PRIMARY_CURSOR_POSITION, 3); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 0, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 5, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + // select all text + DevelTextField::SelectWholeText(field); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, oldSelectionEnd, TEST_LOCATION); + + gSelectionChangedCallbackCalled = false; + + // select none + DevelTextField::SelectNone(field); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_CHECK(gSelectionChangedCallbackCalled); + DALI_TEST_EQUALS(oldSelectionStart, 0, TEST_LOCATION); + DALI_TEST_EQUALS(oldSelectionEnd, 23, TEST_LOCATION); + + END_TEST; } \ No newline at end of file diff --git a/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.cpp b/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.cpp index 563bc02..33fc4e7 100644 --- a/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.cpp +++ b/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.cpp @@ -50,6 +50,11 @@ InputFilteredSignalType& InputFilteredSignal(TextEditor textEditor) return GetImpl(textEditor).InputFilteredSignal(); } +SelectionChangedSignalType& SelectionChangedSignal(TextEditor textEditor) +{ + return GetImpl(textEditor).SelectionChangedSignal(); +} + void SelectWholeText(TextEditor textEditor) { GetImpl(textEditor).SelectWholeText(); diff --git a/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h b/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h index d9c2db6..53a63e2 100644 --- a/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h +++ b/dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h @@ -376,6 +376,27 @@ using InputFilteredSignalType = Signal; + +/** + * @brief This signal is emitted when the selection has been changed. + * + * A callback of the following type may be connected: + * @code + * void YourCallbackName( TextEditor textEditor, uint32_t oldStart, uint32_t oldEnd); + * @endcode + * @param[in] textEditor The instance of TextEditor. + * @return The signal to connect to + */ +DALI_TOOLKIT_API SelectionChangedSignalType& SelectionChangedSignal(TextEditor textEditor); + +/** * @brief Select the whole text of TextEditor. * * @param[in] textEditor The instance of TextEditor. diff --git a/dali-toolkit/devel-api/controls/text-controls/text-field-devel.cpp b/dali-toolkit/devel-api/controls/text-controls/text-field-devel.cpp index dd9bd51..0d9ef15 100644 --- a/dali-toolkit/devel-api/controls/text-controls/text-field-devel.cpp +++ b/dali-toolkit/devel-api/controls/text-controls/text-field-devel.cpp @@ -45,6 +45,11 @@ InputFilteredSignalType& InputFilteredSignal(TextField textField) return GetImpl(textField).InputFilteredSignal(); } +SelectionChangedSignalType& SelectionChangedSignal(TextField textField) +{ + return GetImpl(textField).SelectionChangedSignal(); +} + void SelectWholeText(TextField textField) { GetImpl(textField).SelectWholeText(); diff --git a/dali-toolkit/devel-api/controls/text-controls/text-field-devel.h b/dali-toolkit/devel-api/controls/text-controls/text-field-devel.h index 3f35f9a..59f192d 100644 --- a/dali-toolkit/devel-api/controls/text-controls/text-field-devel.h +++ b/dali-toolkit/devel-api/controls/text-controls/text-field-devel.h @@ -298,6 +298,27 @@ using InputFilteredSignalType = Signal; + +/** + * @brief This signal is emitted when the selection has been changed. + * + * A callback of the following type may be connected: + * @code + * void YourCallbackName( TextField textField, uint32_t oldStart, uint32_t oldEnd); + * @endcode + * @param[in] textField The instance of TextField. + * @return The signal to connect to + */ +DALI_TOOLKIT_API SelectionChangedSignalType& SelectionChangedSignal(TextField textField); + +/** * @brief Select the whole text of TextField. * * @param[in] textField The instance of TextField. diff --git a/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp b/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp index 090bf9a..8236064 100644 --- a/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp +++ b/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp @@ -159,7 +159,7 @@ DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "maxLengthReached", SIGNAL_MA DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "anchorClicked", SIGNAL_ANCHOR_CLICKED ) DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "inputFiltered", SIGNAL_INPUT_FILTERED ) DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "cursorPositionChanged", SIGNAL_CURSOR_POSITION_CHANGED) - +DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "selectionChanged", SIGNAL_SELECTION_CHANGED ) DALI_TYPE_REGISTRATION_END() // clang-format on @@ -1324,6 +1324,11 @@ DevelTextEditor::InputFilteredSignalType& TextEditor::InputFilteredSignal() return mInputFilteredSignal; } +DevelTextEditor::SelectionChangedSignalType& TextEditor::SelectionChangedSignal() +{ + return mSelectionChangedSignal; +} + Text::ControllerPtr TextEditor::GetTextController() { return mController; @@ -1376,6 +1381,14 @@ bool TextEditor::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* editorImpl.InputFilteredSignal().Connect(tracker, functor); } } + else if(0 == strcmp(signalName.c_str(), SIGNAL_SELECTION_CHANGED)) + { + if(editor) + { + Internal::TextEditor& editorImpl(GetImpl(editor)); + editorImpl.SelectionChangedSignal().Connect(tracker, functor); + } + } else { // signalName does not match any signal @@ -1613,6 +1626,11 @@ void TextEditor::OnRelayout(const Vector2& size, RelayoutContainer& container) EmitCursorPositionChangedSignal(); } + if(mSelectionChanged) + { + EmitSelectionChangedSignal(); + } + // The text-editor emits signals when the input style changes. These changes of style are // detected during the relayout process (size negotiation), i.e after the cursor has been moved. Signals // can't be emitted during the size negotiation as the callbacks may update the UI. @@ -1985,6 +2003,31 @@ void TextEditor::InputFiltered(Toolkit::InputFilter::Property::Type type) mInputFilteredSignal.Emit(handle, type); } +void TextEditor::EmitSelectionChangedSignal() +{ + Dali::Toolkit::TextEditor handle(GetOwner()); + mSelectionChangedSignal.Emit(handle, mOldSelectionStart, mOldSelectionEnd); + mSelectionChanged = false; +} + +void TextEditor::SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) +{ + if(((oldStart != newStart) || (oldEnd != newEnd)) && !mSelectionChanged) + { + mSelectionChanged = true; + mOldSelectionStart = oldStart; + mOldSelectionEnd = oldEnd; + + if(mOldSelectionStart > mOldSelectionEnd) + { + //swap + uint32_t temp = mOldSelectionStart; + mOldSelectionStart = mOldSelectionEnd; + mOldSelectionEnd = temp; + } + } +} + void TextEditor::AddDecoration(Actor& actor, bool needsClipping) { if(actor) @@ -2263,7 +2306,8 @@ TextEditor::TextEditor() mScrollBarEnabled(false), mScrollStarted(false), mTextChanged(false), - mCursorPositionChanged(false) + mCursorPositionChanged(false), + mSelectionChanged(false) { } diff --git a/dali-toolkit/internal/controls/text-controls/text-editor-impl.h b/dali-toolkit/internal/controls/text-controls/text-editor-impl.h index 9b22782..d76b41b 100644 --- a/dali-toolkit/internal/controls/text-controls/text-editor-impl.h +++ b/dali-toolkit/internal/controls/text-controls/text-editor-impl.h @@ -103,6 +103,11 @@ public: DevelTextEditor::InputFilteredSignalType& InputFilteredSignal(); /** + * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal() + */ + DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal(); + + /** * Connects a callback function with the object's signals. * @param[in] object The object providing the signal. * @param[in] tracker Used to disconnect the signal. @@ -241,6 +246,11 @@ private: // From Control void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override; /** + * @copydoc Text::SelectableControlInterface::SelectionChanged() + */ + void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override; + + /** * @copydoc Text::EditableControlInterface::AddDecoration() */ void AddDecoration(Actor& actor, bool needsClipping) override; @@ -383,6 +393,11 @@ private: // Implementation void EmitTextChangedSignal(); /** + * @brief Emits SelectionChanged signal. + */ + void EmitSelectionChangedSignal(); + + /** * @brief set RenderActor's position with new scrollPosition * * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled @@ -442,6 +457,7 @@ private: // Data Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal; Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal; Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal; + Toolkit::DevelTextEditor::SelectionChangedSignalType mSelectionChangedSignal; InputMethodContext mInputMethodContext; Text::ControllerPtr mController; @@ -470,10 +486,15 @@ private: // Data bool mScrollStarted : 1; bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout(). bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout(). + bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout(). //args for cursor PositionChanged event unsigned int mOldPosition; + //args for selection changed event + uint32_t mOldSelectionStart; + uint32_t mOldSelectionEnd; + /** * @brief This structure is to connect TextEditor with Accessible functions. */ diff --git a/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp b/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp index ab9878d..a806c70 100644 --- a/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp +++ b/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp @@ -146,6 +146,7 @@ DALI_SIGNAL_REGISTRATION(Toolkit, TextField, "inputStyleChanged", SIGNAL_INP DALI_SIGNAL_REGISTRATION(Toolkit, TextField, "anchorClicked", SIGNAL_ANCHOR_CLICKED ) DALI_SIGNAL_REGISTRATION(Toolkit, TextField, "inputFiltered", SIGNAL_INPUT_FILTERED ) DALI_SIGNAL_REGISTRATION(Toolkit, TextField, "cursorPositionChanged", SIGNAL_CURSOR_POSITION_CHANGED) +DALI_SIGNAL_REGISTRATION(Toolkit, TextField, "selectionChanged", SIGNAL_SELECTION_CHANGED ) DALI_TYPE_REGISTRATION_END() // clang-format on @@ -1263,6 +1264,14 @@ bool TextField::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* fieldImpl.InputFilteredSignal().Connect(tracker, functor); } } + else if(0 == strcmp(signalName.c_str(), SIGNAL_SELECTION_CHANGED)) + { + if(field) + { + Internal::TextField& fieldImpl(GetImpl(field)); + fieldImpl.SelectionChangedSignal().Connect(tracker, functor); + } + } else { // signalName does not match any signal @@ -1302,6 +1311,11 @@ DevelTextField::InputFilteredSignalType& TextField::InputFilteredSignal() return mInputFilteredSignal; } +DevelTextField::SelectionChangedSignalType& TextField::SelectionChangedSignal() +{ + return mSelectionChangedSignal; +} + void TextField::OnInitialize() { Actor self = Self(); @@ -1503,6 +1517,11 @@ void TextField::OnRelayout(const Vector2& size, RelayoutContainer& container) EmitCursorPositionChangedSignal(); } + if(mSelectionChanged) + { + EmitSelectionChangedSignal(); + } + // The text-field emits signals when the input style changes. These changes of style are // detected during the relayout process (size negotiation), i.e after the cursor has been moved. Signals // can't be emitted during the size negotiation as the callbacks may update the UI. @@ -1892,6 +1911,31 @@ void TextField::InputFiltered(Toolkit::InputFilter::Property::Type type) mInputFilteredSignal.Emit(handle, type); } +void TextField::EmitSelectionChangedSignal() +{ + Dali::Toolkit::TextField handle(GetOwner()); + mSelectionChangedSignal.Emit(handle, mOldSelectionStart, mOldSelectionEnd); + mSelectionChanged = false; +} + +void TextField::SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) +{ + if(((oldStart != newStart) || (oldEnd != newEnd)) && !mSelectionChanged) + { + mSelectionChanged = true; + mOldSelectionStart = oldStart; + mOldSelectionEnd = oldEnd; + + if(mOldSelectionStart > mOldSelectionEnd) + { + //swap + uint32_t temp = mOldSelectionStart; + mOldSelectionStart = mOldSelectionEnd; + mOldSelectionEnd = temp; + } + } +} + void TextField::AddDecoration(Actor& actor, bool needsClipping) { if(actor) @@ -2029,7 +2073,8 @@ TextField::TextField() mExceedPolicy(Dali::Toolkit::TextField::EXCEED_POLICY_CLIP), mHasBeenStaged(false), mTextChanged(false), - mCursorPositionChanged(false) + mCursorPositionChanged(false), + mSelectionChanged(false) { } diff --git a/dali-toolkit/internal/controls/text-controls/text-field-impl.h b/dali-toolkit/internal/controls/text-controls/text-field-impl.h index dfd12a2..5cc7e78 100644 --- a/dali-toolkit/internal/controls/text-controls/text-field-impl.h +++ b/dali-toolkit/internal/controls/text-controls/text-field-impl.h @@ -126,6 +126,11 @@ public: */ DevelTextField::InputFilteredSignalType& InputFilteredSignal(); + /** + * @copydoc TextField::SelectionChangedSignal() + */ + DevelTextField::SelectionChangedSignalType& SelectionChangedSignal(); + private: // From Control /** * @copydoc Control::OnInitialize() @@ -232,6 +237,11 @@ private: // From Control void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override; /** + * @copydoc Text::SelectableControlInterface::SelectionChanged() + */ + void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override; + + /** * @copydoc Text::EditableControlInterface::AddDecoration() */ void AddDecoration(Actor& actor, bool needsClipping) override; @@ -353,6 +363,11 @@ private: // Implementation void EmitCursorPositionChangedSignal(); /** + * @brief Emits SelectionChanged signal. + */ + void EmitSelectionChangedSignal(); + + /** * @brief Callback function for when the layout is changed. * @param[in] actor The actor whose layoutDirection is changed. * @param[in] type The layoutDirection. @@ -397,6 +412,7 @@ private: // Data Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal; Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal; Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal; + Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal; InputMethodContext mInputMethodContext; Text::ControllerPtr mController; @@ -417,10 +433,15 @@ private: // Data bool mHasBeenStaged : 1; bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout(). bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout(). + bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout(). //args for cursor position changed event unsigned int mOldPosition; + //args for selection changed event + uint32_t mOldSelectionStart; + uint32_t mOldSelectionEnd; + protected: /** * @brief This structure is to connect TextField with Accessible functions. diff --git a/dali-toolkit/internal/text/text-controller-event-handler.cpp b/dali-toolkit/internal/text/text-controller-event-handler.cpp index 79a917e..bf677e5 100644 --- a/dali-toolkit/internal/text/text-controller-event-handler.cpp +++ b/dali-toolkit/internal/text/text-controller-event-handler.cpp @@ -84,8 +84,17 @@ void Controller::EventHandler::KeyboardFocusLostEvent(Controller& controller) // Init selection position if(controller.mImpl->mEventData->mState == EventData::SELECTING) { + uint32_t oldStart, oldEnd; + oldStart = controller.mImpl->mEventData->mLeftSelectionPosition; + oldEnd = controller.mImpl->mEventData->mRightSelectionPosition; + controller.mImpl->mEventData->mLeftSelectionPosition = controller.mImpl->mEventData->mPrimaryCursorPosition; controller.mImpl->mEventData->mRightSelectionPosition = controller.mImpl->mEventData->mPrimaryCursorPosition; + + if(controller.mImpl->mSelectableControlInterface != nullptr) + { + controller.mImpl->mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, controller.mImpl->mEventData->mPrimaryCursorPosition, controller.mImpl->mEventData->mPrimaryCursorPosition); + } } controller.mImpl->ChangeState(EventData::INACTIVE); @@ -158,12 +167,22 @@ bool Controller::EventHandler::KeyEvent(Controller& controller, const Dali::KeyE // Release the active highlight. if(controller.mImpl->mEventData->mState == EventData::SELECTING) { + uint32_t oldStart, oldEnd; + oldStart = controller.mImpl->mEventData->mLeftSelectionPosition; + oldEnd = controller.mImpl->mEventData->mRightSelectionPosition; + controller.mImpl->ChangeState(EventData::EDITING); // Update selection position. controller.mImpl->mEventData->mLeftSelectionPosition = controller.mImpl->mEventData->mPrimaryCursorPosition; controller.mImpl->mEventData->mRightSelectionPosition = controller.mImpl->mEventData->mPrimaryCursorPosition; controller.mImpl->mEventData->mUpdateCursorPosition = true; + + if(controller.mImpl->mSelectableControlInterface != nullptr) + { + controller.mImpl->mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, controller.mImpl->mEventData->mLeftSelectionPosition, controller.mImpl->mEventData->mRightSelectionPosition); + } + controller.mImpl->RequestRelayout(); } return false; @@ -597,12 +616,21 @@ void Controller::EventHandler::ProcessModifyEvents(Controller& controller) if(NULL != controller.mImpl->mEventData) { + uint32_t oldStart, oldEnd; + oldStart = controller.mImpl->mEventData->mLeftSelectionPosition; + oldEnd = controller.mImpl->mEventData->mRightSelectionPosition; + // When the text is being modified, delay cursor blinking controller.mImpl->mEventData->mDecorator->DelayCursorBlink(); // Update selection position after modifying the text controller.mImpl->mEventData->mLeftSelectionPosition = controller.mImpl->mEventData->mPrimaryCursorPosition; controller.mImpl->mEventData->mRightSelectionPosition = controller.mImpl->mEventData->mPrimaryCursorPosition; + + if(controller.mImpl->mSelectableControlInterface != nullptr && controller.mImpl->mEventData->mState == EventData::SELECTING) + { + controller.mImpl->mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, controller.mImpl->mEventData->mLeftSelectionPosition, controller.mImpl->mEventData->mRightSelectionPosition); + } } // DISCARD temporary text diff --git a/dali-toolkit/internal/text/text-controller-impl-event-handler.cpp b/dali-toolkit/internal/text/text-controller-impl-event-handler.cpp index 9574f8b..06898b6 100644 --- a/dali-toolkit/internal/text/text-controller-impl-event-handler.cpp +++ b/dali-toolkit/internal/text/text-controller-impl-event-handler.cpp @@ -410,7 +410,15 @@ void ControllerImplEventHandler::OnCursorKeyEvent(Controller::Impl& impl, const int cursorPositionDelta = primaryCursorPosition - previousPrimaryCursorPosition; if(cursorPositionDelta > 0 || eventData.mRightSelectionPosition > 0u) // Check the boundary { + uint32_t oldStart = eventData.mLeftSelectionPosition; + uint32_t oldEnd = eventData.mRightSelectionPosition; + eventData.mRightSelectionPosition += cursorPositionDelta; + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition); + } } selecting = true; } @@ -677,8 +685,16 @@ void ControllerImplEventHandler::OnSelectAllEvent(Controller::Impl& impl) 0.f - scrollPosition.y, Controller::NoTextTap::HIGHLIGHT); + uint32_t oldStart = eventData.mLeftSelectionPosition; + uint32_t oldEnd = eventData.mRightSelectionPosition; + eventData.mLeftSelectionPosition = 0u; eventData.mRightSelectionPosition = model->mLogicalModel->mText.Count(); + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition); + } } } } @@ -693,11 +709,19 @@ void ControllerImplEventHandler::OnSelectNoneEvent(Controller::Impl& impl) if(eventData.mSelectionEnabled && eventData.mState == EventData::SELECTING) { eventData.mPrimaryCursorPosition = 0u; + uint32_t oldStart = eventData.mLeftSelectionPosition; + uint32_t oldEnd = eventData.mRightSelectionPosition; + eventData.mLeftSelectionPosition = eventData.mRightSelectionPosition = eventData.mPrimaryCursorPosition; impl.ChangeState(EventData::INACTIVE); eventData.mUpdateCursorPosition = true; eventData.mUpdateInputStyle = true; eventData.mScrollAfterUpdatePosition = true; + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition); + } } } } @@ -716,11 +740,19 @@ void ControllerImplEventHandler::OnSelectRangeEvent(Controller::Impl& impl, cons if(start != end) { + uint32_t oldStart = impl.mEventData->mLeftSelectionPosition; + uint32_t oldEnd = impl.mEventData->mRightSelectionPosition; + // Calculates the logical position from the x,y coords. impl.RepositionSelectionHandles(0.f - scrollPosition.x, 0.f - scrollPosition.y, Controller::NoTextTap::HIGHLIGHT); impl.mEventData->mLeftSelectionPosition = start; impl.mEventData->mRightSelectionPosition = end; + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, start, end); + } } } } @@ -745,6 +777,8 @@ void ControllerImplEventHandler::OnHandlePressed(Controller::Impl& impl, const E matchedCharacter); EventData& eventData = *impl.mEventData; + uint32_t oldStart = eventData.mLeftSelectionPosition; + uint32_t oldEnd = eventData.mRightSelectionPosition; if(Event::GRAB_HANDLE_EVENT == event.type) { @@ -804,6 +838,11 @@ void ControllerImplEventHandler::OnHandlePressed(Controller::Impl& impl, const E eventData.mIsLeftHandleSelected = false; eventData.mIsRightHandleSelected = true; } + + if((impl.mSelectableControlInterface != nullptr) || eventData.mUpdateRightSelectionPosition || eventData.mUpdateLeftSelectionPosition) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition); + } } void ControllerImplEventHandler::OnHandleReleased(Controller::Impl& impl, const Event& event, const bool isSmoothHandlePanEnabled, const bool handleStopScrolling) @@ -829,6 +868,8 @@ void ControllerImplEventHandler::OnHandleReleased(Controller::Impl& impl, const } EventData& eventData = *impl.mEventData; + uint32_t oldStart = eventData.mLeftSelectionPosition; + uint32_t oldEnd = eventData.mRightSelectionPosition; if(Event::GRAB_HANDLE_EVENT == event.type) { @@ -885,6 +926,11 @@ void ControllerImplEventHandler::OnHandleReleased(Controller::Impl& impl, const } } + if((impl.mSelectableControlInterface != nullptr) || eventData.mUpdateRightSelectionPosition || eventData.mUpdateLeftSelectionPosition) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition); + } + eventData.mDecoratorUpdated = true; } @@ -999,6 +1045,8 @@ void ControllerImplEventHandler::OnHandleScrolling(Controller::Impl& impl, const position.y - scrollPosition.y, CharacterHitTest::SCROLL, matchedCharacter); + uint32_t oldStart = eventData.mLeftSelectionPosition; + uint32_t oldEnd = eventData.mRightSelectionPosition; if(leftSelectionHandleEvent) { @@ -1029,6 +1077,11 @@ void ControllerImplEventHandler::OnHandleScrolling(Controller::Impl& impl, const impl.RepositionSelectionHandles(); eventData.mScrollAfterUpdatePosition = !isSmoothHandlePanEnabled; + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData.mLeftSelectionPosition, eventData.mRightSelectionPosition); + } } } eventData.mDecoratorUpdated = true; diff --git a/dali-toolkit/internal/text/text-controller-impl.cpp b/dali-toolkit/internal/text/text-controller-impl.cpp index a96d23c..d810928 100644 --- a/dali-toolkit/internal/text/text-controller-impl.cpp +++ b/dali-toolkit/internal/text/text-controller-impl.cpp @@ -1209,7 +1209,9 @@ void Controller::Impl::SetTextSelectionRange(const uint32_t* pStart, const uint3 if(mEventData->mSelectionEnabled && (pStart || pEnd)) { - uint32_t length = static_cast(mModel->mLogicalModel->mText.Count()); + uint32_t length = static_cast(mModel->mLogicalModel->mText.Count()); + uint32_t oldStart = mEventData->mLeftSelectionPosition; + uint32_t oldEnd = mEventData->mRightSelectionPosition; if(pStart) { @@ -1233,6 +1235,11 @@ void Controller::Impl::SetTextSelectionRange(const uint32_t* pStart, const uint3 mEventData->mUpdateLeftSelectionPosition = true; mEventData->mUpdateRightSelectionPosition = true; } + + if(mSelectableControlInterface != nullptr) + { + mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, mEventData->mLeftSelectionPosition, mEventData->mRightSelectionPosition); + } } } @@ -1265,9 +1272,18 @@ bool Controller::Impl::SetPrimaryCursorPosition(CharacterIndex index, bool focus // If there is no focus, only the value is updated. if(focused) { + bool wasInSelectingState = mEventData->mState == EventData::SELECTING; + uint32_t oldStart = mEventData->mLeftSelectionPosition; + uint32_t oldEnd = mEventData->mRightSelectionPosition; ChangeState(EventData::EDITING); mEventData->mLeftSelectionPosition = mEventData->mRightSelectionPosition = mEventData->mPrimaryCursorPosition; mEventData->mUpdateCursorPosition = true; + + if(mSelectableControlInterface != nullptr && wasInSelectingState) + { + mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, mEventData->mLeftSelectionPosition, mEventData->mRightSelectionPosition); + } + ScrollTextToMatchCursor(); } @@ -1384,9 +1400,17 @@ void Controller::Impl::RetrieveSelection(std::string& selectedText, bool deleteA void Controller::Impl::SetSelection(int start, int end) { + uint32_t oldStart = mEventData->mLeftSelectionPosition; + uint32_t oldEnd = mEventData->mRightSelectionPosition; + mEventData->mLeftSelectionPosition = start; mEventData->mRightSelectionPosition = end; mEventData->mUpdateCursorPosition = true; + + if(mSelectableControlInterface != nullptr) + { + mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, start, end); + } } std::pair Controller::Impl::GetSelectionIndexes() const diff --git a/dali-toolkit/internal/text/text-controller-text-updater.cpp b/dali-toolkit/internal/text/text-controller-text-updater.cpp index 61b8c7c..c200c38 100644 --- a/dali-toolkit/internal/text/text-controller-text-updater.cpp +++ b/dali-toolkit/internal/text/text-controller-text-updater.cpp @@ -69,6 +69,11 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string& (EventData::EDITING_WITH_GRAB_HANDLE == eventData->mState) || (EventData::EDITING_WITH_PASTE_POPUP == eventData->mState)) { + if((impl.mSelectableControlInterface != nullptr) && (EventData::SELECTING == eventData->mState)) + { + impl.mSelectableControlInterface->SelectionChanged(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition, eventData->mPrimaryCursorPosition, eventData->mPrimaryCursorPosition); + } + impl.ChangeState(EventData::EDITING); } } diff --git a/dali-toolkit/internal/text/text-selectable-control-interface.h b/dali-toolkit/internal/text/text-selectable-control-interface.h index 3d5feef..931cf6b 100644 --- a/dali-toolkit/internal/text/text-selectable-control-interface.h +++ b/dali-toolkit/internal/text/text-selectable-control-interface.h @@ -72,6 +72,16 @@ public: * @return The seletced text. */ virtual string GetSelectedText() const = 0; + + /** + * @brief Called when the selection has been changed. + * + * @param oldStart the selection handles start position before the change. + * @param oldEnd the selection handles end position before the change. + * @param newStart the selection handles start position after the change. + * @param newEnd the selection handles end position after the change. + */ + virtual void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) = 0; }; } // namespace Text diff --git a/dali-toolkit/internal/text/text-selection-handle-controller.cpp b/dali-toolkit/internal/text/text-selection-handle-controller.cpp index b082510..cf63deb 100644 --- a/dali-toolkit/internal/text/text-selection-handle-controller.cpp +++ b/dali-toolkit/internal/text/text-selection-handle-controller.cpp @@ -491,6 +491,9 @@ void SelectionHandleController::Reposition(Controller::Impl& impl, float visualX if(characterHit || (Controller::NoTextTap::HIGHLIGHT == action)) { + uint32_t oldStart = eventData->mLeftSelectionPosition; + uint32_t oldEnd = eventData->mRightSelectionPosition; + impl.ChangeState(EventData::SELECTING); eventData->mLeftSelectionPosition = selectionStart; @@ -510,6 +513,11 @@ void SelectionHandleController::Reposition(Controller::Impl& impl, float visualX // Cursor to be positioned at end of selection so if selection interrupted and edit mode restarted the cursor will be at end of selection eventData->mPrimaryCursorPosition = std::max(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition); + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition); + } } else if(Controller::NoTextTap::SHOW_SELECTION_POPUP == action) {