| 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 SkColor kTextureBackground = 0x00AAAAAA; |
| 22 static constexpr SkColor kBackground = 0xCCAAAAAA; |
| 23 static constexpr SkColor kBackgroundHover = 0xCCDDDDDD; |
| 24 static constexpr SkColor kForeground = 0xCC444444; |
| 25 static constexpr SkColor kSeparatorColor = |
| 26 SkColorSetARGBMacro(256 * 0.2, 0, 0, 0); |
| 27 |
| 28 static constexpr float kWidth = 0.672; |
| 29 static constexpr float kHeight = 0.088; |
| 30 static constexpr float kFontHeight = 0.027; |
| 31 static constexpr float kBackButtonWidth = kHeight; |
| 32 static constexpr float kBackIconHeight = 0.05; |
| 33 static constexpr float kBackIconOffset = 0.005; |
| 34 static constexpr float kSecurityFieldWidth = 0.06; |
| 35 static constexpr float kSecurityIconHeight = 0.03; |
| 36 static constexpr float kUrlRightMargin = 0.02; |
| 37 static constexpr float kSeparatorWidth = 0.002; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 UrlBarTexture::UrlBarTexture() = default; |
| 42 |
| 43 UrlBarTexture::~UrlBarTexture() = default; |
| 44 |
| 45 void UrlBarTexture::SetHover(bool hover) { |
| 46 hover_ = hover; |
| 47 } |
| 48 |
| 49 void UrlBarTexture::SetURL(const GURL& gurl) { |
| 50 gurl_ = gurl; |
| 51 } |
| 52 |
| 53 float UrlBarTexture::ToPixels(float meters) const { |
| 54 return meters * size_.width() / kWidth; |
| 55 } |
| 56 |
| 57 void UrlBarTexture::Draw(SkCanvas* canvas, const gfx::Size& texture_size) { |
| 58 size_.set_height(texture_size.height()); |
| 59 size_.set_width(texture_size.width()); |
| 60 |
| 61 canvas->save(); |
| 62 canvas->scale(size_.width() / kWidth, size_.width() / kWidth); |
| 63 |
| 64 // Make a gfx canvas to support utility drawing methods. |
| 65 cc::SkiaPaintCanvas paint_canvas(canvas); |
| 66 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); |
| 67 |
| 68 canvas->drawColor(kTextureBackground); |
| 69 |
| 70 // Back button area. |
| 71 SkRRect round_rect; |
| 72 SkVector rounded_corner = {kHeight / 2, kHeight / 2}; |
| 73 SkVector left_corners[4] = {rounded_corner, {0, 0}, {0, 0}, rounded_corner}; |
| 74 round_rect.setRectRadii({0, 0, kHeight, kHeight}, left_corners); |
| 75 SkPaint paint; |
| 76 paint.setColor(hover_ ? kBackgroundHover : kBackground); |
| 77 canvas->drawRRect(round_rect, paint); |
| 78 |
| 79 // URL area. |
| 80 paint.setColor(kBackground); |
| 81 SkVector right_corners[4] = {{0, 0}, rounded_corner, rounded_corner, {0, 0}}; |
| 82 round_rect.setRectRadii({kHeight, 0, kWidth, kHeight}, right_corners); |
| 83 canvas->drawRRect(round_rect, paint); |
| 84 |
| 85 // Back button / URL separator vertical line. |
| 86 paint.setColor(kSeparatorColor); |
| 87 canvas->drawRect(SkRect::MakeXYWH(kHeight, 0, kSeparatorWidth, kHeight), |
| 88 paint); |
| 89 |
| 90 // Back button icon. |
| 91 canvas->save(); |
| 92 canvas->translate(kHeight / 2 + kBackIconOffset, kHeight / 2); |
| 93 canvas->translate(-kBackIconHeight / 2, -kBackIconHeight / 2); |
| 94 int icon_default_height = GetDefaultSizeOfVectorIcon(ui::kBackArrowIcon); |
| 95 float icon_scale = kBackIconHeight / icon_default_height; |
| 96 canvas->scale(icon_scale, icon_scale); |
| 97 PaintVectorIcon(&gfx_canvas, ui::kBackArrowIcon, kForeground); |
| 98 canvas->restore(); |
| 99 |
| 100 // Site security state icon. |
| 101 // TODO(cjgrant): Plug in the correct icons based on security level. |
| 102 canvas->save(); |
| 103 canvas->translate( |
| 104 kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth / 2, |
| 105 kHeight / 2); |
| 106 canvas->translate(-kSecurityIconHeight / 2, -kSecurityIconHeight / 2); |
| 107 icon_default_height = GetDefaultSizeOfVectorIcon(ui::kBackArrowIcon); |
| 108 icon_scale = kSecurityIconHeight / icon_default_height; |
| 109 canvas->scale(icon_scale, icon_scale); |
| 110 PaintVectorIcon(&gfx_canvas, ui::kBackArrowIcon, kForeground); |
| 111 canvas->restore(); |
| 112 |
| 113 canvas->restore(); |
| 114 |
| 115 // Draw text based on pixel sizes rather than meters, for correct font sizing. |
| 116 int pixel_font_height = texture_size.height() * kFontHeight / kHeight; |
| 117 int text_flags = gfx::Canvas::TEXT_ALIGN_LEFT; |
| 118 float url_x = kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth; |
| 119 float url_width = kWidth - url_x - kUrlRightMargin; |
| 120 gfx_canvas.DrawStringRectWithFlags( |
| 121 base::UTF8ToUTF16(gurl_.spec()), GetDefaultFontList(pixel_font_height), |
| 122 SK_ColorBLACK, |
| 123 gfx::Rect(ToPixels(url_x), 0, ToPixels(url_width), ToPixels(kHeight)), |
| 124 text_flags); |
| 125 } |
| 126 |
| 127 gfx::Size UrlBarTexture::GetPreferredTextureSize(int maximum_width) const { |
| 128 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); |
| 129 } |
| 130 |
| 131 gfx::SizeF UrlBarTexture::GetDrawnSize() const { |
| 132 return size_; |
| 133 } |
| 134 |
| 135 } // namespace vr_shell |
| OLD | NEW |