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

Chinese can be entered in the first position of text_editor, but not in other positions #4430

Closed
xuerenlin opened this issue Apr 28, 2024 · 20 comments · Fixed by #4436
Closed
Labels
bug Something is broken

Comments

@xuerenlin
Copy link

Describe the bug

Chinese can be entered in the first position of text_editor, but not in other positions

To Reproduce
Steps to reproduce the behavior:

  1. git clone eframe_template

  2. setup custom fonts:
    fn setup_custom_fonts(ctx: &egui::Context) {
    // Start with the default fonts (we will be adding to them rather than replacing them).
    let mut fonts = egui::FontDefinitions::default();

    // Install my own font (maybe supporting non-latin characters).
    // .ttf and .otf files supported.
    fonts.font_data.insert(
    "my_font".to_owned(),
    egui::FontData::from_static(include_bytes!(
    "msyh.ttc"
    )),
    );

    // Put my font first (highest priority) for proportional text:
    fonts
    .families
    .entry(egui::FontFamily::Proportional)
    .or_default()
    .insert(0, "my_font".to_owned());

    // Put my font as last fallback for monospace:
    fonts
    .families
    .entry(egui::FontFamily::Monospace)
    .or_default()
    .push("my_font".to_owned());

    // Tell egui to use these fonts:
    ctx.set_fonts(fonts);
    }

  3. cargo run

Expected behavior

check this scrennshorts,“你好” can be entered in the first position, but can not entered after "test"

Screenshots

image

Desktop (please complete the following information):

  • OS: I check windows 11 and unbuntu,has the same behavior.
  • Browser
  • Version
@xuerenlin xuerenlin added the bug Something is broken label Apr 28, 2024
@rustbasic
Copy link
Contributor

rustbasic commented Apr 29, 2024

Check if it works by applying egui Master Version.

It is expected that there will be no problems with the Master Version,
and it will be corrected in the next egui Version (0.27.3 ?) update.

@xuerenlin
Copy link
Author

Check if it works by applying egui Master Version.

It is expected that there will be no problems with the Master Version, and it will be corrected in the next egui Version (0.27.3 ?) update.

Thank you for your reply.

I test the master branch examples/hello_world:
1. In my window 11, it works good !!
2. But I test in ubuntu, the problem has not been resolved.

I modify crates/egui/src/widgets/text_edit/builder.rs like this, and it works.

            ImeEvent::Commit(prediction) => {
                println!("ImeEvent::Commit( {}", prediction);
                if prediction == "\n" || prediction == "\r" {
                    None
                } else {
                    state.ime_enabled = false;

                    if !prediction.is_empty()
                    //&& cursor_range.secondary.ccursor.index
                    //    == state.ime_cursor_range.secondary.ccursor.index
                    {
                        println!(
                            "cursor_range.secondary.ccursor.index:{}",
                            cursor_range.secondary.ccursor.index
                        );
                        println!(
                            "state.ime_cursor_range.secondary.ccursor.index:{}",
                            state.ime_cursor_range.secondary.ccursor.index
                        );

                        let mut ccursor = text.delete_selected(&cursor_range);
                        text.insert_text_at(&mut ccursor, prediction, char_limit);
                        Some(CCursorRange::one(ccursor))
                    } else {
                        let ccursor = cursor_range.primary.ccursor;
                        Some(CCursorRange::one(ccursor))
                    }
                }
            }

Then run this test, got the print below:

