mirror of
https://github.com/storytold/egui.git
synced 2026-11-24 19:42:51 +01:00
Support AccessKit SetValue and ReplaceSelectedText in TextEdit (#8701)
* Closes <https://github.com/emilk/egui/issues/8699> * [x] I have followed the instructions in the PR template `TextEdit` now handles AccessKit's `SetValue` (replaces the whole text, even when the field isn't focused, since assistive technologies set values without focusing first) and `ReplaceSelectedText` (replaces the selection, like a paste). Both are advertised on editable fields only, respect `char_limit`, and turn newlines into spaces in single-line fields. Paste now shares the same helper. Tested with new `egui_kittest` tests, using two small helpers added next to `set_value_accesskit`: `set_text_value_accesskit` and `replace_selected_text_accesskit`. Also tested on Windows with UI Automation, using the repro from the issue: `ValuePattern.SetValue("hello")` now fills the field. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
3 files changed
+242
-51
No files matched your search
@@ -629,43 +629,63 @@ impl<'t> TextEdit<'t> {
|
||||
|
||||
let mut text_changed = false;
|
||||
|
||||
let mut handle_events =
|
||||
|ui: &Ui, galley: &mut Arc<Galley>, layouter, wrap_width, text: &mut dyn TextBuffer| {
|
||||
if interactive && ui.memory(|mem| mem.has_focus(id)) {
|
||||
ui.memory_mut(|mem| mem.set_focus_lock_filter(id, event_filter));
|
||||
let mut handle_events = |ui: &Ui,
|
||||
galley: &mut Arc<Galley>,
|
||||
layouter: LayouterFn<'_>,
|
||||
wrap_width,
|
||||
text: &mut dyn TextBuffer| {
|
||||
// Assistive technologies set the whole value without focusing the field first
|
||||
// (e.g. UI Automation's `ValuePattern.SetValue`), so this can't wait for focus
|
||||
// like the keyboard events below.
|
||||
if interactive
|
||||
&& text.is_mutable()
|
||||
&& let Some(value) = accesskit_set_text_request(ui, id)
|
||||
{
|
||||
let everything = CCursorRange::select_all_str(text.as_str());
|
||||
let ccursor =
|
||||
insert_replacing_selection(text, &everything, &value, multiline, char_limit);
|
||||
*galley = layouter(ui, text, wrap_width);
|
||||
state
|
||||
.cursor
|
||||
.set_char_range(Some(CCursorRange::one(ccursor)));
|
||||
text_changed = true;
|
||||
}
|
||||
|
||||
let default_cursor_range = if cursor_at_end {
|
||||
CCursorRange::one(galley.end())
|
||||
} else {
|
||||
CCursorRange::default()
|
||||
};
|
||||
prev_cursor_range = state.cursor.range(galley);
|
||||
if interactive && ui.memory(|mem| mem.has_focus(id)) {
|
||||
ui.memory_mut(|mem| mem.set_focus_lock_filter(id, event_filter));
|
||||
|
||||
let (changed, new_cursor_range) = events(
|
||||
ui,
|
||||
&mut state,
|
||||
text,
|
||||
galley,
|
||||
layouter,
|
||||
&EventsOptions {
|
||||
id,
|
||||
wrap_width,
|
||||
multiline,
|
||||
password,
|
||||
default_cursor_range,
|
||||
owns_ime_events,
|
||||
char_limit,
|
||||
event_filter,
|
||||
return_key,
|
||||
},
|
||||
);
|
||||
let default_cursor_range = if cursor_at_end {
|
||||
CCursorRange::one(galley.end())
|
||||
} else {
|
||||
CCursorRange::default()
|
||||
};
|
||||
prev_cursor_range = state.cursor.range(galley);
|
||||
|
||||
if changed {
|
||||
text_changed = true;
|
||||
}
|
||||
cursor_range = Some(new_cursor_range);
|
||||
let (changed, new_cursor_range) = events(
|
||||
ui,
|
||||
&mut state,
|
||||
text,
|
||||
galley,
|
||||
layouter,
|
||||
&EventsOptions {
|
||||
id,
|
||||
wrap_width,
|
||||
multiline,
|
||||
password,
|
||||
default_cursor_range,
|
||||
owns_ime_events,
|
||||
char_limit,
|
||||
event_filter,
|
||||
return_key,
|
||||
},
|
||||
);
|
||||
|
||||
if changed {
|
||||
text_changed = true;
|
||||
}
|
||||
};
|
||||
cursor_range = Some(new_cursor_range);
|
||||
}
|
||||
};
|
||||
|
||||
// We need to calculate the galley within the atom closure, so we can calculate it based on
|
||||
// the available width (in case of wrapping multiline text edits). But we show it later,
|
||||
@@ -882,16 +902,16 @@ impl<'t> TextEdit<'t> {
|
||||
.layer_transform_from_global(ui.layer_id())
|
||||
.map_or(pos, |from_global| from_global * pos);
|
||||
|
||||
let mut ccursor = galley.cursor_from_pos(
|
||||
let ccursor = galley.cursor_from_pos(
|
||||
pos - inner_rect.min + state.text_offset + vec2(galley.rect.left(), 0.0),
|
||||
);
|
||||
|
||||
if multiline {
|
||||
text.insert_text_at(&mut ccursor, &pasted, char_limit);
|
||||
} else {
|
||||
let single_line = pasted.replace(['\r', '\n'], " ");
|
||||
text.insert_text_at(&mut ccursor, &single_line, char_limit);
|
||||
}
|
||||
let ccursor = insert_replacing_selection(
|
||||
text,
|
||||
&CCursorRange::one(ccursor),
|
||||
&pasted,
|
||||
multiline,
|
||||
char_limit,
|
||||
);
|
||||
|
||||
state
|
||||
.cursor
|
||||
@@ -1104,8 +1124,11 @@ impl<'t> TextEdit<'t> {
|
||||
accesskit::Role::TextInput
|
||||
};
|
||||
builder.set_role(role);
|
||||
// A `&str` buffer is how callers show selectable text; it cannot be typed into either.
|
||||
if !interactive || !text.is_mutable() {
|
||||
if interactive && text.is_mutable() {
|
||||
builder.add_action(accesskit::Action::SetValue);
|
||||
builder.add_action(accesskit::Action::ReplaceSelectedText);
|
||||
} else {
|
||||
// A `&str` buffer is how callers show selectable text; it cannot be typed into either.
|
||||
builder.set_read_only();
|
||||
}
|
||||
});
|
||||
@@ -1238,17 +1261,31 @@ fn events(
|
||||
if text_to_insert.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let mut ccursor = text.delete_selected(&cursor_range);
|
||||
if multiline {
|
||||
text.insert_text_at(&mut ccursor, text_to_insert, char_limit);
|
||||
} else {
|
||||
let single_line = text_to_insert.replace(['\r', '\n'], " ");
|
||||
text.insert_text_at(&mut ccursor, &single_line, char_limit);
|
||||
}
|
||||
|
||||
let ccursor = insert_replacing_selection(
|
||||
text,
|
||||
&cursor_range,
|
||||
text_to_insert,
|
||||
multiline,
|
||||
char_limit,
|
||||
);
|
||||
Some(CursorMutation::Selection(CCursorRange::one(ccursor)))
|
||||
}
|
||||
}
|
||||
Event::AccessKitActionRequest(accesskit::ActionRequest {
|
||||
action: accesskit::Action::ReplaceSelectedText,
|
||||
target_node,
|
||||
target_tree,
|
||||
data: Some(accesskit::ActionData::Value(text_to_insert)),
|
||||
}) if *target_node == id.accesskit_id() && *target_tree == accesskit::TreeId::ROOT => {
|
||||
let ccursor = insert_replacing_selection(
|
||||
text,
|
||||
&cursor_range,
|
||||
text_to_insert,
|
||||
multiline,
|
||||
char_limit,
|
||||
);
|
||||
Some(CursorMutation::Selection(CCursorRange::one(ccursor)))
|
||||
}
|
||||
Event::Text(text_to_insert) => {
|
||||
// Newlines are handled by `Key::Enter`.
|
||||
if !text_to_insert.is_empty() && text_to_insert != "\n" && text_to_insert != "\r" {
|
||||
@@ -1487,6 +1524,39 @@ fn events(
|
||||
(any_change, cursor_range)
|
||||
}
|
||||
|
||||
/// Replace the selection with `text_to_insert`, the way a paste does: single-line fields turn
|
||||
/// newlines into spaces, and `char_limit` is respected. Returns the cursor after the insertion.
|
||||
fn insert_replacing_selection(
|
||||
text: &mut dyn TextBuffer,
|
||||
cursor_range: &CCursorRange,
|
||||
text_to_insert: &str,
|
||||
multiline: bool,
|
||||
char_limit: usize,
|
||||
) -> CCursor {
|
||||
let mut ccursor = text.delete_selected(cursor_range);
|
||||
if multiline {
|
||||
text.insert_text_at(&mut ccursor, text_to_insert, char_limit);
|
||||
} else {
|
||||
let single_line = text_to_insert.replace(['\r', '\n'], " ");
|
||||
text.insert_text_at(&mut ccursor, &single_line, char_limit);
|
||||
}
|
||||
ccursor
|
||||
}
|
||||
|
||||
/// The text of the last [`accesskit::Action::SetValue`] request for this widget, if any.
|
||||
fn accesskit_set_text_request(ui: &Ui, id: Id) -> Option<String> {
|
||||
ui.input(|input| {
|
||||
use accesskit::{Action, ActionData};
|
||||
input
|
||||
.accesskit_action_requests(id, Action::SetValue)
|
||||
.filter_map(|request| match &request.data {
|
||||
Some(ActionData::Value(value)) => Some(value.to_string()),
|
||||
_ => None,
|
||||
})
|
||||
.last()
|
||||
})
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Returns `Some(new_cursor)` if we did mutate `text`.
|
||||
|
||||
@@ -131,6 +131,27 @@ impl<'tree> Node<'tree> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Send an [`accesskit::Action::SetValue`] action with a text value to the node.
|
||||
///
|
||||
/// This is how assistive technologies set the content of text inputs,
|
||||
/// e.g. UI Automation's `ValuePattern.SetValue` on Windows.
|
||||
pub fn set_text_value_accesskit(&self, value: &str) {
|
||||
self.do_accesskit_action(
|
||||
accesskit::Action::SetValue,
|
||||
Some(accesskit::ActionData::Value(value.into())),
|
||||
);
|
||||
}
|
||||
|
||||
/// Send an [`accesskit::Action::ReplaceSelectedText`] action to the node.
|
||||
///
|
||||
/// This replaces the selection, or inserts at the cursor, in a focused text input.
|
||||
pub fn replace_selected_text_accesskit(&self, text: &str) {
|
||||
self.do_accesskit_action(
|
||||
accesskit::Action::ReplaceSelectedText,
|
||||
Some(accesskit::ActionData::Value(text.into())),
|
||||
);
|
||||
}
|
||||
|
||||
fn do_accesskit_action(&self, action: accesskit::Action, data: Option<accesskit::ActionData>) {
|
||||
let (target_node, target_tree) = self.accesskit_node.locate();
|
||||
self.event(egui::Event::AccessKitActionRequest(
|
||||
|
||||
@@ -749,3 +749,103 @@ fn sensed_ui_can_be_scrolled_into_view() {
|
||||
"not scrolled into view: {rect:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// <https://github.com/emilk/egui/issues/8699>
|
||||
#[test]
|
||||
fn text_edit_value_can_be_set_via_accesskit_without_focus() {
|
||||
let mut harness = Harness::builder().build_ui_state(
|
||||
|ui, text: &mut String| {
|
||||
ui.name_inputs("Filter", |ui| ui.text_edit_singleline(text));
|
||||
},
|
||||
"old".to_owned(),
|
||||
);
|
||||
harness.run();
|
||||
assert!(!harness.get_by_role(Role::TextInput).is_focused());
|
||||
|
||||
harness
|
||||
.get_by_role(Role::TextInput)
|
||||
.set_text_value_accesskit("new\nvalue");
|
||||
harness.run();
|
||||
|
||||
assert_eq!(
|
||||
harness.state(),
|
||||
"new value",
|
||||
"SetValue should replace the text, turning newlines into spaces in a single-line field"
|
||||
);
|
||||
assert_eq!(
|
||||
harness.get_by_role(Role::TextInput).value().as_deref(),
|
||||
Some("new value")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_edit_set_value_respects_char_limit() {
|
||||
let mut harness = Harness::builder().build_ui_state(
|
||||
|ui, text: &mut String| {
|
||||
ui.name_inputs("Filter", |ui| {
|
||||
ui.add(egui::TextEdit::singleline(text).char_limit(3))
|
||||
});
|
||||
},
|
||||
String::new(),
|
||||
);
|
||||
harness.run();
|
||||
|
||||
harness
|
||||
.get_by_role(Role::TextInput)
|
||||
.set_text_value_accesskit("abcdef");
|
||||
harness.run();
|
||||
|
||||
assert_eq!(harness.state(), "abc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_edit_selected_text_can_be_replaced_via_accesskit() {
|
||||
let mut harness = Harness::builder().build_ui_state(
|
||||
|ui, text: &mut String| {
|
||||
ui.name_inputs("Filter", |ui| ui.text_edit_singleline(text));
|
||||
},
|
||||
"ab".to_owned(),
|
||||
);
|
||||
harness.run();
|
||||
harness.get_by_role(Role::TextInput).focus();
|
||||
harness.run();
|
||||
|
||||
// Focusing puts the cursor at the end, so this inserts there:
|
||||
harness
|
||||
.get_by_role(Role::TextInput)
|
||||
.replace_selected_text_accesskit("c");
|
||||
harness.run();
|
||||
|
||||
assert_eq!(harness.state(), "abc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_editable_text_edits_offer_text_actions() {
|
||||
let output = accesskit_output_single_egui_frame(|ui| {
|
||||
CentralPanel::default().show(ui, |ui| {
|
||||
let mut editable = "editable".to_owned();
|
||||
ui.text_edit_singleline(&mut editable);
|
||||
let mut read_only = "read only";
|
||||
ui.add(egui::TextEdit::singleline(&mut read_only));
|
||||
});
|
||||
});
|
||||
|
||||
let text_input = |value: &str| {
|
||||
output
|
||||
.nodes
|
||||
.iter()
|
||||
.map(|(_, node)| node)
|
||||
.find(|node| node.role() == Role::TextInput && node.value() == Some(value))
|
||||
.unwrap_or_else(|| panic!("No text input with value {value:?}; found: {output:#?}"))
|
||||
};
|
||||
|
||||
let editable = text_input("editable");
|
||||
assert!(!editable.is_read_only());
|
||||
assert!(editable.supports_action(Action::SetValue));
|
||||
assert!(editable.supports_action(Action::ReplaceSelectedText));
|
||||
|
||||
let read_only = text_input("read only");
|
||||
assert!(read_only.is_read_only());
|
||||
assert!(!read_only.supports_action(Action::SetValue));
|
||||
assert!(!read_only.supports_action(Action::ReplaceSelectedText));
|
||||
}
|
||||
Reference in new issue
Block a user