Browse Source

Text widget: Reduce unnecessary formatting

Michael Ragazzon 3 years ago
parent
commit
1566efda8d
2 changed files with 59 additions and 39 deletions
  1. 51 35
      Source/Core/Elements/WidgetTextInput.cpp
  2. 8 4
      Source/Core/Elements/WidgetTextInput.h

+ 51 - 35
Source/Core/Elements/WidgetTextInput.cpp

@@ -325,39 +325,39 @@ void WidgetTextInput::ProcessEvent(Event& event)
 		bool numlock = event.GetParameter< int >("num_lock_key", 0) > 0;
 		bool shift = event.GetParameter< int >("shift_key", 0) > 0;
 		bool ctrl = event.GetParameter< int >("ctrl_key", 0) > 0;
+		bool selection_changed = false;
 
 		switch (key_identifier)
 		{
+		// clang-format off
 		case Input::KI_NUMPAD4: if (numlock) break; //-fallthrough
-		case Input::KI_LEFT:    MoveCursorHorizontal(ctrl ? CursorMovement::PreviousWord : CursorMovement::Left, shift); break;
+		case Input::KI_LEFT:    selection_changed = MoveCursorHorizontal(ctrl ? CursorMovement::PreviousWord : CursorMovement::Left, shift); break;
 
 		case Input::KI_NUMPAD6: if (numlock) break; //-fallthrough
-		case Input::KI_RIGHT:   MoveCursorHorizontal(ctrl ? CursorMovement::NextWord : CursorMovement::Right, shift); break;
+		case Input::KI_RIGHT:   selection_changed = MoveCursorHorizontal(ctrl ? CursorMovement::NextWord : CursorMovement::Right, shift); break;
 
 		case Input::KI_NUMPAD8: if (numlock) break; //-fallthrough
-		case Input::KI_UP:      MoveCursorVertical(-1, shift); break;
+		case Input::KI_UP:      selection_changed = MoveCursorVertical(-1, shift); break;
 
 		case Input::KI_NUMPAD2: if (numlock) break; //-fallthrough
-		case Input::KI_DOWN:    MoveCursorVertical(1, shift); break;
+		case Input::KI_DOWN:    selection_changed = MoveCursorVertical(1, shift); break;
 
 		case Input::KI_NUMPAD7: if (numlock) break; //-fallthrough
-		case Input::KI_HOME:    MoveCursorHorizontal(ctrl ? CursorMovement::Begin : CursorMovement::BeginLine, shift); break;
+		case Input::KI_HOME:    selection_changed = MoveCursorHorizontal(ctrl ? CursorMovement::Begin : CursorMovement::BeginLine, shift); break;
 
 		case Input::KI_NUMPAD1: if (numlock) break; //-fallthrough
-		case Input::KI_END:     MoveCursorHorizontal(ctrl ? CursorMovement::End : CursorMovement::EndLine, shift); break;
+		case Input::KI_END:     selection_changed = MoveCursorHorizontal(ctrl ? CursorMovement::End : CursorMovement::EndLine, shift); break;
 
 		case Input::KI_NUMPAD9: if (numlock) break; //-fallthrough
-		case Input::KI_PRIOR:   MoveCursorVertical(-int(internal_dimensions.y / parent->GetLineHeight()) + 1, shift); break;
+		case Input::KI_PRIOR:   selection_changed = MoveCursorVertical(-int(internal_dimensions.y / parent->GetLineHeight()) + 1, shift); break;
 
 		case Input::KI_NUMPAD3: if (numlock) break; //-fallthrough
-		case Input::KI_NEXT:    MoveCursorVertical(int(internal_dimensions.y / parent->GetLineHeight()) - 1, shift); break;
+		case Input::KI_NEXT:    selection_changed = MoveCursorVertical(int(internal_dimensions.y / parent->GetLineHeight()) - 1, shift); break;
 
 		case Input::KI_BACK:
 		{
 			CursorMovement direction = (ctrl ? CursorMovement::PreviousWord : CursorMovement::Left);
-			if (DeleteCharacters(direction))
-				FormatElement();
-
+			DeleteCharacters(direction);
 			ShowCursor(true);
 		}
 		break;
@@ -366,12 +366,11 @@ void WidgetTextInput::ProcessEvent(Event& event)
 		case Input::KI_DELETE:
 		{
 			CursorMovement direction = (ctrl ? CursorMovement::NextWord : CursorMovement::Right);
-			if (DeleteCharacters(direction))
-				FormatElement();
-
+			DeleteCharacters(direction);
 			ShowCursor(true);
 		}
 		break;
+		// clang-format on
 
 		case Input::KI_NUMPADENTER:
 		case Input::KI_RETURN:
@@ -384,8 +383,8 @@ void WidgetTextInput::ProcessEvent(Event& event)
 		{
 			if (ctrl)
 			{
-				MoveCursorHorizontal(CursorMovement::Begin, false);
-				MoveCursorHorizontal(CursorMovement::End, true);
+				selection_changed = MoveCursorHorizontal(CursorMovement::Begin, false);
+				selection_changed |= MoveCursorHorizontal(CursorMovement::End, true);
 			}
 		}
 		break;
@@ -431,6 +430,8 @@ void WidgetTextInput::ProcessEvent(Event& event)
 		}
 
 		event.StopPropagation();
