OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/toolbar/site_chip_view.h" |
| 6 |
| 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/search/search.h" |
| 12 #include "chrome/browser/themes/theme_properties.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 15 #include "chrome/browser/ui/toolbar/toolbar_model.h" |
| 16 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 17 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "grit/theme_resources.h" |
| 21 #include "net/base/net_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/base/theme_provider.h" |
| 24 #include "ui/views/background.h" |
| 25 #include "ui/views/controls/button/label_button.h" |
| 26 #include "ui/views/controls/button/label_button_border.h" |
| 27 #include "ui/views/controls/label.h" |
| 28 #include "ui/views/painter.h" |
| 29 |
| 30 const int kEdgeThickness = 4; |
| 31 const int kIconTextSpacing = 4; |
| 32 const int kTrailingLabelMargin = 2; |
| 33 |
| 34 SiteChipView::SiteChipView(ToolbarView* toolbar_view) |
| 35 : ToolbarButton(this, NULL), |
| 36 toolbar_view_(toolbar_view) { |
| 37 } |
| 38 |
| 39 SiteChipView::~SiteChipView() { |
| 40 } |
| 41 |
| 42 void SiteChipView::Init() { |
| 43 ToolbarButton::Init(); |
| 44 |
| 45 // TODO(gbillock): Would be nice to just use stock LabelButton stuff here. |
| 46 location_icon_view_ = new LocationIconView(toolbar_view_->location_bar()); |
| 47 host_label_ = new views::Label(); |
| 48 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 49 host_label_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); |
| 50 |
| 51 AddChildView(location_icon_view_); |
| 52 AddChildView(host_label_); |
| 53 |
| 54 // temporary background painter |
| 55 const int kBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_EV); |
| 56 background_painter_.reset( |
| 57 views::Painter::CreateImageGridPainter(kBackgroundImages)); |
| 58 |
| 59 // temporary icon filler |
| 60 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed( |
| 61 IDR_OMNIBOX_HTTPS_VALID)); |
| 62 location_icon_view_->ShowTooltip(true); |
| 63 |
| 64 // temporary filler text. |
| 65 host_label_->SetText(ASCIIToUTF16("site.chip")); |
| 66 } |
| 67 |
| 68 bool SiteChipView::ShouldShow() { |
| 69 return chrome::ShouldDisplayOriginChip(); |
| 70 } |
| 71 |
| 72 void SiteChipView::Update(content::WebContents* tab) { |
| 73 Layout(); |
| 74 SchedulePaint(); |
| 75 } |
| 76 |
| 77 gfx::Size SiteChipView::GetPreferredSize() { |
| 78 gfx::Size label_size = host_label_->GetPreferredSize(); |
| 79 gfx::Size icon_size = location_icon_view_->GetPreferredSize(); |
| 80 return gfx::Size(icon_size.width() + label_size.width() + |
| 81 kIconTextSpacing + kTrailingLabelMargin + |
| 82 2 * kEdgeThickness, |
| 83 icon_size.height()); |
| 84 } |
| 85 |
| 86 void SiteChipView::Layout() { |
| 87 location_icon_view_->SetBounds( |
| 88 kEdgeThickness, |
| 89 LocationBarView::kNormalEdgeThickness, |
| 90 location_icon_view_->GetPreferredSize().width(), |
| 91 height() - 2 * LocationBarView::kNormalEdgeThickness); |
| 92 |
| 93 int host_label_x = location_icon_view_->x() + location_icon_view_->width() + |
| 94 kIconTextSpacing; |
| 95 int host_label_width = |
| 96 width() - host_label_x - kEdgeThickness - kTrailingLabelMargin; |
| 97 host_label_->SetBounds(host_label_x, |
| 98 LocationBarView::kNormalEdgeThickness, |
| 99 host_label_width, |
| 100 height() - 2 * LocationBarView::kNormalEdgeThickness); |
| 101 } |
| 102 |
| 103 void SiteChipView::OnPaint(gfx::Canvas* canvas) { |
| 104 gfx::Rect rect(GetLocalBounds()); |
| 105 rect.Inset(LocationBarView::kNormalEdgeThickness, |
| 106 LocationBarView::kNormalEdgeThickness); |
| 107 if (background_painter_.get()) |
| 108 views::Painter::PaintPainterAt(canvas, background_painter_.get(), rect); |
| 109 |
| 110 ToolbarButton::OnPaint(canvas); |
| 111 } |
| 112 |
| 113 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for |
| 114 // this button. |
| 115 void SiteChipView::ButtonPressed(views::Button* sender, |
| 116 const ui::Event& event) { |
| 117 toolbar_view_->location_bar()->GetOmniboxView()->SetFocus(); |
| 118 toolbar_view_->location_bar()->GetOmniboxView()->SelectAll(true); |
| 119 toolbar_view_->location_bar()->GetOmniboxView()->model()-> |
| 120 SetCaretVisibility(true); |
| 121 } |
OLD | NEW |