accesskit_unix/atspi/interfaces/
selection.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;
7use zbus::{fdo, interface, names::OwnedUniqueName};
8
9use crate::atspi::{ObjectId, OwnedObjectAddress};
10
11pub(crate) struct SelectionInterface {
12    bus_name: OwnedUniqueName,
13    node: PlatformNode,
14}
15
16impl SelectionInterface {
17    pub fn new(bus_name: OwnedUniqueName, node: PlatformNode) -> Self {
18        Self { bus_name, node }
19    }
20
21    fn map_error(&self) -> impl '_ + FnOnce(accesskit_atspi_common::Error) -> fdo::Error {
22        |error| crate::util::map_error_from_node(&self.node, error)
23    }
24}
25
26#[interface(name = "org.a11y.atspi.Selection")]
27impl SelectionInterface {
28    #[zbus(property)]
29    fn n_selected_children(&self) -> fdo::Result<i32> {
30        self.node.n_selected_children().map_err(self.map_error())
31    }
32
33    fn get_selected_child(&self, selected_child_index: i32) -> fdo::Result<(OwnedObjectAddress,)> {
34        let child = self
35            .node
36            .selected_child(map_child_index(selected_child_index)?)
37            .map_err(self.map_error())?
38            .map(|child| ObjectId::Node {
39                adapter: self.node.adapter_id(),
40                node: child,
41            });
42        Ok(super::optional_object_address(&self.bus_name, child))
43    }
44
45    fn select_child(&self, child_index: i32) -> fdo::Result<bool> {
46        self.node
47            .select_child(map_child_index(child_index)?)
48            .map_err(self.map_error())
49    }
50
51    fn deselect_selected_child(&self, selected_child_index: i32) -> fdo::Result<bool> {
52        self.node
53            .deselect_selected_child(map_child_index(selected_child_index)?)
54            .map_err(self.map_error())
55    }
56
57    fn is_child_selected(&self, child_index: i32) -> fdo::Result<bool> {
58        self.node
59            .is_child_selected(map_child_index(child_index)?)
60            .map_err(self.map_error())
61    }
62
63    fn select_all(&self) -> fdo::Result<bool> {
64        self.node.select_all().map_err(self.map_error())
65    }
66
67    fn clear_selection(&self) -> fdo::Result<bool> {
68        self.node.clear_selection().map_err(self.map_error())
69    }
70
71    fn deselect_child(&self, child_index: i32) -> fdo::Result<bool> {
72        self.node
73            .deselect_child(map_child_index(child_index)?)
74            .map_err(self.map_error())
75    }
76}
77
78fn map_child_index(index: i32) -> fdo::Result<usize> {
79    index
80        .try_into()
81        .map_err(|_| fdo::Error::InvalidArgs("Index can't be negative.".into()))
82}