| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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/views/accessibility/native_view_accessibility.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "ui/events/event_utils.h" | |
| 10 #include "ui/gfx/native_widget_types.h" | |
| 11 #include "ui/views/controls/native/native_view_host.h" | |
| 12 #include "ui/views/view.h" | |
| 13 #include "ui/views/widget/widget.h" | |
| 14 | |
| 15 namespace views { | |
| 16 | |
| 17 #if !defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) | |
| 18 // static | |
| 19 std::unique_ptr<NativeViewAccessibility> NativeViewAccessibility::Create( | |
| 20 View* view) { | |
| 21 // Use WrapUnique over MakeUnique to invoke the protected constructor. | |
| 22 return base::WrapUnique<NativeViewAccessibility>( | |
| 23 new NativeViewAccessibility(view)); | |
| 24 } | |
| 25 #endif // !defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) | |
| 26 | |
| 27 NativeViewAccessibility::NativeViewAccessibility(View* view) | |
| 28 : view_(view), | |
| 29 parent_widget_(nullptr), | |
| 30 ax_node_(nullptr) { | |
| 31 ax_node_ = ui::AXPlatformNode::Create(this); | |
| 32 } | |
| 33 | |
| 34 NativeViewAccessibility::~NativeViewAccessibility() { | |
| 35 if (ax_node_) | |
| 36 ax_node_->Destroy(); | |
| 37 if (parent_widget_) | |
| 38 parent_widget_->RemoveObserver(this); | |
| 39 } | |
| 40 | |
| 41 gfx::NativeViewAccessible NativeViewAccessibility::GetNativeObject() { | |
| 42 return ax_node_ ? ax_node_->GetNativeViewAccessible() : nullptr; | |
| 43 } | |
| 44 | |
| 45 void NativeViewAccessibility::NotifyAccessibilityEvent(ui::AXEvent event_type) { | |
| 46 if (ax_node_) | |
| 47 ax_node_->NotifyAccessibilityEvent(event_type); | |
| 48 } | |
| 49 | |
| 50 bool NativeViewAccessibility::SetFocused(bool focused) { | |
| 51 if (!ui::AXNodeData::IsFlagSet(GetData().state, ui::AX_STATE_FOCUSABLE)) | |
| 52 return false; | |
| 53 | |
| 54 if (focused) | |
| 55 view_->RequestFocus(); | |
| 56 else if (view_->HasFocus()) | |
| 57 view_->GetFocusManager()->ClearFocus(); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 // ui::AXPlatformNodeDelegate | |
| 62 | |
| 63 const ui::AXNodeData& NativeViewAccessibility::GetData() { | |
| 64 data_ = ui::AXNodeData(); | |
| 65 data_.state = 0; | |
| 66 | |
| 67 // Views may misbehave if their widget is closed; return an unknown role | |
| 68 // rather than possibly crashing. | |
| 69 if (!view_->GetWidget() || view_->GetWidget()->IsClosed()) { | |
| 70 data_.role = ui::AX_ROLE_UNKNOWN; | |
| 71 data_.state = 1 << ui::AX_STATE_DISABLED; | |
| 72 return data_; | |
| 73 } | |
| 74 | |
| 75 view_->GetAccessibleNodeData(&data_); | |
| 76 data_.location = gfx::RectF(view_->GetBoundsInScreen()); | |
| 77 base::string16 description; | |
| 78 view_->GetTooltipText(gfx::Point(), &description); | |
| 79 data_.AddStringAttribute(ui::AX_ATTR_DESCRIPTION, | |
| 80 base::UTF16ToUTF8(description)); | |
| 81 | |
| 82 if (view_->IsAccessibilityFocusable()) | |
| 83 data_.state |= (1 << ui::AX_STATE_FOCUSABLE); | |
| 84 | |
| 85 if (!view_->enabled()) | |
| 86 data_.state |= (1 << ui::AX_STATE_DISABLED); | |
| 87 | |
| 88 if (!view_->IsDrawn()) | |
| 89 data_.state |= (1 << ui::AX_STATE_INVISIBLE); | |
| 90 | |
| 91 return data_; | |
| 92 } | |
| 93 | |
| 94 int NativeViewAccessibility::GetChildCount() { | |
| 95 int child_count = view_->child_count(); | |
| 96 | |
| 97 std::vector<Widget*> child_widgets; | |
| 98 PopulateChildWidgetVector(&child_widgets); | |
| 99 child_count += child_widgets.size(); | |
| 100 | |
| 101 return child_count; | |
| 102 } | |
| 103 | |
| 104 gfx::NativeViewAccessible NativeViewAccessibility::ChildAtIndex(int index) { | |
| 105 // If this is a root view, our widget might have child widgets. Include | |
| 106 std::vector<Widget*> child_widgets; | |
| 107 PopulateChildWidgetVector(&child_widgets); | |
| 108 int child_widget_count = static_cast<int>(child_widgets.size()); | |
| 109 | |
| 110 if (index < view_->child_count()) { | |
| 111 return view_->child_at(index)->GetNativeViewAccessible(); | |
| 112 } else if (index < view_->child_count() + child_widget_count) { | |
| 113 Widget* child_widget = child_widgets[index - view_->child_count()]; | |
| 114 return child_widget->GetRootView()->GetNativeViewAccessible(); | |
| 115 } | |
| 116 | |
| 117 return nullptr; | |
| 118 } | |
| 119 | |
| 120 gfx::NativeWindow NativeViewAccessibility::GetTopLevelWidget() { | |
| 121 if (view_->GetWidget()) | |
| 122 return view_->GetWidget()->GetTopLevelWidget()->GetNativeWindow(); | |
| 123 return nullptr; | |
| 124 } | |
| 125 | |
| 126 gfx::NativeViewAccessible NativeViewAccessibility::GetParent() { | |
| 127 if (view_->parent()) | |
| 128 return view_->parent()->GetNativeViewAccessible(); | |
| 129 | |
| 130 if (parent_widget_) | |
| 131 return parent_widget_->GetRootView()->GetNativeViewAccessible(); | |
| 132 | |
| 133 return nullptr; | |
| 134 } | |
| 135 | |
| 136 gfx::Vector2d NativeViewAccessibility::GetGlobalCoordinateOffset() { | |
| 137 return gfx::Vector2d(0, 0); // location is already in screen coordinates. | |
| 138 } | |
| 139 | |
| 140 gfx::NativeViewAccessible NativeViewAccessibility::HitTestSync(int x, int y) { | |
| 141 if (!view_ || !view_->GetWidget()) | |
| 142 return nullptr; | |
| 143 | |
| 144 // Search child widgets first, since they're on top in the z-order. | |
| 145 std::vector<Widget*> child_widgets; | |
| 146 PopulateChildWidgetVector(&child_widgets); | |
| 147 for (Widget* child_widget : child_widgets) { | |
| 148 View* child_root_view = child_widget->GetRootView(); | |
| 149 gfx::Point point(x, y); | |
| 150 View::ConvertPointFromScreen(child_root_view, &point); | |
| 151 if (child_root_view->HitTestPoint(point)) | |
| 152 return child_root_view->GetNativeViewAccessible(); | |
| 153 } | |
| 154 | |
| 155 gfx::Point point(x, y); | |
| 156 View::ConvertPointFromScreen(view_, &point); | |
| 157 if (!view_->HitTestPoint(point)) | |
| 158 return nullptr; | |
| 159 | |
| 160 // Check if the point is within any of the immediate children of this | |
| 161 // view. We don't have to search further because AXPlatformNode will | |
| 162 // do a recursive hit test if we return anything other than |this| or NULL. | |
| 163 for (int i = view_->child_count() - 1; i >= 0; --i) { | |
| 164 View* child_view = view_->child_at(i); | |
| 165 if (!child_view->visible()) | |
| 166 continue; | |
| 167 | |
| 168 gfx::Point point_in_child_coords(point); | |
| 169 view_->ConvertPointToTarget(view_, child_view, &point_in_child_coords); | |
| 170 if (child_view->HitTestPoint(point_in_child_coords)) | |
| 171 return child_view->GetNativeViewAccessible(); | |
| 172 } | |
| 173 | |
| 174 // If it's not inside any of our children, it's inside this view. | |
| 175 return GetNativeObject(); | |
| 176 } | |
| 177 | |
| 178 gfx::NativeViewAccessible NativeViewAccessibility::GetFocus() { | |
| 179 FocusManager* focus_manager = view_->GetFocusManager(); | |
| 180 View* focused_view = | |
| 181 focus_manager ? focus_manager->GetFocusedView() : nullptr; | |
| 182 return focused_view ? focused_view->GetNativeViewAccessible() : nullptr; | |
| 183 } | |
| 184 | |
| 185 gfx::AcceleratedWidget | |
| 186 NativeViewAccessibility::GetTargetForNativeAccessibilityEvent() { | |
| 187 return gfx::kNullAcceleratedWidget; | |
| 188 } | |
| 189 | |
| 190 bool NativeViewAccessibility::AccessibilityPerformAction( | |
| 191 const ui::AXActionData& data) { | |
| 192 switch (data.action) { | |
| 193 // Handle accessible actions that apply to all Views here. | |
| 194 case ui::AX_ACTION_DO_DEFAULT: | |
| 195 DoDefaultAction(); | |
| 196 return true; | |
| 197 case ui::AX_ACTION_FOCUS: | |
| 198 return SetFocused(true); | |
| 199 case ui::AX_ACTION_BLUR: | |
| 200 return SetFocused(false); | |
| 201 | |
| 202 case ui::AX_ACTION_NONE: | |
| 203 NOTREACHED(); | |
| 204 break; | |
| 205 | |
| 206 // All other actions can potentially be dealt with by the View itself. | |
| 207 default: | |
| 208 return view_->HandleAccessibleAction(data); | |
| 209 break; | |
| 210 } | |
| 211 return false; | |
| 212 } | |
| 213 | |
| 214 void NativeViewAccessibility::DoDefaultAction() { | |
| 215 gfx::Point center = view_->GetLocalBounds().CenterPoint(); | |
| 216 view_->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED, | |
| 217 center, | |
| 218 center, | |
| 219 ui::EventTimeForNow(), | |
| 220 ui::EF_LEFT_MOUSE_BUTTON, | |
| 221 ui::EF_LEFT_MOUSE_BUTTON)); | |
| 222 view_->OnMouseReleased(ui::MouseEvent(ui::ET_MOUSE_RELEASED, | |
| 223 center, | |
| 224 center, | |
| 225 ui::EventTimeForNow(), | |
| 226 ui::EF_LEFT_MOUSE_BUTTON, | |
| 227 ui::EF_LEFT_MOUSE_BUTTON)); | |
| 228 } | |
| 229 | |
| 230 void NativeViewAccessibility::OnWidgetDestroying(Widget* widget) { | |
| 231 if (parent_widget_ == widget) { | |
| 232 parent_widget_->RemoveObserver(this); | |
| 233 parent_widget_ = nullptr; | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 void NativeViewAccessibility::SetParentWidget(Widget* parent_widget) { | |
| 238 if (parent_widget_) | |
| 239 parent_widget_->RemoveObserver(this); | |
| 240 parent_widget_ = parent_widget; | |
| 241 parent_widget_->AddObserver(this); | |
| 242 } | |
| 243 | |
| 244 void NativeViewAccessibility::PopulateChildWidgetVector( | |
| 245 std::vector<Widget*>* result_child_widgets) { | |
| 246 // Only attach child widgets to the root view. | |
| 247 Widget* widget = view_->GetWidget(); | |
| 248 // Note that during window close, a Widget may exist in a state where it has | |
| 249 // no NativeView, but hasn't yet torn down its view hierarchy. | |
| 250 if (!widget || !widget->GetNativeView() || widget->GetRootView() != view_) | |
| 251 return; | |
| 252 | |
| 253 std::set<Widget*> child_widgets; | |
| 254 Widget::GetAllOwnedWidgets(widget->GetNativeView(), &child_widgets); | |
| 255 for (auto iter = child_widgets.begin(); iter != child_widgets.end(); ++iter) { | |
| 256 Widget* child_widget = *iter; | |
| 257 DCHECK_NE(widget, child_widget); | |
| 258 | |
| 259 if (!child_widget->IsVisible()) | |
| 260 continue; | |
| 261 | |
| 262 if (widget->GetNativeWindowProperty(kWidgetNativeViewHostKey)) | |
| 263 continue; | |
| 264 | |
| 265 gfx::NativeViewAccessible child_widget_accessible = | |
| 266 child_widget->GetRootView()->GetNativeViewAccessible(); | |
| 267 ui::AXPlatformNode* child_widget_platform_node = | |
| 268 ui::AXPlatformNode::FromNativeViewAccessible(child_widget_accessible); | |
| 269 if (child_widget_platform_node) { | |
| 270 NativeViewAccessibility* child_widget_view_accessibility = | |
| 271 static_cast<NativeViewAccessibility*>( | |
| 272 child_widget_platform_node->GetDelegate()); | |
| 273 if (child_widget_view_accessibility->parent_widget() != widget) | |
| 274 child_widget_view_accessibility->SetParentWidget(widget); | |
| 275 } | |
| 276 | |
| 277 result_child_widgets->push_back(child_widget); | |
| 278 } | |
| 279 } | |
| 280 | |
| 281 } // namespace views | |
| OLD | NEW |