From acb204611a5c1c11ffd0e698be3c477438cdd9fc Mon Sep 17 00:00:00 2001 From: Victor Cebollada Date: Fri, 10 Mar 2017 11:32:14 +0000 Subject: [PATCH] Fix for Text::Controller::KeyEvent() * When text is selected and any of the volume keys are pressed, the text is cleared. The patch fixes the issue by doing nothing when these key events arrive. Change-Id: I9e265579b55ffdb3605a37f8c10fb1dbb6e2e626 Signed-off-by: Victor Cebollada --- .../src/dali-toolkit/utc-Dali-TextField.cpp | 160 +++++++++++++++++++++ dali-toolkit/internal/text/text-controller.cpp | 35 ++++- 2 files changed, 192 insertions(+), 3 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp b/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp index e4926c6..5f53c6f 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp @@ -2072,3 +2072,163 @@ int utcDaliTextFieldStyleWhilstSelected(void) END_TEST; } + +int utcDaliTextFieldEscKeyLoseFocus(void) +{ + ToolkitTestApplication application; + tet_infoline(" utcDaliTextFieldEscKeyLoseFocus"); + + // Creates a tap event. After creating a tap event the text field should + // have the focus and add text with key events should be possible. + + TextField field = TextField::New(); + DALI_TEST_CHECK( field ); + + Stage::GetCurrent().Add( field ); + + field.SetSize( 300.f, 50.f ); + field.SetParentOrigin( ParentOrigin::TOP_LEFT ); + field.SetAnchorPoint( AnchorPoint::TOP_LEFT ); + + // Avoid a crash when core load gl resources. + application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Add a key event but as the text field has not the focus it should do nothing. + application.ProcessEvent( GenerateKey( "a", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "a", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( field.GetProperty( TextField::Property::TEXT ), std::string(""), TEST_LOCATION ); + + // Create a tap event to touch the text field. + application.ProcessEvent( GenerateTap( Gesture::Possible, 1u, 1u, Vector2( 150.0f, 25.0f ) ) ); + application.ProcessEvent( GenerateTap( Gesture::Started, 1u, 1u, Vector2( 150.0f, 25.0f ) ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Now the text field has the focus, so it can handle the key events. + application.ProcessEvent( GenerateKey( "a", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "a", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::Up ) ); + application.ProcessEvent( GenerateKey( "d", "d", KEY_D_CODE, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "d", "d", KEY_D_CODE, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( field.GetProperty( TextField::Property::TEXT ), std::string("ad"), TEST_LOCATION ); + + // Generate a Esc key event. The text field should lose the focus. + application.ProcessEvent( GenerateKey( "", "", DALI_KEY_ESCAPE, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "", "", DALI_KEY_ESCAPE, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( false, field.HasKeyInputFocus(), TEST_LOCATION ); + + // No more text should be introduced + application.ProcessEvent( GenerateKey( "a", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "a", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( field.GetProperty( TextField::Property::TEXT ), std::string("ad"), TEST_LOCATION ); + + END_TEST; +} + +int utcDaliTextFieldSomeSpecialKeys(void) +{ + ToolkitTestApplication application; + tet_infoline(" utcDaliTextFieldSomeSpecialKeys"); + + // Checks some special keys when the text is selected. + + TextField field = TextField::New(); + DALI_TEST_CHECK( field ); + Stage::GetCurrent().Add( field ); + LoadMarkerImages(application, field); + // Render and notify + application.SendNotification(); + application.Render(); + + const std::string longText( "This is a long text for the size of the text-field." ); + + field.SetProperty( TextField::Property::TEXT, longText ); + field.SetProperty( TextField::Property::POINT_SIZE, 10.f ); + field.SetSize( 300.f, 50.f ); + field.SetParentOrigin( ParentOrigin::TOP_LEFT ); + field.SetAnchorPoint( AnchorPoint::TOP_LEFT ); + + // Avoid a crash when core load gl resources. + application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE ); + // Render and notify + application.SendNotification(); + application.Render(); + + // Create a tap event to touch the text field. + application.ProcessEvent( GenerateTap( Gesture::Possible, 1u, 1u, Vector2( 150.0f, 25.0f ) ) ); + application.ProcessEvent( GenerateTap( Gesture::Started, 1u, 1u, Vector2( 150.0f, 25.0f ) ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Tap first to get the focus. + application.ProcessEvent( GenerateTap( Gesture::Possible, 1u, 1u, Vector2( 1.f, 25.0f ) ) ); + application.ProcessEvent( GenerateTap( Gesture::Started, 1u, 1u, Vector2( 1.f, 25.0f ) ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Double tap to select a word. + application.ProcessEvent( GenerateTap( Gesture::Possible, 2u, 1u, Vector2( 1.f, 25.0f ) ) ); + application.ProcessEvent( GenerateTap( Gesture::Started, 2u, 1u, Vector2( 1.f, 25.0f ) ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Generate a Esc key event. The text field should lose the focus. + application.ProcessEvent( GenerateKey( "XF86PowerOff", "XF86PowerOff", DALI_KEY_POWER, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "XF86PowerOff", "XF86PowerOff", DALI_KEY_POWER, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Generate a Esc key event. The text field should lose the focus. + application.ProcessEvent( GenerateKey( "XF86Menu", "XF86Menu", DALI_KEY_MENU, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "XF86Menu", "XF86Menu", DALI_KEY_MENU, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // Generate a Esc key event. The text field should lose the focus. + application.ProcessEvent( GenerateKey( "XF86Home", "XF86Home", DALI_KEY_HOME, 0, 0, Integration::KeyEvent::Down ) ); + application.ProcessEvent( GenerateKey( "XF86Home", "XF86Home", DALI_KEY_HOME, 0, 0, Integration::KeyEvent::Up ) ); + + // Render and notify + application.SendNotification(); + application.Render(); + + // The text shouldn't be deleted. + DALI_TEST_EQUALS( field.GetProperty( TextField::Property::TEXT ), longText, TEST_LOCATION ); + + END_TEST; +} diff --git a/dali-toolkit/internal/text/text-controller.cpp b/dali-toolkit/internal/text/text-controller.cpp index 55f40e9..6f2d4b7 100644 --- a/dali-toolkit/internal/text/text-controller.cpp +++ b/dali-toolkit/internal/text/text-controller.cpp @@ -1751,7 +1751,8 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent ) { DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" ); - bool textChanged( false ); + bool textChanged = false; + bool relayoutNeeded = false; if( ( NULL != mImpl->mEventData ) && ( keyEvent.state == KeyEvent::Down ) ) @@ -1771,6 +1772,9 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent ) { // Escape key is a special case which causes focus loss KeyboardFocusLostEvent(); + + // Will request for relayout. + relayoutNeeded = true; } else if( ( Dali::DALI_KEY_CURSOR_LEFT == keyCode ) || ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) || @@ -1803,10 +1807,16 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent ) Event event( Event::CURSOR_KEY_EVENT ); event.p1.mInt = keyCode; mImpl->mEventData->mEventQueue.push_back( event ); + + // Will request for relayout. + relayoutNeeded = true; } else if( Dali::DALI_KEY_BACKSPACE == keyCode ) { textChanged = BackspaceKeyEvent(); + + // Will request for relayout. + relayoutNeeded = true; } else if( IsKey( keyEvent, Dali::DALI_KEY_POWER ) || IsKey( keyEvent, Dali::DALI_KEY_MENU ) || @@ -1815,6 +1825,9 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent ) // Power key/Menu/Home key behaviour does not allow edit mode to resume. mImpl->ChangeState( EventData::INACTIVE ); + // Will request for relayout. + relayoutNeeded = true; + // This branch avoids calling the InsertText() method of the 'else' branch which can delete selected text. } else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode ) @@ -1824,6 +1837,11 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent ) // Do nothing. } + else if( ( Dali::DALI_KEY_VOLUME_UP == keyCode ) || ( Dali::DALI_KEY_VOLUME_DOWN == keyCode ) ) + { + // This branch avoids calling the InsertText() method of the 'else' branch which can delete selected text. + // Do nothing. + } else { DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() ); @@ -1833,20 +1851,31 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent ) InsertText( keyString, COMMIT ); textChanged = true; + + // Will request for relayout. + relayoutNeeded = true; } if ( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) && ( mImpl->mEventData->mState != EventData::INACTIVE ) && ( !isNullKey ) && - ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) ) + ( Dali::DALI_KEY_SHIFT_LEFT != keyCode ) && + ( Dali::DALI_KEY_VOLUME_UP != keyCode ) && + ( Dali::DALI_KEY_VOLUME_DOWN != keyCode ) ) { // Should not change the state if the key is the shift send by the imf manager. // Otherwise, when the state is SELECTING the text controller can't send the right // surrounding info to the imf. mImpl->ChangeState( EventData::EDITING ); + + // Will request for relayout. + relayoutNeeded = true; } - mImpl->RequestRelayout(); + if( relayoutNeeded ) + { + mImpl->RequestRelayout(); + } } if( textChanged && -- 2.7.4