Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/android/vr_shell/textures/url_bar_texture.h" | |
| 6 | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "cc/paint/skia_paint_canvas.h" | |
| 9 #include "ui/gfx/canvas.h" | |
| 10 #include "ui/gfx/font.h" | |
| 11 #include "ui/gfx/font_list.h" | |
| 12 #include "ui/gfx/geometry/rect.h" | |
| 13 #include "ui/gfx/paint_vector_icon.h" | |
| 14 #include "ui/gfx/vector_icon_types.h" | |
| 15 #include "ui/vector_icons/vector_icons.h" | |
| 16 | |
| 17 namespace vr_shell { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 static constexpr char kDefaultFontFamily[] = "sans-serif"; | |
|
acondor_
2017/05/05 19:06:41
In order to avoid this duplicated string, maybe we
cjgrant
2017/05/08 14:17:41
Will do.
cjgrant
2017/05/08 15:10:24
Done.
| |
| 22 | |
| 23 static constexpr SkColor kTextureBackground = 0x00AAAAAA; | |
| 24 static constexpr SkColor kBackground = 0xCCAAAAAA; | |
| 25 static constexpr SkColor kBackgroundHover = 0xCCDDDDDD; | |
| 26 static constexpr SkColor kForeground = 0xCC444444; | |
| 27 static constexpr SkColor kSeparatorColor = | |
| 28 SkColorSetARGBMacro(256 * 0.2, 0, 0, 0); | |
| 29 | |
| 30 static constexpr float kWidth = 0.672; | |
| 31 static constexpr float kHeight = 0.088; | |
| 32 static constexpr float kFontHeight = 0.027; | |
| 33 static constexpr float kBackButtonWidth = kHeight; | |
| 34 static constexpr float kBackIconHeight = 0.05; | |
| 35 static constexpr float kBackIconOffset = 0.005; | |
| 36 static constexpr float kSecurityFieldWidth = 0.06; | |
| 37 static constexpr float kSecurityIconHeight = 0.03; | |
| 38 static constexpr float kUrlRightMargin = 0.02; | |
| 39 static constexpr float kSeparatorWidth = 0.002; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 UrlBarTexture::UrlBarTexture() = default; | |
| 44 | |
| 45 UrlBarTexture::~UrlBarTexture() = default; | |
| 46 | |
| 47 void UrlBarTexture::SetHover(bool hover) { | |
| 48 hover_ = hover; | |
| 49 } | |
| 50 | |
| 51 void UrlBarTexture::SetGURL(const GURL& gurl) { | |
| 52 gurl_ = gurl; | |
| 53 } | |
| 54 | |
| 55 float UrlBarTexture::ToPixels(float meters) const { | |
| 56 return meters * size_.width() / kWidth; | |
| 57 } | |
| 58 | |
| 59 void UrlBarTexture::Draw(SkCanvas* canvas, const gfx::Size& texture_size) { | |
| 60 size_.set_height(texture_size.height()); | |
| 61 size_.set_width(texture_size.width()); | |
| 62 | |
| 63 canvas->save(); | |
| 64 canvas->scale(size_.width() / kWidth, size_.width() / kWidth); | |
| 65 | |
| 66 // Make a gfx canvas to support utility drawing methods. | |
| 67 cc::SkiaPaintCanvas paint_canvas(canvas); | |
| 68 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); | |
| 69 | |
| 70 canvas->drawColor(kTextureBackground); | |
| 71 | |
| 72 SkPaint paint; | |
| 73 // TODO(cjgrant): Determine if AA is too heavy. | |
| 74 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
| 75 | |
| 76 // Back button area. | |
| 77 SkRRect round_rect; | |
| 78 SkVector rounded_corner = {kHeight / 2, kHeight / 2}; | |
| 79 SkVector left_corners[4] = {rounded_corner, {0, 0}, {0, 0}, rounded_corner}; | |
| 80 round_rect.setRectRadii({0, 0, kHeight, kHeight}, left_corners); | |
| 81 paint.setColor(hover_ ? kBackgroundHover : kBackground); | |
| 82 canvas->drawRRect(round_rect, paint); | |
| 83 | |
| 84 // URL area. | |
| 85 paint.setColor(kBackground); | |
| 86 SkVector right_corners[4] = {{0, 0}, rounded_corner, rounded_corner, {0, 0}}; | |
| 87 round_rect.setRectRadii({kHeight, 0, kWidth, kHeight}, right_corners); | |
| 88 canvas->drawRRect(round_rect, paint); | |
| 89 | |
| 90 // Back button / URL separator vertical line. | |
| 91 paint.setColor(kSeparatorColor); | |
| 92 canvas->drawRect(SkRect::MakeXYWH(kHeight, 0, kSeparatorWidth, kHeight), | |
| 93 paint); | |
| 94 | |
| 95 // Back button icon. | |
| 96 canvas->save(); | |
| 97 canvas->translate(kHeight / 2 + kBackIconOffset, kHeight / 2); | |
| 98 canvas->translate(-kBackIconHeight / 2, -kBackIconHeight / 2); | |
| 99 int icon_default_height = GetDefaultSizeOfVectorIcon(ui::kBackArrowIcon); | |
| 100 float icon_scale = kBackIconHeight / icon_default_height; | |
| 101 canvas->scale(icon_scale, icon_scale); | |
| 102 PaintVectorIcon(&gfx_canvas, ui::kBackArrowIcon, kForeground); | |
| 103 canvas->restore(); | |
| 104 | |
| 105 // Site security state icon. | |
| 106 // TODO(cjgrant): Plug in the correct icons based on security level. | |
|
acondor_
2017/05/05 19:06:41
Any specific reason for delaying this for later CL
cjgrant
2017/05/08 14:17:41
A few reasons:
- All icons weren't present until I
| |
| 107 canvas->save(); | |
| 108 canvas->translate( | |
| 109 kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth / 2, | |
| 110 kHeight / 2); | |
| 111 canvas->translate(-kSecurityIconHeight / 2, -kSecurityIconHeight / 2); | |
| 112 icon_default_height = GetDefaultSizeOfVectorIcon(ui::kBackArrowIcon); | |
| 113 icon_scale = kSecurityIconHeight / icon_default_height; | |
| 114 canvas->scale(icon_scale, icon_scale); | |
| 115 PaintVectorIcon(&gfx_canvas, ui::kBackArrowIcon, kForeground); | |
| 116 canvas->restore(); | |
| 117 | |
| 118 canvas->restore(); | |
| 119 | |
| 120 // Draw text based on pixel sizes rather than meters, for correct font sizing. | |
| 121 int pixel_font_height = texture_size.height() * kFontHeight / kHeight; | |
| 122 gfx::Font default_font(kDefaultFontFamily, pixel_font_height); | |
| 123 int text_flags = gfx::Canvas::TEXT_ALIGN_LEFT; | |
| 124 float url_x = kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth; | |
| 125 float url_width = kWidth - url_x - kUrlRightMargin; | |
| 126 gfx_canvas.DrawStringRectWithFlags( | |
| 127 base::UTF8ToUTF16(gurl_.spec()), gfx::FontList(default_font), | |
| 128 SK_ColorBLACK, | |
| 129 gfx::Rect(ToPixels(url_x), 0, ToPixels(url_width), ToPixels(kHeight)), | |
| 130 text_flags); | |
| 131 } | |
| 132 | |
| 133 gfx::Size UrlBarTexture::GetPreferredTextureSize(int maximum_width) const { | |
| 134 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); | |
| 135 } | |
| 136 | |
| 137 gfx::SizeF UrlBarTexture::GetDrawnSize() const { | |
| 138 return size_; | |
| 139 } | |
| 140 | |
| 141 } // namespace vr_shell | |
| OLD | NEW |