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

call TextEditOutpu.state.store(...) causes the app to freeze #2797

Closed
lost22git opened this issue Mar 10, 2023 · 3 comments
Closed

call TextEditOutpu.state.store(...) causes the app to freeze #2797

lost22git opened this issue Mar 10, 2023 · 3 comments
Labels
bug Something is broken

Comments

@lost22git
Copy link

lost22git commented Mar 10, 2023

 ui.ctx().input_mut(|i| {
            // Ctrl+J newline and auto-indent
            if cursor_range.is_some()
                && i.consume_shortcut(&KeyboardShortcut {
                    modifiers: Modifiers::CTRL,
                    key: Key::J,
                },)
            {
                let cr = cursor_range.unwrap();
                let row = cr.primary.rcursor.row;
                let line = f.code.lines().nth(row,).unwrap();
                tracing::info!("line => {line}");
                let mut space_count = 0;
                for c in line.chars() {
                    if c == ' ' {
                        space_count += 1;
                    } else if c == '\t' {
                        space_count += 4;
                    } else {
                        break;
                    }
                }
                let line = line.trim_end();
                if line.ends_with('{',) || line.ends_with('[',) || line.ends_with('(',) {
                    space_count += 4;
                }
                tracing::info!("space_count => {space_count}");
                let spaces = " ".repeat(space_count,);
                f.code.insert_text(
                    &format!("\n{spaces}"),
                    galley.cursor_end_of_row(&cr.primary,).ccursor.index,
                );
                editor_out.state.set_cursor_range(Some(CursorRange::one(
                    galley.cursor_end_of_row(&galley.cursor_down_one_row(&cr.primary,),),
                ),),);
                editor_out.state.store(ui.ctx(), code_editor_id,); // *** freeze app
                tracing::info!("==> store successfully!");
            }
@lost22git lost22git added the bug Something is broken label Mar 10, 2023
@lost22git
Copy link
Author

os => win11
eframe => 0.21.3

@YgorSouza
Copy link
Contributor

I think this is the same problem as #2752, and if so, the solution in #2753 also applies. ui.ctx().input_mut acquires an exclusive lock to the context for the duration of the closure, and editor_out.state.store(ui.ctx(), ... also tries to acquire the same exclusive lock to modify the context, so it causes a deadlock. You have to move this part of the code outside the closure.

@lost22git
Copy link
Author

@YgorSouza you are right. thanks! Hope to remind users on the doc ;-)

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

No branches or pull requests

2 participants