summaryrefslogtreecommitdiffstats
path: root/ui/views/accessibility/ax_view_obj_wrapper.cc
blob: 3ea1e04efb2fe687a8ff4e72c6a43c4b1923c966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/accessibility/ax_view_obj_wrapper.h"

#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/events/event_utils.h"
#include "ui/views/accessibility/ax_aura_obj_cache.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace views {

AXViewObjWrapper::AXViewObjWrapper(View* view)  : view_(view) {
  if (view->GetWidget())
    AXAuraObjCache::GetInstance()->GetOrCreate(view->GetWidget());
}

AXViewObjWrapper::~AXViewObjWrapper() {}

AXAuraObjWrapper* AXViewObjWrapper::GetParent() {
  AXAuraObjCache* cache = AXAuraObjCache::GetInstance();
  if (view_->parent())
    return cache->GetOrCreate(view_->parent());

  if (view_->GetWidget())
    return cache->GetOrCreate(view_->GetWidget());

  return NULL;
}

void AXViewObjWrapper::GetChildren(
    std::vector<AXAuraObjWrapper*>* out_children) {
  // TODO(dtseng): Need to handle |Widget| child of |View|.
  for (int i = 0; i < view_->child_count(); ++i) {
    if (!view_->child_at(i)->visible())
      continue;

    AXAuraObjWrapper* child =
        AXAuraObjCache::GetInstance()->GetOrCreate(view_->child_at(i));
    out_children->push_back(child);
  }
}

void AXViewObjWrapper::Serialize(ui::AXNodeData* out_node_data) {
  ui::AXViewState view_data;
  view_->GetAccessibleState(&view_data);

  out_node_data->id = GetID();
  out_node_data->role = view_data.role;

  out_node_data->state = view_data.state();
  if (view_->IsFocusable())
    out_node_data->state |= 1 << ui::AX_STATE_FOCUSABLE;
  if (!view_->visible())
    out_node_data->state |= 1 << ui::AX_STATE_INVISIBLE;

  out_node_data->location = view_->GetBoundsInScreen();

  out_node_data->AddStringAttribute(
      ui::AX_ATTR_NAME, base::UTF16ToUTF8(view_data.name));
  out_node_data->AddStringAttribute(
      ui::AX_ATTR_VALUE, base::UTF16ToUTF8(view_data.value));

  if (view_data.selection_start > -1 && view_data.selection_end > -1) {
    out_node_data->AddIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
                                   view_data.selection_start);

    out_node_data->AddIntAttribute(ui::AX_ATTR_TEXT_SEL_END,
                                   view_data.selection_end);
  }
}

int32_t AXViewObjWrapper::GetID() {
  return AXAuraObjCache::GetInstance()->GetID(view_);
}

void AXViewObjWrapper::DoDefault() {
  gfx::Rect rect = view_->GetLocalBounds();
  gfx::Point center = rect.CenterPoint();
  view_->OnMousePressed(ui::MouseEvent(
      ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
      ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
  view_->OnMouseReleased(ui::MouseEvent(
      ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
      ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
}

void AXViewObjWrapper::Focus() {
  view_->RequestFocus();
}

void AXViewObjWrapper::MakeVisible() {
  // TODO(dtseng): Implement.
}

void AXViewObjWrapper::SetSelection(int32_t start, int32_t end) {
  // TODO(dtseng): Implement.
}

void AXViewObjWrapper::ShowContextMenu() {
  view_->ShowContextMenu(view_->bounds().CenterPoint(),
                         ui::MENU_SOURCE_KEYBOARD);
}

}  // namespace views