Chromium Code Reviews| Index: chrome/browser/ui/views/profiles/profile_indicator_icon.cc |
| diff --git a/chrome/browser/ui/views/profiles/profile_indicator_icon.cc b/chrome/browser/ui/views/profiles/profile_indicator_icon.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3375f615a4662582e66eee54e49d419189abb920 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/profiles/profile_indicator_icon.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
Peter Kasting
2016/05/17 03:51:43
Nit: 2016
Evan Stade
2016/05/17 18:13:09
this is just a trimmed down version of avatar_menu
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/views/profiles/profile_indicator_icon.h" |
| + |
| +#include <stddef.h> |
| + |
| +#include "base/command_line.h" |
| +#include "build/build_config.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/profiles/avatar_menu.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_attributes_entry.h" |
| +#include "chrome/browser/profiles/profile_attributes_storage.h" |
| +#include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/profiles/profile_metrics.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| +#include "chrome/browser/ui/views/frame/browser_view.h" |
| +#include "chrome/browser/ui/views/profiles/profile_chooser_view.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/signin/core/common/profile_management_switches.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "grit/theme_resources.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/views/view_targeter.h" |
| +#include "ui/views/widget/widget.h" |
|
Peter Kasting
2016/05/17 03:51:43
Surely we don't need all these #includes?
Evan Stade
2016/05/17 18:13:09
Done.
|
| + |
| +static inline int Round(double x) { |
|
Peter Kasting
2016/05/17 03:51:43
Use std::round() instead.
Evan Stade
2016/05/17 18:13:09
Done.
|
| + return static_cast<int>(x + 0.5); |
| +} |
| + |
| +ProfileIndicatorIcon::ProfileIndicatorIcon(BrowserView* browser_view) |
| + : browser_view_(browser_view), old_height_(0) { |
| + // In RTL mode, the avatar icon should be looking the opposite direction. |
|
Peter Kasting
2016/05/17 03:51:43
Nit: avatar -> profile (or nothing)
Evan Stade
2016/05/17 18:13:09
removed --- I don't think we want this anywhere ex
|
| + EnableCanvasFlippingForRTLUI(true); |
| +} |
| + |
| +ProfileIndicatorIcon::~ProfileIndicatorIcon() {} |
| + |
| +void ProfileIndicatorIcon::OnPaint(gfx::Canvas* canvas) { |
| + if (base_icon_.IsEmpty()) |
| + return; |
| + |
| + if (old_height_ != height() || modified_icon_.isNull()) { |
| + old_height_ = height(); |
| + modified_icon_ = *profiles::GetAvatarIconForTitleBar(base_icon_, false, |
| + width(), height()) |
| + .ToImageSkia(); |
| + } |
| + |
| + // Scale the image to fit the width of the button. |
| + int dst_width = std::min(modified_icon_.width(), width()); |
| + // Truncate rather than rounding, so that for odd widths we put the extra |
| + // pixel on the left. |
| + int dst_x = (width() - dst_width) / 2; |
| + |
| + // Scale the height and maintain aspect ratio. This means that the |
| + // icon may not fit in the view. That's ok, we just vertically center it. |
| + float scale = static_cast<float>(dst_width) / |
| + static_cast<float>(modified_icon_.width()); |
| + // Round here so that we minimize the aspect ratio drift. |
| + int dst_height = Round(modified_icon_.height() * scale); |
| + // Round rather than truncating, so that for odd heights we select an extra |
| + // pixel below the image center rather than above. This is because the |
| + // incognito image has shadows at the top that make the apparent center below |
| + // the real center. |
| + int dst_y = Round((height() - dst_height) / 2.0); |
|
Peter Kasting
2016/05/17 03:51:43
Nit: 2.0f?
Evan Stade
2016/05/17 18:13:09
Done.
|
| + canvas->DrawImageInt(modified_icon_, 0, 0, modified_icon_.width(), |
| + modified_icon_.height(), dst_x, dst_y, dst_width, |
| + dst_height, false); |
| +} |
| + |
| +void ProfileIndicatorIcon::SetIcon(const gfx::Image& icon) { |
| + base_icon_ = icon; |
| + modified_icon_ = gfx::ImageSkia(); |
| + SchedulePaint(); |
| +} |