Commit a0592070 authored by Tatsuhisa Yamaguchi's avatar Tatsuhisa Yamaguchi Committed by Commit Bot

Reland "Resize sign out button when it's too long."

This is a reland of 181414e9

The original change was reverted by:
https://chromium-review.googlesource.com/c/chromium/src/+/1085067

> ==20322==ERROR: LeakSanitizer: detected memory leaks
> Direct leak of 496 byte(s) in 1 object(s) allocated from:
>     #0 0x69b3d2 in operator new(unsigned long) /b/build/slave/linux_upload_clang/build/src/third_party/llvm/compiler-rt/lib/asan/asan_new_delete.cc:93:3
>     #1 0x65a6dca in ash::TopShortcutsView::TopShortcutsView(ash::UnifiedSystemTrayController*) ash/system/unified/top_shortcuts_view.cc:130:43


Original change's description:
> Resize sign out button when it's too long.
>
> Limits the size of the sign-out / sign-out-all button to keep other
> buttons and minimum spacing in the parent view. This is needed because
> the translated text resource can be very long. Also adding description
> for such text resources to keep them shorter in the next revision.
>
> Bug: 847100
> Change-Id: I8fc79b0b0d3eb661553323a2d9f094f02320fa0a
> Reviewed-on: https://chromium-review.googlesource.com/1080364
> Commit-Queue: Tatsuhisa Yamaguchi <yamaguchi@chromium.org>
> Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
> Reviewed-by: Tetsui Ohkubo <tetsui@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#564016}

