Skip to content

Commit

Permalink
Add the accessible-placeholder-text property
Browse files Browse the repository at this point in the history
  • Loading branch information
DataTriny committed Jun 23, 2024
1 parent d86ae62 commit d89cb12
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 8 deletions.
11 changes: 9 additions & 2 deletions api/cpp/include/slint-testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class ElementHandle
typename R = std::invoke_result_t<Visitor, ElementHandle>>
requires((std::is_constructible_v<bool, R> && std::is_default_constructible_v<R>)
|| std::is_void_v<R>)
static auto visit_elements(const ComponentHandle<T> &component,
Visitor visitor) -> std::invoke_result_t<Visitor, ElementHandle>
static auto visit_elements(const ComponentHandle<T> &component, Visitor visitor)
-> std::invoke_result_t<Visitor, ElementHandle>
{
// using R = std::invoke_result_t<Visitor, ElementHandle>;
auto vrc = component.into_dyn();
Expand Down Expand Up @@ -232,6 +232,13 @@ class ElementHandle
return get_accessible_string_property(cbindgen_private::AccessibleStringProperty::Value);
}

/// Returns the accessible-placeholder-text of that element, if any.
std::optional<SharedString> accessible_placeholder_text() const
{
return get_accessible_string_property(
cbindgen_private::AccessibleStringProperty::PlaceholderText);
}

/// Returns the accessible-description of that element, if any.
std::optional<SharedString> accessible_description() const
{
Expand Down
1 change: 1 addition & 0 deletions docs/reference/src/language/builtins/elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Use the following `accessible-` properties to make your items interact well with
- **`accessible-value-minimum`** (_in_ _float_): The minimum value of the item.
- **`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.

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

Expand Down
3 changes: 2 additions & 1 deletion examples/todo-mvc/ui/views/create_task_view.slint
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export component CreateTaskView {
VerticalLayout {
spacing: SpaceSettings.default-spacing;

Text {
title-label := Text {
text: @tr("Task name");
color: TodoPalette.foreground;
font-size: FontSettings.body-strong.font-size;
Expand All @@ -69,6 +69,7 @@ export component CreateTaskView {

title-input := LineEdit {
placeholder-text: @tr("Describe your task");
accessible-label: title-label.text;
}
}

Expand Down
10 changes: 10 additions & 0 deletions internal/backends/testing/search_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ impl ElementHandle {
.and_then(|item| item.accessible_string_property(AccessibleStringProperty::Value))
}

/// Returns the value of the element's `accessible-placeholder-text` property, if present.
pub fn accessible_placeholder_text(&self) -> Option<SharedString> {
if self.element_index != 0 {
return None;
}
self.item.upgrade().and_then(|item| {
item.accessible_string_property(AccessibleStringProperty::PlaceholderText)
})
}

/// Sets the value of the element's `accessible-value` property. Note that you can only set this
/// property if it is declared in your Slint code.
pub fn set_accessible_value(&self, value: impl Into<SharedString>) {
Expand Down
11 changes: 10 additions & 1 deletion internal/backends/winit/accesskit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,16 @@ impl NodeCollection {
builder.set_numeric_value_step(step);
}

if let Some(value) = item.accessible_string_property(AccessibleStringProperty::Value) {
let value = item.accessible_string_property(AccessibleStringProperty::Value);
if value.is_none() || value.as_ref().is_some_and(|x| x.is_empty()) {
if let Some(placeholder) = item
.accessible_string_property(AccessibleStringProperty::PlaceholderText)
.filter(|x| !x.is_empty())
{
builder.set_placeholder(placeholder.to_string());
}
}
if let Some(value) = value {
if let Ok(value) = value.parse() {
builder.set_numeric_value(value);
} else {
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/typeregister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub fn reserved_accessibility_properties() -> impl Iterator<Item = (&'static str
("accessible-value-maximum", Type::Float32),
("accessible-value-minimum", Type::Float32),
("accessible-value-step", Type::Float32),
("accessible-placeholder-text", Type::String),
("accessible-action-default", Type::Callback { return_type: None, args: vec![] }),
("accessible-action-increment", Type::Callback { return_type: None, args: vec![] }),
("accessible-action-decrement", Type::Callback { return_type: None, args: vec![] }),
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/widgets/cosmic-base/lineedit.slint
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export component LineEdit {
callback edited <=> base.edited;
accessible-role: text-input;
accessible-value <=> text;
accessible-label: placeholder-text;
accessible-placeholder-text: placeholder-text;
accessible-action-set-value(v) => { text = v; edited(v); }

public function set-selection-offsets(start: int, end: int) {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/widgets/cupertino-base/lineedit.slint
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export component LineEdit {
callback edited <=> i-base.edited;
accessible-role: text-input;
accessible-value <=> text;
accessible-label: placeholder-text;
accessible-placeholder-text: placeholder-text;
accessible-action-set-value(v) => { text = v; edited(v); }

public function set-selection-offsets(start: int, end: int) {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/widgets/fluent-base/lineedit.slint
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export component LineEdit {
callback edited <=> i-base.edited;
accessible-role: text-input;
accessible-value <=> text;
accessible-label: placeholder-text;
accessible-placeholder-text: placeholder-text;
accessible-action-set-value(v) => { text = v; edited(v); }

public function set-selection-offsets(start: int, end: int) {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/widgets/material-base/lineedit.slint
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export component LineEdit {
callback edited <=> i-base.edited;
accessible-role: text-input;
accessible-value <=> text;
accessible-label: placeholder-text;
accessible-placeholder-text: placeholder-text;
accessible-action-set-value(v) => { text = v; edited(v); }

public function set-selection-offsets(start: int, end: int) {
Expand Down
1 change: 1 addition & 0 deletions internal/core/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum AccessibleStringProperty {
DelegateFocus,
Description,
Label,
PlaceholderText,
Value,
ValueMaximum,
ValueMinimum,
Expand Down

0 comments on commit d89cb12

Please sign in to comment.