+		if (selection_changed)
+			FormatText();
 	}
 	break;
 
@@ -453,7 +454,8 @@ void WidgetTextInput::ProcessEvent(Event& event)
 	{
 		if (event.GetTargetElement() == parent)
 		{
-			UpdateSelection(false);
+			if (UpdateSelection(false))
+				FormatElement();
 			ShowCursor(true, false);
 		}
 	}
@@ -462,7 +464,8 @@ void WidgetTextInput::ProcessEvent(Event& event)
 	{
 		if (event.GetTargetElement() == parent)
 		{
-			ClearSelection();
+			if (ClearSelection())
+				FormatElement();
 			ShowCursor(false, false);
 		}
 	}
@@ -490,9 +493,10 @@ void WidgetTextInput::ProcessEvent(Event& event)
 			MoveCursorToCharacterBoundaries(false);
 			UpdateCursorPosition(true);
 
-			UpdateSelection(event == EventId::Drag || event.GetParameter< int >("shift_key", 0) > 0);
+			if (UpdateSelection(event == EventId::Drag || event.GetParameter<int>("shift_key", 0) > 0))
+				FormatText();
 
-			ShowCursor(true); 
+			ShowCursor(true);
 			cancel_next_drag = false;
 		}
 	}
@@ -532,11 +536,11 @@ bool WidgetTextInput::AddCharacters(String string)
 	parent->SetAttribute("value", value);
 	absolute_cursor_index += (int)string.size();
 
-	DispatchChangeEvent();
-
 	UpdateCursorPosition(true);
-	UpdateSelection(false);
+	if (UpdateSelection(false))
+		FormatText();
 
+	DispatchChangeEvent();
 	return true;
 }
 
@@ -568,7 +572,7 @@ void WidgetTextInput::CopySelection()
 }
 
 // Moves the cursor along the current line.
-void WidgetTextInput::MoveCursorHorizontal(CursorMovement movement, bool select)
+bool WidgetTextInput::MoveCursorHorizontal(CursorMovement movement, bool select)
 {
 	const String& value = GetValue();
 
@@ -664,12 +668,14 @@ void WidgetTextInput::MoveCursorHorizontal(CursorMovement movement, bool select)
 	MoveCursorToCharacterBoundaries(seek_forward);
 	UpdateCursorPosition(true);
 
-	UpdateSelection(select);
+	bool selection_changed = UpdateSelection(select);
 	ShowCursor(true);
+
+	return selection_changed;
 }
 
 // Moves the cursor up and down the text field.
-void WidgetTextInput::MoveCursorVertical(int distance, bool select)
+bool WidgetTextInput::MoveCursorVertical(int distance, bool select)
 {
 	int cursor_line_index = 0, cursor_character_index = 0;
 	GetRelativeCursorIndices(cursor_line_index, cursor_character_index);
@@ -694,8 +700,10 @@ void WidgetTextInput::MoveCursorVertical(int distance, bool select)
 	MoveCursorToCharacterBoundaries(false);
 	UpdateCursorPosition(false);
 
-	UpdateSelection(select);
+	bool selection_changed = UpdateSelection(select);
 	ShowCursor(true);
+
+	return selection_changed;
 }
 
 void WidgetTextInput::MoveCursorToCharacterBoundaries(bool forward)
