Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: chrome/browser/ui/views/profiles/profile_indicator_icon.cc

Issue 1972033002: Simplify some old avatar menu button code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reinstate ash browser test fix Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/profiles/profile_indicator_icon.h"
6
7 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
8 #include "ui/gfx/canvas.h"
9
10 ProfileIndicatorIcon::ProfileIndicatorIcon() : old_height_(0) {
11 // In RTL mode, the incognito icon should be looking the opposite direction.
12 EnableCanvasFlippingForRTLUI(true);
13 }
14
15 ProfileIndicatorIcon::~ProfileIndicatorIcon() {}
16
17 void ProfileIndicatorIcon::OnPaint(gfx::Canvas* canvas) {
18 if (base_icon_.IsEmpty())
19 return;
20
21 if (old_height_ != height() || modified_icon_.isNull()) {
22 old_height_ = height();
23 modified_icon_ = *profiles::GetAvatarIconForTitleBar(base_icon_, false,
24 width(), height())
25 .ToImageSkia();
26 }
27
28 // Scale the image to fit the width of the button.
29 int dst_width = std::min(modified_icon_.width(), width());
30 // Truncate rather than rounding, so that for odd widths we put the extra
31 // pixel on the left.
32 int dst_x = (width() - dst_width) / 2;
33
34 // Scale the height and maintain aspect ratio. This means that the
35 // icon may not fit in the view. That's ok, we just vertically center it.
36 float scale = static_cast<float>(dst_width) /
37 static_cast<float>(modified_icon_.width());
38 // Round here so that we minimize the aspect ratio drift.
39 int dst_height = std::round(modified_icon_.height() * scale);
40 // Round rather than truncating, so that for odd heights we select an extra
41 // pixel below the image center rather than above. This is because the
42 // incognito image has shadows at the top that make the apparent center below
43 // the real center.
44 int dst_y = std::round((height() - dst_height) / 2.0f);
45 canvas->DrawImageInt(modified_icon_, 0, 0, modified_icon_.width(),
46 modified_icon_.height(), dst_x, dst_y, dst_width,
47 dst_height, false);
48 }
49
50 void ProfileIndicatorIcon::SetIcon(const gfx::Image& icon) {
51 base_icon_ = icon;
52 modified_icon_ = gfx::ImageSkia();
53 SchedulePaint();
54 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/profiles/profile_indicator_icon.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698