Bug: 847100
Change-Id: Ic2641bf8d61d837c56072988033f68f31f3d5da8
Reviewed-on: https://chromium-review.googlesource.com/1085327Reviewed-by: default avatarTetsui Ohkubo <tetsui@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Tatsuhisa Yamaguchi <yamaguchi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564796}
parent 70fa2591
......@@ -223,10 +223,10 @@ This file contains the strings for ash.
<message name="IDS_ASH_STATUS_TRAY_USER_INFO_ACCESSIBILITY" desc="The accessibility string used for an item in user chooser that tells the user name and the mail address.">
<ph name="USERNAME">$1<ex>Jane Doe</ex></ph> <ph name="MAIL">$2<ex>janedoe@example.com</ex></ph>
</message>
<message name="IDS_ASH_STATUS_TRAY_SIGN_OUT" desc="The label used for the button in the status tray to sign out of the system.">
<message name="IDS_ASH_STATUS_TRAY_SIGN_OUT" desc="The label used for the button in the status tray to sign out of the system. Should not exceed about 20 latin characters. Overflowed text is truncated with ellipsis.">
Sign out
</message>
<message name="IDS_ASH_STATUS_TRAY_SIGN_OUT_ALL" desc="The label used for the button in the status tray to sign out all users of the system.">
<message name="IDS_ASH_STATUS_TRAY_SIGN_OUT_ALL" desc="The label used for the button in the status tray to sign out all users of the system. Should not exceed about 20 latin characters. Overflowed text is truncated with ellipsis.">
Sign out all
</message>
<message name="IDS_ASH_STATUS_TRAY_GUEST_LABEL" desc="The label used in the system tray's user card to indicate that the current session is a guest session.">
......
......@@ -43,18 +43,11 @@ UserAvatarButton::UserAvatarButton(views::ButtonListener* listener)
SetFocusForPlatform();
}
class TopShortcutButtonContainer : public views::View {
public:
TopShortcutButtonContainer();
~TopShortcutButtonContainer() override;
} // namespace
// views::View:
void Layout() override;
gfx::Size CalculatePreferredSize() const override;
TopShortcutButtonContainer::TopShortcutButtonContainer() = default;
private:
DISALLOW_COPY_AND_ASSIGN(TopShortcutButtonContainer);
};
TopShortcutButtonContainer::~TopShortcutButtonContainer() = default;
// Buttons are equally spaced by the default value, but the gap will be
// narrowed evenly when the parent view is not large enough.
......@@ -78,6 +71,17 @@ void TopShortcutButtonContainer::Layout() {
(child_area.width() - total_horizontal_size) /
(num_visible - 1)));
int sign_out_button_width = 0;
if (sign_out_button_ && sign_out_button_->visible()) {
// resize the sign-out button
int remainder = child_area.width() -
(num_visible - 1) * kUnifiedTopShortcutButtonMinSpacing -
total_horizontal_size +
sign_out_button_->GetPreferredSize().width();
sign_out_button_width = std::max(
0, std::min(sign_out_button_->GetPreferredSize().width(), remainder));
}
int horizontal_position = child_area.x();
for (int i = 0; i < child_count(); i++) {
views::View* child = child_at(i);
......@@ -85,9 +89,11 @@ void TopShortcutButtonContainer::Layout() {
continue;
gfx::Rect bounds(child_area);
bounds.set_x(horizontal_position);
bounds.set_width(child->GetPreferredSize().width());
int width = (child == sign_out_button_) ? sign_out_button_width
: child->GetPreferredSize().width();
bounds.set_width(width);
child->SetBoundsRect(bounds);
horizontal_position += child->GetPreferredSize().width() + spacing;
horizontal_position += width + spacing;
}
}
......@@ -114,20 +120,21 @@ gfx::Size TopShortcutButtonContainer::CalculatePreferredSize() const {
return gfx::Size(width, max_height);
}
TopShortcutButtonContainer::~TopShortcutButtonContainer() = default;
TopShortcutButtonContainer::TopShortcutButtonContainer() = default;
} // namespace
void TopShortcutButtonContainer::AddSignOutButton(
views::View* sign_out_button) {
AddChildView(sign_out_button);
sign_out_button_ = sign_out_button;
}
TopShortcutsView::TopShortcutsView(UnifiedSystemTrayController* controller)
: controller_(controller), container_(new TopShortcutButtonContainer()) {
: controller_(controller) {
DCHECK(controller_);
auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::kHorizontal, kUnifiedTopShortcutPadding,
kUnifiedTopShortcutSpacing));
layout->set_cross_axis_alignment(views::BoxLayout::CROSS_AXIS_ALIGNMENT_END);
container_ = new TopShortcutButtonContainer();
AddChildView(container_);
if (Shell::Get()->session_controller()->login_status() !=
......@@ -142,7 +149,7 @@ TopShortcutsView::TopShortcutsView(UnifiedSystemTrayController* controller)
const bool can_show_web_ui = TrayPopupUtils::CanOpenWebUISettings();
sign_out_button_ = new SignOutButton(this);
container_->AddChildView(sign_out_button_);
container_->AddSignOutButton(sign_out_button_);
lock_button_ = new TopShortcutButton(this, kSystemMenuLockIcon,
IDS_ASH_STATUS_TRAY_LOCK);
......
......@@ -17,6 +17,27 @@ class TopShortcutButton;
class TopShortcutsViewTest;
class UnifiedSystemTrayController;
// Container for the top shortcut buttons. The view may narrow gaps between
// buttons when there's not enough space. When those doesn't fit in the view
// even after that, the sign-out button will be resized.
class TopShortcutButtonContainer : public views::View {
public:
TopShortcutButtonContainer();
~TopShortcutButtonContainer() override;
// views::View:
void Layout() override;
gfx::Size CalculatePreferredSize() const override;
// Add the sign-out button, which can be resized upon layout.
void AddSignOutButton(views::View* sign_out_button);
private:
views::View* sign_out_button_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(TopShortcutButtonContainer);
};
// Top shortcuts view shown on the top of UnifiedSystemTrayView.
class ASH_EXPORT TopShortcutsView : public views::View,
public views::ButtonListener {
......@@ -38,7 +59,7 @@ class ASH_EXPORT TopShortcutsView : public views::View,
// Owned by views hierarchy.
views::Button* user_avatar_button_ = nullptr;
SignOutButton* sign_out_button_ = nullptr;
views::View* const container_;
TopShortcutButtonContainer* container_ = nullptr;
TopShortcutButton* lock_button_ = nullptr;
TopShortcutButton* settings_button_ = nullptr;
TopShortcutButton* power_button_ = nullptr;
......
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