From da57b9c12dcf7f3480247c5a7ac1df5e1a19deff Mon Sep 17 00:00:00 2001 From: KENZ Date: Fri, 2 May 2025 07:01:55 +0900 Subject: [PATCH] Estimate preedit position without calling layout_runs() --- src/terminal_box.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/terminal_box.rs b/src/terminal_box.rs index 8eac907..ed7f935 100644 --- a/src/terminal_box.rs +++ b/src/terminal_box.rs @@ -235,13 +235,31 @@ where self } - fn input_method<'b>(&self, state: &'b State, layout: Layout<'_>) -> InputMethod<&'b str> { + fn input_method<'b>( + &self, + state: &'b State, + layout: Layout<'_>, + terminal: &std::sync::MutexGuard<'_, Terminal>, + ) -> InputMethod<&'b str> { if !state.is_focused { return InputMethod::Disabled; } + let view_position = layout.position() + [self.padding.left, self.padding.top].into(); + + // Draw cursor + let cursor = terminal.term.lock().renderable_content().cursor; + let col = cursor.point.column.0; + let line = cursor.point.line.0; + let width = terminal.size().cell_width; + let height = terminal.size().cell_height; + let bottom_left = view_position + + Vector::new( + (col as f32 * width).floor(), + ((line + 1) as f32 * height).floor(), + ); InputMethod::Enabled { - cursor: Rectangle::default(), + cursor: Rectangle::new(bottom_left, Size::default()), purpose: input_method::Purpose::Normal, preedit: state.preedit.as_ref().map(input_method::Preedit::as_ref), } @@ -892,7 +910,7 @@ where shell.request_redraw(); } } - shell.request_input_method(&self.input_method(state, layout)); + shell.request_input_method(&self.input_method(state, layout, &terminal)); } cosmic::iced::window::Event::Unfocused => { state.is_focused = false;