| 1 | // Copyright 2023 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 | |
| 6 | use accesskit::Role; |
| 7 | |
| 8 | use crate::node::Node; |
| 9 | |
| 10 | #[derive (Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] |
| 11 | pub enum FilterResult { |
| 12 | Include, |
| 13 | ExcludeNode, |
| 14 | ExcludeSubtree, |
| 15 | } |
| 16 | |
| 17 | pub fn common_filter(node: &Node) -> FilterResult { |
| 18 | if node.is_focused() { |
| 19 | return FilterResult::Include; |
| 20 | } |
| 21 | |
| 22 | if node.is_hidden() { |
| 23 | return FilterResult::ExcludeSubtree; |
| 24 | } |
| 25 | |
| 26 | if let Some(parent: Node<'_>) = node.parent() { |
| 27 | if common_filter(&parent) == FilterResult::ExcludeSubtree { |
| 28 | return FilterResult::ExcludeSubtree; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | let role: Role = node.role(); |
| 33 | if role == Role::GenericContainer || role == Role::TextRun { |
| 34 | return FilterResult::ExcludeNode; |
| 35 | } |
| 36 | |
| 37 | FilterResult::Include |
| 38 | } |
| 39 | |
| 40 | pub fn common_filter_with_root_exception(node: &Node) -> FilterResult { |
| 41 | if node.is_root() { |
| 42 | return FilterResult::Include; |
| 43 | } |
| 44 | common_filter(node) |
| 45 | } |
| 46 | |