Skip to content

Commit 4b6536e

Browse files
committed
desktop: Implement IME
Implements IME support (currently still limited by the core impl).
1 parent 42991f5 commit 4b6536e

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

desktop/src/app.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::util::{
88
};
99
use anyhow::Error;
1010
use gilrs::{Event, EventType, Gilrs};
11+
use ruffle_core::events::{ImeEvent, ImeNotification, PlayerNotification};
1112
use ruffle_core::swf::HeaderExt;
1213
use ruffle_core::PlayerEvent;
1314
use ruffle_render::backend::ViewportDimensions;
@@ -16,7 +17,7 @@ use std::time::Instant;
1617
use url::Url;
1718
use winit::application::ApplicationHandler;
1819
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize, Size};
19-
use winit::event::{ElementState, KeyEvent, Modifiers, StartCause, WindowEvent};
20+
use winit::event::{ElementState, Ime, KeyEvent, Modifiers, StartCause, WindowEvent};
2021
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy};
2122
use winit::keyboard::{Key, NamedKey};
2223
use winit::window::{Fullscreen, Icon, WindowAttributes, WindowId};
@@ -227,6 +228,18 @@ impl MainWindow {
227228
};
228229
self.check_redraw();
229230
}
231+
WindowEvent::Ime(ime) => match ime {
232+
Ime::Enabled => {}
233+
Ime::Preedit(text, cursor) => {
234+
self.player
235+
.handle_event(PlayerEvent::Ime(ImeEvent::Preedit(text, cursor)));
236+
}
237+
Ime::Commit(text) => {
238+
self.player
239+
.handle_event(PlayerEvent::Ime(ImeEvent::Commit(text)));
240+
}
241+
Ime::Disabled => {}
242+
},
230243
_ => (),
231244
}
232245
}
@@ -580,6 +593,33 @@ impl ApplicationHandler<RuffleEvent> for App {
580593
}
581594
}
582595

596+
(Some(main_window), RuffleEvent::PlayerNotification(notification)) => {
597+
match notification {
598+
PlayerNotification::ImeNotification(ImeNotification::ImeReady {
599+
purpose,
600+
cursor_area,
601+
}) => {
602+
let ime_enabled = main_window.preferences.ime_enabled().unwrap_or(false);
603+
main_window.gui.set_ime_allowed(ime_enabled);
604+
main_window.gui.set_ime_purpose(purpose);
605+
main_window.gui.set_ime_cursor_area(cursor_area);
606+
}
607+
PlayerNotification::ImeNotification(ImeNotification::ImePurposeUpdated(
608+
purpose,
609+
)) => {
610+
main_window.gui.set_ime_purpose(purpose);
611+
}
612+
PlayerNotification::ImeNotification(ImeNotification::ImeCursorAreaUpdated(
613+
cursor_area,
614+
)) => {
615+
main_window.gui.set_ime_cursor_area(cursor_area);
616+
}
617+
PlayerNotification::ImeNotification(ImeNotification::ImeNotReady) => {
618+
main_window.gui.set_ime_allowed(false);
619+
}
620+
}
621+
}
622+
583623
(_, RuffleEvent::ExitRequested) => {
584624
event_loop.exit();
585625
}

desktop/src/gui/controller.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::preferences::GlobalPreferences;
88
use anyhow::anyhow;
99
use egui::{Context, ViewportId};
1010
use fontdb::{Database, Family, Query, Source};
11+
use ruffle_core::events::{ImeCursorArea, ImePurpose};
1112
use ruffle_core::{Player, PlayerEvent};
1213
use ruffle_render_wgpu::backend::{request_adapter_and_device, WgpuRenderBackend};
1314
use ruffle_render_wgpu::descriptors::Descriptors;
@@ -21,7 +22,7 @@ use winit::dpi::{PhysicalPosition, PhysicalSize};
2122
use winit::event::WindowEvent;
2223
use winit::event_loop::EventLoopProxy;
2324
use winit::keyboard::{Key, NamedKey};
24-
use winit::window::{Theme, Window};
25+
use winit::window::{ImePurpose as WinitImePurpose, Theme, Window};
2526

2627
use super::{DialogDescriptor, FilePicker};
2728

@@ -269,6 +270,11 @@ impl GuiController {
269270
(x, y)
270271
}
271272

273+
pub fn movie_to_window_position(&self, x: f64, y: f64) -> PhysicalPosition<f64> {
274+
let y = y + self.height_offset();
275+
PhysicalPosition::new(x, y)
276+
}
277+
272278
pub fn render(&mut self, mut player: Option<MutexGuard<Player>>) {
273279
let surface_texture = match self.surface.get_current_texture() {
274280
Ok(surface_texture) => surface_texture,
@@ -439,6 +445,24 @@ impl GuiController {
439445
pub fn open_dialog(&mut self, dialog_event: DialogDescriptor) {
440446
self.gui.dialogs.open_dialog(dialog_event);
441447
}
448+
449+
pub fn set_ime_allowed(&self, allowed: bool) {
450+
self.window.set_ime_allowed(allowed);
451+
}
452+
453+
pub fn set_ime_purpose(&self, purpose: ImePurpose) {
454+
self.window.set_ime_purpose(match purpose {
455+
ImePurpose::Standard => WinitImePurpose::Normal,
456+
ImePurpose::Password => WinitImePurpose::Password,
457+
});
458+
}
459+
460+
pub fn set_ime_cursor_area(&self, cursor_area: ImeCursorArea) {
461+
self.window.set_ime_cursor_area(
462+
self.movie_to_window_position(cursor_area.x, cursor_area.y),
463+
PhysicalSize::new(cursor_area.width, cursor_area.height),
464+
);
465+
}
442466
}
443467

444468
fn create_wgpu_instance(

0 commit comments

Comments
 (0)