Skip to content

Commit

Permalink
Reland "Resize sign out button when it's too long."
Browse files Browse the repository at this point in the history
This is a reland of 181414e

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/1085327
Reviewed-by: Tetsui Ohkubo <tetsui@chromium.org>
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Commit-Queue: Tatsuhisa Yamaguchi <yamaguchi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564796}
  • Loading branch information
Tatsuhisa Yamaguchi authored and Commit Bot committed Jun 6, 2018
1 parent 70fa259 commit a059207
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ash/ash_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -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.">
Expand Down
45 changes: 26 additions & 19 deletions ash/system/unified/top_shortcuts_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -78,16 +71,29 @@ 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);
if (!child->visible())
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;
}
}

Expand All @@ -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() !=
Expand All @@ -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);
Expand Down
23 changes: 22 additions & 1 deletion ash/system/unified/top_shortcuts_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit a059207

Please sign in to comment.