Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ios/host/Sources/BridgingHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions crates/ios/host/Sources/GPUITextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<UIPress>, down: Bool, repeatKey: Bool) -> Set<UIPress> {
// Candidate navigation and confirmation belong to UIKit during composition.
let composing = markedTextRange == nil ? Set<UIPress>() : Set(presses.filter {
Expand Down
1 change: 1 addition & 0 deletions crates/platform/gpui-ios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
21 changes: 21 additions & 0 deletions crates/platform/gpui-ios/src/ios/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
85 changes: 83 additions & 2 deletions crates/platform/gpui-ios/src/text_input.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<Pixels>,
) -> Option<usize> {
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<Pixels>| (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};

Expand Down Expand Up @@ -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<Pixels> {
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}后";
Expand Down
3 changes: 2 additions & 1 deletion crates/runtime/src/app/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading