OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_auralinux.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/singleton.h" |
| 11 #include "ui/accessibility/ax_enums.h" |
| 12 #include "ui/accessibility/ax_node_data.h" |
| 13 #include "ui/accessibility/platform/ax_platform_node_auralinux.h" |
| 14 #include "ui/accessibility/platform/ax_platform_node_delegate.h" |
| 15 #include "ui/gfx/native_widget_types.h" |
| 16 #include "ui/views/views_delegate.h" |
| 17 #include "ui/views/widget/widget.h" |
| 18 #include "ui/views/widget/widget_observer.h" |
| 19 |
| 20 namespace views { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // ATK requires that we have a single root "application" object that's the |
| 25 // owner of all other windows. This is a simple class that implements the |
| 26 // AXPlatformNodeDelegate interface so we can create such an application |
| 27 // object. Every time we create an accessibility object for a View, we add its |
| 28 // top-level widget to a vector so we can return the list of all top-level |
| 29 // windows as children of this application object. |
| 30 class AuraLinuxApplication |
| 31 : public ui::AXPlatformNodeDelegate, |
| 32 public WidgetObserver { |
| 33 public: |
| 34 // Get the single instance of this class. |
| 35 static AuraLinuxApplication* GetInstance() { |
| 36 return Singleton<AuraLinuxApplication>::get(); |
| 37 } |
| 38 |
| 39 // Called every time we create a new accessibility on a View. |
| 40 // Add the top-level widget to our registry so that we can enumerate all |
| 41 // top-level widgets. |
| 42 void RegisterWidget(Widget* widget) { |
| 43 if (!widget) |
| 44 return; |
| 45 |
| 46 widget = widget->GetTopLevelWidget(); |
| 47 if (std::find(widgets_.begin(), widgets_.end(), widget) != widgets_.end()) |
| 48 return; |
| 49 |
| 50 widgets_.push_back(widget); |
| 51 widget->AddObserver(this); |
| 52 } |
| 53 |
| 54 gfx::NativeViewAccessible GetNativeViewAccessible() { |
| 55 return platform_node_->GetNativeViewAccessible(); |
| 56 } |
| 57 |
| 58 // |
| 59 // WidgetObserver overrides. |
| 60 // |
| 61 |
| 62 void OnWidgetDestroying(Widget* widget) override { |
| 63 auto iter = std::find(widgets_.begin(), widgets_.end(), widget); |
| 64 if (iter != widgets_.end()) |
| 65 widgets_.erase(iter); |
| 66 } |
| 67 |
| 68 // |
| 69 // ui::AXPlatformNodeDelegate overrides. |
| 70 // |
| 71 |
| 72 const ui::AXNodeData& GetData() override { |
| 73 return data_; |
| 74 } |
| 75 |
| 76 gfx::NativeViewAccessible GetParent() override { |
| 77 return nullptr; |
| 78 } |
| 79 |
| 80 int GetChildCount() override { |
| 81 return static_cast<int>(widgets_.size()); |
| 82 } |
| 83 |
| 84 gfx::NativeViewAccessible ChildAtIndex(int index) override { |
| 85 if (index < 0 || index >= GetChildCount()) |
| 86 return nullptr; |
| 87 |
| 88 Widget* widget = widgets_[index]; |
| 89 CHECK(widget); |
| 90 return widget->GetRootView()->GetNativeViewAccessible(); |
| 91 } |
| 92 |
| 93 gfx::Vector2d GetGlobalCoordinateOffset() override { |
| 94 return gfx::Vector2d(); |
| 95 } |
| 96 |
| 97 gfx::NativeViewAccessible HitTestSync(int x, int y) override { |
| 98 return nullptr; |
| 99 } |
| 100 |
| 101 gfx::NativeViewAccessible GetFocus() override { |
| 102 return nullptr; |
| 103 } |
| 104 |
| 105 gfx::AcceleratedWidget GetTargetForNativeAccessibilityEvent() override { |
| 106 return gfx::kNullAcceleratedWidget; |
| 107 } |
| 108 |
| 109 void DoDefaultAction() override { |
| 110 } |
| 111 |
| 112 bool SetStringValue(const base::string16& new_value) override { |
| 113 return false; |
| 114 } |
| 115 |
| 116 private: |
| 117 friend struct DefaultSingletonTraits<AuraLinuxApplication>; |
| 118 |
| 119 AuraLinuxApplication() |
| 120 : platform_node_(ui::AXPlatformNode::Create(this)) { |
| 121 data_.role = ui::AX_ROLE_APPLICATION; |
| 122 if (ViewsDelegate::views_delegate) { |
| 123 data_.AddStringAttribute( |
| 124 ui::AX_ATTR_NAME, |
| 125 ViewsDelegate::views_delegate->GetApplicationName()); |
| 126 } |
| 127 ui::AXPlatformNodeAuraLinux::SetApplication(platform_node_); |
| 128 } |
| 129 |
| 130 ~AuraLinuxApplication() override { |
| 131 platform_node_->Destroy(); |
| 132 platform_node_ = nullptr; |
| 133 } |
| 134 |
| 135 ui::AXPlatformNode* platform_node_; |
| 136 ui::AXNodeData data_; |
| 137 std::vector<Widget*> widgets_; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(AuraLinuxApplication); |
| 140 }; |
| 141 |
| 142 } // namespace |
| 143 |
| 144 // static |
| 145 NativeViewAccessibility* NativeViewAccessibility::Create(View* view) { |
| 146 AuraLinuxApplication::GetInstance()->RegisterWidget(view->GetWidget()); |
| 147 return new NativeViewAccessibilityAuraLinux(view); |
| 148 } |
| 149 |
| 150 NativeViewAccessibilityAuraLinux::NativeViewAccessibilityAuraLinux(View* view) |
| 151 : NativeViewAccessibility(view) { |
| 152 } |
| 153 |
| 154 NativeViewAccessibilityAuraLinux::~NativeViewAccessibilityAuraLinux() { |
| 155 } |
| 156 |
| 157 gfx::NativeViewAccessible NativeViewAccessibilityAuraLinux::GetParent() { |
| 158 gfx::NativeViewAccessible parent = NativeViewAccessibility::GetParent(); |
| 159 if (!parent) |
| 160 parent = AuraLinuxApplication::GetInstance()->GetNativeViewAccessible(); |
| 161 return parent; |
| 162 } |
| 163 |
| 164 } // namespace views |
OLD | NEW |