Skip to content
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

Set correct text size for preedit window #2790

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 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::Point;
use crate::{Pixels, Point};

use std::ops::Range;

Expand Down Expand Up @@ -34,15 +34,20 @@ 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> {
/// Creates a new empty [`Preedit`].
pub fn new() -> Self
pub fn new(text_size: Option<impl Into<Pixels>>) -> Self
where
T: Default,
{
Self::default()
Self {
text_size: text_size.map(Into::into),
..Default::default()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need. Let's not ask for any argument and just use Default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll make a change.

Copy link
Contributor Author

@rhysd rhysd Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hecrj I addressed this at cf851e1

}

/// Turns a [`Preedit`] into its owned version.
Expand All @@ -53,6 +58,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 +69,7 @@ impl Preedit {
Preedit {
content: &self.content,
selection: self.selection.clone(),
text_size: self.text_size,
}
}
}
Expand Down Expand Up @@ -90,13 +97,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
12 changes: 8 additions & 4 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,14 +753,18 @@ where
}
Update::InputMethod(update) => match update {
Ime::Toggle(is_open) => {
state.preedit =
is_open.then(input_method::Preedit::new);
state.preedit = is_open.then(|| {
input_method::Preedit::new(self.text_size)
});

shell.request_redraw();
}
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();
}
Expand Down
3 changes: 2 additions & 1 deletion widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ where

state.is_ime_open =
matches!(event, input_method::Event::Opened)
.then(input_method::Preedit::new);
.then(|| input_method::Preedit::new(self.size));

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

shell.request_redraw();
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 @@ -322,7 +322,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