@@ -768,11 +776,14 @@ void WidgetTextInput::ExpandSelection()
 	cursor_wrap_down = true;
 	absolute_cursor_index -= int(p_index - p_left);
 	MoveCursorToCharacterBoundaries(false);
-	UpdateSelection(false);
+	bool selection_changed = UpdateSelection(false);
 
 	absolute_cursor_index += int(p_right - p_left);
 	MoveCursorToCharacterBoundaries(true);
-	UpdateSelection(true);
+	selection_changed |= UpdateSelection(true);
+
+	if (selection_changed)
+		FormatText();
 
 	UpdateCursorPosition(true);
 }
@@ -1170,12 +1181,13 @@ void WidgetTextInput::UpdateCursorPosition(bool update_ideal_cursor_position)
 }
 
 // Expand the text selection to the position of the cursor.
-void WidgetTextInput::UpdateSelection(bool selecting)
+bool WidgetTextInput::UpdateSelection(bool selecting)
 {
+	bool selection_changed = false;
 	if (!selecting)
 	{
 		selection_anchor_index = absolute_cursor_index;
-		ClearSelection();
+		selection_changed = ClearSelection();
 	}
 	else
 	{
@@ -1199,19 +1211,22 @@ void WidgetTextInput::UpdateSelection(bool selecting)
 			selection_begin_index = new_begin_index;
 			selection_length = new_end_index - new_begin_index;
 
-			FormatText();
+			selection_changed = true; 
 		}
 	}
+	
+	return selection_changed;
 }
 
 // Removes the selection of text.
-void WidgetTextInput::ClearSelection()
+bool WidgetTextInput::ClearSelection()
 {
 	if (selection_length > 0)
 	{
 		selection_length = 0;
-		FormatElement();
+		return true;
 	}
+	return false;
 }
 
 // Deletes all selected text and removes the selection.
@@ -1231,7 +1246,8 @@ void WidgetTextInput::DeleteSelection()
 		UpdateCursorPosition(true);
 
 		// Erase our record of the selection.
-		UpdateSelection(false);
+		if (UpdateSelection(false))
+			FormatText();
 	}
 }
 

+ 8 - 4
Source/Core/Elements/WidgetTextInput.h

@@ -121,11 +121,13 @@ private:
 	/// Moves the cursor along the current line.
 	/// @param[in] movement Cursor movement operation.
 	/// @param[in] select True if the movement will also move the selection cursor, false if not.
-	void MoveCursorHorizontal(CursorMovement movement, bool select);
+	/// @return True if selection was changed.
+	bool MoveCursorHorizontal(CursorMovement movement, bool select);
 	/// Moves the cursor up and down the text field.
 	/// @param[in] x How far to move the cursor.
 	/// @param[in] select True if the movement will also move the selection cursor, false if not.
-	void MoveCursorVertical(int distance, bool select);
+	/// @return True if selection was changed.
+	bool MoveCursorVertical(int distance, bool select);
 	// Move the cursor to utf-8 boundaries, in case it was moved into the middle of a multibyte character.
 	/// @param[in] forward True to seek forward, else back.
 	void MoveCursorToCharacterBoundaries(bool forward);
@@ -166,9 +168,11 @@ private:
 
 	/// Expand or shrink the text selection to the position of the cursor.
 	/// @param[in] selecting True if the new position of the cursor should expand / contract the selection area, false if it should only set the anchor for future selections.
-	void UpdateSelection(bool selecting);
+	/// @return True if selection was changed.
+	bool UpdateSelection(bool selecting);
 	/// Removes the selection of text.
-	void ClearSelection();
+	/// @return True if selection was changed.
+	bool ClearSelection();
 	/// Deletes all selected text and removes the selection.
 	void DeleteSelection();
 	/// Copies the selection (if any) to the clipboard.