Skip to content

Commit

Permalink
Set correct text size for preedit window
Browse files Browse the repository at this point in the history
- backports iced-rs#2785
  • Loading branch information
kenz-gelsoft committed Feb 11, 2025
1 parent bbc6414 commit 02370b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
10 changes: 7 additions & 3 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::Point;
use crate::{Pixels, Point};

use std::ops::Range;

Expand Down Expand Up @@ -34,6 +34,8 @@ pub struct Preedit<T = String> {
pub content: T,
/// The selected range of the content.
pub selection: Option<Range<usize>>,
/// The text size of the content.
pub text_size: Option<Pixels>,
}

impl<T> Preedit<T> {
Expand All @@ -53,6 +55,7 @@ impl<T> Preedit<T> {
Preedit {
content: self.content.as_ref().to_owned(),
selection: self.selection.clone(),
text_size: self.text_size,
}
}
}
Expand All @@ -63,6 +66,7 @@ impl Preedit {
Preedit {
content: &self.content,
selection: self.selection.clone(),
text_size: self.text_size,
}
}
}
Expand Down Expand Up @@ -90,13 +94,13 @@ impl InputMethod {
/// let open = InputMethod::Open {
/// position: Point::ORIGIN,
/// 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 {
/// position: Point::ORIGIN,
/// 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;
Expand Down
13 changes: 10 additions & 3 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,19 @@ where
}
Update::InputMethod(update) => match update {
Ime::Toggle(is_open) => {
state.preedit = is_open.then(input_method::Preedit::new);
state.preedit = is_open.then(|| {
let mut preedit = input_method::Preedit::new();
preedit.text_size = self.text_size;
preedit
});
shell.request_redraw(window::RedrawRequest::NextFrame);
}
Ime::Preedit { content, selection } => {
state.preedit =
Some(input_method::Preedit { content, selection });
state.preedit = Some(input_method::Preedit {
content,
selection,
text_size: self.text_size,
});
shell.request_redraw(window::RedrawRequest::NextFrame);
}
Ime::Commit(text) => {
Expand Down
10 changes: 8 additions & 2 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,8 +1192,13 @@ where
let state = state::<Renderer>(tree);

state.is_ime_open =
matches!(event, input_method::Event::Opened)
.then(input_method::Preedit::new);
matches!(event, input_method::Event::Opened).then(
|| {
let mut preedit = input_method::Preedit::new();
preedit.text_size = self.size;
preedit
},
);

shell.request_redraw(window::RedrawRequest::NextFrame);
}
Expand All @@ -1204,6 +1209,7 @@ where
state.is_ime_open = Some(input_method::Preedit {
content: content.to_owned(),
selection: selection.clone(),
text_size: self.size,
});

shell.request_redraw(window::RedrawRequest::NextFrame);
Expand Down
4 changes: 3 additions & 1 deletion winit/src/program/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ where
self.content = Renderer::Paragraph::with_spans(Text {
content: &spans,
bounds: Size::INFINITY,
size: renderer.default_size(),
size: preedit
.text_size
.unwrap_or_else(|| renderer.default_size()),
line_height: text::LineHeight::default(),
font: renderer.default_font(),
horizontal_alignment: alignment::Horizontal::Left,
Expand Down

0 comments on commit 02370b3

Please sign in to comment.