Skip to content

Commit

Permalink
Lock screen: Show tooltip if text is truncated
Browse files Browse the repository at this point in the history
Add tootip to LoginShelfButton when the text of the label is truncated.

Bug: 878340
Change-Id: I28441db54bb49e0ba262f820bbf9041993a36683
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1712675
Reviewed-by: Scott Violet <sky@chromium.org>
Commit-Queue: Roman Aleksandrov <raleksandrov@google.com>
Cr-Commit-Position: refs/heads/master@{#685825}
  • Loading branch information
Roman Aleksandrov authored and Commit Bot committed Aug 10, 2019
1 parent 9c2674b commit 105fbab
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ash/shelf/login_shelf_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ class LoginShelfButton : public views::LabelButton {
return ink_drop;
}

base::string16 GetTooltipText(const gfx::Point& p) const override {
if (label()->IsDisplayTextTruncated())
return label()->GetText();
return base::string16();
}

void PaintDarkColors() {
SetEnabledTextColors(gfx::kGoogleGrey600);
SetImage(views::Button::STATE_NORMAL,
Expand Down
11 changes: 11 additions & 0 deletions ui/views/controls/label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ void Label::SetObscured(bool obscured) {
OnPropertyChanged(&full_text_ + kLabelObscured, kPropertyEffectsLayout);
}

bool Label::IsDisplayTextTruncated() const {
MaybeBuildDisplayText();
if (!full_text_ || full_text_->text().empty())
return false;
auto text_bounds = GetTextBounds();
return (display_text_ &&
display_text_->text() != display_text_->GetDisplayText()) ||
text_bounds.width() > GetContentsBounds().width() ||
text_bounds.height() > GetContentsBounds().height();
}

bool Label::GetAllowCharacterBreak() const {
return full_text_->word_wrap_behavior() == gfx::WRAP_LONG_WORDS ? true
: false;
Expand Down
5 changes: 5 additions & 0 deletions ui/views/controls/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class VIEWS_EXPORT Label : public View,
bool GetObscured() const;
void SetObscured(bool obscured);

// Returns true if some portion of the text is not displayed, either because
// of eliding or clipping.
bool IsDisplayTextTruncated() const;

// Gets/Sets whether multi-line text can wrap mid-word; the default is false.
// TODO(mukai): allow specifying WordWrapBehavior.
bool GetAllowCharacterBreak() const;
Expand Down Expand Up @@ -280,6 +284,7 @@ class VIEWS_EXPORT Label : public View,
FRIEND_TEST_ALL_PREFIXES(LabelTest, EmptyLabel);
FRIEND_TEST_ALL_PREFIXES(LabelTest, FocusBounds);
FRIEND_TEST_ALL_PREFIXES(LabelTest, MultiLineSizingWithElide);
FRIEND_TEST_ALL_PREFIXES(LabelTest, IsDisplayTextTruncated);
friend class LabelSelectionTest;

// ContextMenuController overrides:
Expand Down
27 changes: 27 additions & 0 deletions ui/views/controls/label_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,33 @@ TEST_F(LabelTest, DefaultDirectionalityIsFromText) {
rtl.GetTextDirectionForTesting());
}

TEST_F(LabelTest, IsDisplayTextTruncated) {
const base::string16 text = ASCIIToUTF16("A random string");
label()->SetText(text);

gfx::Size zero_size;
label()->SetElideBehavior(gfx::ELIDE_TAIL);
label()->SetBoundsRect(gfx::Rect(zero_size));
EXPECT_TRUE(label()->IsDisplayTextTruncated());

label()->SetElideBehavior(gfx::NO_ELIDE);
EXPECT_TRUE(label()->IsDisplayTextTruncated());

gfx::Size minimum_size(1, 1);
label()->SetBoundsRect(gfx::Rect(minimum_size));
EXPECT_TRUE(label()->IsDisplayTextTruncated());

gfx::Size enough_size(100, 100);
label()->SetBoundsRect(gfx::Rect(enough_size));
EXPECT_FALSE(label()->IsDisplayTextTruncated());

const base::string16 empty_text;
label()->SetText(empty_text);
EXPECT_FALSE(label()->IsDisplayTextTruncated());
label()->SetBoundsRect(gfx::Rect(zero_size));
EXPECT_FALSE(label()->IsDisplayTextTruncated());
}

TEST_F(LabelSelectionTest, Selectable) {
// By default, labels don't support text selection.
EXPECT_FALSE(label()->GetSelectable());
Expand Down

0 comments on commit 105fbab

Please sign in to comment.