Set correct text size for text in preedit window

This commit is contained in:
rhysd 2025-02-06 01:50:25 +09:00
parent 4bbb5cbc1f
commit fcdf53afde
4 changed files with 25 additions and 11 deletions

View file

@ -1,5 +1,5 @@
//! Listen to input method events. //! Listen to input method events.
use crate::Point; use crate::{Pixels, Point};
use std::ops::Range; use std::ops::Range;
@ -34,15 +34,20 @@ pub struct Preedit<T = String> {
pub content: T, pub content: T,
/// The selected range of the content. /// The selected range of the content.
pub selection: Option<Range<usize>>, pub selection: Option<Range<usize>>,
/// The text size of the content.
pub text_size: Option<Pixels>,
} }
impl<T> Preedit<T> { impl<T> Preedit<T> {
/// Creates a new empty [`Preedit`]. /// Creates a new empty [`Preedit`].
pub fn new() -> Self pub fn new(text_size: Option<impl Into<Pixels>>) -> Self
where where
T: Default, T: Default,
{ {
Self::default() Self {
text_size: text_size.map(Into::into),
..Default::default()
}
} }
/// Turns a [`Preedit`] into its owned version. /// Turns a [`Preedit`] into its owned version.
@ -53,6 +58,7 @@ impl<T> Preedit<T> {
Preedit { Preedit {
content: self.content.as_ref().to_owned(), content: self.content.as_ref().to_owned(),
selection: self.selection.clone(), selection: self.selection.clone(),
text_size: self.text_size,
} }
} }
} }
@ -63,6 +69,7 @@ impl Preedit {
Preedit { Preedit {
content: &self.content, content: &self.content,
selection: self.selection.clone(), selection: self.selection.clone(),
text_size: self.text_size,
} }
} }
} }
@ -90,13 +97,13 @@ impl InputMethod {
/// let open = InputMethod::Open { /// let open = InputMethod::Open {
/// position: Point::ORIGIN, /// position: Point::ORIGIN,
/// purpose: Purpose::Normal, /// purpose: Purpose::Normal,
/// preedit: Some(Preedit { content: "1".to_owned(), selection: None }), /// preedit: Some(Preedit { content: "1".to_owned(), selection: None, text_size: None }),
/// }; /// };
/// ///
/// let open_2 = InputMethod::Open { /// let open_2 = InputMethod::Open {
/// position: Point::ORIGIN, /// position: Point::ORIGIN,
/// purpose: Purpose::Secure, /// purpose: Purpose::Secure,
/// preedit: Some(Preedit { content: "2".to_owned(), selection: None }), /// preedit: Some(Preedit { content: "2".to_owned(), selection: None, text_size: None }),
/// }; /// };
/// ///
/// let mut ime = InputMethod::Disabled; /// let mut ime = InputMethod::Disabled;

View file

@ -753,14 +753,18 @@ where
} }
Update::InputMethod(update) => match update { Update::InputMethod(update) => match update {
Ime::Toggle(is_open) => { Ime::Toggle(is_open) => {
state.preedit = state.preedit = is_open.then(|| {
is_open.then(input_method::Preedit::new); input_method::Preedit::new(self.text_size)
});
shell.request_redraw(); shell.request_redraw();
} }
Ime::Preedit { content, selection } => { Ime::Preedit { content, selection } => {
state.preedit = state.preedit = Some(input_method::Preedit {
Some(input_method::Preedit { content, selection }); content,
selection,
text_size: self.text_size,
});
shell.request_redraw(); shell.request_redraw();
} }

View file

@ -1262,7 +1262,7 @@ where
state.is_ime_open = state.is_ime_open =
matches!(event, input_method::Event::Opened) matches!(event, input_method::Event::Opened)
.then(input_method::Preedit::new); .then(|| input_method::Preedit::new(self.size));
shell.request_redraw(); shell.request_redraw();
} }
@ -1273,6 +1273,7 @@ where
state.is_ime_open = Some(input_method::Preedit { state.is_ime_open = Some(input_method::Preedit {
content: content.to_owned(), content: content.to_owned(),
selection: selection.clone(), selection: selection.clone(),
text_size: self.size,
}); });
shell.request_redraw(); shell.request_redraw();

View file

@ -322,7 +322,9 @@ where
self.content = Renderer::Paragraph::with_spans(Text { self.content = Renderer::Paragraph::with_spans(Text {
content: &spans, content: &spans,
bounds: Size::INFINITY, bounds: Size::INFINITY,
size: renderer.default_size(), size: preedit
.text_size
.unwrap_or_else(|| renderer.default_size()),
line_height: text::LineHeight::default(), line_height: text::LineHeight::default(),
font: renderer.default_font(), font: renderer.default_font(),
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,