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 // Adds a label to the end of the notification text. If |format_as_key|, |
50 views::Label* key_name_label_; | 56 // surrounds the label in a rounded-rect border to indicate that it is a |
51 views::View* key_name_; | 57 // keyboard key. |
52 views::Label* after_key_; | 58 void AddTextSegment(const base::string16& text, bool format_as_key); |
| 59 |
| 60 const gfx::FontList& font_list_; |
| 61 SkColor foreground_color_; |
| 62 SkColor background_color_; |
| 63 |
| 64 base::string16 text_; |
53 | 65 |
54 DISALLOW_COPY_AND_ASSIGN(InstructionView); | 66 DISALLOW_COPY_AND_ASSIGN(InstructionView); |
55 }; | 67 }; |
56 | 68 |
57 SubtleNotificationView::InstructionView::InstructionView( | 69 SubtleNotificationView::InstructionView::InstructionView( |
58 const base::string16& text, | 70 const base::string16& text, |
59 const gfx::FontList& font_list, | 71 const gfx::FontList& font_list, |
60 SkColor foreground_color, | 72 SkColor foreground_color, |
61 SkColor background_color) { | 73 SkColor background_color) |
62 // Spacing around the key name. | 74 : font_list_(font_list), |
63 const int kKeyNameMarginHorizPx = 7; | 75 foreground_color_(foreground_color), |
64 const int kKeyNameBorderPx = 1; | 76 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. | 77 // The |between_child_spacing| is the horizontal margin of the key name. |
69 views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, | 78 views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, |
70 0, 0, kKeyNameMarginHorizPx); | 79 0, 0, kKeyNameMarginHorizPx); |
71 SetLayoutManager(layout); | 80 SetLayoutManager(layout); |
72 | 81 |
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); | 82 SetText(text); |
101 } | 83 } |
102 | 84 |
103 void SubtleNotificationView::InstructionView::SetText( | 85 void SubtleNotificationView::InstructionView::SetText( |
104 const base::string16& text) { | 86 const base::string16& text) { |
| 87 // Avoid replacing the contents with the same text. |
| 88 if (text == text_) |
| 89 return; |
| 90 |
| 91 RemoveAllChildViews(true); |
| 92 |
105 // Parse |text|, looking for pipe-delimited segment. | 93 // Parse |text|, looking for pipe-delimited segment. |
106 std::vector<base::string16> segments = | 94 std::vector<base::string16> segments = |
107 base::SplitString(text, base::ASCIIToUTF16("|"), base::TRIM_WHITESPACE, | 95 base::SplitString(text, base::ASCIIToUTF16("|"), base::TRIM_WHITESPACE, |
108 base::SPLIT_WANT_ALL); | 96 base::SPLIT_WANT_ALL); |
109 // Expect 1 or 3 pieces (either no pipe-delimited segments, or one). | 97 // SplitString() returns empty strings for zero-length segments, so given an |
110 DCHECK(segments.size() <= 1 || segments.size() == 3); | 98 // even number of pipes, there should always be an odd number of segments. |
| 99 // The exception is if |text| is entirely empty, in which case the returned |
| 100 // list is also empty (rather than containing a single empty string). |
| 101 DCHECK(segments.empty() || segments.size() % 2 == 1); |
111 | 102 |
112 before_key_->SetText(segments.size() ? segments[0] : base::string16()); | 103 // Add text segment, alternating between non-key (no border) and key (border) |
| 104 // formatting. |
| 105 bool format_as_key = false; |
| 106 for (const auto& segment : segments) { |
| 107 AddTextSegment(segment, format_as_key); |
| 108 format_as_key = !format_as_key; |
| 109 } |
113 | 110 |
114 if (segments.size() < 3) { | 111 text_ = text; |
115 key_name_->SetVisible(false); | 112 } |
116 after_key_->SetVisible(false); | 113 |
| 114 void SubtleNotificationView::InstructionView::AddTextSegment( |
| 115 const base::string16& text, bool format_as_key) { |
| 116 views::Label* label = new views::Label(text, font_list_); |
| 117 label->SetEnabledColor(foreground_color_); |
| 118 label->SetBackgroundColor(background_color_); |
| 119 if (!format_as_key) { |
| 120 AddChildView(label); |
117 return; | 121 return; |
118 } | 122 } |
119 | 123 |
120 before_key_->SetText(segments[0]); | 124 views::View* key = new views::View; |
121 key_name_label_->SetText(segments[1]); | 125 views::BoxLayout* key_name_layout = new views::BoxLayout( |
122 key_name_->SetVisible(true); | 126 views::BoxLayout::kHorizontal, kKeyNamePaddingPx, 0, 0); |
123 after_key_->SetVisible(true); | 127 key_name_layout->set_minimum_cross_axis_size( |
124 after_key_->SetText(segments[2]); | 128 label->GetPreferredSize().height() + kKeyNamePaddingPx * 2); |
| 129 key->SetLayoutManager(key_name_layout); |
| 130 key->AddChildView(label); |
| 131 // The key name has a border around it. |
| 132 std::unique_ptr<views::Border> border(views::Border::CreateRoundedRectBorder( |
| 133 kKeyNameBorderPx, kKeyNameCornerRadius, foreground_color_)); |
| 134 key->SetBorder(std::move(border)); |
| 135 AddChildView(key); |
125 } | 136 } |
126 | 137 |
127 SubtleNotificationView::SubtleNotificationView( | 138 SubtleNotificationView::SubtleNotificationView( |
128 views::LinkListener* link_listener) | 139 views::LinkListener* link_listener) |
129 : instruction_view_(nullptr), link_(nullptr) { | 140 : instruction_view_(nullptr), link_(nullptr) { |
130 const SkColor kForegroundColor = SK_ColorWHITE; | 141 const SkColor kForegroundColor = SK_ColorWHITE; |
131 | 142 |
132 std::unique_ptr<views::BubbleBorder> bubble_border(new views::BubbleBorder( | 143 std::unique_ptr<views::BubbleBorder> bubble_border(new views::BubbleBorder( |
133 views::BubbleBorder::NONE, views::BubbleBorder::NO_ASSETS, | 144 views::BubbleBorder::NONE, views::BubbleBorder::NO_ASSETS, |
134 kBackgroundColor)); | 145 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 | 201 // 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 | 202 // 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 | 203 // shrinking while we animate the height of the popup to give the impression |
193 // that it is sliding off the top of the screen. | 204 // that it is sliding off the top of the screen. |
194 // TODO(mgiuca): This probably isn't necessary now that there is no slide | 205 // TODO(mgiuca): This probably isn't necessary now that there is no slide |
195 // animation. Remove it. | 206 // animation. Remove it. |
196 popup->GetRootView()->SetLayoutManager(nullptr); | 207 popup->GetRootView()->SetLayoutManager(nullptr); |
197 | 208 |
198 return popup; | 209 return popup; |
199 } | 210 } |
OLD | NEW |