diff --git a/Cargo.lock b/Cargo.lock index 6afc4d625..c6fe1df0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4302,6 +4302,7 @@ dependencies = [ "pathfinder_geometry", "raw-window-handle", "smallvec", + "unicode-segmentation", "uuid", "zed-font-kit", ] diff --git a/crates/ios/host/Sources/BridgingHeader.h b/crates/ios/host/Sources/BridgingHeader.h index caee57ba0..73e675e6e 100644 --- a/crates/ios/host/Sources/BridgingHeader.h +++ b/crates/ios/host/Sources/BridgingHeader.h @@ -72,6 +72,7 @@ void gpui_ios_select_text(TextRange range); void gpui_ios_unmark_text(void); TextRect gpui_ios_text_rect(TextRange range); size_t gpui_ios_character_at_point(double x, double y); +void gpui_ios_move_caret_toward(double x, double y); void gpui_ios_delete_backward(void); void gpui_ios_paste_text(const uint8_t *bytes, size_t length); uint8_t gpui_ios_key_event(const uint8_t *key_bytes, size_t key_length, diff --git a/crates/ios/host/Sources/GPUITextInput.swift b/crates/ios/host/Sources/GPUITextInput.swift index 10c8c2267..f281d5f05 100644 --- a/crates/ios/host/Sources/GPUITextInput.swift +++ b/crates/ios/host/Sources/GPUITextInput.swift @@ -30,6 +30,7 @@ final class GPUITextInput: UIView, UITextInput { var returnKeyType: UIReturnKeyType = .default var enablesReturnKeyAutomatically = false var isSecureTextEntry = false + private var floatingCursorOffset: CGPoint? private var pasteGeneration: UInt64 = 0 private var inputOwner: UInt64 = 0 @@ -286,6 +287,18 @@ final class GPUITextInput: UIView, UITextInput { return tokenizer.rangeEnclosingPosition(position, with: .character, inDirection: UITextDirection.storage(.forward)) } + // Keep the drag relative to the caret, so it does not jump when the spacebar is first held. + func beginFloatingCursor(at point: CGPoint) { + guard let caret = selectedTextRange?.end else { return } + let rect = caretRect(for: caret) + floatingCursorOffset = CGPoint(x: rect.midX - point.x, y: rect.midY - point.y) + } + func updateFloatingCursor(at point: CGPoint) { + guard let offset = floatingCursorOffset else { return } + edit { gpui_ios_move_caret_toward(point.x + offset.x, point.y + offset.y) } + } + func endFloatingCursor() { floatingCursorOffset = nil } + private func unhandledPresses(_ presses: Set, down: Bool, repeatKey: Bool) -> Set { // Candidate navigation and confirmation belong to UIKit during composition. let composing = markedTextRange == nil ? Set() : Set(presses.filter { diff --git a/crates/platform/gpui-ios/Cargo.toml b/crates/platform/gpui-ios/Cargo.toml index e974ca95e..65b1fbcc4 100644 --- a/crates/platform/gpui-ios/Cargo.toml +++ b/crates/platform/gpui-ios/Cargo.toml @@ -17,6 +17,7 @@ parking_lot = "0.12" futures = "0.3" raw-window-handle = "0.6" smallvec = "1" +unicode-segmentation = "1" [dev-dependencies] gpui = { package = "gpui-fast", version = "0.1.10", features = ["test-support"], default-features = false } diff --git a/crates/platform/gpui-ios/src/ios/ffi.rs b/crates/platform/gpui-ios/src/ios/ffi.rs index 7ab2da55a..149fc00e5 100644 --- a/crates/platform/gpui-ios/src/ios/ffi.rs +++ b/crates/platform/gpui-ios/src/ios/ffi.rs @@ -719,6 +719,27 @@ pub extern "C" fn gpui_ios_character_at_point(x: f64, y: f64) -> usize { index.unwrap_or(NOT_FOUND) } +#[unsafe(no_mangle)] +pub extern "C" fn gpui_ios_move_caret_toward(x: f64, y: f64) { + with_window(|window| { + window.input_activity(); + window.with_input_handler(|handler| { + let Some(selection) = handler.selected_text_range(true) else { + return; + }; + let head = if selection.reversed { + selection.range.start + } else { + selection.range.end + }; + let target = point(px(x as f32), px(y as f32)); + if let Some(offset) = text_input::caret_toward(handler, head, target) { + text_input::select_text(handler, offset..offset); + } + }); + }); +} + #[unsafe(no_mangle)] pub extern "C" fn gpui_ios_delete_backward() { with_window(IosWindow::delete_backward); diff --git a/crates/platform/gpui-ios/src/text_input.rs b/crates/platform/gpui-ios/src/text_input.rs index 5680b0879..1ffb0bd8d 100644 --- a/crates/platform/gpui-ios/src/text_input.rs +++ b/crates/platform/gpui-ios/src/text_input.rs @@ -1,5 +1,6 @@ -use gpui::PlatformInputHandler; +use gpui::{Bounds, Pixels, PlatformInputHandler, Point}; use std::ops::Range; +use unicode_segmentation::UnicodeSegmentation; pub const NOT_FOUND: usize = isize::MAX as usize; @@ -113,10 +114,63 @@ pub fn commit_marked_text(handler: &mut PlatformInputHandler) { } } +/// The caret offset nearest `target`, walking one grapheme at a time from the +/// caret at `from`. +/// +/// gpui-base 0.8.0's `character_index_for_point` tests the point against every +/// line as if that line began at the top of the input, so it finds no index +/// below the first line, and none past a line's last glyph. Caret bounds are +/// right for every visible offset, so the walk follows those. +pub fn caret_toward( + handler: &mut PlatformInputHandler, + from: usize, + target: Point, +) -> Option { + let length = text_length(handler)?; + let text = read_text(handler, 0..length)?; + let mut stops = vec![0]; + stops.extend(text.graphemes(true).scan(0, |end, grapheme| { + *end += grapheme.encode_utf16().count(); + Some(*end) + })); + let mut caret = |index: usize| handler.bounds_for_range(stops[index]..stops[index]); + let mut at = stops.iter().rposition(|stop| *stop <= from)?; + let mut here = caret(at)?; + loop { + let next = if target.y >= here.bottom() { + (at + 1..stops.len()).find_map(|index| { + Some((index, caret(index)?)).filter(|(_, b)| b.top() >= here.bottom()) + }) + } else if target.y < here.top() { + (0..at).rev().find_map(|index| { + Some((index, caret(index)?)).filter(|(_, b)| b.bottom() <= here.top()) + }) + } else { + None + }; + let Some((index, bounds)) = next else { break }; + (at, here) = (index, bounds); + } + let distance = |bounds: &Bounds| (bounds.origin.x - target.x).abs(); + let row = here.top(); + for step in [1, -1] { + while let Some(index) = at + .checked_add_signed(step) + .filter(|index| *index < stops.len()) + && let Some(bounds) = caret(index) + && bounds.top() == row + && distance(&bounds) < distance(&here) + { + (at, here) = (index, bounds); + } + } + Some(stops[at]) +} + #[cfg(test)] mod tests { use super::*; - use gpui::{ElementInputHandler, Entity, TestAppContext, VisualTestContext}; + use gpui::{ElementInputHandler, Entity, TestAppContext, VisualTestContext, point, px}; use gpui_base::input::{InputEvent, TextareaState}; use std::{cell::Cell, rc::Rc}; @@ -222,6 +276,33 @@ mod tests { assert_eq!(input.read_with(cx, |input, _| input.value()), "你好"); } + #[gpui::test] + fn caret_toward_reaches_later_lines_and_line_ends(cx: &mut TestAppContext) { + let (input, cx, mut handler) = textarea(cx); + cx.simulate_input("前😀e\u{301}"); + cx.simulate_keystrokes("shift-enter"); + cx.simulate_input("second"); + cx.update(|window, cx| window.draw(cx).clear(cx)); + assert_eq!( + input.read_with(cx, |input, _| input.value()), + "前😀e\u{301}\nsecond" + ); + fn caret(handler: &mut PlatformInputHandler, offset: usize) -> Bounds { + handler.bounds_for_range(offset..offset).unwrap() + } + let (first_end, second) = (caret(&mut handler, 5), caret(&mut handler, 9)); + assert!(second.top() >= first_end.bottom()); + let below_and_beyond = point(second.right() + px(1000.), second.center().y); + assert_eq!(caret_toward(&mut handler, 0, second.center()), Some(9)); + assert_eq!(caret_toward(&mut handler, 0, below_and_beyond), Some(12)); + let first_line_end = point(first_end.right() + px(1000.), first_end.center().y); + assert_eq!(caret_toward(&mut handler, 12, first_line_end), Some(5)); + for offset in [0, 1, 3, 5] { + let target = caret(&mut handler, offset).center(); + assert_eq!(caret_toward(&mut handler, 12, target), Some(offset)); + } + } + #[test] fn keyboard_ranges_preserve_cjk_emoji_and_combining_text() { let text = "前😀e\u{301}后"; diff --git a/crates/runtime/src/app/tests.rs b/crates/runtime/src/app/tests.rs index 4ab87c69b..45db81faa 100644 --- a/crates/runtime/src/app/tests.rs +++ b/crates/runtime/src/app/tests.rs @@ -5607,7 +5607,8 @@ fn model_fallback_stops_active_session_when_abort_on_model_fallback_is_enabled() }); assert!(matches!(receiver.try_recv(), Ok(SessionCommand::Shutdown))); - cx.run_until_parked(); + // The session's records reach clients once its log has been read. + cx.run_until(|state| state.log_hydrations.is_empty()); let outgoing = cx.drain_outgoing(); assert!(outgoing.iter().any(|message| matches!( message,