ImeEvent::Commit( 中国
cursor_range.secondary.ccursor.index:6
**state.ime_cursor_range.secondary.ccursor.index:0**
ImeEvent::Commit( 你好
cursor_range.secondary.ccursor.index:8
**state.ime_cursor_range.secondary.ccursor.index:0**

But I don't know why state.ime_cursor_range.secondary.ccursor.index is allway 0

@rustbasic
Copy link
Contributor

@xuerenlin

Please check if the following Pull Request has a problem in Ubuntu. If not, check if there is a problem in Windows as well and let us know the results.

@xuerenlin
Copy link
Author

@rustbasic

I test #4436 in Ubuntu.
The first time I entered "你好", it was not displayed in the text_edit box.
When I enter "你好" again, it's normal. Then every input is normal.
It looks like it's the first time that the control status is abnormal.

In text_edit/builder.rs fn events()

The First enter,get bellow ImeEvent:
ImeEvent::Commit: prediction=你好
ImeEvent::Disabled

The Second enter,get bellow ImeEvent:
ImeEvent::Enabled
ImeEvent::Commit: prediction=你好
ImeEvent::Disabled

The First enter, do not trigger ImeEvent::Enabled event.

@rustbasic
Copy link
Contributor

@xuerenlin

Again,
Please check if the following Pull Request has a problem in Ubuntu. If not, check if there is a problem in Windows as well and let us know the results.

@xuerenlin
Copy link
Author

@rustbasic

Test in Windows it works good,In text_edit/builder.rs fn events()
Every input get bellow ImeEvent:

  ImeEvent::Enabled
  ImeEvent::Enabled
  ImeEvent::Commit prediction=你好
  ImeEvent::Disabled
  ImeEvent::Disabled

The above tests used SougoPinyin(a popular IME in China).

Then I use MicrosoftPinyin(Windows default chinese IME) Test,get bellow ImeEvent:

  ImeEvent::Enabled
  ImeEvent::Preedit text_mark=n
  ImeEvent::Preedit text_mark=ni
  ImeEvent::Preedit text_mark=ni'h
  ImeEvent::Preedit text_mark=ni'ha
  ImeEvent::Preedit text_mark=ni'hao
  ImeEvent::Enabled
  ImeEvent::Commit prediction=你好
  ImeEvent::Disabled
  ImeEvent::Disabled

This result may be due to SougoPinyin not following the IME rules well?
And In Ubuntu system, I test GooglePinyin again, it has the same problem like SougoPinyin.

@xuerenlin
Copy link
Author

@xuerenlin

Again, Please check if the following Pull Request has a problem in Ubuntu. If not, check if there is a problem in Windows as well and let us know the results.

Still #4436? I run "git pull" and test again, has the same problem.

@rustbasic
Copy link
Contributor

@xuerenlin

Please print the winit::event::Ime:: event details at the following location and let us know.
I'll check tomorrow.

crates/egui-winit/src/lib.rs
330 line
WindowEvent::Ime(ime) => {

@rustbasic
Copy link
Contributor

Because it is difficult to apply to all languages, there are no exact rules.
However, it is true that SougoPinyin has a unique behavior.

@xuerenlin
Copy link
Author

crates/egui-winit/src/lib.rs 331 line, I add print:

WindowEvent::Ime(ime) => {
    println!("ime event is: {:?}", ime);

When app run, print:
ime event is: Enabled

Every time, I enter "你好", print:

ime event is: Preedit("", None)
ime event is: Commit("你好")

So total print information is:

ime event is: Enabled
ime event is: Preedit("", None)
ime event is: Commit("你好")
ime event is: Preedit("", None)
ime event is: Commit("你好")
ime event is: Preedit("", None)
ime event is: Commit("你好")
ime event is: Preedit("", None)
ime event is: Commit("你好")
ime event is: Preedit("", None)
ime event is: Commit("你好")

@rustbasic
Copy link
Contributor

This is the problem

When app run, print:
ime event is: Enabled

@rustbasic
Copy link
Contributor

rustbasic commented May 1, 2024

@xuerenlin

Please check if it is possible as follows.
let me know result, please.

  1. Apply new IME for chinese #4436.

  2. Find a way to prevent winit::event::Ime::Enabled from occurring when the program starts.

OR

  1. Call ime_event_disable() in egui-winit/src/lib.rs only once when the program starts.
    (Alternatively, it is expected to operate normally even if you call ime_event_disable() every time before inputting.)

@xuerenlin
Copy link
Author

@rustbasic
I call ime_event_disable() once time, and it works.
Here is my test code:
image

@rustbasic
Copy link
Contributor

@xuerenlin

Is it okay to call ime_event_disable() from your program without changing egui-winit/src/lib.rs?

Or is there anything I need to change in egui-winit/src/lib.rs to use ime_event_disable()?

@xuerenlin
Copy link
Author

@rustbasic

Yes, I change egui-winit/src/lib.rs, add call ime_event_disable() as follows:

                match ime {
                    winit::event::Ime::Enabled | winit::event::Ime::Preedit(_, None) => {
                        self.ime_event_enable();
                    }
                    winit::event::Ime::Preedit(text, Some(_cursor)) => {
                        self.ime_event_enable();
                        self.egui_input
                            .events
                            .push(egui::Event::Ime(egui::ImeEvent::Preedit(text.clone())));
                    }
                    winit::event::Ime::Commit(text) => {
                        self.egui_input
                            .events
                            .push(egui::Event::Ime(egui::ImeEvent::Commit(text.clone())));
                        self.ime_event_disable();
                    }
                    winit::event::Ime::Disabled => {
                        self.ime_event_disable();
                    }
                };
                ///////////////////////////////////////////////////////////////
                //following is my test code:
                static DISABLE_IME_ENABLE_AT_START: OnceLock<i32> = OnceLock::new();
                DISABLE_IME_ENABLE_AT_START.get_or_init(|| {
                    self.ime_event_disable();
                    println!("ime_event_disable");
                    return 0;
                });

You need to consider the specific solution. I'm just testing whether calling ime_event_disable() once when the program starts can solve the problem.

@rustbasic
Copy link
Contributor

@xuerenlin

Do not modify egui-winit/src/lib.rs,
Make sure your program can call ime_event_disable().

Let's work together to get it in a usable condition.
Ultimately, approval from 'emilk' is required.

@xuerenlin
Copy link
Author

@rustbasic
Sorry, I have just been using egui for a few days and I am still not familiar with it. So I don't understand how to call ime_event_disable() in my program.
Is something can do in raw_input_hook() function ?

@rustbasic
Copy link
Contributor

rustbasic commented May 3, 2024

@xuerenlin

Again,
Please check if the following Pull Request has a problem in Ubuntu and Windows.
Please test various IMEs.
let us know the results.

@xuerenlin
Copy link
Author

xuerenlin commented May 5, 2024

@rustbasic
I tested the following scenarios and all of them worked properly !

  1. Windows + MicrosoftPinyin
  2. Windows + SougouPinyin
  3. Ubuntu + SougouPinyin
  4. Ubuntu + GooglePinyin

@rustbasic
Copy link
Contributor

@emilk @xuerenlin

I expect this to work, and we've been told it will work, so we hope this gets approved.

emilk pushed a commit that referenced this issue May 10, 2024
* Completed.

* Closes #4430

IME for chinese
emilk pushed a commit that referenced this issue Aug 5, 2024
Fix: Changed the handling method of `Ime::Preedit(_, None)`

Fix: backspace fail after ime input

* Related #4358
* Related #4430 
* Related #4436
* Related #4794 
* Related #4896
* Closes #4908 

Issues: backspace fail after ime input
* #4908 (Chinese)

Changed the handling method of `Ime::Preedit(_, None)`
rib pushed a commit to EmbarkStudios/egui that referenced this issue Sep 30, 2024
Fix: Changed the handling method of `Ime::Preedit(_, None)`

Fix: backspace fail after ime input

* Related emilk#4358
* Related emilk#4430 
* Related emilk#4436
* Related emilk#4794 
* Related emilk#4896
* Closes emilk#4908 

Issues: backspace fail after ime input
* emilk#4908 (Chinese)

Changed the handling method of `Ime::Preedit(_, None)`
486c pushed a commit to 486c/egui that referenced this issue Oct 9, 2024
Fix: Changed the handling method of `Ime::Preedit(_, None)`

Fix: backspace fail after ime input

* Related emilk#4358
* Related emilk#4430 
* Related emilk#4436
* Related emilk#4794 
* Related emilk#4896
* Closes emilk#4908 

Issues: backspace fail after ime input
* emilk#4908 (Chinese)

Changed the handling method of `Ime::Preedit(_, None)`
hacknus pushed a commit to hacknus/egui that referenced this issue Oct 30, 2024
* Completed.

* Closes emilk#4430

IME for chinese
hacknus pushed a commit to hacknus/egui that referenced this issue Oct 30, 2024
Fix: Changed the handling method of `Ime::Preedit(_, None)`

Fix: backspace fail after ime input

* Related emilk#4358
* Related emilk#4430 
* Related emilk#4436
* Related emilk#4794 
* Related emilk#4896
* Closes emilk#4908 

Issues: backspace fail after ime input
* emilk#4908 (Chinese)

Changed the handling method of `Ime::Preedit(_, None)`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants