| 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 "ui/message_center/views/custom_notification_view.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "ui/base/ime/input_method.h" | |
| 10 #include "ui/base/ime/text_input_client.h" | |
| 11 #include "ui/base/ime/text_input_type.h" | |
| 12 #include "ui/gfx/geometry/size.h" | |
| 13 #include "ui/message_center/message_center_style.h" | |
| 14 #include "ui/message_center/views/message_center_controller.h" | |
| 15 #include "ui/views/background.h" | |
| 16 #include "ui/views/controls/button/image_button.h" | |
| 17 #include "ui/views/controls/image_view.h" | |
| 18 #include "ui/views/painter.h" | |
| 19 | |
| 20 namespace message_center { | |
| 21 | |
| 22 // static | |
| 23 const char CustomNotificationView::kViewClassName[] = "CustomNotificationView"; | |
| 24 | |
| 25 CustomNotificationView::CustomNotificationView( | |
| 26 MessageCenterController* controller, | |
| 27 const Notification& notification) | |
| 28 : MessageView(controller, notification) { | |
| 29 DCHECK_EQ(NOTIFICATION_TYPE_CUSTOM, notification.type()); | |
| 30 | |
| 31 auto custom_content = notification.delegate()->CreateCustomContent(); | |
| 32 | |
| 33 contents_view_ = custom_content->view.release(); | |
| 34 DCHECK(contents_view_); | |
| 35 AddChildView(contents_view_); | |
| 36 | |
| 37 contents_view_delegate_ = std::move(custom_content->delegate); | |
| 38 DCHECK(contents_view_delegate_); | |
| 39 | |
| 40 if (contents_view_->background()) { | |
| 41 background_view()->background()->SetNativeControlColor( | |
| 42 contents_view_->background()->get_color()); | |
| 43 } | |
| 44 | |
| 45 focus_painter_ = views::Painter::CreateSolidFocusPainter( | |
| 46 kFocusBorderColor, gfx::Insets(0, 1, 3, 2)); | |
| 47 } | |
| 48 | |
| 49 CustomNotificationView::~CustomNotificationView() {} | |
| 50 | |
| 51 void CustomNotificationView::OnContentFocused() { | |
| 52 SchedulePaint(); | |
| 53 } | |
| 54 | |
| 55 void CustomNotificationView::OnContentBlured() { | |
| 56 SchedulePaint(); | |
| 57 } | |
| 58 | |
| 59 void CustomNotificationView::SetDrawBackgroundAsActive(bool active) { | |
| 60 // Do nothing if |contents_view_| has a background. | |
| 61 if (contents_view_->background()) | |
| 62 return; | |
| 63 | |
| 64 MessageView::SetDrawBackgroundAsActive(active); | |
| 65 } | |
| 66 | |
| 67 bool CustomNotificationView::IsCloseButtonFocused() const { | |
| 68 if (!contents_view_delegate_) | |
| 69 return false; | |
| 70 return contents_view_delegate_->IsCloseButtonFocused(); | |
| 71 } | |
| 72 | |
| 73 void CustomNotificationView::RequestFocusOnCloseButton() { | |
| 74 if (contents_view_delegate_) | |
| 75 contents_view_delegate_->RequestFocusOnCloseButton(); | |
| 76 } | |
| 77 | |
| 78 const char* CustomNotificationView::GetClassName() const { | |
| 79 return kViewClassName; | |
| 80 } | |
| 81 | |
| 82 void CustomNotificationView::UpdateControlButtonsVisibility() { | |
| 83 if (contents_view_delegate_) | |
| 84 contents_view_delegate_->UpdateControlButtonsVisibility(); | |
| 85 } | |
| 86 | |
| 87 gfx::Size CustomNotificationView::GetPreferredSize() const { | |
| 88 const gfx::Insets insets = GetInsets(); | |
| 89 const int contents_width = kNotificationWidth - insets.width(); | |
| 90 const int contents_height = contents_view_->GetHeightForWidth(contents_width); | |
| 91 return gfx::Size(kNotificationWidth, contents_height + insets.height()); | |
| 92 } | |
| 93 | |
| 94 void CustomNotificationView::Layout() { | |
| 95 MessageView::Layout(); | |
| 96 | |
| 97 contents_view_->SetBoundsRect(GetContentsBounds()); | |
| 98 | |
| 99 // If the content view claims focus, defer focus handling to the content view. | |
| 100 if (contents_view_->IsFocusable()) | |
| 101 SetFocusBehavior(FocusBehavior::NEVER); | |
| 102 } | |
| 103 | |
| 104 bool CustomNotificationView::HasFocus() const { | |
| 105 // In case that focus handling is defered to the content view, asking the | |
| 106 // content view about focus. | |
| 107 if (contents_view_ && contents_view_->IsFocusable()) | |
| 108 return contents_view_->HasFocus(); | |
| 109 else | |
| 110 return MessageView::HasFocus(); | |
| 111 } | |
| 112 | |
| 113 void CustomNotificationView::RequestFocus() { | |
| 114 if (contents_view_ && contents_view_->IsFocusable()) | |
| 115 contents_view_->RequestFocus(); | |
| 116 else | |
| 117 MessageView::RequestFocus(); | |
| 118 } | |
| 119 | |
| 120 void CustomNotificationView::OnPaint(gfx::Canvas* canvas) { | |
| 121 MessageView::OnPaint(canvas); | |
| 122 if (contents_view_ && contents_view_->IsFocusable()) | |
| 123 views::Painter::PaintFocusPainter(contents_view_, canvas, | |
| 124 focus_painter_.get()); | |
| 125 } | |
| 126 | |
| 127 bool CustomNotificationView::OnKeyPressed(const ui::KeyEvent& event) { | |
| 128 if (contents_view_) { | |
| 129 ui::InputMethod* input_method = contents_view_->GetInputMethod(); | |
| 130 if (input_method) { | |
| 131 ui::TextInputClient* text_input_client = | |
| 132 input_method->GetTextInputClient(); | |
| 133 if (text_input_client && | |
| 134 text_input_client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) { | |
| 135 // If the focus is in an edit box, we skip the special key handling for | |
| 136 // back space and return keys. So that these key events are sent to the | |
| 137 // arc container correctly without being handled by the message center. | |
| 138 return false; | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 return MessageView::OnKeyPressed(event); | |
| 144 } | |
| 145 | |
| 146 void CustomNotificationView::ChildPreferredSizeChanged(View* child) { | |
| 147 // Notify MessageCenterController when the custom content changes size, | |
| 148 // as it may need to relayout. | |
| 149 if (controller()) | |
| 150 controller()->UpdateNotificationSize(notification_id()); | |
| 151 } | |
| 152 | |
| 153 bool CustomNotificationView::HandleAccessibleAction( | |
| 154 const ui::AXActionData& action) { | |
| 155 if (contents_view_) | |
| 156 return contents_view_->HandleAccessibleAction(action); | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 } // namespace message_center | |
| OLD | NEW |