| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/accessibility/native_view_accessibility.h" | 5 #include "ui/views/accessibility/native_view_accessibility.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "ui/accessibility/platform/ax_platform_node.h" |
| 8 #include "ui/events/event_utils.h" | 12 #include "ui/events/event_utils.h" |
| 9 #include "ui/gfx/native_widget_types.h" | 13 #include "ui/gfx/native_widget_types.h" |
| 10 #include "ui/views/controls/native/native_view_host.h" | 14 #include "ui/views/controls/native/native_view_host.h" |
| 11 #include "ui/views/view.h" | 15 #include "ui/views/view.h" |
| 12 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 13 | 17 |
| 14 namespace views { | 18 namespace views { |
| 15 | 19 |
| 16 #if !defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) | |
| 17 // static | 20 // static |
| 18 NativeViewAccessibility* NativeViewAccessibility::Create(View* view) { | 21 std::unique_ptr<NativeViewAccessibility> NativeViewAccessibility::CreateForView( |
| 19 return new NativeViewAccessibility(view); | 22 View* view) { |
| 23 return base::WrapUnique(Create(view)); |
| 20 } | 24 } |
| 21 #endif // !defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) | |
| 22 | 25 |
| 23 NativeViewAccessibility::NativeViewAccessibility(View* view) | 26 NativeViewAccessibility::NativeViewAccessibility(View* view) |
| 24 : view_(view), | 27 : view_(view), |
| 25 parent_widget_(nullptr), | 28 parent_widget_(nullptr), |
| 26 ax_node_(nullptr) { | 29 ax_node_(nullptr) { |
| 27 ax_node_ = ui::AXPlatformNode::Create(this); | 30 ax_node_ = ui::AXPlatformNode::Create(this); |
| 28 } | 31 } |
| 29 | 32 |
| 30 NativeViewAccessibility::~NativeViewAccessibility() { | 33 NativeViewAccessibility::~NativeViewAccessibility() { |
| 31 if (ax_node_) | 34 if (ax_node_) |
| 32 ax_node_->Destroy(); | 35 ax_node_->Destroy(); |
| 33 if (parent_widget_) | 36 if (parent_widget_) |
| 34 parent_widget_->RemoveObserver(this); | 37 parent_widget_->RemoveObserver(this); |
| 35 } | 38 } |
| 36 | 39 |
| 37 gfx::NativeViewAccessible NativeViewAccessibility::GetNativeObject() { | 40 gfx::NativeViewAccessible NativeViewAccessibility::GetNativeObject() { |
| 38 return ax_node_ ? ax_node_->GetNativeViewAccessible() : nullptr; | 41 return ax_node_ ? ax_node_->GetNativeViewAccessible() : nullptr; |
| 39 } | 42 } |
| 40 | 43 |
| 41 void NativeViewAccessibility::Destroy() { | |
| 42 delete this; | |
| 43 } | |
| 44 | |
| 45 void NativeViewAccessibility::NotifyAccessibilityEvent(ui::AXEvent event_type) { | 44 void NativeViewAccessibility::NotifyAccessibilityEvent(ui::AXEvent event_type) { |
| 46 if (ax_node_) | 45 if (ax_node_) |
| 47 ax_node_->NotifyAccessibilityEvent(event_type); | 46 ax_node_->NotifyAccessibilityEvent(event_type); |
| 48 } | 47 } |
| 49 | 48 |
| 50 bool NativeViewAccessibility::SetFocused(bool focused) { | 49 bool NativeViewAccessibility::SetFocused(bool focused) { |
| 51 if (!ui::AXNodeData::IsFlagSet(GetData().state, ui::AX_STATE_FOCUSABLE)) | 50 if (!ui::AXNodeData::IsFlagSet(GetData().state, ui::AX_STATE_FOCUSABLE)) |
| 52 return false; | 51 return false; |
| 53 | 52 |
| 54 if (focused) | 53 if (focused) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 gfx::NativeWindow NativeViewAccessibility::GetTopLevelWidget() { | 119 gfx::NativeWindow NativeViewAccessibility::GetTopLevelWidget() { |
| 121 if (view_->GetWidget()) | 120 if (view_->GetWidget()) |
| 122 return view_->GetWidget()->GetTopLevelWidget()->GetNativeWindow(); | 121 return view_->GetWidget()->GetTopLevelWidget()->GetNativeWindow(); |
| 123 return nullptr; | 122 return nullptr; |
| 124 } | 123 } |
| 125 | 124 |
| 126 gfx::NativeViewAccessible NativeViewAccessibility::GetParent() { | 125 gfx::NativeViewAccessible NativeViewAccessibility::GetParent() { |
| 127 if (view_->parent()) | 126 if (view_->parent()) |
| 128 return view_->parent()->GetNativeViewAccessible(); | 127 return view_->parent()->GetNativeViewAccessible(); |
| 129 | 128 |
| 130 if (parent_widget_) | 129 return GetNativeViewAccessibleForWidget(); |
| 131 return parent_widget_->GetRootView()->GetNativeViewAccessible(); | |
| 132 | |
| 133 return nullptr; | |
| 134 } | 130 } |
| 135 | 131 |
| 136 gfx::Vector2d NativeViewAccessibility::GetGlobalCoordinateOffset() { | 132 gfx::Vector2d NativeViewAccessibility::GetGlobalCoordinateOffset() { |
| 137 return gfx::Vector2d(0, 0); // location is already in screen coordinates. | 133 return gfx::Vector2d(0, 0); // location is already in screen coordinates. |
| 138 } | 134 } |
| 139 | 135 |
| 140 gfx::NativeViewAccessible NativeViewAccessibility::HitTestSync(int x, int y) { | 136 gfx::NativeViewAccessible NativeViewAccessibility::HitTestSync(int x, int y) { |
| 141 if (!view_ || !view_->GetWidget()) | 137 if (!view_ || !view_->GetWidget()) |
| 142 return nullptr; | 138 return nullptr; |
| 143 | 139 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 } | 230 } |
| 235 } | 231 } |
| 236 | 232 |
| 237 void NativeViewAccessibility::SetParentWidget(Widget* parent_widget) { | 233 void NativeViewAccessibility::SetParentWidget(Widget* parent_widget) { |
| 238 if (parent_widget_) | 234 if (parent_widget_) |
| 239 parent_widget_->RemoveObserver(this); | 235 parent_widget_->RemoveObserver(this); |
| 240 parent_widget_ = parent_widget; | 236 parent_widget_ = parent_widget; |
| 241 parent_widget_->AddObserver(this); | 237 parent_widget_->AddObserver(this); |
| 242 } | 238 } |
| 243 | 239 |
| 240 gfx::NativeViewAccessible |
| 241 NativeViewAccessibility::GetNativeViewAccessibleForWidget() { |
| 242 if (parent_widget_) |
| 243 return parent_widget_->GetRootView()->GetNativeViewAccessible(); |
| 244 return nullptr; |
| 245 } |
| 246 |
| 247 // static |
| 248 NativeViewAccessibility* NativeViewAccessibility::GetForView(View* view) { |
| 249 #if defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) |
| 250 // Retrieving the gfx::NativeViewAccessible also ensures that a |
| 251 // NativeViewAccessibility exists for the given View. |
| 252 gfx::NativeViewAccessible native_object = view->GetNativeViewAccessible(); |
| 253 ui::AXPlatformNode* node = |
| 254 ui::AXPlatformNode::FromNativeViewAccessible(native_object); |
| 255 DCHECK(node); |
| 256 return static_cast<NativeViewAccessibility*>(node->GetDelegate()); |
| 257 #else |
| 258 // Platforms (currently ChromeOS) without a native implementation also have no |
| 259 // gfx::NativeViewAccessible, so make sure a NativeViewAccessibility exists |
| 260 // and return the instance belonging to |view|. |
| 261 view->GetNativeViewAccessible(); |
| 262 return view->native_view_accessibility_.get(); |
| 263 #endif |
| 264 } |
| 265 |
| 266 #if !defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) |
| 267 // static |
| 268 NativeViewAccessibility* NativeViewAccessibility::Create(View* view) { |
| 269 return new NativeViewAccessibility(view); |
| 270 } |
| 271 #endif // !defined(PLATFORM_HAS_NATIVE_VIEW_ACCESSIBILITY_IMPL) |
| 272 |
| 244 void NativeViewAccessibility::PopulateChildWidgetVector( | 273 void NativeViewAccessibility::PopulateChildWidgetVector( |
| 245 std::vector<Widget*>* result_child_widgets) { | 274 std::vector<Widget*>* result_child_widgets) { |
| 246 // Only attach child widgets to the root view. | 275 // Only attach child widgets to the root view. |
| 247 Widget* widget = view_->GetWidget(); | 276 Widget* widget = view_->GetWidget(); |
| 248 if (!widget || widget->GetRootView() != view_) | 277 if (!widget || widget->GetRootView() != view_) |
| 249 return; | 278 return; |
| 250 | 279 |
| 251 std::set<Widget*> child_widgets; | 280 std::set<Widget*> child_widgets; |
| 252 Widget::GetAllOwnedWidgets(widget->GetNativeView(), &child_widgets); | 281 Widget::GetAllOwnedWidgets(widget->GetNativeView(), &child_widgets); |
| 253 for (auto iter = child_widgets.begin(); iter != child_widgets.end(); ++iter) { | 282 for (auto iter = child_widgets.begin(); iter != child_widgets.end(); ++iter) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 270 child_widget_platform_node->GetDelegate()); | 299 child_widget_platform_node->GetDelegate()); |
| 271 if (child_widget_view_accessibility->parent_widget() != widget) | 300 if (child_widget_view_accessibility->parent_widget() != widget) |
| 272 child_widget_view_accessibility->SetParentWidget(widget); | 301 child_widget_view_accessibility->SetParentWidget(widget); |
| 273 } | 302 } |
| 274 | 303 |
| 275 result_child_widgets->push_back(child_widget); | 304 result_child_widgets->push_back(child_widget); |
| 276 } | 305 } |
| 277 } | 306 } |
| 278 | 307 |
| 279 } // namespace views | 308 } // namespace views |
| OLD | NEW |