Implement character index for point (#23989)
Fixes #22939 Fixes #23970 Supersedes https://github.com/zed-industries/zed/pull/23469 Release Notes: - Fixed a bug where Zed could crash with certain input sources on macOS --------- Co-authored-by: Louis Brunner <louis.brunner.fr@gmail.com> Co-authored-by: ben <ben@zed.dev>
This commit is contained in:
parent
7da60995cc
commit
cfe0932c0a
9 changed files with 221 additions and 129 deletions
|
@ -17,8 +17,8 @@ use cocoa::{
|
|||
},
|
||||
base::{id, nil},
|
||||
foundation::{
|
||||
NSArray, NSAutoreleasePool, NSDictionary, NSFastEnumeration, NSInteger, NSPoint, NSRect,
|
||||
NSSize, NSString, NSUInteger,
|
||||
NSArray, NSAutoreleasePool, NSDictionary, NSFastEnumeration, NSInteger, NSNotFound,
|
||||
NSPoint, NSRect, NSSize, NSString, NSUInteger,
|
||||
},
|
||||
};
|
||||
use core_graphics::display::{CGDirectDisplayID, CGPoint, CGRect};
|
||||
|
@ -227,6 +227,11 @@ unsafe fn build_classes() {
|
|||
accepts_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
);
|
||||
|
||||
decl.add_method(
|
||||
sel!(characterIndexForPoint:),
|
||||
character_index_for_point as extern "C" fn(&Object, Sel, NSPoint) -> u64,
|
||||
);
|
||||
|
||||
decl.register()
|
||||
};
|
||||
}
|
||||
|
@ -1687,17 +1692,7 @@ extern "C" fn first_rect_for_character_range(
|
|||
range: NSRange,
|
||||
_: id,
|
||||
) -> NSRect {
|
||||
let frame: NSRect = unsafe {
|
||||
let state = get_window_state(this);
|
||||
let lock = state.lock();
|
||||
let mut frame = NSWindow::frame(lock.native_window);
|
||||
let content_layout_rect: CGRect = msg_send![lock.native_window, contentLayoutRect];
|
||||
let style_mask: NSWindowStyleMask = msg_send![lock.native_window, styleMask];
|
||||
if !style_mask.contains(NSWindowStyleMask::NSFullSizeContentViewWindowMask) {
|
||||
frame.origin.y -= frame.size.height - content_layout_rect.size.height;
|
||||
}
|
||||
frame
|
||||
};
|
||||
let frame = get_frame(this);
|
||||
with_input_handler(this, |input_handler| {
|
||||
input_handler.bounds_for_range(range.to_range()?)
|
||||
})
|
||||
|
@ -1718,6 +1713,20 @@ extern "C" fn first_rect_for_character_range(
|
|||
)
|
||||
}
|
||||
|
||||
fn get_frame(this: &Object) -> NSRect {
|
||||
unsafe {
|
||||
let state = get_window_state(this);
|
||||
let lock = state.lock();
|
||||
let mut frame = NSWindow::frame(lock.native_window);
|
||||
let content_layout_rect: CGRect = msg_send![lock.native_window, contentLayoutRect];
|
||||
let style_mask: NSWindowStyleMask = msg_send![lock.native_window, styleMask];
|
||||
if !style_mask.contains(NSWindowStyleMask::NSFullSizeContentViewWindowMask) {
|
||||
frame.origin.y -= frame.size.height - content_layout_rect.size.height;
|
||||
}
|
||||
frame
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn insert_text(this: &Object, _: Sel, text: id, replacement_range: NSRange) {
|
||||
unsafe {
|
||||
let is_attributed_string: BOOL =
|
||||
|
@ -1831,6 +1840,24 @@ extern "C" fn accepts_first_mouse(this: &Object, _: Sel, _: id) -> BOOL {
|
|||
YES
|
||||
}
|
||||
|
||||
extern "C" fn character_index_for_point(this: &Object, _: Sel, position: NSPoint) -> u64 {
|
||||
let position = screen_point_to_gpui_point(this, position);
|
||||
with_input_handler(this, |input_handler| {
|
||||
input_handler.character_index_for_point(position)
|
||||
})
|
||||
.flatten()
|
||||
.map(|index| index as u64)
|
||||
.unwrap_or(NSNotFound as u64)
|
||||
}
|
||||
|
||||
fn screen_point_to_gpui_point(this: &Object, position: NSPoint) -> Point<Pixels> {
|
||||
let frame = get_frame(this);
|
||||
let window_x = position.x - frame.origin.x;
|
||||
let window_y = frame.size.height - (position.y - frame.origin.y);
|
||||
let position = point(px(window_x as f32), px(window_y as f32));
|
||||
position
|
||||
}
|
||||
|
||||
extern "C" fn dragging_entered(this: &Object, _: Sel, dragging_info: id) -> NSDragOperation {
|
||||
let window_state = unsafe { get_window_state(this) };
|
||||
let position = drag_event_position(&window_state, dragging_info);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue