Repository navigation
input: Run custom context menu builders after releasing the input - #3433
Merged
Merged
Conversation
The right-click handler ran inside `cx.defer_in`, which still holds the input entity, so a custom `context_menu` builder that read the input (cursor, selection, text) panicked with "already being updated". Run it through `window.defer` instead and do the focus bookkeeping in a separate update afterwards. Fixes longbridge#3427
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3427
Description
A custom context menu builder set with
on_context_menuran insidecx.defer_in, which executes the callback inside the input'supdate. A builder that reads the input (cursor, selection, text) to decide its items then panicked with "cannot read ... InputBaseState ... while it is already being updated".handle_right_click_menunow defers withwindow.deferand a weak handle to the input, so the handler runs with the input released. The focus bookkeeping that used to run in the same lease (reusing an already-focused popup, capturing the menu's focus forset_selection_focus) runs right after the handler ininput.update, unchanged. If the input is gone by the time the deferred call runs, nothing happens, as before.Both deferrals push the same deferred effect, so the handler still runs at the same point in the effect cycle. Textarea and Editor go through the same function.
The change was written with an AI coding assistant; I reviewed and tested it.
How to Test
context_menu_handler_can_read_its_input(crates/base/src/input/base/state.rs): the handler reads the input's cursor. Onmainit panics with the lease error from the issue; with this change it reads the expected cursor.cargo test -p gpui-base --lib input(335 passed), including the existingcontext_menu_handler_is_deferred_and_respects_disabledthat covers the focus and popup-reuse paths.cargo test -p gpui-kit --features test-support,component --test menu --test input --test input_focus(12, 179, 4 passed).cargo clippy -p gpui-base -p gpui-component --all-targets -- --deny warningsandcargo fmt --all --checkclean.Checklist
cargo runfor story tests related to the changes. (Not run; covered by the input and menu tests above.)