This commit is contained in:
Simon Hausmann 2025-07-07 19:38:45 +02:00 committed by GitHub
commit 59ffdfd5b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 3 deletions

View file

@ -512,10 +512,52 @@ pub struct TextInput {
pressed: Cell<u8>,
undo_items: Cell<SharedVector<UndoItem>>,
redo_items: Cell<SharedVector<UndoItem>>,
cursor_anchor_bounds_handler: crate::properties::ChangeTracker,
}
impl Item for TextInput {
fn init(self: Pin<&Self>, _self_rc: &ItemRc) {}
fn init(self: Pin<&Self>, self_rc: &ItemRc) {
self.cursor_anchor_bounds_handler.init_delayed(
self_rc.downgrade(),
|self_weak| {
let Some(textinput_rc) = self_weak.upgrade() else { return false };
let Some(textinput) = textinput_rc.downcast::<TextInput>() else { return false };
let textinput = textinput.as_pin_ref();
let text: SharedString = textinput.text();
let anchor_offset = textinput.anchor_position_byte_offset();
let cursor_offset = textinput.cursor_position_byte_offset();
let anchor_in_bounds =
safe_byte_offset(anchor_offset, &text) as i32 == anchor_offset;
let cursor_in_bounds =
safe_byte_offset(cursor_offset, &text) as i32 == anchor_offset;
!anchor_in_bounds || !cursor_in_bounds
},
|self_weak, offsets_out_of_bounds| {
if *offsets_out_of_bounds {
let Some(textinput_rc) = self_weak.upgrade() else {
return;
};
let Some(textinput) = textinput_rc.downcast::<TextInput>() else {
return;
};
let textinput = textinput.as_pin_ref();
let text: SharedString = textinput.text();
let anchor_offset =
safe_byte_offset(textinput.anchor_position_byte_offset(), &text) as i32;
let cursor_offset =
safe_byte_offset(textinput.cursor_position_byte_offset(), &text) as i32;
textinput.anchor_position_byte_offset.set(anchor_offset);
textinput.cursor_position_byte_offset.set(cursor_offset);
}
},
);
}
fn layout_info(
self: Pin<&Self>,

View file

@ -8,12 +8,14 @@ TestCase := Window {
padding: 0;
spacing: 0;
ti := TextInput { }
Rectangle { }
}
property <string> text <=> ti.text;
property <bool> input_focused: ti.has_focus;
property<int> test_cursor_pos: ti.cursor_position_byte_offset;
property <int> test_cursor_pos: ti.cursor_position_byte_offset;
property <int> test_anchor_pos: ti.anchor_position_byte_offset;
}
/*
@ -32,8 +34,17 @@ assert_eq!(instance.get_text(), "Yo");
slint_testing::send_keyboard_string_sequence(&instance, "Hello Again");
assert_eq!(instance.get_text(), "YoHello Again");
instance.set_text("Yo".into());
// Issue #331: assert_eq!(instance.get_test_cursor_pos(), 2);
slint_testing::mock_elapsed_time(1); // let change handlers run
assert_eq!(instance.get_test_cursor_pos(), 2);
assert_eq!(instance.get_test_anchor_pos(), 2);
slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
slint_testing::mock_elapsed_time(1); // let change handlers run
assert_eq!(instance.get_test_cursor_pos(), 1);
assert_eq!(instance.get_test_anchor_pos(), 1);
```
*/