| OLD | NEW |
| (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/subtle_notification_view.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "third_party/skia/include/core/SkColor.h" |
| 12 #include "ui/base/resource/resource_bundle.h" |
| 13 #include "ui/gfx/font_list.h" |
| 14 #include "ui/views/bubble/bubble_border.h" |
| 15 #include "ui/views/controls/label.h" |
| 16 #include "ui/views/controls/link.h" |
| 17 #include "ui/views/layout/box_layout.h" |
| 18 #include "ui/views/widget/widget.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Space between the site info label and the link. |
| 23 const int kMiddlePaddingPx = 30; |
| 24 |
| 25 const int kOuterPaddingHorizPx = 40; |
| 26 const int kOuterPaddingVertPx = 8; |
| 27 |
| 28 // Partially-transparent background color. |
| 29 const SkColor kBackgroundColor = SkColorSetARGB(0xcc, 0x28, 0x2c, 0x32); |
| 30 |
| 31 } // namespace |
| 32 |
| 33 // Class containing the instruction text. Contains fancy styling on the keyboard |
| 34 // key (not just a simple label). |
| 35 class SubtleNotificationView::InstructionView : public views::View { |
| 36 public: |
| 37 // Creates an InstructionView with specific text. |text| may contain a single |
| 38 // segment delimited by a pair of pipes ('|'); this segment will be displayed |
| 39 // as a keyboard key. e.g., "Press |Esc| to exit" will have "Esc" rendered as |
| 40 // a key. |
| 41 InstructionView(const base::string16& text, |
| 42 const gfx::FontList& font_list, |
| 43 SkColor foreground_color, |
| 44 SkColor background_color); |
| 45 |
| 46 void SetText(const base::string16& text); |
| 47 |
| 48 private: |
| 49 views::Label* before_key_; |
| 50 views::Label* key_name_label_; |
| 51 views::View* key_name_; |
| 52 views::Label* after_key_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(InstructionView); |
| 55 }; |
| 56 |
| 57 SubtleNotificationView::InstructionView::InstructionView( |
| 58 const base::string16& text, |
| 59 const gfx::FontList& font_list, |
| 60 SkColor foreground_color, |
| 61 SkColor background_color) { |
| 62 // Spacing around the key name. |
| 63 const int kKeyNameMarginHorizPx = 7; |
| 64 const int kKeyNameBorderPx = 1; |
| 65 const int kKeyNameCornerRadius = 2; |
| 66 const int kKeyNamePaddingPx = 5; |
| 67 |
| 68 // The |between_child_spacing| is the horizontal margin of the key name. |
| 69 views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, |
| 70 0, 0, kKeyNameMarginHorizPx); |
| 71 SetLayoutManager(layout); |
| 72 |
| 73 before_key_ = new views::Label(base::string16(), font_list); |
| 74 before_key_->SetEnabledColor(foreground_color); |
| 75 before_key_->SetBackgroundColor(background_color); |
| 76 AddChildView(before_key_); |
| 77 |
| 78 key_name_label_ = new views::Label(base::string16(), font_list); |
| 79 key_name_label_->SetEnabledColor(foreground_color); |
| 80 key_name_label_->SetBackgroundColor(background_color); |
| 81 |
| 82 key_name_ = new views::View; |
| 83 views::BoxLayout* key_name_layout = new views::BoxLayout( |
| 84 views::BoxLayout::kHorizontal, kKeyNamePaddingPx, 0, 0); |
| 85 key_name_layout->set_minimum_cross_axis_size( |
| 86 key_name_label_->GetPreferredSize().height() + kKeyNamePaddingPx * 2); |
| 87 key_name_->SetLayoutManager(key_name_layout); |
| 88 key_name_->AddChildView(key_name_label_); |
| 89 // The key name has a border around it. |
| 90 std::unique_ptr<views::Border> border(views::Border::CreateRoundedRectBorder( |
| 91 kKeyNameBorderPx, kKeyNameCornerRadius, foreground_color)); |
| 92 key_name_->SetBorder(std::move(border)); |
| 93 AddChildView(key_name_); |
| 94 |
| 95 after_key_ = new views::Label(base::string16(), font_list); |
| 96 after_key_->SetEnabledColor(foreground_color); |
| 97 after_key_->SetBackgroundColor(background_color); |
| 98 AddChildView(after_key_); |
| 99 |
| 100 SetText(text); |
| 101 } |
| 102 |
| 103 void SubtleNotificationView::InstructionView::SetText( |
| 104 const base::string16& text) { |
| 105 // Parse |text|, looking for pipe-delimited segment. |
| 106 std::vector<base::string16> segments = |
| 107 base::SplitString(text, base::ASCIIToUTF16("|"), base::TRIM_WHITESPACE, |
| 108 base::SPLIT_WANT_ALL); |
| 109 // Expect 1 or 3 pieces (either no pipe-delimited segments, or one). |
| 110 DCHECK(segments.size() <= 1 || segments.size() == 3); |
| 111 |
| 112 before_key_->SetText(segments.size() ? segments[0] : base::string16()); |
| 113 |
| 114 if (segments.size() < 3) { |
| 115 key_name_->SetVisible(false); |
| 116 after_key_->SetVisible(false); |
| 117 return; |
| 118 } |
| 119 |
| 120 before_key_->SetText(segments[0]); |
| 121 key_name_label_->SetText(segments[1]); |
| 122 key_name_->SetVisible(true); |
| 123 after_key_->SetVisible(true); |
| 124 after_key_->SetText(segments[2]); |
| 125 } |
| 126 |
| 127 SubtleNotificationView::SubtleNotificationView( |
| 128 views::LinkListener* link_listener) |
| 129 : instruction_view_(nullptr), link_(nullptr) { |
| 130 const SkColor kForegroundColor = SK_ColorWHITE; |
| 131 |
| 132 std::unique_ptr<views::BubbleBorder> bubble_border(new views::BubbleBorder( |
| 133 views::BubbleBorder::NONE, views::BubbleBorder::NO_ASSETS, |
| 134 kBackgroundColor)); |
| 135 set_background(new views::BubbleBackground(bubble_border.get())); |
| 136 SetBorder(std::move(bubble_border)); |
| 137 |
| 138 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 139 const gfx::FontList& font_list = |
| 140 rb.GetFontList(ui::ResourceBundle::MediumFont); |
| 141 |
| 142 instruction_view_ = new InstructionView(base::string16(), font_list, |
| 143 kForegroundColor, kBackgroundColor); |
| 144 |
| 145 link_ = new views::Link(); |
| 146 link_->SetFocusBehavior(FocusBehavior::NEVER); |
| 147 link_->set_listener(link_listener); |
| 148 link_->SetFontList(font_list); |
| 149 link_->SetPressedColor(kForegroundColor); |
| 150 link_->SetEnabledColor(kForegroundColor); |
| 151 link_->SetBackgroundColor(kBackgroundColor); |
| 152 link_->SetVisible(false); |
| 153 |
| 154 int outer_padding_horiz = kOuterPaddingHorizPx; |
| 155 int outer_padding_vert = kOuterPaddingVertPx; |
| 156 AddChildView(instruction_view_); |
| 157 AddChildView(link_); |
| 158 |
| 159 views::BoxLayout* layout = |
| 160 new views::BoxLayout(views::BoxLayout::kHorizontal, outer_padding_horiz, |
| 161 outer_padding_vert, kMiddlePaddingPx); |
| 162 SetLayoutManager(layout); |
| 163 } |
| 164 |
| 165 SubtleNotificationView::~SubtleNotificationView() {} |
| 166 |
| 167 void SubtleNotificationView::UpdateContent( |
| 168 const base::string16& instruction_text, |
| 169 const base::string16& link_text) { |
| 170 instruction_view_->SetText(instruction_text); |
| 171 instruction_view_->SetVisible(!instruction_text.empty()); |
| 172 link_->SetText(link_text); |
| 173 link_->SetVisible(!link_text.empty()); |
| 174 } |
| 175 |
| 176 // static |
| 177 views::Widget* SubtleNotificationView::CreatePopupWidget( |
| 178 gfx::NativeView parent_view, |
| 179 SubtleNotificationView* view, |
| 180 bool accept_events) { |
| 181 // Initialize the popup. |
| 182 views::Widget* popup = new views::Widget; |
| 183 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 184 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 185 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 186 params.parent = parent_view; |
| 187 params.accept_events = accept_events; |
| 188 popup->Init(params); |
| 189 popup->SetContentsView(view); |
| 190 // We set layout manager to nullptr to prevent the widget from sizing its |
| 191 // contents to the same size as itself. This prevents the widget contents from |
| 192 // shrinking while we animate the height of the popup to give the impression |
| 193 // that it is sliding off the top of the screen. |
| 194 // TODO(mgiuca): This probably isn't necessary now that there is no slide |
| 195 // animation. Remove it. |
| 196 popup->GetRootView()->SetLayoutManager(nullptr); |
| 197 |
| 198 return popup; |
| 199 } |
| OLD | NEW |