-
Notifications
You must be signed in to change notification settings - Fork 50
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
Can't summon soft_input with egui + winit + wgpu #44
Comments
Hi, Good Android IME support is something that still needs work in Egui, Winit and android-activity unfortunately but is something I'd like to make some progress on. You can see an initial attempt I made at enabling input method support in More recently I also looked at creating initial branches for Winit + Egui to experiment with this too: I would probably recommend first looking at the Maybe you could take a look over some of the above branches to see if they are a helpful starting point. At least in terms of using JNI to access the let ctx = ndk_context::android_context();
let jvm_ptr = ctx.vm();
let jvm = unsafe { jni::JavaVM::from_raw(jvm_ptr.cast()).expect("Expected to find JVM via ndk_context crate") };
let env = jvm.attach_current_thread_permanently().unwrap();
let activity_ptr = ctx.context();
let activity = unsafe { jni::objects::JObject::from_raw(activity_ptr as jni::sys::jobject) };
|
There is an implement in miniquad/miniquad, use a normal Activity. |
@tkkcc if you just want to show the keyboard you should be able to use https://docs.rs/android-activity/0.4.0/android_activity/struct.AndroidApp.html#method.show_soft_input which should be equivalent to what macroquad does. ...but, unfortunately, that won't be very useful without also having input method support too. macroquad has a custom subclass of the In When android-activity is built for When android-activity is built for I've at least smoke tested that |
I have a similar issue, I do use |
agdk-eframe demo is able to show and hide input method via button codeuse eframe::egui;
use eframe::{NativeOptions, Renderer};
#[cfg(target_os = "android")]
use winit::platform::android::activity::AndroidApp;
struct DemoApp {
app: AndroidApp,
}
impl eframe::App for DemoApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
if ui.button("ime show").clicked() {
self.app.show_soft_input(false);
}
if ui.button("ime hide").clicked() {
self.app.hide_soft_input(false);
}
});
}
}
fn _main(mut options: NativeOptions, app: AndroidApp) {
let demo_app = DemoApp { app };
eframe::run_native("My egui App", options, Box::new(|_cc| Box::new(demo_app)));
}
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app: AndroidApp) {
use winit::platform::android::EventLoopBuilderExtAndroid;
android_logger::init_once(android_logger::Config::default().with_min_level(log::Level::Info));
let mut options = NativeOptions::default();
let app2 = app.clone();
options.event_loop_builder = Some(Box::new(move |builder| {
builder.with_android_app(app);
}));
_main(options, app2);
} i don't know if clone an AndroidApp will cause something wrong. i don't know how to access options.event_loop_builder from ui update fn. |
tmp.mp4now i can achieve some level (very buggy) of input experience. based on agdk-eframe demo, picking the last commit of
and in pub fn input_events<F>(&self, mut callback: F)
where
F: FnMut(&InputEvent) -> InputStatus,
{
// eprintln!("555 {}", (*app_ptr).textInputState);
unsafe {
let app_ptr = self.native_app.as_ptr();
if (*app_ptr).textInputState != 0 {
let state = self.text_input_state();
callback(&InputEvent::TextEvent(state));
(*app_ptr).textInputState = 0;
}
}
let buf = unsafe {
let app_ptr = self.native_app.as_ptr();
let input_buffer = ffi::android_app_swap_input_buffers(app_ptr);
if input_buffer.is_null() {
return;
}
InputBuffer::from_ptr(NonNull::new_unchecked(input_buffer)) issues:
|
I'm also looking into keyboard support on android (and iOS). Based on @rib s work I have been able to get pretty decent support working on android 🥳 Screen_Recording_20230708_144153.mp4(opening and closing the keyboard also works, even if it's not in the video) Unfortunately for this to work, I had to add completely new events to winit, basically passing the TextInputState struct from android activity all the way through to egui, and then handling the logic there. The most difficult part was finding the right places in egui to send the correct state updates when selecting a new text field or updating the selection. If you'd like to try this you can use my relevant branches by adding this to your Cargo.toml (your project must be using game-activity for keyboard input to work): [patch.crates-io]
winit = { git = "https://github.com/lucasmerlin/winit", branch = "v0.28.x_ime_support" }
egui = { git = "https://github.com/lucasmerlin/egui", branch = "mobile_ime_support"}
eframe = { git = "https://github.com/lucasmerlin/egui", branch = "mobile_ime_support"}
egui-wgpu = { git = "https://github.com/lucasmerlin/egui", branch = "mobile_ime_support"}
android-activity = { git = "https://github.com/lucasmerlin/android-activity", branch = "ime_support"} These are the relevant branches: For this to feel native on android what's still missing would be egui showing the composition range with underlined text instead of the blue selection highlight. But I think that should be pretty straightforward. Also missing of course is to update the insets / resize the window when the keyboard opens. Next, I'll look into iOS input support, I already have opening the keyboard and typing working but I'll also try to get autocomplete and suggestions. |
show_soft_input() is still not working in NativeActivity with winit 0.29.10 (android-activity 0.5.0), nothing happens, no error on Android 8.1 or Android 11. I call it in android_main and after a touch event. On Android 13 I get: I suspect this has to do with using a GPU surface. https://github.com/rib/android-activity/pull/24 is a dead link. GameActivity examples (rust-android-examples) immediately crash on my devices (Android 8.1, Android 11 and Android 13) with various errors, so I can't consider that for production. I'll try to do it through JNI. |
Since this repository was migrated/transferred to the |
Ok, thanks. Shouldn't ANativeActivity_onCreate simply call it's super.onCreate? |
Why would that be required, given that The Java layer that powers It's unclear what line your link is supposed to link to. |
Ok, I see. I guess you don't get soft input on a window surface; you'd need a view. It might be possible to achieve this with NativeActivity if you create a SurfaceView in the same way that is done for GameActivity. But I guess that's kind of deprecated and people will just have to use GameActivity for soft input on a GPU surface. |
It works with NativeActivity if you use public static void JNI_showKeyboard() {
InputMethodManager im = (InputMethodManager)Main.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(window.getDecorView(), InputMethodManager.SHOW_IMPLICIT);
} Still, there's a number of issues which prevent it to be usable in current state:
|
Hi. I'm trying to display soft keyboard in simple app by calling
show_soft_input
:https://github.com/inferrna/hello_world_android_egui/blob/try_keyboard/src/lib.rs#L357
but nothing happens, even logcat contains no messages related to soft input, keyboard, etc.
Googling I found a lot complains about ANativeActivity_showSoftInput()
There is another way to do it — via JNI call. But it requires access to native_activity, which is currently hidden.
The text was updated successfully, but these errors were encountered: