OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #ifndef UI_ACCESSIBILITY_AX_ABSTRACT_POSITION_H_ |
| 6 #define UI_ACCESSIBILITY_AX_ABSTRACT_POSITION_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "ui/accessibility/ax_enums.h" |
| 13 #include "ui/accessibility/ax_export.h" |
| 14 |
| 15 namespace ui { |
| 16 |
| 17 class AXAbstractPosition; |
| 18 |
| 19 // Defines the type of position in the accessibility tree. |
| 20 // A tree position is used when referring to a specific child of a node in the |
| 21 // accessibility tree. |
| 22 // A text position is used when referring to a specific character of text inside |
| 23 // a particular node. |
| 24 // A null position is used to signify that the provided data is invalid or that |
| 25 // a boundary has been reached. |
| 26 enum class AXPositionKind { NULL_POSITION, TREE_POSITION, TEXT_POSITION }; |
| 27 |
| 28 // Plain old data to serialize and deserialize a position. See AXPosition. |
| 29 struct AX_EXPORT AXPositionData { |
| 30 AXPositionKind kind; |
| 31 int tree_id; |
| 32 int32_t anchor_id; |
| 33 int child_index; |
| 34 int text_offset; |
| 35 AXTextAffinity affinity; |
| 36 }; |
| 37 |
| 38 // An immutable move-only type that wraps a polymorphic AXAbstractPosition. |
| 39 class AX_EXPORT AXPositionPointer { |
| 40 public: |
| 41 // Allow moving. Declaring this implicitly deletes other constructors. |
| 42 AXPositionPointer(AXPositionPointer&& other); |
| 43 |
| 44 // Allow implicit upcasting when using std::move on a std::unique_ptr. |
| 45 template <class Concrete> |
| 46 AXPositionPointer(std::unique_ptr<Concrete> other) : ptr_(other.release()) {} |
| 47 |
| 48 // Allow implicit conversion from nullptr. |
| 49 AXPositionPointer(std::nullptr_t); |
| 50 |
| 51 ~AXPositionPointer(); |
| 52 |
| 53 explicit operator bool() const { return ptr_.get() != nullptr; } |
| 54 const AXAbstractPosition* operator->() const { return ptr_.get(); } |
| 55 |
| 56 // Downcasting must be explicit. |
| 57 template <class Concrete> |
| 58 const Concrete* DownCastTo() const { |
| 59 return static_cast<Concrete*>(ptr_.get()); |
| 60 } |
| 61 |
| 62 private: |
| 63 std::unique_ptr<AXAbstractPosition> ptr_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(AXPositionPointer); |
| 66 }; |
| 67 |
| 68 using AXRangePointer = std::pair<AXPositionPointer, AXPositionPointer>; |
| 69 |
| 70 // Represents a tree or text position. An AXAbstractPosition exposes the methods |
| 71 // required to manipulate the position, without having to know details of how to |
| 72 // represent it. |
| 73 class AX_EXPORT AXAbstractPosition { |
| 74 public: |
| 75 static constexpr int INVALID_TREE_ID = -1; |
| 76 static constexpr int32_t INVALID_ANCHOR_ID = -1; |
| 77 static constexpr int BEFORE_TEXT = -1; |
| 78 static constexpr int INVALID_INDEX = -2; |
| 79 static constexpr int INVALID_OFFSET = -1; |
| 80 |
| 81 // TODO(tapted): Make this constexpr. It can't be currently because it tickles |
| 82 // a bogus warning due to an MSVC compiler bug -- https://goo.gl/TGW4Ji . The |
| 83 // warning can be suppressed, but this is usually passed by reference anyway. |
| 84 static const AXPositionData kNullData; |
| 85 |
| 86 virtual ~AXAbstractPosition() {} |
| 87 |
| 88 virtual void ToData(AXPositionData* data) const = 0; |
| 89 virtual bool IsNull() const = 0; |
| 90 |
| 91 // Compare according to the "Compare" C++ standard concept (this < other). |
| 92 virtual bool Compare(const AXPositionPointer& other) const = 0; |
| 93 |
| 94 virtual AXPositionPointer PositionAtEndOfAnchor() const = 0; |
| 95 virtual AXPositionPointer PositionAtStartOfAnchor() const = 0; |
| 96 |
| 97 virtual AXPositionPointer NextCharacterPosition() const = 0; |
| 98 virtual AXPositionPointer PreviousCharacterPosition() const = 0; |
| 99 |
| 100 virtual AXPositionPointer NextWordStartPosition() const = 0; |
| 101 virtual AXPositionPointer PreviousWordStartPosition() const = 0; |
| 102 virtual AXPositionPointer NextWordEndPosition() const = 0; |
| 103 virtual AXPositionPointer PreviousWordEndPosition() const = 0; |
| 104 |
| 105 virtual AXPositionPointer NextLineStartPosition() const = 0; |
| 106 virtual AXPositionPointer PreviousLineStartPosition() const = 0; |
| 107 virtual AXPositionPointer NextLineEndPosition() const = 0; |
| 108 virtual AXPositionPointer PreviousLineEndPosition() const = 0; |
| 109 |
| 110 protected: |
| 111 AXAbstractPosition() {} |
| 112 }; |
| 113 |
| 114 } // namespace ui |
| 115 |
| 116 #endif // UI_ACCESSIBILITY_AX_ABSTRACT_POSITION_H_ |
OLD | NEW |