Commit 3a0f2975 authored by Tessa Nijssen's avatar Tessa Nijssen Committed by Commit Bot

Set Horizontal Alignment for Textfields

Previously, when a Textfield instance changed its text direction, no
change was made to its horizontal alignment. Now,
ChangeTextDirectionAndLayoutAlignment() calls
SetHorizontalAlignment() with the correct alignment based on
text direction.

Text direction   Horizontal alignment
Right-to-left -> Aligned right
Left-to-right -> Aligned left

This fixes a bug where toggling RTL in a Textfield did not change the
text writing direction.

A unit test was also added to test
ChangeTextDirectionAndLayoutAlignment(). This test checks that a call
to the method properly updates the textfield's text direction and
horizontal alignment.

Bug: 841123
Change-Id: I8dadc1272a3474dc72ae9470bdc7d9949e8fd854
Reviewed-on: https://chromium-review.googlesource.com/1060626
Commit-Queue: Tessa Nijssen <tnijssen@google.com>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarSarah Chan <spqchan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#559397}
parent 555b6835
......@@ -1637,13 +1637,14 @@ bool Textfield::ChangeTextDirectionAndLayoutAlignment(
// Restore text directionality mode when the indicated direction matches the
// current forced mode; otherwise, force the mode indicated. This helps users
// manage BiDi text layout without getting stuck in forced LTR or RTL modes.
const gfx::DirectionalityMode mode = direction == base::i18n::RIGHT_TO_LEFT
? gfx::DIRECTIONALITY_FORCE_RTL
: gfx::DIRECTIONALITY_FORCE_LTR;
if (mode == GetRenderText()->directionality_mode())
GetRenderText()->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT);
else
GetRenderText()->SetDirectionalityMode(mode);
const bool default_rtl = direction == base::i18n::RIGHT_TO_LEFT;
const auto new_mode = default_rtl ? gfx::DIRECTIONALITY_FORCE_RTL
: gfx::DIRECTIONALITY_FORCE_LTR;
auto* render_text = GetRenderText();
const bool modes_match = new_mode == render_text->directionality_mode();
render_text->SetDirectionalityMode(modes_match ? gfx::DIRECTIONALITY_FROM_TEXT
: new_mode);
SetHorizontalAlignment(default_rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT);
SchedulePaint();
return true;
}
......
......@@ -3643,4 +3643,31 @@ TEST_F(TextfieldTest, FocusReasonFocusBlurFocus) {
textfield_->GetFocusReason());
}
TEST_F(TextfieldTest, ChangeTextDirectionAndLayoutAlignmentTest) {
InitTextfield();
textfield_->ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection::RIGHT_TO_LEFT);
EXPECT_EQ(textfield_->GetTextDirection(),
base::i18n::TextDirection::RIGHT_TO_LEFT);
EXPECT_EQ(textfield_->GetHorizontalAlignment(),
gfx::HorizontalAlignment::ALIGN_RIGHT);
textfield_->ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection::RIGHT_TO_LEFT);
const base::string16& text = test_api_->GetRenderText()->GetDisplayText();
base::i18n::TextDirection text_direction =
base::i18n::GetFirstStrongCharacterDirection(text);
EXPECT_EQ(textfield_->GetTextDirection(), text_direction);
EXPECT_EQ(textfield_->GetHorizontalAlignment(),
gfx::HorizontalAlignment::ALIGN_RIGHT);
textfield_->ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection::LEFT_TO_RIGHT);
EXPECT_EQ(textfield_->GetTextDirection(),
base::i18n::TextDirection::LEFT_TO_RIGHT);
EXPECT_EQ(textfield_->GetHorizontalAlignment(),
gfx::HorizontalAlignment::ALIGN_LEFT);
}
} // namespace views
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment