Skip to content

Report cursor size to input method #2918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions core/src/input_method.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Listen to input method events.
use crate::{Pixels, Point};
use crate::{Pixels, Rectangle};

use std::ops::Range;

Expand All @@ -10,8 +10,8 @@ pub enum InputMethod<T = String> {
Disabled,
/// Input method is enabled.
Enabled {
/// The position at which the input method dialog should be placed.
position: Point,
/// The area which the input method dialog should not cover.
cursor_position: Rectangle,
/// The [`Purpose`] of the input method.
purpose: Purpose,
/// The preedit to overlay on top of the input method dialog, if needed.
Expand Down Expand Up @@ -130,11 +130,11 @@ impl<T> InputMethod<T> {
match self {
Self::Disabled => InputMethod::Disabled,
Self::Enabled {
position,
cursor_position,
purpose,
preedit,
} => InputMethod::Enabled {
position: *position,
cursor_position: *cursor_position,
purpose: *purpose,
preedit: preedit.as_ref().map(Preedit::to_owned),
},
Expand Down
7 changes: 4 additions & 3 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,11 @@ where
);

if !had_input_method {
if let InputMethod::Enabled { position, .. } =
shell.input_method_mut()
if let InputMethod::Enabled {
cursor_position, ..
} = shell.input_method_mut()
{
*position = *position - translation;
*cursor_position = *cursor_position - translation;
}
}
};
Expand Down
8 changes: 5 additions & 3 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ where
self.text_size.unwrap_or_else(|| renderer.default_size()),
);

let position =
cursor + translation + Vector::new(0.0, f32::from(line_height));
let position = cursor + translation;

InputMethod::Enabled {
position,
cursor_position: Rectangle::new(
position,
Size::new(0.0, f32::from(line_height)),
),
purpose: input_method::Purpose::Normal,
preedit: state.preedit.as_ref().map(input_method::Preedit::as_ref),
}
Expand Down
5 changes: 4 additions & 1 deletion widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ where
+ alignment_offset;

InputMethod::Enabled {
position: Point::new(x, text_bounds.y + text_bounds.height),
cursor_position: Rectangle::new(
Point::new(x, text_bounds.y),
Size::new(0.0, text_bounds.height),
),
purpose: if self.is_secure {
input_method::Purpose::Secure
} else {
Expand Down
22 changes: 13 additions & 9 deletions winit/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ where
pub renderer: P::Renderer,
pub redraw_at: Option<Instant>,
preedit: Option<Preedit<P::Renderer>>,
ime_state: Option<(Point, input_method::Purpose)>,
ime_state: Option<(Rectangle, input_method::Purpose)>,
}

impl<P, C> Window<P, C>
Expand Down Expand Up @@ -217,11 +217,11 @@ where
self.disable_ime();
}
InputMethod::Enabled {
position,
cursor_position,
purpose,
preedit,
} => {
self.enable_ime(position, purpose);
self.enable_ime(cursor_position, purpose);

if let Some(preedit) = preedit {
if preedit.content.is_empty() {
Expand All @@ -231,7 +231,7 @@ where
self.preedit.take().unwrap_or_else(Preedit::new);

overlay.update(
position,
cursor_position.position(),
&preedit,
self.state.background_color(),
&self.renderer,
Expand Down Expand Up @@ -260,19 +260,23 @@ where
}
}

fn enable_ime(&mut self, position: Point, purpose: input_method::Purpose) {
fn enable_ime(
&mut self,
cursor: Rectangle,
purpose: input_method::Purpose,
) {
if self.ime_state.is_none() {
self.raw.set_ime_allowed(true);
}

if self.ime_state != Some((position, purpose)) {
if self.ime_state != Some((cursor, purpose)) {
self.raw.set_ime_cursor_area(
LogicalPosition::new(position.x, position.y),
LogicalSize::new(10, 10), // TODO?
LogicalPosition::new(cursor.x, cursor.y),
LogicalSize::new(cursor.width, cursor.height),
);
self.raw.set_ime_purpose(conversion::ime_purpose(purpose));

self.ime_state = Some((position, purpose));
self.ime_state = Some((cursor, purpose));
}
}

Expand Down