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/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "third_party/skia/include/core/SkColor.h" | 12 #include "third_party/skia/include/core/SkColor.h" |
| 13 #include "ui/accessibility/ax_view_state.h" |
12 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/gfx/font_list.h" | 15 #include "ui/gfx/font_list.h" |
14 #include "ui/views/bubble/bubble_border.h" | 16 #include "ui/views/bubble/bubble_border.h" |
15 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
16 #include "ui/views/controls/link.h" | 18 #include "ui/views/controls/link.h" |
17 #include "ui/views/layout/box_layout.h" | 19 #include "ui/views/layout/box_layout.h" |
18 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 21 #include "ui/views/widget/widget_observer.h" |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 // Space between the site info label and the link. | 25 // Space between the site info label and the link. |
23 const int kMiddlePaddingPx = 30; | 26 const int kMiddlePaddingPx = 30; |
24 | 27 |
25 const int kOuterPaddingHorizPx = 40; | 28 const int kOuterPaddingHorizPx = 40; |
26 const int kOuterPaddingVertPx = 8; | 29 const int kOuterPaddingVertPx = 8; |
27 | 30 |
28 // Partially-transparent background color. | 31 // Partially-transparent background color. |
29 const SkColor kBackgroundColor = SkColorSetARGB(0xcc, 0x28, 0x2c, 0x32); | 32 const SkColor kBackgroundColor = SkColorSetARGB(0xcc, 0x28, 0x2c, 0x32); |
30 | 33 |
31 // Spacing around the key name. | 34 // Spacing around the key name. |
32 const int kKeyNameMarginHorizPx = 7; | 35 const int kKeyNameMarginHorizPx = 7; |
33 const int kKeyNameBorderPx = 1; | 36 const int kKeyNameBorderPx = 1; |
34 const int kKeyNameCornerRadius = 2; | 37 const int kKeyNameCornerRadius = 2; |
35 const int kKeyNamePaddingPx = 5; | 38 const int kKeyNamePaddingPx = 5; |
36 | 39 |
| 40 // Delimiter indicating there should be a segment displayed as a keyboard key. |
| 41 const char kKeyNameDelimiter[] = "|"; |
| 42 |
37 } // namespace | 43 } // namespace |
38 | 44 |
39 // Class containing the instruction text. Contains fancy styling on the keyboard | 45 // Class containing the instruction text. Contains fancy styling on the keyboard |
40 // key (not just a simple label). | 46 // key (not just a simple label). |
41 class SubtleNotificationView::InstructionView : public views::View { | 47 class SubtleNotificationView::InstructionView : public views::View { |
42 public: | 48 public: |
43 // Creates an InstructionView with specific text. |text| may contain one or | 49 // Creates an InstructionView with specific text. |text| may contain one or |
44 // more segments delimited by a pair of pipes ('|'); each of these segments | 50 // more segments delimited by a pair of pipes ('|'); each of these segments |
45 // will be displayed as a keyboard key. e.g., "Press |Alt|+|Q| to exit" will | 51 // will be displayed as a keyboard key. e.g., "Press |Alt|+|Q| to exit" will |
46 // have "Alt" and "Q" rendered as keys. | 52 // have "Alt" and "Q" rendered as keys. |
47 InstructionView(const base::string16& text, | 53 InstructionView(const base::string16& text, |
48 const gfx::FontList& font_list, | 54 const gfx::FontList& font_list, |
49 SkColor foreground_color, | 55 SkColor foreground_color, |
50 SkColor background_color); | 56 SkColor background_color); |
51 | 57 |
| 58 base::string16 text() const { return text_; } |
52 void SetText(const base::string16& text); | 59 void SetText(const base::string16& text); |
53 | 60 |
54 private: | 61 private: |
55 // Adds a label to the end of the notification text. If |format_as_key|, | 62 // Adds a label to the end of the notification text. If |format_as_key|, |
56 // surrounds the label in a rounded-rect border to indicate that it is a | 63 // surrounds the label in a rounded-rect border to indicate that it is a |
57 // keyboard key. | 64 // keyboard key. |
58 void AddTextSegment(const base::string16& text, bool format_as_key); | 65 void AddTextSegment(const base::string16& text, bool format_as_key); |
59 | 66 |
60 const gfx::FontList& font_list_; | 67 const gfx::FontList& font_list_; |
61 SkColor foreground_color_; | 68 SkColor foreground_color_; |
(...skipping 23 matching lines...) Expand all Loading... |
85 void SubtleNotificationView::InstructionView::SetText( | 92 void SubtleNotificationView::InstructionView::SetText( |
86 const base::string16& text) { | 93 const base::string16& text) { |
87 // Avoid replacing the contents with the same text. | 94 // Avoid replacing the contents with the same text. |
88 if (text == text_) | 95 if (text == text_) |
89 return; | 96 return; |
90 | 97 |
91 RemoveAllChildViews(true); | 98 RemoveAllChildViews(true); |
92 | 99 |
93 // Parse |text|, looking for pipe-delimited segment. | 100 // Parse |text|, looking for pipe-delimited segment. |
94 std::vector<base::string16> segments = | 101 std::vector<base::string16> segments = |
95 base::SplitString(text, base::ASCIIToUTF16("|"), base::TRIM_WHITESPACE, | 102 base::SplitString(text, base::ASCIIToUTF16(kKeyNameDelimiter), |
96 base::SPLIT_WANT_ALL); | 103 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
97 // SplitString() returns empty strings for zero-length segments, so given an | 104 // SplitString() returns empty strings for zero-length segments, so given an |
98 // even number of pipes, there should always be an odd number of segments. | 105 // 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 | 106 // 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). | 107 // list is also empty (rather than containing a single empty string). |
101 DCHECK(segments.empty() || segments.size() % 2 == 1); | 108 DCHECK(segments.empty() || segments.size() % 2 == 1); |
102 | 109 |
103 // Add text segment, alternating between non-key (no border) and key (border) | 110 // Add text segment, alternating between non-key (no border) and key (border) |
104 // formatting. | 111 // formatting. |
105 bool format_as_key = false; | 112 bool format_as_key = false; |
106 for (const auto& segment : segments) { | 113 for (const auto& segment : segments) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 SubtleNotificationView::~SubtleNotificationView() {} | 183 SubtleNotificationView::~SubtleNotificationView() {} |
177 | 184 |
178 void SubtleNotificationView::UpdateContent( | 185 void SubtleNotificationView::UpdateContent( |
179 const base::string16& instruction_text, | 186 const base::string16& instruction_text, |
180 const base::string16& link_text) { | 187 const base::string16& link_text) { |
181 instruction_view_->SetText(instruction_text); | 188 instruction_view_->SetText(instruction_text); |
182 instruction_view_->SetVisible(!instruction_text.empty()); | 189 instruction_view_->SetVisible(!instruction_text.empty()); |
183 link_->SetText(link_text); | 190 link_->SetText(link_text); |
184 link_->SetVisible(!link_text.empty()); | 191 link_->SetVisible(!link_text.empty()); |
185 Layout(); | 192 Layout(); |
| 193 |
| 194 NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, true); |
| 195 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); |
186 } | 196 } |
187 | 197 |
188 // static | 198 // static |
189 views::Widget* SubtleNotificationView::CreatePopupWidget( | 199 views::Widget* SubtleNotificationView::CreatePopupWidget( |
190 gfx::NativeView parent_view, | 200 gfx::NativeView parent_view, |
191 SubtleNotificationView* view, | 201 SubtleNotificationView* view, |
192 bool accept_events) { | 202 bool accept_events) { |
193 // Initialize the popup. | 203 // Initialize the popup. |
194 views::Widget* popup = new views::Widget; | 204 views::Widget* popup = new views::Widget; |
195 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | 205 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
196 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 206 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
197 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 207 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
198 params.parent = parent_view; | 208 params.parent = parent_view; |
199 params.accept_events = accept_events; | 209 params.accept_events = accept_events; |
200 popup->Init(params); | 210 popup->Init(params); |
201 popup->SetContentsView(view); | 211 popup->SetContentsView(view); |
202 // We set layout manager to nullptr to prevent the widget from sizing its | 212 // We set layout manager to nullptr to prevent the widget from sizing its |
203 // contents to the same size as itself. This prevents the widget contents from | 213 // contents to the same size as itself. This prevents the widget contents from |
204 // shrinking while we animate the height of the popup to give the impression | 214 // shrinking while we animate the height of the popup to give the impression |
205 // that it is sliding off the top of the screen. | 215 // that it is sliding off the top of the screen. |
206 // TODO(mgiuca): This probably isn't necessary now that there is no slide | 216 // TODO(mgiuca): This probably isn't necessary now that there is no slide |
207 // animation. Remove it. | 217 // animation. Remove it. |
208 popup->GetRootView()->SetLayoutManager(nullptr); | 218 popup->GetRootView()->SetLayoutManager(nullptr); |
209 | 219 |
| 220 // Allow accessibility events to be sent when the view is shown. |
| 221 popup->AddObserver(view); |
| 222 |
210 return popup; | 223 return popup; |
211 } | 224 } |
| 225 |
| 226 void SubtleNotificationView::SetAccessibleName( |
| 227 const base::string16& accessible_name) { |
| 228 base::RemoveChars(accessible_name, base::ASCIIToUTF16(kKeyNameDelimiter), |
| 229 &accessible_name_); |
| 230 } |
| 231 |
| 232 void SubtleNotificationView::GetAccessibleState(ui::AXViewState* state) { |
| 233 state->role = ui::AX_ROLE_LABEL_TEXT; |
| 234 |
| 235 // If there is no accessible name set, use the displayed text, but ensure the |
| 236 // '|' delimiters are removed. |
| 237 if (accessible_name_.empty()) { |
| 238 base::string16 accessible_name; |
| 239 base::RemoveChars(instruction_view_->text(), |
| 240 base::ASCIIToUTF16(kKeyNameDelimiter), &accessible_name); |
| 241 state->name = accessible_name; |
| 242 } else { |
| 243 state->name = accessible_name_; |
| 244 } |
| 245 } |
| 246 |
| 247 void SubtleNotificationView::OnWidgetClosing(views::Widget* widget) { |
| 248 if (widget && widget == GetWidget()) |
| 249 widget->RemoveObserver(this); |
| 250 } |
| 251 |
| 252 void SubtleNotificationView::OnWidgetVisibilityChanged(views::Widget* widget, |
| 253 bool visible) { |
| 254 if (visible) |
| 255 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); |
| 256 } |
OLD | NEW |