Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accessible-selectable and accessible-selected properties #6533

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api/cpp/include/slint-testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,32 @@ class ElementHandle
return std::nullopt;
}

/// Returns the accessible-selected of that element, if any.
std::optional<bool> accessible_selected() const
{
if (auto result = get_accessible_string_property(
cbindgen_private::AccessibleStringProperty::Selected)) {
if (*result == "true")
return true;
else if (*result == "false")
return false;
}
return std::nullopt;
}

/// Returns the accessible-selectable of that element, if any.
std::optional<bool> accessible_selectable() const
{
if (auto result = get_accessible_string_property(
cbindgen_private::AccessibleStringProperty::Selectable)) {
if (*result == "true")
return true;
else if (*result == "false")
return false;
}
return std::nullopt;
}

/// Sets the accessible-value of that element.
///
/// Setting the value will invoke the `accessible-action-set-value` callback.
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/src/language/builtins/elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Use the following `accessible-` properties to make your items interact well with
- **`accessible-value-step`** (_in_ _float_) The smallest increment or decrement by which the current value can change. This corresponds to the step by which a handle on a slider can be dragged.
- **`accessible-value`** (_in_ _string_): The current value of the item.
- **`accessible-placeholder-text`** (_in_ _string_): A placeholder text to use when the item's value is empty. Applies to text elements.
- **`accessible-selectable`** (_in_ _bool_): Whether the element can be selected or not.
- **`accessible-selected`** (_in_ _bool_): Whether the element is selected or not. This maps to the "is-selected" state of listview items.

You can also use the following callbacks that are going to be called by the accessibility framework:

Expand Down
2 changes: 2 additions & 0 deletions examples/energy-monitor/ui/widgets/list_view.slint
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ component ListViewItem {
padding-right: Theme.spaces.medium;
spacing: Theme.spaces.medium;
accessible-role: list-item;
accessible-selectable: true;
accessible-selected: root.selected;

i-text := Text {
horizontal-stretch: 1;
Expand Down
22 changes: 22 additions & 0 deletions internal/backends/testing/search_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,28 @@ impl ElementHandle {
.and_then(|item| item.parse().ok())
}

/// Returns the value of the `accessible-selected` property, if present
pub fn accessible_selected(&self) -> Option<bool> {
if self.element_index != 0 {
return None;
}
self.item
.upgrade()
.and_then(|item| item.accessible_string_property(AccessibleStringProperty::Selected))
.and_then(|item| item.parse().ok())
}

/// Returns the value of the `accessible-selectable` property, if present
pub fn accessible_selectable(&self) -> Option<bool> {
if self.element_index != 0 {
return None;
}
self.item
.upgrade()
.and_then(|item| item.accessible_string_property(AccessibleStringProperty::Selectable))
.and_then(|item| item.parse().ok())
}

/// Returns the size of the element in logical pixels. This corresponds to the value of the `width` and
/// `height` properties in Slint code. Returns a zero size if the element is not valid.
pub fn size(&self) -> i_slint_core::api::LogicalSize {
Expand Down
10 changes: 10 additions & 0 deletions internal/backends/winit/accesskit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ impl NodeCollection {
builder.set_placeholder(placeholder.to_string());
}

if item
.accessible_string_property(AccessibleStringProperty::Selectable)
.is_some_and(|x| x == "true")
{
builder.set_selected(
item.accessible_string_property(AccessibleStringProperty::Selected)
.is_some_and(|x| x == "true"),
);
}

let supported = item.supported_accessibility_actions();
if supported.contains(SupportedAccessibilityAction::Default) {
builder.add_action(accesskit::Action::Default);
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/typeregister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub fn reserved_accessibility_properties() -> impl Iterator<Item = (&'static str
"accessible-action-set-value",
Type::Callback { return_type: None, args: vec![Type::String] },
),
("accessible-selectable", Type::Bool),
("accessible-selected", Type::Bool),
]
.into_iter()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/widgets/cosmic/components.slint
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export component ListItem {
layout := HorizontalLayout {
padding-bottom: 8px;
accessible-role: list-item;
accessible-selectable: true;
accessible-selected: root.is-selected;

StateLayerBase {
width: 100%;
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/widgets/cupertino/components.slint
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export component ListItem {
padding-left: root.padding-horizontal;
padding-right: root.padding-horizontal;
accessible-role: list-item;
accessible-selectable: true;
accessible-selected: root.is-selected;

i-background := Rectangle {
background: transparent;
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/widgets/fluent/components.slint
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export component ListItem {
padding-right: 16px;
spacing: 4px;
accessible-role: list-item;
accessible-selectable: true;
accessible-selected: root.is-selected;

i-text := Text {
text: root.item.text;
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/widgets/material/components.slint
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export component ListItem {
padding-left: 12px;
padding-right: 12px;
accessible-role: list-item;
accessible-selectable: true;
accessible-selected: root.is-selected;

label := Text {
text: root.item.text;
Expand Down
2 changes: 2 additions & 0 deletions internal/core/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum AccessibleStringProperty {
Description,
Label,
PlaceholderText,
Selectable,
Selected,
Value,
ValueMaximum,
ValueMinimum,
Expand Down
Loading