Skip to content

Commit

Permalink
Update lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustbasic authored Jun 12, 2024
1 parent 99bfca4 commit e361d65
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,34 +828,38 @@ impl State {
self.clipboard.set(copied_text);
}

let allow_ime = ime.is_some();
if self.allow_ime != allow_ime {
self.allow_ime = allow_ime;
crate::profile_scope!("set_ime_allowed");
window.set_ime_allowed(allow_ime);
}

if let Some(ime) = ime {
let pixels_per_point = pixels_per_point(&self.egui_ctx, window);
let ime_rect_px = pixels_per_point * ime.rect;
if self.ime_rect_px != Some(ime_rect_px)
|| self.egui_ctx.input(|i| !i.events.is_empty())
{
self.ime_rect_px = Some(ime_rect_px);
crate::profile_scope!("set_ime_cursor_area");
window.set_ime_cursor_area(
winit::dpi::PhysicalPosition {
x: ime_rect_px.min.x,
y: ime_rect_px.min.y,
},
winit::dpi::PhysicalSize {
width: ime_rect_px.width(),
height: ime_rect_px.height(),
},
if self.allow_ime != ime.allowed_ime {
self.allow_ime = ime.allowed_ime;
crate::profile_scope!("set_ime_allowed");
self.egui_ctx.send_viewport_cmd_to(
*viewport_id,
ViewportCommand::IMEAllowed(self.allow_ime),
);
}
} else {
self.ime_rect_px = None;

if ime.ime_enabled && ime.visible {
let pixels_per_point = pixels_per_point(&self.egui_ctx, window);
let ime_rect_px = ime.cursor_rect * pixels_per_point;

let is_need_cursor_area = self.ime_rect_px != Some(ime_rect_px)
&& self
.egui_ctx
.input_for(*viewport_id, |i| !i.events.is_empty());

if is_need_cursor_area {
self.ime_rect_px = Some(ime_rect_px);
crate::profile_scope!("set_ime_cursor_area");
self.egui_ctx.send_viewport_cmd_to(
*viewport_id,
ViewportCommand::IMERect(ime.cursor_rect),
);
} else {
self.ime_rect_px = None;
}
} else {
self.ime_rect_px = None;
}
}

#[cfg(feature = "accesskit")]
Expand Down Expand Up @@ -1441,14 +1445,18 @@ fn process_viewport_command(
let winit_icon = icon.and_then(|icon| to_winit_icon(&icon));
window.set_window_icon(winit_icon);
}
ViewportCommand::IMERect(rect) => {
ViewportCommand::IMERect(ime_rect) => {
let ime_rect_px = ime_rect * pixels_per_point;
window.set_ime_cursor_area(
PhysicalPosition::new(pixels_per_point * rect.min.x, pixels_per_point * rect.min.y),
PhysicalSize::new(
pixels_per_point * rect.size().x,
pixels_per_point * rect.size().y,
),
PhysicalPosition::new(ime_rect_px.min.x, ime_rect_px.min.y),
PhysicalSize::new(ime_rect_px.width(), ime_rect_px.height()),
);

egui_ctx.output_mut(|o| {
if let Some(ime) = &mut o.ime {
ime.cursor_rect = ime_rect;
}
});
}
ViewportCommand::IMEAllowed(v) => window.set_ime_allowed(v),
ViewportCommand::IMEPurpose(p) => window.set_ime_purpose(match p {
Expand Down

0 comments on commit e361d65

Please sign in to comment.