fix(editor): anchor Callout emoji candidates to the caret

This commit is contained in:
DarkSky committed 2026-12-02 15:31:12 +09:00
1 parent 8e1e32acdf
commit 77d76401a2
1 file changed
+116 -5
@@ -31,6 +31,7 @@ const GAP: f32 = 4.0;
const SCROLL_GUTTER: f32 = 6.0;
const ICON_SIZE: f32 = 16.0;
const SEARCH_FONT: f32 = 13.0;
const SEARCH_CARET_HEIGHT: f32 = 16.0;
type Dispatch = Rc<dyn Fn(&OverlayEvent, &mut Window, &mut App)>;
pub(super) struct PickerPanel {
@@ -290,8 +291,8 @@ impl PickerPanel {
SingleLineInputSpec {
text_room,
inner_height: input_height,
caret_height: 16.0,
caret_top: (input_height - 16.0) * 0.5,
caret_height: SEARCH_CARET_HEIGHT,
caret_top: (input_height - SEARCH_CARET_HEIGHT) * 0.5,
placeholder: "Search emoji",
focused,
},
@@ -707,12 +708,32 @@ impl EntityInputHandler for PickerState {
fn bounds_for_range(
&mut self,
_: Range<usize>,
bounds: Bounds<Pixels>,
range_utf16: Range<usize>,
_: Bounds<Pixels>,
_: &mut Window,
_: &mut Context<Self>,
) -> Option<Bounds<Pixels>> {
Some(bounds)
let SearchHitTest {
bounds,
line,
scroll_x,
} = self.search_hit_test.as_ref()?;
let x_at = |offset| {
let byte = utf16_to_byte(&line.text, offset);
(f32::from(line.x_for_index(byte)) - scroll_x).round()
};
let start = x_at(range_utf16.start);
let end = x_at(range_utf16.end);
let room = f32::from(bounds.size.width);
let left = start.min(end).clamp(0.0, (room - 1.0).max(0.0));
let right = start.max(end).clamp((left + 1.0).min(room), room);
Some(Bounds::new(
Point::new(
bounds.origin.x + px(left),
bounds.origin.y + (bounds.size.height - px(SEARCH_CARET_HEIGHT)) * 0.5,
),
ui::size(px(right - left), px(SEARCH_CARET_HEIGHT)),
))
}
fn character_index_for_point(
@@ -742,3 +763,93 @@ impl EntityInputHandler for PickerState {
cx.notify();
}
}
#[cfg(test)]
mod tests {
use ui::{Application, HeadlessPlatform, WindowOptions, point, size};
use super::*;
#[test]
fn callout_search_ime_bounds_follow_utf16_ranges_and_scroll() {
let platform = HeadlessPlatform::new(320, 200, 1.0);
let app = Application::with_platform(platform).run_embedded(|cx| {
crate::register_bundled_fonts(cx).expect("register fonts");
});
app.update(|cx| {
let window = cx
.open_window(WindowOptions::default(), |_, cx| cx.new(|_| ui::EmptyView))
.expect("open window");
window
.update(cx, |_, window, cx| {
let state = cx.new(|cx| PickerState {
field: LinkBar::default(),
search_hit_test: None,
scroll_y: 0.0,
focus: cx.focus_handle(),
previous_focus: None,
});
state.update(cx, |state, cx| {
for query in ["rocket", "r🚀ocket"] {
state.field.replace(None, query);
let shown: SharedString = state.field.text().into();
let mut font = ui::font(FONT_SANS);
font.fallbacks = Some(crate::default_font_fallbacks());
let line = window.text_system().shape_line(
shown,
px(SEARCH_FONT),
&[TextRun {
len: query.len(),
font,
color: Default::default(),
background_color: None,
underline: None,
strikethrough: None,
}],
None,
);
let width = f32::from(line.width());
let room = if query == "rocket" {
200.0
} else {
width.round() - 3.0
};
let scroll_x = single_line_scroll_x(width, width, room);
let bounds =
Bounds::new(point(px(30.0), px(20.0)), size(px(room), px(32.0)));
let after_emoji = f32::from(line.x_for_index(5)).round() - scroll_x;
state.search_hit_test = Some(SearchHitTest {
bounds,
line,
scroll_x,
});
let mut ime_bounds = |range| {
state
.bounds_for_range(range, bounds, window, cx)
.expect("IME bounds")
};
let start =
Bounds::new(point(px(30.0), px(28.0)), size(px(1.0), px(16.0)));
assert_eq!(ime_bounds(0..0), start);
if query == "rocket" {
let end = ime_bounds(6..6);
assert_eq!(end.origin.x, start.origin.x + px(width.round()));
assert!(end.origin.x > start.origin.x);
assert_eq!(end.size, start.size);
} else {
assert_eq!(scroll_x, 4.0);
assert!(after_emoji > 0.0);
let end = ime_bounds(3..3);
assert_eq!(end.origin.x, start.origin.x + px(after_emoji));
assert_eq!(ime_bounds(1..1), ime_bounds(2..2));
assert_eq!(ime_bounds(1..3).right(), end.origin.x);
assert_eq!(ime_bounds(8..8).right(), bounds.right());
}
state.field = LinkBar::default();
}
});
})
.expect("update window");
});
}
}