Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/subtle_notification_view.h" | 5 #include "chrome/browser/ui/views/subtle_notification_view.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 // Space between the site info label and the link. | 22 // Space between the site info label and the link. |
| 23 const int kMiddlePaddingPx = 30; | 23 const int kMiddlePaddingPx = 30; |
| 24 | 24 |
| 25 const int kOuterPaddingHorizPx = 40; | 25 const int kOuterPaddingHorizPx = 40; |
| 26 const int kOuterPaddingVertPx = 8; | 26 const int kOuterPaddingVertPx = 8; |
| 27 | 27 |
| 28 // Partially-transparent background color. | 28 // Partially-transparent background color. |
| 29 const SkColor kBackgroundColor = SkColorSetARGB(0xcc, 0x28, 0x2c, 0x32); | 29 const SkColor kBackgroundColor = SkColorSetARGB(0xcc, 0x28, 0x2c, 0x32); |
| 30 | 30 |
| 31 // Spacing around the key name. | |
| 32 const int kKeyNameMarginHorizPx = 7; | |
| 33 const int kKeyNameBorderPx = 1; | |
| 34 const int kKeyNameCornerRadius = 2; | |
| 35 const int kKeyNamePaddingPx = 5; | |
| 36 | |
| 31 } // namespace | 37 } // namespace |
| 32 | 38 |
| 33 // Class containing the instruction text. Contains fancy styling on the keyboard | 39 // Class containing the instruction text. Contains fancy styling on the keyboard |
| 34 // key (not just a simple label). | 40 // key (not just a simple label). |
| 35 class SubtleNotificationView::InstructionView : public views::View { | 41 class SubtleNotificationView::InstructionView : public views::View { |
| 36 public: | 42 public: |
| 37 // Creates an InstructionView with specific text. |text| may contain a single | 43 // Creates an InstructionView with specific text. |text| may contain one or |
| 38 // segment delimited by a pair of pipes ('|'); this segment will be displayed | 44 // more segments delimited by a pair of pipes ('|'); each of these segments |
| 39 // as a keyboard key. e.g., "Press |Esc| to exit" will have "Esc" rendered as | 45 // will be displayed as a keyboard key. e.g., "Press |Alt|+|Q| to exit" will |
| 40 // a key. | 46 // have "Alt" and "Q" rendered as keys. |
| 41 InstructionView(const base::string16& text, | 47 InstructionView(const base::string16& text, |
| 42 const gfx::FontList& font_list, | 48 const gfx::FontList& font_list, |
| 43 SkColor foreground_color, | 49 SkColor foreground_color, |
| 44 SkColor background_color); | 50 SkColor background_color); |
| 45 | 51 |
| 46 void SetText(const base::string16& text); | 52 void SetText(const base::string16& text); |
| 47 | 53 |
| 48 private: | 54 private: |
| 49 views::Label* before_key_; | 55 void AddLabelSegment(const base::string16& text); |
| 50 views::Label* key_name_label_; | 56 void AddKeyNameSegment(const base::string16& text); |
| 51 views::View* key_name_; | 57 |
| 52 views::Label* after_key_; | 58 const gfx::FontList& font_list_; |
| 59 SkColor foreground_color_; | |
| 60 SkColor background_color_; | |
| 61 | |
| 62 base::string16 text_; | |
| 53 | 63 |
| 54 DISALLOW_COPY_AND_ASSIGN(InstructionView); | 64 DISALLOW_COPY_AND_ASSIGN(InstructionView); |
| 55 }; | 65 }; |
| 56 | 66 |
| 57 SubtleNotificationView::InstructionView::InstructionView( | 67 SubtleNotificationView::InstructionView::InstructionView( |
| 58 const base::string16& text, | 68 const base::string16& text, |
| 59 const gfx::FontList& font_list, | 69 const gfx::FontList& font_list, |
| 60 SkColor foreground_color, | 70 SkColor foreground_color, |
| 61 SkColor background_color) { | 71 SkColor background_color) |
| 62 // Spacing around the key name. | 72 : font_list_(font_list), |
| 63 const int kKeyNameMarginHorizPx = 7; | 73 foreground_color_(foreground_color), |
| 64 const int kKeyNameBorderPx = 1; | 74 background_color_(background_color) { |
| 65 const int kKeyNameCornerRadius = 2; | |
| 66 const int kKeyNamePaddingPx = 5; | |
| 67 | |
| 68 // The |between_child_spacing| is the horizontal margin of the key name. | 75 // The |between_child_spacing| is the horizontal margin of the key name. |
| 69 views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, | 76 views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, |
| 70 0, 0, kKeyNameMarginHorizPx); | 77 0, 0, kKeyNameMarginHorizPx); |
| 71 SetLayoutManager(layout); | 78 SetLayoutManager(layout); |
| 72 | 79 |
| 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); | 80 SetText(text); |
| 101 } | 81 } |
| 102 | 82 |
| 103 void SubtleNotificationView::InstructionView::SetText( | 83 void SubtleNotificationView::InstructionView::SetText( |
| 104 const base::string16& text) { | 84 const base::string16& text) { |
| 85 // Avoid replacing the contents with the same text. | |
| 86 if (text == text_) | |
| 87 return; | |
| 88 | |
| 89 RemoveAllChildViews(true); | |
| 90 | |
| 105 // Parse |text|, looking for pipe-delimited segment. | 91 // Parse |text|, looking for pipe-delimited segment. |
|
Peter Kasting
2016/05/19 10:08:37
Don't do things this way -- see my comments in you
Matt Giuca
2016/05/19 10:33:59
See my response there. Pipes may not have been the
Peter Kasting
2016/05/19 19:45:08
I forgot this is in the common code, not in the ba
Matt Giuca
2016/05/19 22:11:47
https://bugs.chromium.org/p/chromium/issues/detail
| |
| 106 std::vector<base::string16> segments = | 92 std::vector<base::string16> segments = |
| 107 base::SplitString(text, base::ASCIIToUTF16("|"), base::TRIM_WHITESPACE, | 93 base::SplitString(text, base::ASCIIToUTF16("|"), base::TRIM_WHITESPACE, |
| 108 base::SPLIT_WANT_ALL); | 94 base::SPLIT_WANT_ALL); |
| 109 // Expect 1 or 3 pieces (either no pipe-delimited segments, or one). | 95 // Expect an odd number of pieces (from an even number of pipes), or none. |
| 110 DCHECK(segments.size() <= 1 || segments.size() == 3); | 96 DCHECK(segments.empty() || segments.size() % 2 == 1); |
|
Peter Kasting
2016/05/19 10:08:37
This check isn't right.
If the localizers localiz
Matt Giuca
2016/05/19 10:33:59
SplitString does the right thing here: empty pipe-
Peter Kasting
2016/05/19 19:45:08
Hmm... this maybe deserves more comments to avoid
Matt Giuca
2016/05/23 04:22:57
Done.
| |
| 111 | 97 |
| 112 before_key_->SetText(segments.size() ? segments[0] : base::string16()); | 98 // Add text segment, alternating between non-key (no border) and key (border) |
| 113 | 99 // formatting. |
| 114 if (segments.size() < 3) { | 100 bool format_as_key = false; |
| 115 key_name_->SetVisible(false); | 101 for (const auto& segment : segments) { |
| 116 after_key_->SetVisible(false); | 102 if (format_as_key) |
| 117 return; | 103 AddKeyNameSegment(segment); |
| 104 else | |
| 105 AddLabelSegment(segment); | |
|
Peter Kasting
2016/05/19 10:08:37
Nit: AddLabelSegment() is almost a pure subset of
Matt Giuca
2016/05/23 04:22:57
Done.
| |
| 106 format_as_key = !format_as_key; | |
| 118 } | 107 } |
| 119 | 108 |
| 120 before_key_->SetText(segments[0]); | 109 text_ = text; |
| 121 key_name_label_->SetText(segments[1]); | 110 } |
| 122 key_name_->SetVisible(true); | 111 |
| 123 after_key_->SetVisible(true); | 112 void SubtleNotificationView::InstructionView::AddLabelSegment( |
| 124 after_key_->SetText(segments[2]); | 113 const base::string16& text) { |
| 114 views::Label* label = new views::Label(text, font_list_); | |
| 115 label->SetEnabledColor(foreground_color_); | |
| 116 label->SetBackgroundColor(background_color_); | |
| 117 AddChildView(label); | |
| 118 } | |
| 119 | |
| 120 void SubtleNotificationView::InstructionView::AddKeyNameSegment( | |
| 121 const base::string16& text) { | |
| 122 views::Label* label = new views::Label(text, font_list_); | |
| 123 label->SetEnabledColor(foreground_color_); | |
| 124 label->SetBackgroundColor(background_color_); | |
| 125 | |
| 126 views::View* key = new views::View; | |
| 127 views::BoxLayout* key_name_layout = new views::BoxLayout( | |
| 128 views::BoxLayout::kHorizontal, kKeyNamePaddingPx, 0, 0); | |
| 129 key_name_layout->set_minimum_cross_axis_size( | |
| 130 label->GetPreferredSize().height() + kKeyNamePaddingPx * 2); | |
| 131 key->SetLayoutManager(key_name_layout); | |
| 132 key->AddChildView(label); | |
| 133 // The key name has a border around it. | |
| 134 std::unique_ptr<views::Border> border(views::Border::CreateRoundedRectBorder( | |
| 135 kKeyNameBorderPx, kKeyNameCornerRadius, foreground_color_)); | |
| 136 key->SetBorder(std::move(border)); | |
| 137 AddChildView(key); | |
| 125 } | 138 } |
| 126 | 139 |
| 127 SubtleNotificationView::SubtleNotificationView( | 140 SubtleNotificationView::SubtleNotificationView( |
| 128 views::LinkListener* link_listener) | 141 views::LinkListener* link_listener) |
| 129 : instruction_view_(nullptr), link_(nullptr) { | 142 : instruction_view_(nullptr), link_(nullptr) { |
| 130 const SkColor kForegroundColor = SK_ColorWHITE; | 143 const SkColor kForegroundColor = SK_ColorWHITE; |
| 131 | 144 |
| 132 std::unique_ptr<views::BubbleBorder> bubble_border(new views::BubbleBorder( | 145 std::unique_ptr<views::BubbleBorder> bubble_border(new views::BubbleBorder( |
| 133 views::BubbleBorder::NONE, views::BubbleBorder::NO_ASSETS, | 146 views::BubbleBorder::NONE, views::BubbleBorder::NO_ASSETS, |
| 134 kBackgroundColor)); | 147 kBackgroundColor)); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 // We set layout manager to nullptr to prevent the widget from sizing its | 203 // 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 | 204 // 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 | 205 // shrinking while we animate the height of the popup to give the impression |
| 193 // that it is sliding off the top of the screen. | 206 // that it is sliding off the top of the screen. |
| 194 // TODO(mgiuca): This probably isn't necessary now that there is no slide | 207 // TODO(mgiuca): This probably isn't necessary now that there is no slide |
| 195 // animation. Remove it. | 208 // animation. Remove it. |
| 196 popup->GetRootView()->SetLayoutManager(nullptr); | 209 popup->GetRootView()->SetLayoutManager(nullptr); |
| 197 | 210 |
| 198 return popup; | 211 return popup; |
| 199 } | 212 } |
| OLD | NEW |