accesskit_unix/atspi/interfaces/
text.rs

1// Copyright 2024 The AccessKit Authors. All rights reserved.
2// Licensed under the Apache License, Version 2.0 (found in
3// the LICENSE-APACHE file) or the MIT license (found in
4// the LICENSE-MIT file), at your option.
5
6use accesskit_atspi_common::{PlatformNode, Rect};
7use atspi::{CoordType, Granularity, ScrollType};
8use std::collections::HashMap;
9use zbus::fdo;
10
11pub(crate) struct TextInterface {
12    node: PlatformNode,
13}
14
15impl TextInterface {
16    pub fn new(node: PlatformNode) -> Self {
17        Self { node }
18    }
19
20    fn map_error(&self) -> impl '_ + FnOnce(accesskit_atspi_common::Error) -> fdo::Error {
21        |error| crate::util::map_error_from_node(&self.node, error)
22    }
23}
24
25#[dbus_interface(name = "org.a11y.atspi.Text")]
26impl TextInterface {
27    #[dbus_interface(property)]
28    fn character_count(&self) -> fdo::Result<i32> {
29        self.node.character_count().map_err(self.map_error())
30    }
31
32    #[dbus_interface(property)]
33    fn caret_offset(&self) -> fdo::Result<i32> {
34        self.node.caret_offset().map_err(self.map_error())
35    }
36
37    fn get_string_at_offset(
38        &self,
39        offset: i32,
40        granularity: Granularity,
41    ) -> fdo::Result<(String, i32, i32)> {
42        self.node
43            .string_at_offset(offset, granularity)
44            .map_err(self.map_error())
45    }
46
47    fn get_text(&self, start_offset: i32, end_offset: i32) -> fdo::Result<String> {
48        self.node
49            .text(start_offset, end_offset)
50            .map_err(self.map_error())
51    }
52
53    fn set_caret_offset(&self, offset: i32) -> fdo::Result<bool> {
54        self.node.set_caret_offset(offset).map_err(self.map_error())
55    }
56
57    fn get_attribute_value(&self, offset: i32, attribute_name: &str) -> fdo::Result<String> {
58        self.node
59            .text_attribute_value(offset, attribute_name)
60            .map_err(self.map_error())
61    }
62
63    fn get_attributes(&self, offset: i32) -> fdo::Result<(HashMap<String, String>, i32, i32)> {
64        self.node.text_attributes(offset).map_err(self.map_error())
65    }
66
67    fn get_default_attributes(&self) -> fdo::Result<HashMap<String, String>> {
68        self.node
69            .default_text_attributes()
70            .map_err(self.map_error())
71    }
72
73    fn get_character_extents(&self, offset: i32, coord_type: CoordType) -> fdo::Result<Rect> {
74        self.node
75            .character_extents(offset, coord_type)
76            .map_err(self.map_error())
77    }
78
79    fn get_offset_at_point(&self, x: i32, y: i32, coord_type: CoordType) -> fdo::Result<i32> {
80        self.node
81            .offset_at_point(x, y, coord_type)
82            .map_err(self.map_error())
83    }
84
85    fn get_n_selections(&self) -> fdo::Result<i32> {
86        self.node.n_selections().map_err(self.map_error())
87    }
88
89    fn get_selection(&self, selection_num: i32) -> fdo::Result<(i32, i32)> {
90        self.node.selection(selection_num).map_err(self.map_error())
91    }
92
93    fn add_selection(&self, start_offset: i32, end_offset: i32) -> fdo::Result<bool> {
94        self.node
95            .add_selection(start_offset, end_offset)
96            .map_err(self.map_error())
97    }
98
99    fn remove_selection(&self, selection_num: i32) -> fdo::Result<bool> {
100        self.node
101            .remove_selection(selection_num)
102            .map_err(self.map_error())
103    }
104
105    fn set_selection(
106        &self,
107        selection_num: i32,
108        start_offset: i32,
109        end_offset: i32,
110    ) -> fdo::Result<bool> {
111        self.node
112            .set_selection(selection_num, start_offset, end_offset)
113            .map_err(self.map_error())
114    }
115
116    fn get_range_extents(
117        &self,
118        start_offset: i32,
119        end_offset: i32,
120        coord_type: CoordType,
121    ) -> fdo::Result<Rect> {
122        self.node
123            .range_extents(start_offset, end_offset, coord_type)
124            .map_err(self.map_error())
125    }
126
127    fn get_attribute_run(
128        &self,
129        offset: i32,
130        include_defaults: bool,
131    ) -> fdo::Result<(HashMap<String, String>, i32, i32)> {
132        self.node
133            .text_attribute_run(offset, include_defaults)
134            .map_err(self.map_error())
135    }
136
137    fn scroll_substring_to(
138        &self,
139        start_offset: i32,
140        end_offset: i32,
141        scroll_type: ScrollType,
142    ) -> fdo::Result<bool> {
143        self.node
144            .scroll_substring_to(start_offset, end_offset, scroll_type)
145            .map_err(self.map_error())
146    }
147
148    fn scroll_substring_to_point(
149        &self,
150        start_offset: i32,
151        end_offset: i32,
152        coord_type: CoordType,
153        x: i32,
154        y: i32,
155    ) -> fdo::Result<bool> {
156        self.node
157            .scroll_substring_to_point(start_offset, end_offset, coord_type, x, y)
158            .map_err(self.map_error())
159    }
160}